Sha256: e663f9279e39f02c28097069a24ee3e7cd4e74737ec6c8af8156013012f21606

Contents?: true

Size: 946 Bytes

Versions: 8

Compression:

Stored size: 946 Bytes

Contents

class Gon
  module Escaper
    extend ActionView::Helpers::JavaScriptHelper
    extend ActionView::Helpers::TagHelper

    class << self

      def escape_unicode(javascript)
        if javascript
          result = escape_line_separator(javascript)
          javascript.html_safe? ? result.html_safe : result
        end
      end

      def javascript_tag(content, type, cdata, nonce)
        options = {}
        options.merge!( { type: 'text/javascript' } ) if type
        options.merge!( { nonce: nonce } ) if nonce

        content_tag(:script, javascript_cdata_section(content, cdata).html_safe, options)
      end

      def javascript_cdata_section(content, cdata)
        if cdata
          "\n//#{cdata_section("\n#{content}\n//")}\n"
        else
          "\n#{content}\n"
        end
      end

      private

      def escape_line_separator(javascript)
        javascript.gsub(/\\u2028/u, '&#x2028;')
      end

    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
gon-6.4.0 lib/gon/escaper.rb
gon-6.3.2 lib/gon/escaper.rb
gon-6.3.1 lib/gon/escaper.rb
gon-6.2.1 lib/gon/escaper.rb
gon-6.2.0 lib/gon/escaper.rb
gon-6.1.0 lib/gon/escaper.rb
gon-6.0.1 lib/gon/escaper.rb
gon-6.0.0 lib/gon/escaper.rb