Skip to content
Browse files

Merge pull request #1558 from tardate/3-0-stable

Fix for issue 1553 - escape_javascript to support SafeBuffer strings
  • Loading branch information...
2 parents eff72b2 + 75ecdfb commit 910b34d3b041e3a2017e7f39dcee058d6fd2c0e2 @josevalim josevalim committed Jun 8, 2011
View
2 actionpack/lib/action_view/helpers/javascript_helper.rb
@@ -49,7 +49,7 @@ module JavaScriptHelper
# Escape carrier returns and single and double quotes for JavaScript segments.
def escape_javascript(javascript)
if javascript
- javascript.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { JS_ESCAPE_MAP[$1] }
+ javascript.gsub(/(\\|<\/|\r\n|[\n\r"'])/) {|match| JS_ESCAPE_MAP[match] }
else
''
end
View
7 actionpack/test/template/javascript_helper_test.rb
@@ -31,6 +31,13 @@ def test_escape_javascript
assert_equal %(dont <\\/close> tags), escape_javascript(%(dont </close> tags))
end
+ def test_escape_javascript_with_safebuffer
+ given = %('quoted' "double-quoted" new-line:\n </closed>)
+ expect = %(\\'quoted\\' \\"double-quoted\\" new-line:\\n <\\/closed>)
+ assert_equal expect, escape_javascript(given)
+ assert_equal expect, escape_javascript(ActiveSupport::SafeBuffer.new(given))
+ end
+
def test_button_to_function
assert_dom_equal %(<input type="button" onclick="alert('Hello world!');" value="Greeting" />),
button_to_function("Greeting", "alert('Hello world!')")

0 comments on commit 910b34d

Please sign in to comment.
Something went wrong with that request. Please try again.