Manually increment XPath indexes.
This ensures that the index is only incremented for elements that match a node test.
This commit is contained in:
parent
81a62a6f00
commit
fbf3ae6e36
|
@ -169,15 +169,14 @@ module Oga
|
||||||
# @return [Oga::XML::NodeSet]
|
# @return [Oga::XML::NodeSet]
|
||||||
#
|
#
|
||||||
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
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue