Module: Oga::XML::Entities
- Defined in:
- lib/oga/xml/entities.rb
Overview
Module for encoding/decoding XML and HTML entities. The mapping of HTML entities can be found in HTML::Entities::DECODE_MAPPING.
Constant Summary collapse
- DECODE_MAPPING =
Hash containing XML entities and the corresponding characters.
The
&
mapping must come last to ensure proper conversion of non encoded to encoded forms (see Text#to_xml). { '<' => '<', '>' => '>', ''' => "'", '"' => '"', '&' => '&', }
- ENCODE_MAPPING =
Hash containing characters and the corresponding XML entities.
{ '&' => '&', '>' => '>', '<' => '<', }
- ENCODE_ATTRIBUTE_MAPPING =
Hash containing characters and the corresponding XML entities to use when encoding XML/HTML attribute values.
{ '&' => '&', '>' => '>', '<' => '<', "'" => ''', '"' => '"' }
- AMPERSAND =
'&'.freeze
- REGULAR_ENTITY =
Regexp for matching XML/HTML entities such as " ".
/&[a-zA-Z0-9]+;/
- CODEPOINT_ENTITY =
Regexp for matching XML/HTML entities such as "&".
/&#(x)?([a-zA-Z0-9]+);/
- ENCODE_REGEXP =
Regexp.new(ENCODE_MAPPING.keys.join('|'))
- ENCODE_ATTRIBUTE_REGEXP =
Regexp.new(ENCODE_ATTRIBUTE_MAPPING.keys.join('|'))
Class Method Summary collapse
-
.decode(input, mapping = DECODE_MAPPING) ⇒ String
Decodes XML entities.
-
.encode(input, mapping = ENCODE_MAPPING) ⇒ String
Encodes special characters as XML entities.
-
.encode_attribute(input) ⇒ String
Encodes special characters in an XML attribute value.
Class Method Details
.decode(input, mapping = DECODE_MAPPING) ⇒ String
Decodes XML entities.
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/oga/xml/entities.rb', line 86 def self.decode(input, mapping = DECODE_MAPPING) return input unless input.include?(AMPERSAND) input = input.gsub(REGULAR_ENTITY, mapping) if input.include?(AMPERSAND) input = input.gsub(CODEPOINT_ENTITY) do |match| [$1 ? Integer($2, 16) : Integer($2, 10)].pack('U*') end end input end |
.encode(input, mapping = ENCODE_MAPPING) ⇒ String
Encodes special characters as XML entities.
107 108 109 |
# File 'lib/oga/xml/entities.rb', line 107 def self.encode(input, mapping = ENCODE_MAPPING) input.gsub(ENCODE_REGEXP, mapping) end |
.encode_attribute(input) ⇒ String
Encodes special characters in an XML attribute value.
117 118 119 |
# File 'lib/oga/xml/entities.rb', line 117 def self.encode_attribute(input) input.gsub(ENCODE_ATTRIBUTE_REGEXP, ENCODE_ATTRIBUTE_MAPPING) end |