XPath support for absolute paths without tests.

This allows Oga to parse and evaluate the XPath expression "/". This expression
can be used to select just the root node/document.

This fixes #35.
This commit is contained in:
Yorick Peterse 2014-09-15 17:06:58 +02:00
parent 398aaf68bc
commit b06eadc812
4 changed files with 19 additions and 1 deletions

View File

@ -131,7 +131,8 @@ module Oga
context = XML::NodeSet.new([@document])
end
return on_path(ast_node, context)
# If the expression is just "/" we'll just return the current context.
return ast_node.children.empty? ? context : on_path(ast_node, context)
end
##

View File

@ -66,6 +66,7 @@ rule
absolute_path
: T_SLASH path_members { s(:absolute_path, *val[1]) }
| T_SLASH path_member { s(:absolute_path, val[1]) }
| T_SLASH { s(:absolute_path) }
;
# Whenever a bare test is used (e.g. just "A") this actually means "child::A".

View File

@ -30,6 +30,18 @@ describe Oga::XPath::Evaluator do
end
end
context 'absolute paths without node tests' do
before do
@set = described_class.new(@document).evaluate('/')
end
it_behaves_like :node_set, :length => 1
example 'return the root document' do
@set[0].is_a?(Oga::XML::Document).should == true
end
end
context 'invalid absolute paths' do
before do
@set = described_class.new(@document).evaluate('/x/a')

View File

@ -20,5 +20,9 @@ describe Oga::XPath::Parser do
s(:axis, 'child', s(:test, nil, 'B'))
)
end
example 'parse an absolute path without a node test' do
parse_xpath('/').should == s(:absolute_path)
end
end
end