XPath compiler support for "ancestor"

This commit is contained in:
Yorick Peterse 2015-07-18 01:08:36 +02:00
parent f96b30fb1f
commit 4f03bf19c1
2 changed files with 19 additions and 0 deletions

View File

@ -192,6 +192,21 @@ module Oga
self_test.followed_by(ancestors_test) self_test.followed_by(ancestors_test)
end 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. # Processes a node test predicate.
# #

View File

@ -22,6 +22,10 @@ describe Oga::XPath::Compiler do
evaluate_xpath(@c1, 'ancestor::*[1]').should == node_set(@b1) evaluate_xpath(@c1, 'ancestor::*[1]').should == node_set(@b1)
end 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 it 'returns an empty node set for non existing ancestors' do
evaluate_xpath(@c1, 'ancestor::c').should == node_set evaluate_xpath(@c1, 'ancestor::c').should == node_set
end end