Skip to content

Commit

Permalink
Make sure that undefined is always returned for undefined data proper…
Browse files Browse the repository at this point in the history
…ties. Fixes #6166.
  • Loading branch information
jeresig committed Feb 26, 2010
1 parent 7f5179b commit 9e06903
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 16 additions & 6 deletions test/unit/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" );
Expand Down Expand Up @@ -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("<div/>");

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};

Expand Down

0 comments on commit 9e06903

Please sign in to comment.