Fixed XML entity encoding/decoding ordering.

Thanks to @krasnoukhov for providing the initial patch, which this commit is
largely based on.

This fixes #49.
This commit is contained in:
Yorick Peterse 2014-11-17 22:39:43 +01:00
parent 675eb562e2
commit ad4f650c5d
2 changed files with 32 additions and 4 deletions

View File

@ -4,15 +4,15 @@ module Oga
## ##
# Hash containing XML entities and the corresponding characters. # Hash containing XML entities and the corresponding characters.
# #
# The `&` mapping must come first to ensure proper conversion of non # The `&` mapping must come last to ensure proper conversion of non
# encoded to encoded forms (see {Oga::XML::Text#to_xml}). # encoded to encoded forms (see {Oga::XML::Text#to_xml}).
# #
# @return [Hash] # @return [Hash]
# #
DECODE_MAPPING = { DECODE_MAPPING = {
'&' => '&',
'&lt;' => '<', '&lt;' => '<',
'&gt;' => '>' '&gt;' => '>',
'&amp;' => '&'
} }
## ##
@ -20,7 +20,11 @@ module Oga
# #
# @return [Hash] # @return [Hash]
# #
ENCODE_MAPPING = DECODE_MAPPING.invert ENCODE_MAPPING = {
'&' => '&amp;',
'>' => '&gt;',
'<' => '&lt;'
}
## ##
# Decodes XML entities. # Decodes XML entities.

View File

@ -13,6 +13,22 @@ describe Oga::XML::Entities do
example 'decode &gt; into >' do example 'decode &gt; into >' do
described_class.decode('&gt;').should == '>' described_class.decode('&gt;').should == '>'
end end
example 'decode &amp;gt; into &gt;' do
described_class.decode('&amp;gt;').should == '&gt;'
end
example 'decode &amp;&amp;gt; into &>' do
described_class.decode('&amp;&amp;gt;').should == '&&gt;'
end
example 'decode &amp;lt; into <' do
described_class.decode('&amp;lt;').should == '&lt;'
end
example 'decode &amp;&amp;lt; into &<' do
described_class.decode('&amp;&amp;lt;').should == '&&lt;'
end
end end
context 'encode' do context 'encode' do
@ -27,5 +43,13 @@ describe Oga::XML::Entities do
example 'encode > as &gt;' do example 'encode > as &gt;' do
described_class.encode('>').should == '&gt;' described_class.encode('>').should == '&gt;'
end end
example 'encode &gt; as &amp;gt;' do
described_class.encode('&gt;').should == '&amp;gt;'
end
example 'encode &lt; as &amp;lt;' do
described_class.encode('&lt;').should == '&amp;lt;'
end
end end
end end