diff --git a/lib/oga/xpath/evaluator.rb b/lib/oga/xpath/evaluator.rb index 2efb983..089aae3 100644 --- a/lib/oga/xpath/evaluator.rb +++ b/lib/oga/xpath/evaluator.rb @@ -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. diff --git a/spec/oga/xpath/evaluator/axes_spec.rb b/spec/oga/xpath/evaluator/axes_spec.rb index 519c4a6..f313ebf 100644 --- a/spec/oga/xpath/evaluator/axes_spec.rb +++ b/spec/oga/xpath/evaluator/axes_spec.rb @@ -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 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 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 diff --git a/spec/support/shared_examples.rb b/spec/support/shared_examples.rb index 552dd3f..ede42dd 100644 --- a/spec/support/shared_examples.rb +++ b/spec/support/shared_examples.rb @@ -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