From: Jason Bedard Date: Sun, 19 Jul 2015 00:47:31 +0000 (-0700) Subject: Data: remove user data in cleanData X-Git-Tag: 2.2.0~174 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=5fe76c663f8a4986af62edb434a1708c006d0b21;p=jquery.git Data: remove user data in cleanData Fixes gh-2503 Closes gh-2480 --- diff --git a/src/manipulation.js b/src/manipulation.js index 4bac1417f..00d65de5a 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -270,19 +270,24 @@ jQuery.extend({ i = 0; for ( ; (elem = elems[ i ]) !== undefined; i++ ) { - if ( jQuery.acceptData( elem ) && (data = elem[ dataPriv.expando ])) { - if ( data.events ) { - for ( type in data.events ) { - if ( special[ type ] ) { - jQuery.event.remove( elem, type ); - - // This is a shortcut to avoid jQuery.event.remove's overhead - } else { - jQuery.removeEvent( elem, type, data.handle ); + if ( jQuery.acceptData( elem ) ) { + if ( (data = elem[ dataPriv.expando ] ) ) { + if ( data.events ) { + for ( type in data.events ) { + if ( special[ type ] ) { + jQuery.event.remove( elem, type ); + + // This is a shortcut to avoid jQuery.event.remove's overhead + } else { + jQuery.removeEvent( elem, type, data.handle ); + } } } + delete elem[ dataPriv.expando ]; + } + if ( elem[ dataUser.expando ] ) { + delete elem[ dataUser.expando ]; } - delete elem[ dataPriv.expando ]; } } } diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js index c1dac6993..fffbf38ce 100644 --- a/test/unit/manipulation.js +++ b/test/unit/manipulation.js @@ -2064,7 +2064,7 @@ test( "jQuery.cleanData", function() { }); test( "jQuery.cleanData eliminates all private data (gh-2127)", function() { - expect( 2 ); + expect( 3 ); var div = jQuery( "
" ).appendTo( "#qunit-fixture" ); @@ -2074,12 +2074,34 @@ test( "jQuery.cleanData eliminates all private data (gh-2127)", function() { div.remove(); + ok( !jQuery.hasData( div[ 0 ] ), "Removed element hasData should return false" ); + ok( jQuery.isEmptyObject( jQuery._data( div[ 0 ] ) ), "Private data is empty after node is removed" ); div.remove(); }); +test( "jQuery.cleanData eliminates all public data", function() { + expect( 2 ); + + var key, + div = jQuery( "
" ); + div.data( "some", "data" ); + ok( !jQuery.isEmptyObject( jQuery.data( div[ 0 ] ) ), "Ensure some public data exists" ); + + div.remove(); + + ok( !jQuery.hasData( div[ 0 ] ), "Removed element hasData should return false" ); + + // Make sure the expando is gone + for ( key in div[ 0 ] ) { + if ( /^jQuery/.test( key ) ) { + ok( false, "Expando was not removed when there was no more data" ); + } + } +}); + test( "domManip plain-text caching (trac-6779)", function() { expect( 1 );