0
@@ -2,7 +2,8 @@ require 'erb'
0
- HTML_ESCAPE = { '&' => '&', '"' => '"', '>' => '>', '<' => '<' }
0
+ HTML_ESCAPE = { '&' => '&', '>' => '>', '<' => '<', '"' => '"' }
0
+ JSON_ESCAPE = { '&' => '\u0026', '>' => '\u003E', '<' => '\u003C'}
0
# A utility method for escaping HTML tag characters.
0
# This method is also aliased as <tt>h</tt>.
0
@@ -16,6 +17,23 @@ class ERB
0
s.to_s.gsub(/[&"><]/) { |special| HTML_ESCAPE[special] }
0
+ # A utility method for escaping HTML entities in JSON strings.
0
+ # This method is also aliased as <tt>j</tt>.
0
+ # In your ERb templates, use this method to escape any HTML entities:
0
+ # <%=j @person.to_json %>
0
+ # puts json_escape("is a > 0 & a < 10?")
0
+ # # => is a \u003E 0 \u0026 a \u003C 10?
0
+ s.to_s.gsub(/[&"><]/) { |special| JSON_ESCAPE[special] }
0
+ module_function :json_escape
Comments
No one has commented yet.