Skip to content

Commit 97e134f

Browse files
committed
data should not add expando unless actually adding data
1 parent 24ffc39 commit 97e134f

File tree

2 files changed

+45
-20
lines changed

2 files changed

+45
-20
lines changed

src/data.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,33 @@ jQuery.extend({
1313

1414
var id = elem[ expando ], cache = jQuery.cache, thisCache;
1515

16-
// Compute a unique ID for the element
17-
if(!id) id = elem[ expando ] = ++uuid;
18-
1916
// Handle the case where there's no name immediately
20-
if ( !name ) { return id; }
17+
if ( !name ) {
18+
return id;
19+
}
20+
21+
// Compute a unique ID for the element
22+
if ( !id ) {
23+
id = ++uuid;
24+
}
2125

2226
// Avoid generating a new cache unless none exists and we
2327
// want to manipulate it.
24-
if( cache[ id ] )
28+
if ( cache[ id ] ) {
2529
thisCache = cache[ id ];
26-
else if( typeof data === "undefined" )
30+
} else if ( typeof data === "undefined" ) {
2731
thisCache = emptyObject;
28-
else
32+
} else {
2933
thisCache = cache[ id ] = {};
34+
}
3035

3136
// Prevent overriding the named cache with undefined values
32-
if ( data !== undefined ) thisCache[ name ] = data;
33-
34-
if(name === true) return thisCache;
35-
else return thisCache[name];
37+
if ( data !== undefined ) {
38+
elem[ expando ] = id;
39+
thisCache[ name ] = data;
40+
}
41+
42+
return name === true ? thisCache : thisCache[ name ];
3643
},
3744

3845
removeData: function( elem, name ) {
@@ -49,26 +56,29 @@ jQuery.extend({
4956
delete thisCache[ name ];
5057

5158
// If we've removed all the data, remove the element's cache
52-
if( jQuery.isEmptyObject(thisCache) )
59+
if ( jQuery.isEmptyObject(thisCache) ) {
5360
jQuery.removeData( elem );
61+
}
5462
}
5563

5664
// Otherwise, we want to remove all of the element's data
5765
} else {
5866
// Clean up the element expando
5967
try {
6068
delete elem[ expando ];
61-
} catch(e){
69+
} catch( e ) {
6270
// IE has trouble directly removing the expando
6371
// but it's ok with using removeAttribute
64-
if ( elem.removeAttribute )
72+
if ( elem.removeAttribute ) {
6573
elem.removeAttribute( expando );
74+
}
6675
}
6776

6877
// Completely remove the data cache
6978
delete cache[ id ];
7079
}
7180
},
81+
7282
queue: function( elem, type, data ) {
7383
if( !elem ) return;
7484

test/unit/data.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,46 @@
11
module("data");
22

33
test("expando", function(){
4-
expect(4);
4+
expect(7);
55

66
equals("expando" in jQuery, true, "jQuery is exposing the expando");
77

88
var obj = {};
9+
jQuery.data(obj);
10+
equals( jQuery.expando in obj, false, "jQuery.data did not add an expando to the object" );
11+
12+
jQuery.data(obj, true);
13+
equals( jQuery.expando in obj, false, "jQuery.data did not add an expando to the object" );
14+
15+
jQuery.data(obj, 'test');
16+
equals( jQuery.expando in obj, false, "jQuery.data did not add an expando to the object" );
17+
918
jQuery.data(obj, "foo", "bar");
10-
11-
equals(jQuery.expando in obj, true, "jQuery.data added an expando to the object");
19+
equals( jQuery.expando in obj, true, "jQuery.data added an expando to the object" );
1220

1321
var id = obj[jQuery.expando];
14-
equals( id in jQuery.cache, true, "jQuery.data added an entry to jQuery.cache");
22+
equals( id in jQuery.cache, true, "jQuery.data added an entry to jQuery.cache" );
1523

16-
equals( jQuery.cache[id].foo, "bar", "jQuery.data worked correctly");
24+
equals( jQuery.cache[id].foo, "bar", "jQuery.data worked correctly" );
1725
});
1826

1927
test("jQuery.data", function() {
20-
expect(5);
28+
expect(6);
2129
var div = jQuery("#foo")[0];
2230
equals( jQuery.data(div, "test"), undefined, "Check for no data exists" );
31+
2332
jQuery.data(div, "test", "success");
2433
equals( jQuery.data(div, "test"), "success", "Check for added data" );
34+
35+
var data = jQuery.data(div, true);
36+
same( data, { "test": "success" }, "Return complete data set" );
37+
2538
jQuery.data(div, "test", "overwritten");
2639
equals( jQuery.data(div, "test"), "overwritten", "Check for overwritten data" );
40+
2741
jQuery.data(div, "test", undefined);
2842
equals( jQuery.data(div, "test"), "overwritten", "Check that data wasn't removed");
43+
2944
jQuery.data(div, "test", null);
3045
ok( jQuery.data(div, "test") === null, "Check for null data");
3146
});

0 commit comments

Comments
 (0)