Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Make escape_javascript happy to handle SafeBuffers #1561

Closed
wants to merge 1 commit into from

3 participants

@sikachu
Collaborator

Port of #1558 to 3-1-stable

@tardate tardate Make escape_javascript happy to handle SafeBuffers
* see GH#1553
* allow for the fact that gsub on SafeBuffer does not pass match variables $1, $2 etc to a block
4663893
@josevalim
Owner

Merged.

@josevalim josevalim closed this
@jake3030 jake3030 referenced this pull request from a commit in jake3030/rails
Dan Pickett Fixed ActionView::TestCase current url context [#1561 state:resolved]
Signed-off-by: Joshua Peek <josh@joshpeek.com>
38412ec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 8, 2011
  1. @tardate @sikachu

    Make escape_javascript happy to handle SafeBuffers

    tardate authored sikachu committed
    * see GH#1553
    * allow for the fact that gsub on SafeBuffer does not pass match variables $1, $2 etc to a block
This page is out of date. Refresh to see the latest.
View
2  actionpack/lib/action_view/helpers/javascript_helper.rb
@@ -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
View
7 actionpack/test/template/javascript_helper_test.rb
@@ -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!')")
Something went wrong with that request. Please try again.