Class: Moxml::C14n::CharacterEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/moxml/c14n/character_encoder.rb

Overview

Character encoder for C14N output. Handles the entity escaping mandated by W3C C14N 1.0 §2.4 / 1.1 §2.4 for text and attribute values.

Constant Summary collapse

TEXT_ESCAPES =
{
  "&" => "&",
  "<" => "&lt;",
  ">" => "&gt;",
  "\r" => "&#xD;",
}.freeze
ATTRIBUTE_ESCAPES =
TEXT_ESCAPES.merge(
  '"' => "&quot;",
  "\t" => "&#x9;",
  "\n" => "&#xA;",
).freeze

Instance Method Summary collapse

Instance Method Details

#encode_attribute(value) ⇒ Object



24
25
26
# File 'lib/moxml/c14n/character_encoder.rb', line 24

def encode_attribute(value)
  value.to_s.gsub(/[&<"\t\n\r]/, ATTRIBUTE_ESCAPES)
end

#encode_text(text) ⇒ Object



20
21
22
# File 'lib/moxml/c14n/character_encoder.rb', line 20

def encode_text(text)
  text.to_s.gsub(/[&<>\r]/, TEXT_ESCAPES)
end