mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Extract json string escaping
This commit is contained in:
parent
5577d561f9
commit
4a8d2ef0a5
2 changed files with 33 additions and 38 deletions
|
@ -11,6 +11,33 @@ module ActiveSupport
|
|||
# matches YAML-formatted dates
|
||||
DATE_REGEX = /^(?:\d{4}-\d{2}-\d{2}|\d{4}-\d{1,2}-\d{1,2}[ \t]+\d{1,2}:\d{2}:\d{2}(\.[0-9]*)?(([ \t]*)Z|[-+]\d{2}?(:\d{2})?))$/
|
||||
|
||||
module Encoding #:nodoc:
|
||||
mattr_accessor :escape_regex
|
||||
|
||||
ESCAPED_CHARS = {
|
||||
"\010" => '\b',
|
||||
"\f" => '\f',
|
||||
"\n" => '\n',
|
||||
"\r" => '\r',
|
||||
"\t" => '\t',
|
||||
'"' => '\"',
|
||||
'\\' => '\\\\',
|
||||
'>' => '\u003E',
|
||||
'<' => '\u003C',
|
||||
'&' => '\u0026'
|
||||
}
|
||||
|
||||
def self.escape(string)
|
||||
json = '"' + string.gsub(escape_regex) { |s| ESCAPED_CHARS[s] }
|
||||
json.force_encoding('ascii-8bit') if respond_to?(:force_encoding)
|
||||
json.gsub(/([\xC0-\xDF][\x80-\xBF]|
|
||||
[\xE0-\xEF][\x80-\xBF]{2}|
|
||||
[\xF0-\xF7][\x80-\xBF]{3})+/nx) { |s|
|
||||
s.unpack("U*").pack("n*").unpack("H*")[0].gsub(/.{4}/, '\\\\u\&')
|
||||
} + '"'
|
||||
end
|
||||
end
|
||||
|
||||
class << self
|
||||
attr_reader :backend
|
||||
delegate :decode, :to => :backend
|
||||
|
@ -34,9 +61,7 @@ module ActiveSupport
|
|||
end
|
||||
|
||||
class << self
|
||||
def escape_html_entities_in_json
|
||||
@escape_html_entities_in_json
|
||||
end
|
||||
attr_reader :escape_html_entities_in_json
|
||||
|
||||
def escape_html_entities_in_json=(value)
|
||||
ActiveSupport::JSON::Encoding.escape_regex = \
|
||||
|
@ -48,8 +73,9 @@ module ActiveSupport
|
|||
@escape_html_entities_in_json = value
|
||||
end
|
||||
end
|
||||
|
||||
JSON.backend = 'Yaml'
|
||||
end
|
||||
|
||||
require 'active_support/json/encoding'
|
||||
ActiveSupport.escape_html_entities_in_json = true
|
||||
ActiveSupport::JSON.backend = 'Yaml'
|
||||
|
||||
require 'active_support/json/encoding'
|
||||
|
|
|
@ -1,36 +1,5 @@
|
|||
module ActiveSupport
|
||||
module JSON
|
||||
module Encoding
|
||||
mattr_accessor :escape_regex
|
||||
|
||||
ESCAPED_CHARS = {
|
||||
"\010" => '\b',
|
||||
"\f" => '\f',
|
||||
"\n" => '\n',
|
||||
"\r" => '\r',
|
||||
"\t" => '\t',
|
||||
'"' => '\"',
|
||||
'\\' => '\\\\',
|
||||
'>' => '\u003E',
|
||||
'<' => '\u003C',
|
||||
'&' => '\u0026'
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
ActiveSupport.escape_html_entities_in_json = true
|
||||
|
||||
class String
|
||||
def rails_to_json(options = nil) #:nodoc:
|
||||
json = '"' + gsub(ActiveSupport::JSON::Encoding.escape_regex) { |s|
|
||||
ActiveSupport::JSON::Encoding::ESCAPED_CHARS[s]
|
||||
}
|
||||
json.force_encoding('ascii-8bit') if respond_to?(:force_encoding)
|
||||
json.gsub(/([\xC0-\xDF][\x80-\xBF]|
|
||||
[\xE0-\xEF][\x80-\xBF]{2}|
|
||||
[\xF0-\xF7][\x80-\xBF]{3})+/nx) { |s|
|
||||
s.unpack("U*").pack("n*").unpack("H*")[0].gsub(/.{4}/, '\\\\u\&')
|
||||
} + '"'
|
||||
ActiveSupport::JSON::Encoding.escape(self)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue