From a726b44191372b6ebf9d7dc103f2d0a826e60909 Mon Sep 17 00:00:00 2001 From: Ronny Springer Date: Fri, 11 Oct 2013 10:09:54 +0200 Subject: [PATCH] Ref 2263134b: Better identify potential JSON. Close gh-1401. (cherry picked from commit 083edd60a6c189f17ff1087431e849dd11c111b2) Conflicts: src/data.js --- src/data.js | 2 +- test/unit/data.js | 57 +++++++++++++++++++++++++++++++++++++---------- 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/src/data.js b/src/data.js index 877f8888052..16dd4276a3c 100644 --- a/src/data.js +++ b/src/data.js @@ -5,7 +5,7 @@ define([ "./data/accepts" ], function( jQuery, deletedIds, support ) { -var rbrace = /(?:\{[\w\W]*\}|\[[\w\W]*\])$/, +var rbrace = /^(?:\{[\w\W]*\}|\[[\w\W]*\])$/, rmultiDash = /([A-Z])/g; function dataAttr( elem, key, data ) { diff --git a/test/unit/data.js b/test/unit/data.js index 7f2ee43c730..9c9d54f39a0 100644 --- a/test/unit/data.js +++ b/test/unit/data.js @@ -239,9 +239,12 @@ test("jQuery(plain Object).data(String, Object).data(String)", function() { }); test("data-* attributes", function() { - expect(40); + expect( 43 ); + var prop, i, l, metadata, elem, obj, obj2, check, num, num2, + parseJSON = jQuery.parseJSON, + nativeParse = typeof JSON !== "undefined" && JSON.parse, div = jQuery("
"), child = jQuery("
"), dummy = jQuery("
"); @@ -297,6 +300,19 @@ test("data-* attributes", function() { equal( child.data("other"), "test", "Make sure value was pulled in properly from a .data()." ); + // attribute parsing + i = l = 0; + jQuery.parseJSON = function() { + i++; + return parseJSON.apply( this, arguments ); + }; + if ( nativeParse ) { + JSON.parse = function() { + l++; + return nativeParse.apply( this, arguments ); + }; + } + child .attr("data-true", "true") .attr("data-false", "false") @@ -310,6 +326,8 @@ test("data-* attributes", function() { .attr("data-bigassnum", "123456789123456789123456789") .attr("data-badjson", "{123}") .attr("data-badjson2", "[abc]") + .attr("data-notjson", " {}") + .attr("data-notjson2", "[] ") .attr("data-empty", "") .attr("data-space", " ") .attr("data-null", "null") @@ -317,21 +335,36 @@ test("data-* attributes", function() { strictEqual( child.data("true"), true, "Primitive true read from attribute"); 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"), "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("five"), 5, "Integer read from attribute"); + strictEqual( child.data("point"), 5.5, "Floating-point number read from attribute"); + strictEqual( child.data("pointe"), "5.5E3", + "Exponential-notation number read from attribute as string"); + strictEqual( child.data("grande"), "5.574E9", + "Big exponential-notation number read from attribute as string"); + strictEqual( child.data("hexadecimal"), "0x42", + "Hexadecimal number read from attribute as string"); + strictEqual( child.data("pointbad"), "5..5", + "Extra-point non-number read from attribute as string"); + strictEqual( child.data("pointbad2"), "-.", + "No-digit non-number read from attribute as string"); + strictEqual( child.data("bigassnum"), "123456789123456789123456789", + "Bad bigass number read from attribute as string"); + strictEqual( child.data("badjson"), "{123}", "Bad JSON object read from attribute as string"); + strictEqual( child.data("badjson2"), "[abc]", "Bad JSON array read from attribute as string"); + strictEqual( child.data("notjson"), " {}", + "JSON object with leading non-JSON read from attribute as string"); + strictEqual( child.data("notjson2"), "[] ", + "JSON array with trailing non-JSON read from attribute as string"); strictEqual( child.data("empty"), "", "Empty string read from attribute"); - strictEqual( child.data("space"), " ", "Empty string read from attribute"); + strictEqual( child.data("space"), " ", "Whitespace string read from attribute"); strictEqual( child.data("null"), null, "Primitive null read from attribute"); strictEqual( child.data("string"), "test", "Typical string read from attribute"); + equal( i || l, 2, "Correct number of JSON parse attempts when reading from attributes" ); + jQuery.parseJSON = parseJSON; + if ( nativeParse ) { + JSON.parse = nativeParse; + } child.remove(); // tests from metadata plugin