Support for the XPath concat() function.

This commit is contained in:
Yorick Peterse 2014-08-23 20:24:18 +02:00
parent 431a253000
commit b688c6dc1b
2 changed files with 53 additions and 2 deletions

View File

@ -786,8 +786,29 @@ module Oga
end
##
# Processes an `(int)` node. This method simply returns the value as a
# Float.
# Processes the `concat()` function call.
#
# This function call converts its arguments to strings and concatenates
# them. In case of node sets the text of the set is used.
#
# @param [Oga::XML::NodeSet] context
# @param [Oga::XPath::Node] first
# @param [Oga::XPath::Node] second
# @param [Array<Oga::XPath::Node>] rest
#
def on_call_concat(context, first, second, *rest)
args = [first, second] + rest
retval = ''
args.each do |arg|
retval << on_call_string(context, arg)
end
return retval
end
##
# Processes an `(int)` node.
#
# @param [Oga::XPath::Node] ast_node
# @param [Oga::XML::NodeSet] context

View File

@ -0,0 +1,30 @@
require 'spec_helper'
describe Oga::XPath::Evaluator do
context 'concat() function' do
before do
@document = parse('<root><a>foo</a><b>bar</b></root>')
@evaluator = described_class.new(@document)
end
example 'concatenate two strings' do
@evaluator.evaluate('concat("foo", "bar")').should == 'foobar'
end
example 'concatenate two integers' do
@evaluator.evaluate('concat(10, 20)').should == '1020'
end
example 'concatenate two floats' do
@evaluator.evaluate('concat(10.5, 20.5)').should == '10.520.5'
end
example 'concatenate two node sets' do
@evaluator.evaluate('concat(root/a, root/b)').should == 'foobar'
end
example 'concatenate a node set and a string' do
@evaluator.evaluate('concat(root/a, "baz")').should == 'foobaz'
end
end
end