Make JavaScriptHelper#j() an alias for JavaScriptHelper#escape_javascript() -- note this then supersedes the Object#j() method that the JSON gem adds within templates using the JavaScriptHelper [DHH]

This commit is contained in:
David Heinemeier Hansson 2011-03-26 10:28:39 -07:00
parent 45c233ef81
commit cb3e96a447
2 changed files with 7 additions and 0 deletions

@ -1,5 +1,7 @@
*Rails 3.1.0 (unreleased)* *Rails 3.1.0 (unreleased)*
* Make JavaScriptHelper#j() an alias for JavaScriptHelper#escape_javascript() -- note this then supersedes the Object#j() method that the JSON gem adds within templates using the JavaScriptHelper [DHH]
* Sensitive query string parameters (specified in config.filter_parameters) will now be filtered out from the request paths in the log file. [Prem Sichanugrist, fxn] * Sensitive query string parameters (specified in config.filter_parameters) will now be filtered out from the request paths in the log file. [Prem Sichanugrist, fxn]
* URL parameters which return false for to_param now appear in the query string (previously they were removed) [Andrew White] * URL parameters which return false for to_param now appear in the query string (previously they were removed) [Andrew White]

@ -47,6 +47,9 @@ module JavaScriptHelper
"'" => "\\'" } "'" => "\\'" }
# Escape carrier returns and single and double quotes for JavaScript segments. # Escape carrier 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) def escape_javascript(javascript)
if javascript if javascript
javascript.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { JS_ESCAPE_MAP[$1] } javascript.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { JS_ESCAPE_MAP[$1] }
@ -55,6 +58,8 @@ def escape_javascript(javascript)
end end
end end
alias_method :escape_javascript, :j
# Returns a JavaScript tag with the +content+ inside. Example: # Returns a JavaScript tag with the +content+ inside. Example:
# javascript_tag "alert('All is good')" # javascript_tag "alert('All is good')"
# #