Module: Oga::XML::Entities

Defined in:
lib/oga/xml/entities.rb

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

{
  '&lt;'   => '<',
  '&#60;'  => '<',
  '&gt;'   => '>',
  '&#62;'  => '>',
  '&apos;' => "'",
  '&#39;'  => "'",
  '&quot;' => '"',
  '&#34;'  => '"',
  '&amp;'  => '&',
  '&#38;'  => '&',
}
ENCODE_MAPPING =

Hash containing characters and the corresponding XML entities.

{
  '&' => '&amp;',
  '"' => '&quot;',
  "'" => '&apos;',
  '>' => '&gt;',
  '<' => '&lt;',
}

Class Method Summary collapse

Class Method Details

.decode(input) ⇒ String

Decodes XML entities.


44
45
46
47
48
49
50
51
52
# File 'lib/oga/xml/entities.rb', line 44

def self.decode(input)
  if input.include?('&')
    DECODE_MAPPING.each do |find, replace|
      input = input.gsub(find, replace)
    end
  end

  return input
end

.encode(input) ⇒ String

Encodes special characters as XML entities.


60
61
62
63
64
65
66
# File 'lib/oga/xml/entities.rb', line 60

def self.encode(input)
  ENCODE_MAPPING.each do |from, to|
    input = input.gsub(from, to) if input.include?(from)
  end

  return input
end