aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Gibson <richard.gibson@gmail.com>2013-10-28 11:35:53 -0400
committerRichard Gibson <richard.gibson@gmail.com>2013-10-28 11:39:43 -0400
commitd8576139c14fb777c24867d7f72d70d7334aabd1 (patch)
treeaeb40d630b2f922e54dc58e2e75a60eb0fc1d138
parent5823bf3f44c025f7b755175d2f8c38da321a20de (diff)
downloadjquery-d8576139c14fb777c24867d7f72d70d7334aabd1.tar.gz
jquery-d8576139c14fb777c24867d7f72d70d7334aabd1.zip
Fix #14459: Use jQuery.parseJSON instead of JSON.parse
(cherry picked from commit ced9d155f010bc456745a55a63ca959db9d405d3)
-rw-r--r--src/data.js2
-rw-r--r--test/unit/data.js14
2 files changed, 3 insertions, 13 deletions
diff --git a/src/data.js b/src/data.js
index 16dd4276a..248a5df50 100644
--- a/src/data.js
+++ b/src/data.js
@@ -25,7 +25,7 @@ function dataAttr( elem, key, data ) {
// Only convert to a number if it doesn't change the string
+data + "" === data ? +data :
rbrace.test( data ) ? jQuery.parseJSON( data ) :
- data;
+ data;
} catch( e ) {}
// Make sure we set the data so it isn't changed later
diff --git a/test/unit/data.js b/test/unit/data.js
index 9c9d54f39..1e857ae6e 100644
--- a/test/unit/data.js
+++ b/test/unit/data.js
@@ -244,7 +244,6 @@ test("data-* attributes", function() {
var prop, i, l, metadata, elem,
obj, obj2, check, num, num2,
parseJSON = jQuery.parseJSON,
- nativeParse = typeof JSON !== "undefined" && JSON.parse,
div = jQuery("<div>"),
child = jQuery("<div data-myobj='old data' data-ignored=\"DOM\" data-other='test'></div>"),
dummy = jQuery("<div data-myobj='old data' data-ignored=\"DOM\" data-other='test'></div>");
@@ -301,17 +300,11 @@ test("data-* attributes", function() {
equal( child.data("other"), "test", "Make sure value was pulled in properly from a .data()." );
// attribute parsing
- i = l = 0;
+ i = 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")
@@ -359,12 +352,9 @@ test("data-* attributes", function() {
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" );
+ equal( i, 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