module Rack::Tracker::JavaScriptHelper

This module is extracted from Rails to provide reliable javascript escaping.

@see github.com/rails/rails/blob/master/actionview/lib/action_view/helpers/javascript_helper.rb

Constants

JS_ESCAPE_MAP

Public Instance Methods

escape_javascript(javascript) click to toggle source

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' %>');
# File lib/rack/tracker/javascript_helper.rb, line 25
def escape_javascript(javascript)
  if javascript
    javascript.to_s.gsub(/(\\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"'])/u) { |match| JS_ESCAPE_MAP[match] }
  else
    ''
  end
end
Also aliased as: j
j(javascript)
Alias for: escape_javascript