Permalink
Browse files

Merge pull request #1561 from sikachu/3-1-escapejs

Make escape_javascript happy to handle SafeBuffers
  • Loading branch information...
2 parents caa0b1d + 4663893 commit abc029d64e9a6936d0d3ba84baaf9389d96f53d1 @josevalim josevalim committed Jun 8, 2011
@@ -18,7 +18,7 @@ module JavaScriptHelper
# $('some_element').replaceWith('<%=j render 'some/element_template' %>');
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
@@ -30,6 +30,13 @@ def test_escape_javascript
assert_equal %(dont <\\/close> tags), j(%(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 abc029d

Please sign in to comment.