From 638b6751e1ed22baa85a6974757d461c2b71bbd3 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Mon, 14 Feb 2011 00:54:40 +0000 Subject: [PATCH] * (bug 25571) Xml::encodeJsVar now passes floats natively instead of converting to strings Added unit test cases for int, float, and strings that look like int or float. --- includes/Xml.php | 4 ++-- tests/phpunit/includes/XmlTest.php | 32 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/includes/Xml.php b/includes/Xml.php index b293b1878b..412f041ec2 100644 --- a/includes/Xml.php +++ b/includes/Xml.php @@ -583,8 +583,8 @@ class Xml { $s = $value ? 'true' : 'false'; } elseif ( is_null( $value ) ) { $s = 'null'; - } elseif ( is_int( $value ) ) { - $s = $value; + } elseif ( is_int( $value ) || is_float( $value ) ) { + $s = strval($value); } elseif ( is_array( $value ) && // Make sure it's not associative. array_keys($value) === range( 0, count($value) - 1 ) || count($value) == 0 diff --git a/tests/phpunit/includes/XmlTest.php b/tests/phpunit/includes/XmlTest.php index 4086ce64d0..d9647e51a4 100644 --- a/tests/phpunit/includes/XmlTest.php +++ b/tests/phpunit/includes/XmlTest.php @@ -248,4 +248,36 @@ class XmlTest extends MediaWikiTestCase { 'encodeJsVar() with object' ); } + + function testEncodeJsVarInt() { + $this->assertEquals( + '123456', + Xml::encodeJsVar( 123456 ), + 'encodeJsVar() with int' + ); + } + + function testEncodeJsVarFloat() { + $this->assertEquals( + '1.23456', + Xml::encodeJsVar( 1.23456 ), + 'encodeJsVar() with float' + ); + } + + function testEncodeJsVarIntString() { + $this->assertEquals( + '"123456"', + Xml::encodeJsVar( '123456' ), + 'encodeJsVar() with int-like string' + ); + } + + function testEncodeJsVarFloatString() { + $this->assertEquals( + '"1.23456"', + Xml::encodeJsVar( '1.23456' ), + 'encodeJsVar() with float-like string' + ); + } } -- 2.20.1