diff options
author | Andrew Vit <andrew@avit.ca> | 2018-08-09 02:26:46 -0700 |
---|---|---|
committer | Andrew Vit <andrew@avit.ca> | 2018-09-21 12:30:40 -0700 |
commit | dd0cfb03b233e23b13e087f1cd39169b1ff6d936 (patch) | |
tree | 32754939d03b01d8cb33c6110bef31465f3f3301 /actionview/lib | |
parent | 0193b89be6827fb6aa20f83af164901dc3e31590 (diff) | |
download | rails-dd0cfb03b233e23b13e087f1cd39169b1ff6d936.tar.gz rails-dd0cfb03b233e23b13e087f1cd39169b1ff6d936.tar.bz2 rails-dd0cfb03b233e23b13e087f1cd39169b1ff6d936.zip |
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 %>
Diffstat (limited to 'actionview/lib')
-rw-r--r-- | actionview/lib/action_view/helpers/javascript_helper.rb | 9 |
1 files changed, 5 insertions, 4 deletions
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 |