Better setup for XPath predicates.

This allows filtering of nodes by indexes (e.g. using last() or a literal
number) and uses the correct index range (1 to N in XPath). The function
position() is not yet supported as this requires access to the current node,
which isn't passed down the call stack just yet.
This commit is contained in:
Yorick Peterse 2014-08-19 19:56:56 +02:00
parent 202f74a8eb
commit e0895be675
1 changed files with 20 additions and 1 deletions

View File

@ -106,7 +106,26 @@ module Oga
end
# Filter the nodes based on the predicate.
nodes = process(predicate, nodes) if predicate
if predicate
new_nodes = XML::NodeSet.new
nodes.each_with_index do |current, index|
xpath_index = index + 1
# TODO: pass the current node for functions such as position().
retval = process(predicate, nodes)
# Non empty node set? Keep the current node
if retval.is_a?(XML::NodeSet) and !retval.empty?
new_nodes << current
# In case of a number we'll use it as the index.
elsif retval.is_a?(Numeric) && retval.to_i == xpath_index
new_nodes << current
end
end
nodes = new_nodes
end
return nodes
end