module AngularRailsTemplates::CompactJavaScriptEscape
Constants
- JS_ESCAPE_MAP
inspired by Rails' action_view/helpers/javascript_helper.rb
Public Instance Methods
escape_javascript(raw)
click to toggle source
We want to deliver the shortist valid javascript escaped string Count the number of “ vs ' If more ', escape ” If more “, escape ' If equal, prefer to escape ”
# File lib/angular-rails-templates/compact_javascript_escape.rb, line 19 def escape_javascript(raw) if raw quote = raw.count(%{'}) >= raw.count(%{"}) ? %{"} : %{'} escaped = raw.gsub(/(\\|\r\n|[\n\r#{quote}])/u) {|match| JS_ESCAPE_MAP[match] } "#{quote}#{escaped}#{quote}" else '""' end end