1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Change JSON to encode %w(< > &) as 4 digit hex codes to be in compliance with the JSON spec. Closes #9975 [josh, chuyeow, tpope]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8050 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Rick Olson 2007-10-29 00:18:43 +00:00
parent df0765d8dc
commit c708346688
6 changed files with 22 additions and 19 deletions

View file

@ -38,14 +38,14 @@ class JavaScriptHelperTest < Test::Unit::TestCase
html = link_to_function( "Greet me!" ) do |page|
page.replace_html 'header', "<h1>Greetings</h1>"
end
assert_dom_equal %(<a href="#" onclick="Element.update(&quot;header&quot;, &quot;\\074h1\\076Greetings\\074\\/h1\\076&quot;);; return false;">Greet me!</a>), html
assert_dom_equal %(<a href="#" onclick="Element.update(&quot;header&quot;, &quot;\\u003Ch1\\u003EGreetings\\u003C\\/h1\\u003E&quot;);; return false;">Greet me!</a>), html
end
def test_link_to_function_with_rjs_block_and_options
html = link_to_function( "Greet me!", :class => "updater" ) do |page|
page.replace_html 'header', "<h1>Greetings</h1>"
end
assert_dom_equal %(<a href="#" class="updater" onclick="Element.update(&quot;header&quot;, &quot;\\074h1\\076Greetings\\074\\/h1\\076&quot;);; return false;">Greet me!</a>), html
assert_dom_equal %(<a href="#" class="updater" onclick="Element.update(&quot;header&quot;, &quot;\\u003Ch1\\u003EGreetings\\u003C\\/h1\\u003E&quot;);; return false;">Greet me!</a>), html
end
def test_link_to_function_with_href
@ -67,14 +67,14 @@ class JavaScriptHelperTest < Test::Unit::TestCase
html = button_to_function( "Greet me!" ) do |page|
page.replace_html 'header', "<h1>Greetings</h1>"
end
assert_dom_equal %(<input type="button" onclick="Element.update(&quot;header&quot;, &quot;\\074h1\\076Greetings\\074\\/h1\\076&quot;);;" value="Greet me!" />), html
assert_dom_equal %(<input type="button" onclick="Element.update(&quot;header&quot;, &quot;\\u003Ch1\\u003EGreetings\\u003C\\/h1\\u003E&quot;);;" value="Greet me!" />), html
end
def test_button_to_function_with_rjs_block_and_options
html = button_to_function( "Greet me!", :class => "greeter" ) do |page|
page.replace_html 'header', "<h1>Greetings</h1>"
end
assert_dom_equal %(<input type="button" class="greeter" onclick="Element.update(&quot;header&quot;, &quot;\\074h1\\076Greetings\\074\\/h1\\076&quot;);;" value="Greet me!" />), html
assert_dom_equal %(<input type="button" class="greeter" onclick="Element.update(&quot;header&quot;, &quot;\\u003Ch1\\u003EGreetings\\u003C\\/h1\\u003E&quot;);;" value="Greet me!" />), html
end
def test_button_to_function_with_onclick

View file

@ -303,23 +303,23 @@ class JavaScriptGeneratorTest < Test::Unit::TestCase
end
def test_insert_html_with_string
assert_equal 'new Insertion.Top("element", "\\074p\\076This is a test\\074\\/p\\076");',
assert_equal 'new Insertion.Top("element", "\\u003Cp\\u003EThis is a test\\u003C\\/p\\u003E");',
@generator.insert_html(:top, 'element', '<p>This is a test</p>')
assert_equal 'new Insertion.Bottom("element", "\\074p\076This is a test\\074\\/p\076");',
assert_equal 'new Insertion.Bottom("element", "\\u003Cp\u003EThis is a test\\u003C\\/p\u003E");',
@generator.insert_html(:bottom, 'element', '<p>This is a test</p>')
assert_equal 'new Insertion.Before("element", "\\074p\076This is a test\\074\\/p\076");',
assert_equal 'new Insertion.Before("element", "\\u003Cp\u003EThis is a test\\u003C\\/p\u003E");',
@generator.insert_html(:before, 'element', '<p>This is a test</p>')
assert_equal 'new Insertion.After("element", "\\074p\076This is a test\\074\\/p\076");',
assert_equal 'new Insertion.After("element", "\\u003Cp\u003EThis is a test\\u003C\\/p\u003E");',
@generator.insert_html(:after, 'element', '<p>This is a test</p>')
end
def test_replace_html_with_string
assert_equal 'Element.update("element", "\\074p\\076This is a test\\074\\/p\\076");',
assert_equal 'Element.update("element", "\\u003Cp\\u003EThis is a test\\u003C\\/p\\u003E");',
@generator.replace_html('element', '<p>This is a test</p>')
end
def test_replace_element_with_string
assert_equal 'Element.replace("element", "\\074div id=\"element\"\\076\\074p\\076This is a test\\074\\/p\\076\\074\\/div\\076");',
assert_equal 'Element.replace("element", "\\u003Cdiv id=\"element\"\\u003E\\u003Cp\\u003EThis is a test\\u003C\\/p\\u003E\\u003C\\/div\\u003E");',
@generator.replace('element', '<div id="element"><p>This is a test</p></div>')
end
@ -375,10 +375,10 @@ class JavaScriptGeneratorTest < Test::Unit::TestCase
@generator.replace_html('baz', '<p>This is a test</p>')
assert_equal <<-EOS.chomp, @generator.to_s
new Insertion.Top("element", "\\074p\\076This is a test\\074\\/p\\076");
new Insertion.Bottom("element", "\\074p\\076This is a test\\074\\/p\\076");
new Insertion.Top("element", "\\u003Cp\\u003EThis is a test\\u003C\\/p\\u003E");
new Insertion.Bottom("element", "\\u003Cp\\u003EThis is a test\\u003C\\/p\\u003E");
["foo", "bar"].each(Element.remove);
Element.update("baz", "\\074p\\076This is a test\\074\\/p\\076");
Element.update("baz", "\\u003Cp\\u003EThis is a test\\u003C\\/p\\u003E");
EOS
end

View file

@ -11,7 +11,7 @@ class SerializationTest < Test::Unit::TestCase
:avatar => 'binarydata',
:created_at => Time.utc(2006, 8, 1),
:awesome => false,
:preferences => { :gem => 'ruby' }
:preferences => { :gem => '<strong>ruby</strong>' }
}
@contact = Contact.new(@contact_attributes)

View file

@ -1,5 +1,7 @@
*SVN*
* Change JSON to encode %w(< > &) as 4 digit hex codes to be in compliance with the JSON spec. Closes #9975 [josh, chuyeow, tpope]
* Fix JSON encoding/decoding bugs dealing with /'s. Closes #9990 [Rick, theamazingrando]
* Introduce a base class for all test cases used by rails applications. ActiveSupport::TestCase [Koz]

View file

@ -9,8 +9,9 @@ module ActiveSupport
"\t" => '\t',
'"' => '\"',
'\\' => '\\\\',
">" => '\076',
'<' => '\074',
'>' => '\u003E',
'<' => '\u003C',
'&' => '\u0026',
'/' => '\\/'
}
end
@ -19,7 +20,7 @@ end
class String
def to_json(options = nil) #:nodoc:
'"' + gsub(/[\010\f\n\r\t"\\><\/]/) { |s|
'"' + gsub(/[\010\f\n\r\t"\\><&\/]/) { |s|
ActiveSupport::JSON::Encoding::ESCAPED_CHARS[s]
}.gsub(/([\xC0-\xDF][\x80-\xBF]|
[\xE0-\xEF][\x80-\xBF]{2}|

View file

@ -13,8 +13,8 @@ class TestJSONEncoding < Test::Unit::TestCase
NumericTests = [[ 1, %(1) ],
[ 2.5, %(2.5) ]]
StringTests = [[ 'this is the <string>', %("this is the \\074string\\076")],
[ 'a "string" with quotes', %("a \\"string\\" with quotes") ],
StringTests = [[ 'this is the <string>', %("this is the \\u003Cstring\\u003E")],
[ 'a "string" with quotes & an ampersand', %("a \\"string\\" with quotes \\u0026 an ampersand") ],
[ 'http://test.host/posts/1', %("http:\\/\\/test.host\\/posts\\/1")]]
ArrayTests = [[ ['a', 'b', 'c'], %([\"a\", \"b\", \"c\"]) ],