Basic support for the XPath name() function.
This commit is contained in:
parent
e7019ceb4c
commit
1dd6416bea
|
@ -711,6 +711,31 @@ module Oga
|
|||
return node.is_a?(XML::Element) ? node.name : ''
|
||||
end
|
||||
|
||||
##
|
||||
# Processes the `name()` function call.
|
||||
#
|
||||
# This function call is similar to `local-name()` (see
|
||||
# {#on_call_local_name}) except that it includes the namespace name if
|
||||
# present.
|
||||
#
|
||||
# @param [Oga::XML::NodeSet] context
|
||||
# @param [Oga::XPath::Node] expression
|
||||
# @return [Oga::XML::NodeSet]
|
||||
#
|
||||
def on_call_name(context, expression = nil)
|
||||
node = function_node(context, expression)
|
||||
|
||||
if node.is_a?(XML::Element)
|
||||
if node.namespace
|
||||
return "#{node.namespace.name}:#{node.name}"
|
||||
else
|
||||
return node.name
|
||||
end
|
||||
else
|
||||
return ''
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# Processes the `namespace-uri()` function call.
|
||||
#
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Oga::XPath::Evaluator do
|
||||
context '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 name of the <x:a> node' do
|
||||
@evaluator.evaluate('name(root/x:a)').should == 'x:a'
|
||||
end
|
||||
|
||||
example 'return the name of the <b> node' do
|
||||
@evaluator.evaluate('name(root/b)').should == 'b'
|
||||
end
|
||||
|
||||
example 'return only the name of the first node in the set' do
|
||||
@evaluator.evaluate('name(root/*)').should == 'x:a'
|
||||
end
|
||||
|
||||
example 'return an empty string by default' do
|
||||
@evaluator.evaluate('name(foo)').should == ''
|
||||
end
|
||||
|
||||
example 'raise a TypeError for invalid argument types' do
|
||||
block = lambda { @evaluator.evaluate('name("foo")') }
|
||||
|
||||
block.should raise_error(TypeError)
|
||||
end
|
||||
end
|
||||
|
||||
context 'inside predicates' do
|
||||
before do
|
||||
@set = @evaluator.evaluate('root/b[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