Added XML::Node#each_ancestor
This method can be used to walk through the ancestor tree of a Node.
This commit is contained in:
parent
db39b25546
commit
52741a3b78
|
@ -214,6 +214,26 @@ module Oga
|
||||||
def xml?
|
def xml?
|
||||||
!html?
|
!html?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Yields all ancestor elements of the current node.
|
||||||
|
#
|
||||||
|
# @example
|
||||||
|
# some_element.each_ancestor do |node|
|
||||||
|
# # ...
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# @yieldparam [Oga::XML::Node]
|
||||||
|
#
|
||||||
|
def each_ancestor
|
||||||
|
node = parent
|
||||||
|
|
||||||
|
while node.is_a?(XML::Element)
|
||||||
|
yield node
|
||||||
|
|
||||||
|
node = node.parent
|
||||||
|
end
|
||||||
|
end
|
||||||
end # Element
|
end # Element
|
||||||
end # XML
|
end # XML
|
||||||
end # Oga
|
end # Oga
|
||||||
|
|
|
@ -276,4 +276,18 @@ describe Oga::XML::Node do
|
||||||
node.xml?.should == false
|
node.xml?.should == false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#each_ancestor' do
|
||||||
|
before do
|
||||||
|
@child2 = Oga::XML::Element.new(:name => 'c')
|
||||||
|
@child1 = Oga::XML::Element.new(:name => 'b', :children => [@child2])
|
||||||
|
@root = Oga::XML::Element.new(:name => 'a', :children => [@child1])
|
||||||
|
@document = Oga::XML::Document.new(:children => [@root])
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'yields all the ancestor elements' do
|
||||||
|
expect { |b| @child2.each_ancestor(&b) }
|
||||||
|
.to yield_successive_args(@child1, @root)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue