Added XML::Node#html? and XML::Node#xml?

The former has been moved over from XML::Text, the latter just inverts
html?.
This commit is contained in:
Yorick Peterse 2015-03-26 01:02:32 +01:00
parent 4ad502958d
commit b6fcd326ef
3 changed files with 48 additions and 11 deletions

View File

@ -163,6 +163,22 @@ module Oga
node_set.insert(index, other)
end
##
# @return [TrueClass|FalseClass]
#
def html?
root = root_node
return root.is_a?(Document) && root.html?
end
##
# @return [TrueClass|FalseClass]
#
def xml?
return !html?
end
end # Element
end # XML
end # Oga

View File

@ -48,17 +48,6 @@ module Oga
return Entities.encode(super)
end
private
##
# @return [TrueClass|FalseClass]
#
def html?
root = root_node
return root.is_a?(Document) && root.html?
end
end # Text
end # XML
end # Oga

View File

@ -204,4 +204,36 @@ describe Oga::XML::Node do
@container.children[1].should == other
end
end
describe '#html?' do
it 'returns true if the node resides within an HTML document' do
node = described_class.new
document = Oga::XML::Document.new(:children => [node], :type => :html)
node.html?.should == true
end
it 'returns false if the node resides within an XML document' do
node = described_class.new
document = Oga::XML::Document.new(:children => [node], :type => :xml)
node.html?.should == false
end
end
describe '#xml?' do
it 'returns true if the node resides within an XML document' do
node = described_class.new
document = Oga::XML::Document.new(:children => [node], :type => :xml)
node.xml?.should == true
end
it 'returns false if the node resides within an HTML document' do
node = described_class.new
document = Oga::XML::Document.new(:children => [node], :type => :html)
node.xml?.should == false
end
end
end