File: escaper.rb

package info (click to toggle)
ruby-gon 5.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 496 kB
  • ctags: 145
  • sloc: ruby: 1,416; makefile: 5
file content (36 lines) | stat: -rw-r--r-- 851 bytes parent folder | download
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
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)
        type = { type: 'text/javascript' } if type
        content_tag(:script, javascript_cdata_section(content, cdata).html_safe, type)
      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