From dd0cfb03b233e23b13e087f1cd39169b1ff6d936 Mon Sep 17 00:00:00 2001 From: Andrew Vit Date: Thu, 9 Aug 2018 02:26:46 -0700 Subject: Let escape_javascript handle conversion to string This brings `escape_javascript` in line with the behavior of `json_escape` and allows other value types to be output without needing explicit casting in the view template. Example: <%= javascript_tag do %> var locale = '<%== j I18n.locale %>'; // locale is a symbol <% end %> --- actionview/lib/action_view/helpers/javascript_helper.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'actionview/lib/action_view/helpers') diff --git a/actionview/lib/action_view/helpers/javascript_helper.rb b/actionview/lib/action_view/helpers/javascript_helper.rb index 830088bea3..f80d0bd4ed 100644 --- a/actionview/lib/action_view/helpers/javascript_helper.rb +++ b/actionview/lib/action_view/helpers/javascript_helper.rb @@ -25,12 +25,13 @@ module ActionView # # $('some_element').replaceWith('<%= j render 'some/element_template' %>'); def escape_javascript(javascript) - if javascript - result = javascript.gsub(/(\\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"'])/u) { |match| JS_ESCAPE_MAP[match] } - javascript.html_safe? ? result.html_safe : result + javascript = javascript.to_s + if javascript.empty? + result = "" else - "" + result = javascript.gsub(/(\\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"'])/u) { |match| JS_ESCAPE_MAP[match] } end + javascript.html_safe? ? result.html_safe : result end alias_method :j, :escape_javascript -- cgit v1.2.3