Manually increment XPath indexes.

This ensures that the index is only incremented for elements that match a node
test.
This commit is contained in:
Yorick Peterse 2014-09-03 23:51:53 +02:00
parent 81a62a6f00
commit fbf3ae6e36
2 changed files with 28 additions and 5 deletions

View File

@ -171,13 +171,12 @@ module Oga
def on_test(ast_node, context) def on_test(ast_node, context)
nodes = XML::NodeSet.new nodes = XML::NodeSet.new
predicate = ast_node.children[2] predicate = ast_node.children[2]
xpath_index = 1
context.each_with_index do |xml_node, index| context.each do |xml_node|
next unless node_matches?(xml_node, ast_node) next unless node_matches?(xml_node, ast_node)
if predicate if predicate
xpath_index = index + 1
retval = with_node_set(context) do retval = with_node_set(context) do
process(predicate, XML::NodeSet.new([xml_node])) process(predicate, XML::NodeSet.new([xml_node]))
end end
@ -197,6 +196,8 @@ module Oga
else else
nodes << xml_node nodes << xml_node
end end
xpath_index += 1
end end
return nodes return nodes

View File

@ -0,0 +1,22 @@
require 'spec_helper'
describe Oga::XPath::Evaluator do
context 'predicates' do
before do
@document = parse('<root><a></a><b>10</b><b>20</b></root>')
@evaluator = described_class.new(@document)
end
context 'using predicate indexes' do
before do
@set = @evaluator.evaluate('root/b[2]')
end
it_behaves_like :node_set, :length => 1
example 'return the second <b> node' do
@set[0].should == @document.children[0].children[-1]
end
end
end
end