Class: String

Inherits:
Object show all
Defined in:
lib/rad/_support/string.rb

Constant Summary collapse

HTML_ESCAPE_MAP =
{ '&' => '&amp;',  '>' => '&gt;',   '<' => '&lt;', '"' => '&quot;' }
JS_ESCAPE_MAP =
{
  '\\'    => '\\\\',
  '</'    => '<\/',
  "\r\n"  => '\n',
  "\n"    => '\n',
  "\r"    => '\n',
  '"'     => '\\"',
  "'"     => "\\'"
}

Instance Method Summary collapse

Instance Method Details

#html_escapeObject



7
8
9
10
11
12
13
# File 'lib/rad/_support/string.rb', line 7

def html_escape
  # ERB and Rack both are replacing '/' character, and it's wrong because it destroys links (like '/default/my_item')
  # ERB::Util.html_escape self
  # Rack::Utils.escape_html self

  gsub(/[&"><]/){|special| HTML_ESCAPE_MAP[special]}
end

#js_escapeObject



34
35
36
# File 'lib/rad/_support/string.rb', line 34

def js_escape
  gsub(/(\\|<\/|\r\n|[\n\r"'])/){JS_ESCAPE_MAP[$1]}
end

#json_escapeObject



2
3
4
# File 'lib/rad/_support/string.rb', line 2

def json_escape
  ERB::Util.json_escape self
end

#marksObject

String marks, like :format, :safe



39
# File 'lib/rad/_support/string.rb', line 39

def marks; @marks ||= OpenObject.new end

#url_escapeObject



15
16
17
18
# File 'lib/rad/_support/string.rb', line 15

def url_escape
  # TODO2 change to Rack::Utils.escape
  CGI.escape self
end

#url_unescapeObject



20
21
22
23
# File 'lib/rad/_support/string.rb', line 20

def url_unescape
  # TODO2 change to Rack::Utils.unescape
  CGI.unescape self
end