From: Dave Methvin Date: Wed, 25 Jul 2012 14:19:09 +0000 (-0400) Subject: Fix #7579. Don't convert to number if it changes the string. Close gh-852. X-Git-Tag: 1.8rc1~20 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ce15bd7d0c14106521bd21179b1507f2863d1960;p=jquery.git Fix #7579. Don't convert to number if it changes the string. Close gh-852. Net effect here is that hex numbers and most exponential-format numbers or long sequences of digits will remain strings rather than being coerced to numbers. `The people have spoken. --- diff --git a/src/data.js b/src/data.js index c7de049a2..f51a3e520 100644 --- a/src/data.js +++ b/src/data.js @@ -304,8 +304,9 @@ function dataAttr( elem, key, data ) { data = data === "true" ? true : data === "false" ? false : data === "null" ? null : - jQuery.isNumeric( data ) ? +data : - rbrace.test( data ) ? jQuery.parseJSON( data ) : + // Only convert to a number if it doesn't change the string + +data + "" === data ? +data : + rbrace.test( data ) ? jQuery.parseJSON( data ) : data; } catch( e ) {} diff --git a/test/unit/data.js b/test/unit/data.js index 3083a947d..7f4ad6c36 100644 --- a/test/unit/data.js +++ b/test/unit/data.js @@ -294,7 +294,7 @@ test(".data(String) and .data(String, Object)", function() { }); test("data-* attributes", function() { - expect(38); + expect(40); var div = jQuery("
"), child = jQuery("
"), dummy = jQuery("
"); @@ -357,9 +357,11 @@ test("data-* attributes", function() { .attr("data-five", "5") .attr("data-point", "5.5") .attr("data-pointe", "5.5E3") + .attr("data-grande", "5.574E9") .attr("data-hexadecimal", "0x42") .attr("data-pointbad", "5..5") .attr("data-pointbad2", "-.") + .attr("data-bigassnum", "123456789123456789123456789") .attr("data-badjson", "{123}") .attr("data-badjson2", "[abc]") .attr("data-empty", "") @@ -371,10 +373,12 @@ test("data-* attributes", function() { strictEqual( child.data("false"), false, "Primitive false read from attribute"); strictEqual( child.data("five"), 5, "Primitive number read from attribute"); strictEqual( child.data("point"), 5.5, "Primitive number read from attribute"); - strictEqual( child.data("pointe"), 5500, "Primitive number read from attribute"); - strictEqual( child.data("hexadecimal"), 66, "Hexadecimal number read from attribute"); + strictEqual( child.data("pointe"), "5.5E3", "Floating point exponential number read from attribute"); + strictEqual( child.data("grande"), "5.574E9", "Big exponential number read from attribute"); + strictEqual( child.data("hexadecimal"), "0x42", "Hexadecimal number read from attribute"); strictEqual( child.data("pointbad"), "5..5", "Bad number read from attribute"); strictEqual( child.data("pointbad2"), "-.", "Bad number read from attribute"); + strictEqual( child.data("bigassnum"), "123456789123456789123456789", "Bad bigass number read from attribute"); strictEqual( child.data("badjson"), "{123}", "Bad number read from attribute"); strictEqual( child.data("badjson2"), "[abc]", "Bad number read from attribute"); strictEqual( child.data("empty"), "", "Empty string read from attribute");