From 075763b71a72f5973729c4fc8e0b4d839af6cd95 Mon Sep 17 00:00:00 2001 From: Corey Frang Date: Tue, 5 Nov 2013 15:03:28 -0500 Subject: [PATCH] Fix #14101: $().data() should be undefined, not null - Backported 30eee925dbc9f8d465ccf064f48c24061ffdc378 for 1.x --- src/data.js | 5 ++--- test/unit/data.js | 6 ++++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/data.js b/src/data.js index 248a5df50..8b4b0efca 100644 --- a/src/data.js +++ b/src/data.js @@ -272,8 +272,7 @@ jQuery.extend({ jQuery.fn.extend({ data: function( key, value ) { - var i, name, - data = null, + var i, name, data, elem = this[0], attrs = elem && elem.attributes; @@ -319,7 +318,7 @@ jQuery.fn.extend({ // Gets one value // Try to fetch any internally stored data first - elem ? dataAttr( elem, key, jQuery.data( elem, key ) ) : null; + elem ? dataAttr( elem, key, jQuery.data( elem, key ) ) : undefined; }, removeData: function( key ) { diff --git a/test/unit/data.js b/test/unit/data.js index 1e857ae6e..c62dcf952 100644 --- a/test/unit/data.js +++ b/test/unit/data.js @@ -164,6 +164,12 @@ test("jQuery.acceptData", function() { ok( !jQuery.acceptData( document.createTextNode("") ), "text" ); }); +// attempting to access the data of an undefined jQuery element should be undefined +test("jQuery().data() === undefined (#14101)", 2, function() { + strictEqual(jQuery().data(), undefined); + strictEqual(jQuery().data("key"), undefined); +}); + test(".data()", function() { expect(5); -- 2.39.5