diff --git a/lib/oga.rb b/lib/oga.rb index b53f8da..d671571 100644 --- a/lib/oga.rb +++ b/lib/oga.rb @@ -6,6 +6,7 @@ require 'thread' require_relative 'oga/version' require_relative 'oga/oga' require_relative 'oga/lru' +require_relative 'oga/entity_decoder' # Load these first so that the native extensions don't have to define the # Oga::XML namespace. diff --git a/lib/oga/entity_decoder.rb b/lib/oga/entity_decoder.rb new file mode 100644 index 0000000..24eaa4a --- /dev/null +++ b/lib/oga/entity_decoder.rb @@ -0,0 +1,21 @@ +module Oga + module EntityDecoder + ## + # @see [decode] + # + def self.try_decode(input, html = false) + return input ? decode(input, html) : nil + end + + ## + # @param [String] input + # @param [TrueClass|FalseClass] html + # @return [String] + # + def self.decode(input, html = false) + decoder = html ? HTML::Entities : XML::Entities + + return decoder.decode(input) + end + end # EntityDecoder +end # Oga diff --git a/lib/oga/xml/text.rb b/lib/oga/xml/text.rb index 16711b0..6d8115c 100644 --- a/lib/oga/xml/text.rb +++ b/lib/oga/xml/text.rb @@ -27,8 +27,7 @@ module Oga # def text unless @decoded - decoder = html? ? HTML::Entities : Entities - @text = decoder.decode(@text) + @text = EntityDecoder.try_decode(@text, html?) @decoded = true end diff --git a/spec/oga/entity_decoder_spec.rb b/spec/oga/entity_decoder_spec.rb new file mode 100644 index 0000000..1ba1be2 --- /dev/null +++ b/spec/oga/entity_decoder_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' + +describe Oga::EntityDecoder do + describe 'try_decode' do + it 'returns nil if the input is also nil' do + described_class.try_decode(nil).should be_nil + end + + it 'decodes XML entities' do + described_class.try_decode('<') + .should == Oga::XML::Entities::DECODE_MAPPING['<'] + end + + it 'decodes HTML entities' do + described_class.try_decode('©', true) + .should == Oga::HTML::Entities::DECODE_MAPPING['©'] + end + end + + describe 'decode' do + it 'decodes XML entities' do + described_class.decode('<') + .should == Oga::XML::Entities::DECODE_MAPPING['<'] + end + + it 'decodes HTML entities' do + described_class.decode('©', true) + .should == Oga::HTML::Entities::DECODE_MAPPING['©'] + end + end +end