Sha256: 67890a5790084f73c6e0dedd11e4dcf47ee15f3e89c937c35efba1623c898453

Contents?: true

Size: 750 Bytes

Versions: 9

Compression:

Stored size: 750 Bytes

Contents

# encoding: utf-8
module Apiary
  module Helpers
    module 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_method :j, :escape_javascript
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
apiaryio-0.4.2 lib/apiary/helpers/javascript_helper.rb
apiaryio-0.4.1 lib/apiary/helpers/javascript_helper.rb
apiaryio-0.4.0-x86_64-darwin-13 lib/apiary/helpers/javascript_helper.rb
apiaryio-0.3.5 lib/apiary/helpers/javascript_helper.rb
apiaryio-0.3.4 lib/apiary/helpers/javascript_helper.rb
apiaryio-0.3.3 lib/apiary/helpers/javascript_helper.rb
apiaryio-0.3.2 lib/apiary/helpers/javascript_helper.rb
apiaryio-0.3.1 lib/apiary/helpers/javascript_helper.rb
apiaryio-0.3.0 lib/apiary/helpers/javascript_helper.rb