Sha256: f8a2d3222672dabeb9af94297e981a446bf3842db37912ea89a2e519cd37beb3

Contents?: true

Size: 765 Bytes

Versions: 39

Compression:

Stored size: 765 Bytes

Contents

# frozen_string_literal: true

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.freeze

    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

39 entries across 39 versions & 1 rubygems

Version Path
react_on_rails-13.4.0 lib/react_on_rails/json_output.rb
react_on_rails-13.3.5 lib/react_on_rails/json_output.rb
react_on_rails-13.3.4 lib/react_on_rails/json_output.rb
react_on_rails-13.3.3 lib/react_on_rails/json_output.rb
react_on_rails-13.3.2 lib/react_on_rails/json_output.rb
react_on_rails-13.3.1 lib/react_on_rails/json_output.rb
react_on_rails-13.3.0 lib/react_on_rails/json_output.rb
react_on_rails-13.2.0 lib/react_on_rails/json_output.rb
react_on_rails-13.1.0 lib/react_on_rails/json_output.rb
react_on_rails-13.0.2 lib/react_on_rails/json_output.rb
react_on_rails-13.0.1 lib/react_on_rails/json_output.rb
react_on_rails-13.0.0 lib/react_on_rails/json_output.rb
react_on_rails-13.0.0.beta.0 lib/react_on_rails/json_output.rb
react_on_rails-12.6.0 lib/react_on_rails/json_output.rb
react_on_rails-12.5.2 lib/react_on_rails/json_output.rb
react_on_rails-12.5.1 lib/react_on_rails/json_output.rb
react_on_rails-12.5.0 lib/react_on_rails/json_output.rb
react_on_rails-12.4.0 lib/react_on_rails/json_output.rb
react_on_rails-12.4.0.rc.0 lib/react_on_rails/json_output.rb
react_on_rails-12.3.0 lib/react_on_rails/json_output.rb