File: escaper.rb

package info (click to toggle)
ruby-gon 6.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 504 kB
  • sloc: ruby: 1,383; makefile: 9
file content (39 lines) | stat: -rw-r--r-- 946 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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