Sha256: 0769a2483aec12487c83e35252f848582a09b975c24ad33192bd77ccbf7675a5
Contents?: true
Size: 1.06 KB
Versions: 18
Compression:
Stored size: 1.06 KB
Contents
# This module is extracted from Rails to provide reliable javascript escaping. # # @see https://github.com/rails/rails/blob/master/actionview/lib/action_view/helpers/javascript_helper.rb module Rack::Tracker::JavaScriptHelper JS_ESCAPE_MAP = { '\\' => '\\\\', '</' => '<\/', "\r\n" => '\n', "\n" => '\n', "\r" => '\n', '"' => '\\"', "'" => "\\'" } JS_ESCAPE_MAP["\342\200\250".force_encoding(Encoding::UTF_8).encode!] = '
' JS_ESCAPE_MAP["\342\200\251".force_encoding(Encoding::UTF_8).encode!] = '
' # 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 javascript.to_s.gsub(/(\\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"'])/u) { |match| JS_ESCAPE_MAP[match] } else '' end end alias_method :j, :escape_javascript end
Version data entries
18 entries across 18 versions & 1 rubygems