Class: Canon::Xml::CharacterEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/canon/xml/character_encoder.rb

Overview

Character encoder for C14N 1.1 Handles UTF-8 encoding and character reference encoding per spec

Instance Method Summary collapse

Instance Method Details

#encode_attribute(value) ⇒ Object

Encode attribute value Replace: & → &amp;, < → &lt;, “ → &quot;,

#x9 → &#x9;, #xA → &#xA;, #xD → &#xD;


24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/canon/xml/character_encoder.rb', line 24

def encode_attribute(value)
  value.gsub(/[&<"\t\n\r]/) do |char|
    case char
    when "&" then "&amp;"
    when "<" then "&lt;"
    when '"' then "&quot;"
    when "\t" then "&#x9;"
    when "\n" then "&#xA;"
    when "\r" then "&#xD;"
    end
  end
end

#encode_text(text) ⇒ Object

Encode text node content Replace: & → &amp;, < → &lt;, > → &gt;, #xD → &#xD;



10
11
12
13
14
15
16
17
18
19
# File 'lib/canon/xml/character_encoder.rb', line 10

def encode_text(text)
  text.gsub(/[&<>\r]/) do |char|
    case char
    when "&" then "&amp;"
    when "<" then "&lt;"
    when ">" then "&gt;"
    when "\r" then "&#xD;"
    end
  end
end