From eb0b71478ad4c37fdc4ec77bf2cf7c6b65dd36c4 Mon Sep 17 00:00:00 2001 From: Daniel Schierbeck Date: Tue, 23 Aug 2011 13:42:36 +0200 Subject: Simplify JavaScriptHelper#escape_javascript Use the Linus-style conditional. Also fix the documentation slightly. Signed-off-by: Andrew White --- actionpack/lib/action_view/helpers/javascript_helper.rb | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/helpers/javascript_helper.rb b/actionpack/lib/action_view/helpers/javascript_helper.rb index 474ea53f91..d01e62378b 100644 --- a/actionpack/lib/action_view/helpers/javascript_helper.rb +++ b/actionpack/lib/action_view/helpers/javascript_helper.rb @@ -20,17 +20,16 @@ module ActionView JS_ESCAPE_MAP["\342\200\250"] = '
' end - # Escape carrier returns and single and double quotes for JavaScript segments. + # Escapes carriage returns and single and double quotes for JavaScript segments. + # # Also available through the alias j(). This is particularly helpful in JavaScript responses, like: # # $('some_element').replaceWith('<%=j render 'some/element_template' %>'); def escape_javascript(javascript) - if javascript - result = javascript.gsub(/(\\|<\/|\r\n|\342\200\250|[\n\r"'])/u) {|match| JS_ESCAPE_MAP[match] } - javascript.html_safe? ? result.html_safe : result - else - '' - end + return "" if javascript.empty? + + result = javascript.gsub(/(\\|<\/|\r\n|\342\200\250|[\n\r"'])/u) {|match| JS_ESCAPE_MAP[match] } + javascript.html_safe? ? result.html_safe : result end alias_method :j, :escape_javascript -- cgit v1.2.3