Basic support for the XPath "parent" axis.

The usage of `parent::node()` is not yet supported.
This commit is contained in:
Yorick Peterse 2014-08-05 09:34:57 +02:00
parent c0a6610d65
commit 375f3d7870
2 changed files with 63 additions and 0 deletions

View File

@ -307,6 +307,27 @@ module Oga
return nodes return nodes
end end
##
# Evaluates the `parent` axis.
#
# @param [Oga::XPath::Node] ast_node
# @param [Oga::XML::NodeSet] context
# @return [Oga::XML::NodeSet]
#
def on_axis_parent(ast_node, context)
nodes = XML::NodeSet.new
context.each do |context_node|
next unless has_parent?(context_node)
parent = context_node.parent
nodes << parent if node_matches?(parent, ast_node)
end
return nodes
end
## ##
# Returns a node set containing all the child nodes of the given set of # Returns a node set containing all the child nodes of the given set of
# nodes. # nodes.

View File

@ -0,0 +1,42 @@
require 'spec_helper'
describe Oga::XPath::Evaluator do
context 'parent axis' do
before do
@document = parse('<a><b></b></a>')
@evaluator = described_class.new(@document)
end
context 'matching nodes without parents' do
before do
@set = @evaluator.evaluate('parent::a')
end
it_behaves_like :empty_node_set
end
context 'matching nodes with parents' do
before do
@set = @evaluator.evaluate('a/b/parent::a')
end
it_behaves_like :node_set, :length => 1
example 'return the <a> node' do
@set[0].should == @document.children[0]
end
end
context 'matching nodes with parents using the short axis form' do
before do
@set = @evaluator.evaluate('a/b/parent::node()')
end
it_behaves_like :node_set, :length => 1
example 'return the <a> node' do
@set[0].should == @document.children[0]
end
end
end
end