diff --git a/lib/oga/xpath/evaluator.rb b/lib/oga/xpath/evaluator.rb index 3079104..a112d04 100644 --- a/lib/oga/xpath/evaluator.rb +++ b/lib/oga/xpath/evaluator.rb @@ -307,6 +307,27 @@ module Oga return nodes end + ## + # Evaluates the `parent` axis. + # + # @param [Oga::XPath::Node] ast_node + # @param [Oga::XML::NodeSet] context + # @return [Oga::XML::NodeSet] + # + def on_axis_parent(ast_node, context) + nodes = XML::NodeSet.new + + context.each do |context_node| + next unless has_parent?(context_node) + + parent = context_node.parent + + nodes << parent if node_matches?(parent, ast_node) + end + + return nodes + end + ## # Returns a node set containing all the child nodes of the given set of # nodes. diff --git a/spec/oga/xpath/evaluator/axes/parent_spec.rb b/spec/oga/xpath/evaluator/axes/parent_spec.rb new file mode 100644 index 0000000..26eb34e --- /dev/null +++ b/spec/oga/xpath/evaluator/axes/parent_spec.rb @@ -0,0 +1,42 @@ +require 'spec_helper' + +describe Oga::XPath::Evaluator do + context 'parent axis' do + before do + @document = parse('') + @evaluator = described_class.new(@document) + end + + context 'matching nodes without parents' do + before do + @set = @evaluator.evaluate('parent::a') + end + + it_behaves_like :empty_node_set + end + + context 'matching nodes with parents' do + before do + @set = @evaluator.evaluate('a/b/parent::a') + end + + it_behaves_like :node_set, :length => 1 + + example 'return the node' do + @set[0].should == @document.children[0] + end + end + + context 'matching nodes with parents using the short axis form' do + before do + @set = @evaluator.evaluate('a/b/parent::node()') + end + + it_behaves_like :node_set, :length => 1 + + example 'return the node' do + @set[0].should == @document.children[0] + end + end + end +end