Skip to content

Commit 13b5152

Browse files
committed
Make sure .data() can be called on an empty set. Fixes #6434.
1 parent 4a64f9a commit 13b5152

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/data.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,12 @@ jQuery.extend({
101101

102102
jQuery.fn.extend({
103103
data: function( key, value ) {
104-
if ( typeof key === "undefined" && this.length ) {
105-
return jQuery.data( this[0] );
106-
104+
if ( typeof key === "undefined" ) {
105+
if ( this.length ) {
106+
return jQuery.data( this[0] );
107+
} else {
108+
return undefined;
109+
}
107110
} else if ( typeof key === "object" ) {
108111
return this.each(function() {
109112
jQuery.data( this, key );

test/unit/data.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,22 @@ test("jQuery.data", function() {
6060
});
6161

6262
test(".data()", function() {
63-
expect(1);
63+
expect(2);
64+
65+
ok( jQuery().data() === undefined, "Check that .data() called on an empty set returns undefined" );
6466

6567
var div = jQuery("#foo");
6668
div.data("test", "success");
6769
same( div.data(), {test: "success"}, "data() get the entire data object" )
6870
})
6971

7072
test(".data(String) and .data(String, Object)", function() {
71-
expect(23);
72-
var div = jQuery("<div/>");
73+
expect(25);
74+
75+
ok( jQuery().data("foo") === undefined, "Check that .data(String) called on an empty set returns undefined" );
76+
ok( jQuery().data("foo", {"foo":"bar"}).data("foo") === undefined, "Check that .data(String, Object) called on an empty set is chainable" );
7377

78+
var div = jQuery("<div/>");
7479
ok( div.data("test") === undefined, "Check for no data exists" );
7580

7681
div.data("test", "success");

0 commit comments

Comments
 (0)