Sha256: 3cc85d958fbdd014549ce8f7f8b1c9fc72c138a8728a16a8b312b5f89db8a2f3

Contents?: true

Size: 957 Bytes

Versions: 12

Compression:

Stored size: 957 Bytes

Contents

class String
  def json_escape
    ERB::Util.json_escape self
  end
  
  HTML_ESCAPE_MAP = { '&' => '&amp;',  '>' => '&gt;',   '<' => '&lt;', '"' => '&quot;' }
  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
  
  def url_escape
    # TODO2 change to Rack::Utils.escape
    CGI.escape self
  end
  
  def url_unescape
    # TODO2 change to Rack::Utils.unescape
    CGI.unescape self
  end
  
  JS_ESCAPE_MAP = {
    '\\'    => '\\\\',
    '</'    => '<\/',
    "\r\n"  => '\n',
    "\n"    => '\n',
    "\r"    => '\n',
    '"'     => '\\"',
    "'"     => "\\'" 
  }  
  def js_escape
    gsub(/(\\|<\/|\r\n|[\n\r"'])/){JS_ESCAPE_MAP[$1]}
  end
  
  # String marks, like :format, :safe 
  def marks; @marks ||= OpenObject.new end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
rad_core-0.2.6 lib/rad/_support/string.rb
rad_core-0.2.5 lib/rad/_support/string.rb
rad_core-0.2.4 lib/rad/_support/string.rb
rad_core-0.2.3 lib/rad/_support/string.rb
rad_core-0.2.2 lib/rad/_support/string.rb
rad_core-0.2.1 lib/rad/_support/string.rb
rad_core-0.2.0 lib/rad/_support/string.rb
rad_core-0.0.30 lib/rad/_support/string.rb
rad_core-0.0.29 lib/rad/_support/string.rb
rad_core-0.0.28 lib/rad/_support/string.rb
rad_core-0.0.27 lib/rad/_support/string.rb
rad_core-0.0.26 lib/rad/_support/string.rb