Support for the XPath child axis.

This commit is contained in:
Yorick Peterse 2014-07-22 21:24:45 +02:00
parent ec08b41737
commit a0ecba6321
3 changed files with 76 additions and 1 deletions

View File

@ -150,7 +150,7 @@ module Oga
while has_parent?(xml_node)
xml_node = xml_node.parent
if node_matches?(xml_node, node)
if can_match_node?(xml_node) and node_matches?(xml_node, node)
nodes << xml_node
break
end
@ -206,6 +206,17 @@ module Oga
return nodes
end
##
# Evaluates the `child` axis. This simply delegates work to {#on_test}.
#
# @param [Oga::XPath::Node] node
# @param [Oga::XML::NodeSet] context
# @return [Oga::XML::NodeSet]
#
def on_axis_child(node, context)
return on_test(node, context)
end
##
# Returns a node set containing all the child nodes of the given set of
# nodes.

View File

@ -34,6 +34,14 @@ describe Oga::XPath::Evaluator do
@set[0].name.should == 'a'
end
end
context 'missing ancestors' do
before do
@set = @evaluator.evaluate('ancestor::foobar')
end
it_behaves_like :empty_node_set
end
end
context 'ancestor-or-self axis' do
@ -115,5 +123,51 @@ describe Oga::XPath::Evaluator do
@set[0].name.should == 'x'
end
end
context 'missing attributes' do
before do
@set = @evaluator.evaluate('attribute::bar')
end
it_behaves_like :empty_node_set
end
end
context 'child axis' do
before do
@evaluator = described_class.new(@document)
end
context 'direct children' do
before do
@set = @evaluator.evaluate('child::a')
end
it_behaves_like :node_set, :length => 1
example 'return the <a> node' do
@set[0].name.should == 'a'
end
end
context 'nested children' do
before do
@set = @evaluator.evaluate('child::a/child::b')
end
it_behaves_like :node_set, :length => 1
example 'return the <b> node' do
@set[0].name.should == 'b'
end
end
context 'invalid children' do
before do
@set = @evaluator.evaluate('child::foobar')
end
it_behaves_like :empty_node_set
end
end
end

View File

@ -7,3 +7,13 @@ shared_examples :node_set do |options|
@set.length.should == options[:length]
end
end
shared_examples :empty_node_set do
example 'return a NodeSet instance' do
@set.is_a?(Oga::XML::NodeSet).should == true
end
example 'return the right amount of rows' do
@set.empty?.should == true
end
end