Basic support for the preceding-sibling xpath axis
This commit is contained in:
parent
e34a1ed481
commit
8a9b26fa73
|
@ -354,6 +354,33 @@ module Oga
|
|||
return nodes
|
||||
end
|
||||
|
||||
##
|
||||
# Evaluates the `preceding-sibling` axis.
|
||||
#
|
||||
# @param [Oga::XPath::Node] ast_node
|
||||
# @param [Oga::XML::NodeSet] context
|
||||
# @return [Oga::XML::NodeSet]
|
||||
#
|
||||
def on_axis_preceding_sibling(ast_node, context)
|
||||
nodes = XML::NodeSet.new
|
||||
|
||||
context.each do |context_node|
|
||||
check = true
|
||||
parent = has_parent?(context_node) ? context_node.parent : nil
|
||||
|
||||
@document.each_node do |doc_node|
|
||||
# Test everything *until* we hit the current context node.
|
||||
if doc_node == context_node
|
||||
break
|
||||
elsif doc_node.parent == parent and 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.
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
context 'preceding-sibling axis' do
|
||||
before do
|
||||
@document = parse(<<-EOF.strip.gsub(/\s+/m, ''))
|
||||
<root>
|
||||
<foo>
|
||||
</foo>
|
||||
<bar>
|
||||
<foo></foo>
|
||||
<baz></baz>
|
||||
</bar>
|
||||
</root>
|
||||
EOF
|
||||
|
||||
@second_foo = @document.children[0].children[1].children[0]
|
||||
@evaluator = described_class.new(@document)
|
||||
end
|
||||
|
||||
context 'matching nodes in the current context' do
|
||||
before do
|
||||
@set = @evaluator.evaluate('root/bar/baz/preceding-sibling::foo')
|
||||
end
|
||||
|
||||
it_behaves_like :node_set, :length => 1
|
||||
|
||||
example 'return the second <foo> node' do
|
||||
@set[0].should == @second_foo
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue