Proper handling of decimals for string() calls.

When calling the string() XPath function floats with zero decimals (10.0, 5.0,
etc) should result in a string without any decimals. Ruby converts 10.0 to
"10.0" whereas XPath expects "10".
This commit is contained in:
Yorick Peterse 2014-08-25 23:21:36 +02:00
parent 6c0c5ab720
commit a60057db5c
2 changed files with 8 additions and 1 deletions

View File

@ -784,6 +784,13 @@ module Oga
if convert.respond_to?(:text)
return convert.text
else
# If we have a number that has a zero decimal (e.g. 10.0) we want to
# get rid of that decimal. For this we'll first convert the number to
# an integer.
if convert.is_a?(Float) and convert.modulo(1).zero?
convert = convert.to_i
end
return convert.to_s
end
end

View File

@ -37,7 +37,7 @@ describe Oga::XPath::Evaluator do
end
example 'convert an integer to a string' do
@evaluator.evaluate('string(10)').should == '10.0'
@evaluator.evaluate('string(10)').should == '10'
end
example 'convert a float to a string' do