Support for the XPath child axis.
This commit is contained in:
parent
ec08b41737
commit
a0ecba6321
|
@ -150,7 +150,7 @@ module Oga
|
||||||
while has_parent?(xml_node)
|
while has_parent?(xml_node)
|
||||||
xml_node = xml_node.parent
|
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
|
nodes << xml_node
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
@ -206,6 +206,17 @@ module Oga
|
||||||
return nodes
|
return nodes
|
||||||
end
|
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
|
# Returns a node set containing all the child nodes of the given set of
|
||||||
# nodes.
|
# nodes.
|
||||||
|
|
|
@ -34,6 +34,14 @@ describe Oga::XPath::Evaluator do
|
||||||
@set[0].name.should == 'a'
|
@set[0].name.should == 'a'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'missing ancestors' do
|
||||||
|
before do
|
||||||
|
@set = @evaluator.evaluate('ancestor::foobar')
|
||||||
|
end
|
||||||
|
|
||||||
|
it_behaves_like :empty_node_set
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'ancestor-or-self axis' do
|
context 'ancestor-or-self axis' do
|
||||||
|
@ -115,5 +123,51 @@ describe Oga::XPath::Evaluator do
|
||||||
@set[0].name.should == 'x'
|
@set[0].name.should == 'x'
|
||||||
end
|
end
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,3 +7,13 @@ shared_examples :node_set do |options|
|
||||||
@set.length.should == options[:length]
|
@set.length.should == options[:length]
|
||||||
end
|
end
|
||||||
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
|
||||||
|
|
Loading…
Reference in New Issue