Sha256: a04b5f7b504feb612f27edd09b2b30feabaa665bd63d8058eab5758918542bea

Contents?: true

Size: 634 Bytes

Versions: 13

Compression:

Stored size: 634 Bytes

Contents

# encoding: utf-8

module Apiary::Helpers::JavascriptHelper
  JS_ESCAPE_MAP = {
    '\\'    => '\\\\',
    '</'    => '<\/',
    "\r\n"  => '\n',
    "\n"    => '\n',
    "\r"    => '\n',
    '"'     => '\\"',
    "'"     => "\\'"
  }

  JS_ESCAPE_MAP["\342\200\250".force_encoding(Encoding::UTF_8).encode!] = '&#x2028;'
  JS_ESCAPE_MAP["\342\200\251".force_encoding(Encoding::UTF_8).encode!] = '&#x2029;'

  def escape_javascript(javascript)
    if javascript
      javascript.gsub(/(\\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"'])/u) { |match| JS_ESCAPE_MAP[match] }
    else
      ''
    end
  end

  alias j escape_javascript
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
apiaryio-0.17.0 lib/apiary/helpers/javascript_helper.rb
apiaryio-0.16.1 lib/apiary/helpers/javascript_helper.rb
apiaryio-0.16.0 lib/apiary/helpers/javascript_helper.rb
apiaryio-0.15.2 lib/apiary/helpers/javascript_helper.rb
apiaryio-0.15.1 lib/apiary/helpers/javascript_helper.rb
apiaryio-0.15.0 lib/apiary/helpers/javascript_helper.rb
apiaryio-0.14.1 lib/apiary/helpers/javascript_helper.rb
apiaryio-0.14.0 lib/apiary/helpers/javascript_helper.rb
apiaryio-0.13.0 lib/apiary/helpers/javascript_helper.rb
apiaryio-0.12.0 lib/apiary/helpers/javascript_helper.rb
apiaryio-0.11.1 lib/apiary/helpers/javascript_helper.rb
apiaryio-0.11.0 lib/apiary/helpers/javascript_helper.rb
apiaryio-0.10.2 lib/apiary/helpers/javascript_helper.rb