Module: ERB::Util

Defined in:
lib/sinarey_support/core_ext/string/output_safety.rb

Constant Summary collapse

HTML_ECP =
{ '&' => '&amp;',  '>' => '&gt;',   '<' => '&lt;', '"' => '&quot;', "'" => '&#39;' }
JSON_ECP =
{ '&' => '\u0026', '>' => '\u003E', '<' => '\u003C' }
HTML_ECP_ONCE =
/["><']|&(?!([a-zA-Z]+|(#\d+));)/
JSON_ECP_REGEXP =
/[&"><]/

Class Method Summary collapse

Class Method Details

.html_escape_once(s) ⇒ Object



12
13
14
15
# File 'lib/sinarey_support/core_ext/string/output_safety.rb', line 12

def html_escape_once(s)
  result = s.to_s.gsub(HTML_ECP_ONCE, HTML_ECP)
  s.html_safe? ? result.html_safe : result
end

.json_escape(s) ⇒ Object



19
20
21
22
# File 'lib/sinarey_support/core_ext/string/output_safety.rb', line 19

def json_escape(s)
  result = s.to_s.gsub(JSON_ECP_REGEXP, JSON_ECP)
  s.html_safe? ? result.html_safe : result
end

.sinarey_escape(value) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/sinarey_support/core_ext/string/output_safety.rb', line 26

def sinarey_escape(value)
  if value.html_safe?
    value.to_s
  else
    value.to_s.gsub(/[&"'><]/, HTML_ECP)
  end
end