From: Brion Vibber Date: Mon, 14 Feb 2011 00:54:40 +0000 (+0000) Subject: * (bug 25571) Xml::encodeJsVar now passes floats natively instead of converting to... X-Git-Tag: 1.31.0-rc.0~31997 X-Git-Url: http://git.cyclocoop.org/%24self?a=commitdiff_plain;h=638b6751e1ed22baa85a6974757d461c2b71bbd3;p=lhc%2Fweb%2Fwiklou.git * (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. --- 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' + ); + } }