Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Simplify JavaScriptHelper#escape_javascript

Use the Linus-style conditional. Also fix the documentation slightly.

Signed-off-by: Andrew White <andyw@pixeltrix.co.uk>
  • Loading branch information...
commit eb0b71478ad4c37fdc4ec77bf2cf7c6b65dd36c4 1 parent 29004de
Daniel Schierbeck dasch authored pixeltrix committed
Showing with 6 additions and 7 deletions.
  1. +6 −7 actionpack/lib/action_view/helpers/javascript_helper.rb
13 actionpack/lib/action_view/helpers/javascript_helper.rb
View
@@ -20,17 +20,16 @@ module JavaScriptHelper
JS_ESCAPE_MAP["\342\200\250"] = '&#x2028;'
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

2 comments on commit eb0b714

Andrea Campi

Linus-style? That's the first time I hear it

Andrea Campi

Linus-style? That's the first time I hear it

Please sign in to comment.
Something went wrong with that request. Please try again.