File tree Expand file tree Collapse file tree 2 files changed +12
-2
lines changed
Expand file tree Collapse file tree 2 files changed +12
-2
lines changed Original file line number Diff line number Diff line change @@ -12,7 +12,9 @@ module JavaScriptHelper
1212 "\n " => '\n' ,
1313 "\r " => '\n' ,
1414 '"' => '\\"' ,
15- "'" => "\\ '"
15+ "'" => "\\ '" ,
16+ "`" => "\\ `" ,
17+ "$" => "\\ $"
1618 }
1719
1820 JS_ESCAPE_MAP [ ( +"\342 \200 \250 " ) . force_encoding ( Encoding ::UTF_8 ) . encode! ] = "
"
@@ -29,7 +31,7 @@ def escape_javascript(javascript)
2931 if javascript . empty?
3032 result = ""
3133 else
32- result = javascript . gsub ( /(\\ |<\/ |\r \n |\342 \200 \250 |\342 \200 \251 |[\n \r "'])/u ) { |match | JS_ESCAPE_MAP [ match ] }
34+ result = javascript . gsub ( /(\\ |<\/ |\r \n |\342 \200 \250 |\342 \200 \251 |[\n \r "']|[`]|[$] )/u ) { |match | JS_ESCAPE_MAP [ match ] }
3335 end
3436 javascript . html_safe? ? result . html_safe : result
3537 end
Original file line number Diff line number Diff line change @@ -36,6 +36,14 @@ def test_escape_javascript
3636 assert_equal %(dont <\\ /close> tags) , j ( %(dont </close> tags) )
3737 end
3838
39+ def test_escape_backtick
40+ assert_equal "\\ `" , escape_javascript ( "`" )
41+ end
42+
43+ def test_escape_dollar_sign
44+ assert_equal "\\ $" , escape_javascript ( "$" )
45+ end
46+
3947 def test_escape_javascript_with_safebuffer
4048 given = %('quoted' "double-quoted" new-line:\n </closed>)
4149 expect = %(\\ 'quoted\\ ' \\ "double-quoted\\ " new-line:\\ n <\\ /closed>)
You can’t perform that action at this time.
0 commit comments