Added Oga::EntityDecoder
This module removes some of the code duplication needed to determine what entity decoder to use.
This commit is contained in:
parent
3f6aa04e91
commit
ef7f50137a
|
@ -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.
|
||||
|
|
|
@ -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
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue