Support for the XPath local-name() function.
This commit is contained in:
parent
2deb7a6d84
commit
9e20b5ca3e
|
@ -157,12 +157,12 @@ module Oga
|
|||
xpath_index = index + 1
|
||||
retval = with_node(current) { process(predicate, nodes) }
|
||||
|
||||
# Non empty node set? Keep the current node
|
||||
if retval.is_a?(XML::NodeSet) and !retval.empty?
|
||||
new_nodes << current
|
||||
# Numeric values are used as node set indexes.
|
||||
if retval.is_a?(Numeric)
|
||||
new_nodes << current if retval.to_i == xpath_index
|
||||
|
||||
# In case of a number we'll use it as the index.
|
||||
elsif retval.is_a?(Numeric) && retval.to_i == xpath_index
|
||||
# Node sets, strings, etc
|
||||
elsif retval and !retval.empty?
|
||||
new_nodes << current
|
||||
end
|
||||
end
|
||||
|
@ -693,6 +693,34 @@ module Oga
|
|||
return nodes
|
||||
end
|
||||
|
||||
##
|
||||
# Processes the `local-name()` function call.
|
||||
#
|
||||
# This function call returns the name of one of the following:
|
||||
#
|
||||
# * The current context node (if any)
|
||||
# * The first node in the supplied node set
|
||||
#
|
||||
# @param [Oga::XML::NodeSet] context
|
||||
# @param [Oga::XPath::Node] expression
|
||||
# @return [Oga::XML::NodeSet]
|
||||
#
|
||||
def on_call_local_name(context, expression = nil)
|
||||
if expression
|
||||
node = process(expression, context)
|
||||
|
||||
if node.is_a?(XML::NodeSet)
|
||||
node = node.first
|
||||
else
|
||||
raise TypeError, 'local-name() only takes node sets as arguments'
|
||||
end
|
||||
else
|
||||
node = current_node
|
||||
end
|
||||
|
||||
return node.is_a?(XML::Element) ? node.name : ''
|
||||
end
|
||||
|
||||
##
|
||||
# Processes an `(int)` node. This method simply returns the value as a
|
||||
# Float.
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
context 'local-name() function' do
|
||||
before do
|
||||
@document = parse('<root xmlns:x="y"><x:a></x:a><b></b></root>')
|
||||
@evaluator = described_class.new(@document)
|
||||
end
|
||||
|
||||
context 'outside predicates' do
|
||||
example 'return the local name of the <x:a> node' do
|
||||
@evaluator.evaluate('local-name(root/x:a)').should == 'a'
|
||||
end
|
||||
|
||||
example 'return the local name of the <b> node' do
|
||||
@evaluator.evaluate('local-name(root/b)').should == 'b'
|
||||
end
|
||||
|
||||
example 'return only the name of the first node in the set' do
|
||||
@evaluator.evaluate('local-name(root/*)').should == 'a'
|
||||
end
|
||||
|
||||
example 'return an empty string by default' do
|
||||
@evaluator.evaluate('local-name(foo)').should == ''
|
||||
end
|
||||
|
||||
example 'raise a TypeError for invalid argument types' do
|
||||
block = lambda { @evaluator.evaluate('local-name("foo")') }
|
||||
|
||||
block.should raise_error(TypeError)
|
||||
end
|
||||
end
|
||||
|
||||
context 'inside predicates' do
|
||||
before do
|
||||
@set = @evaluator.evaluate('root/b[local-name()]')
|
||||
end
|
||||
|
||||
it_behaves_like :node_set, :length => 1
|
||||
|
||||
example 'return the <b> node' do
|
||||
@set[0].should == @document.children[0].children[1]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue