From 9e06903a99caf5619d0db858ed3d24f0e6ee15db Mon Sep 17 00:00:00 2001 From: jeresig Date: Fri, 26 Feb 2010 12:35:04 -0500 Subject: [PATCH] Make sure that undefined is always returned for undefined data properties. Fixes #6166. --- src/data.js | 2 +- test/unit/data.js | 22 ++++++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/data.js b/src/data.js index b9986f58d..a97ac0419 100644 --- a/src/data.js +++ b/src/data.js @@ -25,7 +25,7 @@ jQuery.extend({ var id = elem[ expando ], cache = jQuery.cache, thisCache; if ( !id && typeof name === "string" && data === undefined ) { - return null; + return; } // Compute a unique ID for the element diff --git a/test/unit/data.js b/test/unit/data.js index 79b23c693..0d75bc444 100644 --- a/test/unit/data.js +++ b/test/unit/data.js @@ -24,12 +24,15 @@ test("expando", function(){ }); test("jQuery.data", function() { - expect(8); - var div = jQuery("#foo")[0]; - equals( jQuery.data(div, "test"), undefined, "Check for no data exists" ); + expect(9); + var div = document.createElement("div"); + + ok( jQuery.data(div, "test") === undefined, "Check for no data exists" ); jQuery.data(div, "test", "success"); equals( jQuery.data(div, "test"), "success", "Check for added data" ); + + ok( jQuery.data(div, "notexist") === undefined, "Check for no data exists" ); var data = jQuery.data(div); same( data, { "test": "success" }, "Return complete data set" ); @@ -57,18 +60,25 @@ test(".data()", function() { }) test(".data(String) and .data(String, Object)", function() { - expect(22); - var div = jQuery("#foo"); - equals( div.data("test"), undefined, "Check for no data exists" ); + expect(23); + var div = jQuery("
"); + + ok( div.data("test") === undefined, "Check for no data exists" ); + div.data("test", "success"); equals( div.data("test"), "success", "Check for added data" ); + div.data("test", "overwritten"); equals( div.data("test"), "overwritten", "Check for overwritten data" ); + div.data("test", undefined); equals( div.data("test"), "overwritten", "Check that data wasn't removed"); + div.data("test", null); ok( div.data("test") === null, "Check for null data"); + ok( div.data("notexist") === undefined, "Check for no data exists" ); + div.data("test", "overwritten"); var hits = {test:0}, gets = {test:0}; -- 2.39.5