Sha256: 109dbf2b2cb17aa917b68d534937fb71279bad15855e4ae9f14d8699963399ad

Contents?: true

Size: 727 Bytes

Versions: 14

Compression:

Stored size: 727 Bytes

Contents

require "active_support/core_ext/string/output_safety"

module ReactOnRails
  class JsonOutput
    ESCAPE_REPLACEMENT = {
      "&" => '\u0026',
      ">" => '\u003e',
      "<" => '\u003c',
      "\u2028" => '\u2028',
      "\u2029" => '\u2029'
    }.freeze
    ESCAPE_REGEXP = /[\u2028\u2029&><]/u

    def self.escape(json)
      return escape_without_erb_util(json) if Utils.rails_version_less_than_4_1_1

      ERB::Util.json_escape(json)
    end

    def self.escape_without_erb_util(json)
      # https://github.com/rails/rails/blob/60257141462137331387d0e34931555cf0720886/activesupport/lib/active_support/core_ext/string/output_safety.rb#L113

      json.to_s.gsub(ESCAPE_REGEXP, ESCAPE_REPLACEMENT)
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
react_on_rails-8.0.1 lib/react_on_rails/json_output.rb
react_on_rails-8.0.0 lib/react_on_rails/json_output.rb
react_on_rails-8.0.0.beta.3 lib/react_on_rails/json_output.rb
react_on_rails-8.0.0.beta.2 lib/react_on_rails/json_output.rb
react_on_rails-8.0.0.beta.1 lib/react_on_rails/json_output.rb
react_on_rails-7.1.0.beta.3 lib/react_on_rails/json_output.rb
react_on_rails-7.0.4 lib/react_on_rails/json_output.rb
react_on_rails-7.1.0.beta.2 lib/react_on_rails/json_output.rb
react_on_rails-7.0.3 lib/react_on_rails/json_output.rb
react_on_rails-7.1.0.beta.1 lib/react_on_rails/json_output.rb
react_on_rails-7.0.2 lib/react_on_rails/json_output.rb
react_on_rails-7.0.1 lib/react_on_rails/json_output.rb
react_on_rails-7.0.0 lib/react_on_rails/json_output.rb
react_on_rails-6.10.1 lib/react_on_rails/json_output.rb