ensure u2029 is escaped in escape_javascript helper

This commit is contained in:
benmmurphy 2012-02-22 16:11:03 +00:00 committed by Ben Murphy
parent 4d2d0d9906
commit c8168a7cdc
2 changed files with 5 additions and 1 deletions

@ -14,6 +14,8 @@ module JavaScriptHelper
}
JS_ESCAPE_MAP["\342\200\250".force_encoding('UTF-8').encode!] = '
'
JS_ESCAPE_MAP["\342\200\251".force_encoding('UTF-8').encode!] = '
'
# Escapes carriage returns and single and double quotes for JavaScript segments.
#
@ -22,7 +24,7 @@ module JavaScriptHelper
# $('some_element').replaceWith('<%=j render 'some/element_template' %>');
def escape_javascript(javascript)
if javascript
result = javascript.gsub(/(\\|<\/|\r\n|\342\200\250|[\n\r"'])/u) {|match| JS_ESCAPE_MAP[match] }
result = javascript.gsub(/(\\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"'])/u) {|match| JS_ESCAPE_MAP[match] }
javascript.html_safe? ? result.html_safe : result
else
''

@ -28,6 +28,8 @@ def test_escape_javascript
assert_equal %(backslash\\\\test), escape_javascript( %(backslash\\test) )
assert_equal %(dont <\\/close> tags), escape_javascript(%(dont </close> tags))
assert_equal %(unicode &#x2028; newline), escape_javascript(%(unicode \342\200\250 newline).force_encoding('UTF-8').encode!)
assert_equal %(unicode &#x2029; newline), escape_javascript(%(unicode \342\200\251 newline).force_encoding('UTF-8').encode!)
assert_equal %(dont <\\/close> tags), j(%(dont </close> tags))
end