fix escape_javascript for unicode character \u2028.

This commit is contained in:
Teng Siong Ong 2011-08-21 15:14:45 -05:00
parent f25d65d2fb
commit f6ceb944ea
2 changed files with 4 additions and 2 deletions

View File

@ -10,7 +10,8 @@ module ActionView
"\n" => '\n', "\n" => '\n',
"\r" => '\n', "\r" => '\n',
'"' => '\\"', '"' => '\\"',
"'" => "\\'" } "'" => "\\'",
"\342\200\250" => '
' }
# Escape carrier returns and single and double quotes for JavaScript segments. # Escape carrier returns and single and double quotes for JavaScript segments.
# Also available through the alias j(). This is particularly helpful in JavaScript responses, like: # Also available through the alias j(). This is particularly helpful in JavaScript responses, like:
@ -18,7 +19,7 @@ module ActionView
# $('some_element').replaceWith('<%=j render 'some/element_template' %>'); # $('some_element').replaceWith('<%=j render 'some/element_template' %>');
def escape_javascript(javascript) def escape_javascript(javascript)
if javascript if javascript
result = javascript.gsub(/(\\|<\/|\r\n|[\n\r"'])/) {|match| JS_ESCAPE_MAP[match] } result = javascript.gsub(/(\\|<\/|\r\n|\342\200\250|[\n\r"'])/) {|match| JS_ESCAPE_MAP[match] }
javascript.html_safe? ? result.html_safe : result javascript.html_safe? ? result.html_safe : result
else else
'' ''

View File

@ -27,6 +27,7 @@ class JavaScriptHelperTest < ActionView::TestCase
assert_equal %(This \\"thing\\" is really\\n netos\\'), escape_javascript(%(This "thing" is really\n netos')) assert_equal %(This \\"thing\\" is really\\n netos\\'), escape_javascript(%(This "thing" is really\n netos'))
assert_equal %(backslash\\\\test), escape_javascript( %(backslash\\test) ) assert_equal %(backslash\\\\test), escape_javascript( %(backslash\\test) )
assert_equal %(dont <\\/close> tags), escape_javascript(%(dont </close> tags)) assert_equal %(dont <\\/close> tags), escape_javascript(%(dont </close> tags))
assert_equal %(unicode &#x2028; newline), escape_javascript(%(unicode \342\200\250 newline))
assert_equal %(dont <\\/close> tags), j(%(dont </close> tags)) assert_equal %(dont <\\/close> tags), j(%(dont </close> tags))
end end