From a60057db5ce43061704aa0f8f8f93938b4b40161 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Mon, 25 Aug 2014 23:21:36 +0200 Subject: [PATCH] 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". --- lib/oga/xpath/evaluator.rb | 7 +++++++ spec/oga/xpath/evaluator/calls/string_spec.rb | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/oga/xpath/evaluator.rb b/lib/oga/xpath/evaluator.rb index 28b8204..9d1b6df 100644 --- a/lib/oga/xpath/evaluator.rb +++ b/lib/oga/xpath/evaluator.rb @@ -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 diff --git a/spec/oga/xpath/evaluator/calls/string_spec.rb b/spec/oga/xpath/evaluator/calls/string_spec.rb index b45b543..c09e310 100644 --- a/spec/oga/xpath/evaluator/calls/string_spec.rb +++ b/spec/oga/xpath/evaluator/calls/string_spec.rb @@ -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