From a56612ffd799798324b6867aa77b1746fe9508a1 Mon Sep 17 00:00:00 2001 From: RSL Date: Mon, 4 Jan 2010 12:53:39 -0500 Subject: [PATCH] Make escape_javascript a little more duck-friendly --- .../lib/action_view/helpers/javascript_helper.rb | 4 ++-- actionpack/test/template/javascript_helper_test.rb | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/actionpack/lib/action_view/helpers/javascript_helper.rb b/actionpack/lib/action_view/helpers/javascript_helper.rb index 8f64acf..c20cf9d 100644 --- a/actionpack/lib/action_view/helpers/javascript_helper.rb +++ b/actionpack/lib/action_view/helpers/javascript_helper.rb @@ -139,8 +139,8 @@ module ActionView # 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] } + if javascript && javascript.respond_to?(:to_s) + javascript.to_s.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { JS_ESCAPE_MAP[$1] } else '' end diff --git a/actionpack/test/template/javascript_helper_test.rb b/actionpack/test/template/javascript_helper_test.rb index f0f686f..2886ada 100644 --- a/actionpack/test/template/javascript_helper_test.rb +++ b/actionpack/test/template/javascript_helper_test.rb @@ -23,6 +23,7 @@ class JavaScriptHelperTest < ActionView::TestCase assert_equal %(This \\"thing\\" is really\\n netos\\'), escape_javascript(%(This "thing" is really\n netos')) assert_equal %(backslash\\\\test), escape_javascript( %(backslash\\test) ) assert_equal %(dont <\\/close> tags), escape_javascript(%(dont tags)) + assert_equal '1', escape_javascript(1) end def test_link_to_function -- 1.6.5.2