diff --git a/lib/oga/xpath/compiler.rb b/lib/oga/xpath/compiler.rb index 5f1327b..6f65c8c 100644 --- a/lib/oga/xpath/compiler.rb +++ b/lib/oga/xpath/compiler.rb @@ -192,6 +192,21 @@ module Oga self_test.followed_by(ancestors_test) end + ## + # Processes the `ancestor` axis. + # + # @param [AST::Node] ast + # @param [Oga::Ruby::Node] input + # @return [Oga::Ruby::Node] + # + def on_axis_ancestor(ast, input, &block) + parent = literal('parent') + + input.each_ancestor.add_block(parent) do + process(ast, parent, &block).if_true { yield parent } + end + end + ## # Processes a node test predicate. # diff --git a/spec/oga/xpath/compiler/axes/ancestor_spec.rb b/spec/oga/xpath/compiler/axes/ancestor_spec.rb index bcffee7..5772ba3 100644 --- a/spec/oga/xpath/compiler/axes/ancestor_spec.rb +++ b/spec/oga/xpath/compiler/axes/ancestor_spec.rb @@ -22,6 +22,10 @@ describe Oga::XPath::Compiler do evaluate_xpath(@c1, 'ancestor::*[1]').should == node_set(@b1) end + it 'returns a node set containing all ancestors' do + evaluate_xpath(@c1, 'ancestor::*').should == node_set(@b1, @a1) + end + it 'returns an empty node set for non existing ancestors' do evaluate_xpath(@c1, 'ancestor::c').should == node_set end