Support for the XPath string-length() function.

This commit is contained in:
Yorick Peterse 2014-08-25 23:46:18 +02:00
parent a60057db5c
commit 06bed1cfdd
2 changed files with 58 additions and 0 deletions

View File

@ -966,6 +966,22 @@ module Oga
return haystack_str[start_index..stop_index]
end
##
# Processes the `string-length()` function.
#
# This function returns the length of the string given in the 1st argument
# *or* the current context node. If the expression is not a string it's
# converted to a string using the `string()` function.
#
# @see [#on_call_string]
# @param [Oga::XML::NodeSet] context
# @param [Oga::XPath::Node] expression
# @return [Float]
#
def on_call_string_length(context, expression = nil)
return on_call_string(context, expression).length.to_f
end
##
# Processes an `(int)` node.
#

View File

@ -0,0 +1,42 @@
require 'spec_helper'
describe Oga::XPath::Evaluator do
context 'string-length() function' do
before do
@document = parse('<root><a>x</a></root>')
@evaluator = described_class.new(@document)
end
context 'outside predicates' do
example 'return the length of a literal string' do
@evaluator.evaluate('string-length("foo")').should == 3.0
end
example 'return the length of a literal integer' do
@evaluator.evaluate('string-length(10)').should == 2.0
end
example 'return the length of a literal float' do
# This includes the counting of the dot. That is, "10.5".length => 4
@evaluator.evaluate('string-length(10.5)').should == 4.0
end
example 'return the length of a string in a node set' do
@evaluator.evaluate('string-length(root)').should == 1.0
end
end
context 'inside predicates' do
before do
# Since the length of <a> is 1 this should just return the <a> node.
@set = @evaluator.evaluate('root/a[string-length()]')
end
it_behaves_like :node_set, :length => 1
example 'return the <a> node' do
@set[0].should == @document.children[0].children[0]
end
end
end
end