From ad4f650c5d1a883ef56ca37ef775d778a6ab4b00 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Mon, 17 Nov 2014 22:39:43 +0100 Subject: [PATCH] Fixed XML entity encoding/decoding ordering. Thanks to @krasnoukhov for providing the initial patch, which this commit is largely based on. This fixes #49. --- lib/oga/xml/entities.rb | 12 ++++++++---- spec/oga/xml/entities_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/lib/oga/xml/entities.rb b/lib/oga/xml/entities.rb index c054705..3fcef2b 100644 --- a/lib/oga/xml/entities.rb +++ b/lib/oga/xml/entities.rb @@ -4,15 +4,15 @@ module Oga ## # 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}). # # @return [Hash] # DECODE_MAPPING = { - '&' => '&', '<' => '<', - '>' => '>' + '>' => '>', + '&' => '&' } ## @@ -20,7 +20,11 @@ module Oga # # @return [Hash] # - ENCODE_MAPPING = DECODE_MAPPING.invert + ENCODE_MAPPING = { + '&' => '&', + '>' => '>', + '<' => '<' + } ## # Decodes XML entities. diff --git a/spec/oga/xml/entities_spec.rb b/spec/oga/xml/entities_spec.rb index 89e2de0..6a080ff 100644 --- a/spec/oga/xml/entities_spec.rb +++ b/spec/oga/xml/entities_spec.rb @@ -13,6 +13,22 @@ describe Oga::XML::Entities do example 'decode > into >' do described_class.decode('>').should == '>' end + + example 'decode &gt; into >' do + described_class.decode('&gt;').should == '>' + end + + example 'decode &&gt; into &>' do + described_class.decode('&&gt;').should == '&>' + end + + example 'decode &lt; into <' do + described_class.decode('&lt;').should == '<' + end + + example 'decode &&lt; into &<' do + described_class.decode('&&lt;').should == '&<' + end end context 'encode' do @@ -27,5 +43,13 @@ describe Oga::XML::Entities do example 'encode > as >' do described_class.encode('>').should == '>' end + + example 'encode > as &gt;' do + described_class.encode('>').should == '&gt;' + end + + example 'encode < as &lt;' do + described_class.encode('<').should == '&lt;' + end end end