public this repo is viewable by everyone
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
add json_escape ERB util to escape html entities in json strings that are 
output in HTML pages. [rick]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9241 
5ecf4fe2-1ee6-0310-87b1-e25e094e27de
technoweenie (author)
about 1 month ago
commit  0ff7a2d89fc95dcb0a32ed92aab7156b0778a7ea
tree    af15ea4a71c680931264823859e8c71e2f6da1b5
parent  0bea3f8391e985157f3aecdf50a5d61de7aa7f0c
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *SVN*
0
 
0
+* add json_escape ERB util to escape html entities in json strings that are output in HTML pages. [rick]
0
+
0
 * Provide a helper proxy to access helper methods from outside views. Closes #10839 [Josh Peek]
0
   e.g. ApplicationController.helpers.simple_format(text)
0
 
...
2
3
4
5
 
 
6
7
8
...
16
17
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
20
21
...
2
3
4
 
5
6
7
8
9
...
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
0
@@ -2,7 +2,8 @@ require 'erb'
0
 
0
 class ERB
0
   module Util
0
- HTML_ESCAPE = { '&' => '&amp;', '"' => '&quot;', '>' => '&gt;', '<' => '&lt;' }
0
+ HTML_ESCAPE = { '&' => '&amp;', '>' => '&gt;', '<' => '&lt;', '"' => '&quot;' }
0
+ JSON_ESCAPE = { '&' => '\u0026', '>' => '\u003E', '<' => '\u003C'}
0
 
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
     def html_escape(s)
0
       s.to_s.gsub(/[&"><]/) { |special| HTML_ESCAPE[special] }
0
     end
0
+
0
+ # A utility method for escaping HTML entities in JSON strings.
0
+ # This method is also aliased as <tt>j</tt>.
0
+ #
0
+ # In your ERb templates, use this method to escape any HTML entities:
0
+ # <%=j @person.to_json %>
0
+ #
0
+ # ==== Example:
0
+ # puts json_escape("is a > 0 & a < 10?")
0
+ # # => is a \u003E 0 \u0026 a \u003C 10?
0
+ def json_escape(s)
0
+ s.to_s.gsub(/[&"><]/) { |special| JSON_ESCAPE[special] }
0
+ end
0
+
0
+ alias j json_escape
0
+ module_function :j
0
+ module_function :json_escape
0
   end
0
 end
0
 
...
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
 
 
 
17
18
19
 
 
 
 
 
20
21
22
...
2
3
4
 
 
 
 
 
 
 
 
5
 
 
 
6
7
8
9
10
 
 
11
12
13
14
15
16
17
18
0
@@ -2,21 +2,17 @@ require 'abstract_unit'
0
 
0
 class ErbUtilTest < Test::Unit::TestCase
0
   include ERB::Util
0
-
0
- def test_amp
0
- assert_equal '&amp;', html_escape('&')
0
- end
0
-
0
- def test_quot
0
- assert_equal '&quot;', html_escape('"')
0
- end
0
 
0
- def test_lt
0
- assert_equal '&lt;', html_escape('<')
0
- end
0
+ ERB::Util::HTML_ESCAPE.each do |given, expected|
0
+ define_method "test_html_escape_#{expected.gsub /\W/, ''}" do
0
+ assert_equal expected, html_escape(given)
0
+ end
0
 
0
- def test_gt
0
- assert_equal '&gt;', html_escape('>')
0
+ unless given == '"'
0
+ define_method "test_json_escape_#{expected.gsub /\W/, ''}" do
0
+ assert_equal ERB::Util::JSON_ESCAPE[given], json_escape(given)
0
+ end
0
+ end
0
   end
0
   
0
   def test_rest_in_ascii

Comments

    No one has commented yet.