diff --git a/lib/oga/xpath/evaluator.rb b/lib/oga/xpath/evaluator.rb
index 12f871f..422a043 100644
--- a/lib/oga/xpath/evaluator.rb
+++ b/lib/oga/xpath/evaluator.rb
@@ -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.
diff --git a/spec/oga/xpath/evaluator/calls/local_name_spec.rb b/spec/oga/xpath/evaluator/calls/local_name_spec.rb
new file mode 100644
index 0000000..7d66d4d
--- /dev/null
+++ b/spec/oga/xpath/evaluator/calls/local_name_spec.rb
@@ -0,0 +1,46 @@
+require 'spec_helper'
+
+describe Oga::XPath::Evaluator do
+ context 'local-name() function' do
+ before do
+ @document = parse('')
+ @evaluator = described_class.new(@document)
+ end
+
+ context 'outside predicates' do
+ example 'return the local name of the node' do
+ @evaluator.evaluate('local-name(root/x:a)').should == 'a'
+ end
+
+ example 'return the local name of the 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 node' do
+ @set[0].should == @document.children[0].children[1]
+ end
+ end
+ end
+end