Skip to content

Commit 56bb677

Browse files
committed
Data: remove the expando when there's no more data
Fixes gh-1760 Close gh-2271
1 parent 764dc94 commit 56bb677

File tree

4 files changed

+48
-13
lines changed

4 files changed

+48
-13
lines changed

src/data/Data.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,7 @@ Data.prototype = {
120120
return;
121121
}
122122

123-
if ( key === undefined ) {
124-
this.register( owner );
125-
126-
} else {
123+
if ( key !== undefined ) {
127124

128125
// Support array or space separated string of keys
129126
if ( jQuery.isArray( key ) ) {
@@ -147,6 +144,11 @@ Data.prototype = {
147144
delete cache[ key[ i ] ];
148145
}
149146
}
147+
148+
// Remove the expando if there's no more data
149+
if ( key === undefined || jQuery.isEmptyObject( cache ) ) {
150+
delete owner[ this.expando ];
151+
}
150152
},
151153
hasData: function( owner ) {
152154
var cache = owner[ this.expando ];

src/event.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,9 @@ jQuery.event = {
216216
}
217217
}
218218

219-
// Remove the expando if it's no longer used
219+
// Remove data and the expando if it's no longer used
220220
if ( jQuery.isEmptyObject( events ) ) {
221-
// Normally this should go through the data api
222-
// but since event.js owns these properties,
223-
// this exception is made for the sake of optimizing
224-
// the operation.
225-
delete elemData.handle;
226-
delete elemData.events;
221+
dataPriv.remove( elem, "handle events" );
227222
}
228223
},
229224

test/unit/data.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,24 @@ test("Check proper data removal of non-element descendants nodes (#8335)", 1, fu
837837
});
838838

839839
testIframeWithCallback( "enumerate data attrs on body (#14894)", "data/dataAttrs.html", function( result ) {
840-
expect(1);
840+
expect( 1 );
841+
842+
equal( result, "ok", "enumeration of data- attrs on body" );
843+
});
844+
845+
test( "Check that the expando is removed when there's no more data", function() {
846+
expect( 1 );
841847

842-
equal(result, "ok", "enumeration of data- attrs on body" );
848+
var key,
849+
div = jQuery( "<div/>" );
850+
div.data( "some", "data" );
851+
equal( div.data( "some" ), "data", "Data is added" );
852+
div.removeData( "some" );
853+
854+
// Make sure the expando is gone
855+
for ( key in div[ 0 ] ) {
856+
if ( /^jQuery/.test( key ) ) {
857+
ok( false, "Expando was not removed when there was no more data" );
858+
}
859+
}
843860
});

test/unit/event.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2657,6 +2657,27 @@ test( "Inline event result is returned (#13993)", function() {
26572657
equal( result, 42, "inline handler returned value" );
26582658
});
26592659

2660+
test( ".off() removes the expando when there's no more data", function() {
2661+
expect( 1 );
2662+
2663+
var key,
2664+
div = jQuery( "<div/>" ).appendTo( "#qunit-fixture" );
2665+
2666+
div.on( "click", false );
2667+
div.on( "custom", function() {
2668+
ok( true, "Custom event triggered" );
2669+
} );
2670+
div.trigger( "custom" );
2671+
div.off( "click custom" );
2672+
2673+
// Make sure the expando is gone
2674+
for ( key in div[ 0 ] ) {
2675+
if ( /^jQuery/.test( key ) ) {
2676+
ok( false, "Expando was not removed when there was no more data" );
2677+
}
2678+
}
2679+
});
2680+
26602681
// This tests are unreliable in Firefox
26612682
if ( !(/firefox/i.test( window.navigator.userAgent )) ) {
26622683
test( "Check order of focusin/focusout events", 2, function() {

0 commit comments

Comments
 (0)