Module: AngularRailsTemplates::CompactJavaScriptEscape
- Included in:
- Processor
- Defined in:
- lib/angular-rails-templates/compact_javascript_escape.rb
Constant Summary collapse
- JS_ESCAPE_MAP =
inspired by Rails’ action_view/helpers/javascript_helper.rb
{ '\\' => '\\\\', "\r\n" => '\n', "\n" => '\n', "\r" => '\n', '"' => '\\"', "'" => "\\'" }
Instance Method Summary collapse
-
#escape_javascript(raw) ⇒ Object
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 ”.
Instance Method Details
#escape_javascript(raw) ⇒ Object
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 ”
19 20 21 22 23 24 25 26 27 |
# 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 |