5 HTML_ESCAPE
= { '&' => '&', '>' => '>', '<' => '<', '"' => '"' }
6 JSON_ESCAPE
= { '&' => '\u0026', '>' => '\u003E', '<' => '\u003C' }
8 # A utility method for escaping HTML tag characters.
9 # This method is also aliased as <tt>h</tt>.
11 # In your ERb templates, use this method to escape any unsafe content. For example:
12 # <%=h @person.name %>
15 # puts html_escape("is a > 0 & a < 10?")
16 # # => is a > 0 & a < 10?
18 s
.to_s
.gsub(/[&"><]/) { |special
| HTML_ESCAPE
[special
] }
21 # A utility method for escaping HTML entities in JSON strings.
22 # This method is also aliased as <tt>j</tt>.
24 # In your ERb templates, use this method to escape any HTML entities:
25 # <%=j @person.to_json %>
28 # puts json_escape("is a > 0 & a < 10?")
29 # # => is a \u003E 0 \u0026 a \u003C 10?
31 s
.to_s
.gsub(/[&"><]/) { |special
| JSON_ESCAPE
[special
] }
36 module_function
:json_escape