Support for the XPath descendant axis.
This commit is contained in:
parent
2ed7c457a5
commit
dd37b028a0
|
@ -217,6 +217,25 @@ module Oga
|
|||
return on_test(node, context)
|
||||
end
|
||||
|
||||
##
|
||||
# Evaluator the `descendant` axis. This method processes child nodes until
|
||||
# the very end of the tree, no "short-circuiting" mechanism is used.
|
||||
#
|
||||
# @param [Oga::XPath::Node] node
|
||||
# @param [Oga::XML::NodeSet] context
|
||||
# @return [Oga::XML::NodeSet]
|
||||
#
|
||||
def on_axis_descendant(node, context)
|
||||
nodes = on_test(node, context)
|
||||
children = child_nodes(context)
|
||||
|
||||
unless children.empty?
|
||||
nodes += on_axis_descendant(node, children)
|
||||
end
|
||||
|
||||
return nodes
|
||||
end
|
||||
|
||||
##
|
||||
# Returns a node set containing all the child nodes of the given set of
|
||||
# nodes.
|
||||
|
|
|
@ -2,7 +2,7 @@ require 'spec_helper'
|
|||
|
||||
describe Oga::XPath::Evaluator do
|
||||
before do
|
||||
@document = parse('<a><b><c></c></b><d></d></a>')
|
||||
@document = parse('<a><b><c></c></b><d></d><a></a></a>')
|
||||
@c_node = @document.children[0].children[0].children[0]
|
||||
end
|
||||
|
||||
|
@ -170,4 +170,61 @@ describe Oga::XPath::Evaluator do
|
|||
it_behaves_like :empty_node_set
|
||||
end
|
||||
end
|
||||
|
||||
context 'descendant axis' do
|
||||
before do
|
||||
@evaluator = described_class.new(@document)
|
||||
|
||||
@first_a = @document.children[0]
|
||||
@second_a = @first_a.children[-1]
|
||||
end
|
||||
|
||||
context 'direct descendants' do
|
||||
before do
|
||||
@set = @evaluator.evaluate('descendant::a')
|
||||
end
|
||||
|
||||
it_behaves_like :node_set, :length => 2
|
||||
|
||||
example 'return the first <a> node' do
|
||||
@set[0].should == @first_a
|
||||
end
|
||||
|
||||
example 'return the second <a> node' do
|
||||
@set[1].should == @second_a
|
||||
end
|
||||
end
|
||||
|
||||
context 'nested descendants' do
|
||||
before do
|
||||
@set = @evaluator.evaluate('descendant::c')
|
||||
end
|
||||
|
||||
it_behaves_like :node_set, :length => 1
|
||||
|
||||
example 'return the <c> node' do
|
||||
@set[0].name.should == 'c'
|
||||
end
|
||||
end
|
||||
|
||||
context 'descendants of a specific node' do
|
||||
before do
|
||||
@set = @evaluator.evaluate('a/descendant::a')
|
||||
end
|
||||
|
||||
it_behaves_like :node_set, :length => 1
|
||||
|
||||
example 'return the second <a> node' do
|
||||
@set[0].should == @second_a
|
||||
end
|
||||
end
|
||||
|
||||
context 'invalid descendants' do
|
||||
before do
|
||||
@set = @evaluator.evaluate('descendant::foobar')
|
||||
end
|
||||
|
||||
it_behaves_like :empty_node_set
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue