Basic support for the XPath "preceding" axis.

This commit is contained in:
Yorick Peterse 2014-08-05 10:16:37 +02:00
parent 375f3d7870
commit fc1d9776f3
2 changed files with 79 additions and 0 deletions

View File

@ -328,6 +328,32 @@ module Oga
return nodes
end
##
# Evaluates the `preceding` axis.
#
# @param [Oga::XPath::Node] ast_node
# @param [Oga::XML::NodeSet] context
# @return [Oga::XML::NodeSet]
#
def on_axis_preceding(ast_node, context)
nodes = XML::NodeSet.new
context.each do |context_node|
check = true
@document.each_node do |doc_node|
# Test everything *until* we hit the current context node.
if doc_node == context_node
break
elsif node_matches?(doc_node, ast_node)
nodes << doc_node
end
end
end
return nodes
end
##
# Returns a node set containing all the child nodes of the given set of
# nodes.

View File

@ -0,0 +1,53 @@
require 'spec_helper'
describe Oga::XPath::Evaluator do
context 'preceding axis' do
before do
@document = parse(<<-EOF.strip.gsub(/\s+/m, ''))
<root>
<foo>
<bar></bar>
<baz>
<baz></baz>
</baz>
</foo>
<baz></baz>
</root>
EOF
@first_foo = @document.children[0].children[0]
@first_bar = @first_foo.children[0]
@first_baz = @first_foo.children[1]
@second_baz = @first_baz.children[0]
@evaluator = described_class.new(@document)
end
context 'matching nodes in the current context' do
before do
@set = @evaluator.evaluate('root/foo/baz/preceding::bar')
end
it_behaves_like :node_set, :length => 1
example 'return the first <bar> node' do
@set[0].should == @first_bar
end
end
context 'matching nodes in other contexts' do
before do
@set = @evaluator.evaluate('root/baz/preceding::baz')
end
it_behaves_like :node_set, :length => 2
example 'return the first <baz> node' do
@set[0].should == @first_baz
end
example 'return the second <baz> node' do
@set[1].should == @second_baz
end
end
end
end