public
Fork of rails/rails
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/anildigital/rails.git
Search Repo:
Add config.active_support.escape_html_entities_in_json to allow disabling 
of html entity escaping.  [rick]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9238 
5ecf4fe2-1ee6-0310-87b1-e25e094e27de
technoweenie (author)
Mon Apr 07 20:45:26 -0700 2008
commit  605196c4483bbba932804e78cd6d4166f4e29bbb
tree    4f682a944ab11aabaf8b9208ff571ebe4aecbd19
parent  4b68982f649c5b230b3e99954a71404c7cec03c7
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *SVN*
0
 
0
+* Add config.active_support.escape_html_entities_in_json to allow disabling of html entity escaping. [rick]
0
+
0
 * Improve documentation. [Xavier Noria]
0
 
0
 * Modified ActiveSupport::Callbacks::Callback#call to accept multiple arguments.
...
1
2
 
3
4
 
5
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
8
9
...
31
32
33
 
 
 
...
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
...
47
48
49
50
51
52
0
@@ -1,9 +1,25 @@
0
-require 'active_support/json/encoding'
0
-require 'active_support/json/decoding'
0
+
0
 
0
 module ActiveSupport
0
+ # If true, use ISO 8601 format for dates and times. Otherwise, fall back to the ActiveSupport legacy format.
0
   mattr_accessor :use_standard_json_time_format
0
 
0
+ class << self
0
+ def escape_html_entities_in_json
0
+ @escape_html_entities_in_json
0
+ end
0
+
0
+ def escape_html_entities_in_json=(value)
0
+ ActiveSupport::JSON::Encoding.escape_regex = \
0
+ if value
0
+ /[\010\f\n\r\t"\\><&]/
0
+ else
0
+ /[\010\f\n\r\t"\\]/
0
+ end
0
+ @escape_html_entities_in_json = value
0
+ end
0
+ end
0
+
0
   module JSON
0
     RESERVED_WORDS = %w(
0
       abstract delete goto private transient
0
@@ -31,3 +47,6 @@ module ActiveSupport
0
     end
0
   end
0
 end
0
+
0
+require 'active_support/json/encoding'
0
+require 'active_support/json/decoding'
...
1
2
3
 
 
4
5
6
...
17
18
19
 
 
20
21
22
 
23
24
25
...
1
2
3
4
5
6
7
8
...
19
20
21
22
23
24
25
 
26
27
28
29
0
@@ -1,6 +1,8 @@
0
 module ActiveSupport
0
   module JSON
0
     module Encoding
0
+ mattr_accessor :escape_regex
0
+
0
       ESCAPED_CHARS = {
0
         "\010" => '\b',
0
         "\f" => '\f',
0
@@ -17,9 +19,11 @@ module ActiveSupport
0
   end
0
 end
0
 
0
+ActiveSupport.escape_html_entities_in_json = true
0
+
0
 class String
0
   def to_json(options = nil) #:nodoc:
0
- json = '"' + gsub(/[\010\f\n\r\t"\\><&]/) { |s|
0
+ json = '"' + gsub(ActiveSupport::JSON::Encoding.escape_regex) { |s|
0
       ActiveSupport::JSON::Encoding::ESCAPED_CHARS[s]
0
     }
0
     json.force_encoding('ascii-8bit') if respond_to?(:force_encoding)
...
1
2
3
4
5
...
1
 
2
3
4
0
@@ -1,5 +1,4 @@
0
 require 'active_support/json/variable'
0
-
0
 require 'active_support/json/encoders/object' # Require explicitly for rdoc.
0
 Dir["#{File.dirname(__FILE__)}/encoders/**/*.rb"].each do |file|
0
   basename = File.basename(file, '.rb')
...
38
39
40
 
41
42
43
44
 
45
46
47
48
49
 
50
51
52
...
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
0
@@ -38,15 +38,18 @@ class TestJSONEncoding < Test::Unit::TestCase
0
   StandardDateTests = [[ Date.new(2005,2,1), %("2005-02-01") ]]
0
   StandardTimeTests = [[ Time.utc(2005,2,1,15,15,10), %("2005-02-01T15:15:10Z") ]]
0
   StandardDateTimeTests = [[ DateTime.civil(2005,2,1,15,15,10), %("2005-02-01T15:15:10+00:00") ]]
0
+ StandardStringTests = [[ 'this is the <string>', %("this is the <string>")]]
0
 
0
   constants.grep(/Tests$/).each do |class_tests|
0
     define_method("test_#{class_tests[0..-6].underscore}") do
0
       begin
0
+ ActiveSupport.escape_html_entities_in_json = class_tests !~ /^Standard/
0
         ActiveSupport.use_standard_json_time_format = class_tests =~ /^Standard/
0
         self.class.const_get(class_tests).each do |pair|
0
           assert_equal pair.last, pair.first.to_json
0
         end
0
       ensure
0
+ ActiveSupport.escape_html_entities_in_json = false
0
         ActiveSupport.use_standard_json_time_format = false
0
       end
0
     end

Comments

    No one has commented yet.