DRY out removeData/cleanData, closes gh-838.

This commit is contained in:
Richard Gibson 2012-07-06 09:22:44 -04:00 committed by Dave Methvin
parent 7532bd7df5
commit f8baea8f7a
2 changed files with 63 additions and 74 deletions

View File

@ -128,16 +128,11 @@ jQuery.extend({
var thisCache, i, l, var thisCache, i, l,
// Reference to internal data cache key
internalKey = jQuery.expando,
isNode = elem.nodeType, isNode = elem.nodeType,
// See jQuery.data for more information // See jQuery.data for more information
cache = isNode ? jQuery.cache : elem, cache = isNode ? jQuery.cache : elem,
id = isNode ? elem[ jQuery.expando ] : jQuery.expando;
// See jQuery.data for more information
id = isNode ? elem[ internalKey ] : internalKey;
// If there is already no cache entry for this object, there is no // If there is already no cache entry for this object, there is no
// purpose in continuing // purpose in continuing
@ -192,32 +187,18 @@ jQuery.extend({
} }
} }
// Browsers that fail expando deletion also refuse to delete expandos on // Destroy the cache
// the window, but it will allow it on all other JS objects; other browsers if ( isNode ) {
// don't care jQuery.cleanData( [ elem ], true );
// Ensure that `cache` is not a window object #10080
if ( jQuery.support.deleteExpando || !cache.setInterval ) { // Use delete when supported for expandos or `cache` is not a window per isWindow (#10080)
} else if ( jQuery.support.deleteExpando || cache != cache.window ) {
delete cache[ id ]; delete cache[ id ];
// When all else fails, null
} else { } else {
cache[ id ] = null; cache[ id ] = null;
} }
// We destroyed the cache and need to eliminate the expando on the node to avoid
// false lookups in the cache for entries that no longer exist
if ( isNode ) {
jQuery.deletedIds.push( id );
// IE does not allow us to delete expando properties from nodes,
// nor does it have a removeAttribute function on Document nodes;
// we must handle all of these cases
if ( jQuery.support.deleteExpando ) {
delete elem[ internalKey ];
} else if ( elem.removeAttribute ) {
elem.removeAttribute( internalKey );
} else {
elem[ internalKey ] = null;
}
}
}, },
// For internal use only. // For internal use only.
@ -227,15 +208,10 @@ jQuery.extend({
// A method for determining if a DOM node can handle the data expando // A method for determining if a DOM node can handle the data expando
acceptData: function( elem ) { acceptData: function( elem ) {
if ( elem.nodeName ) { var noData = elem.nodeName && jQuery.noData[ elem.nodeName.toLowerCase() ];
var match = jQuery.noData[ elem.nodeName.toLowerCase() ];
if ( match ) { // nodes accept data unless otherwise specified; rejection can be conditional
return !(match === true || elem.getAttribute("classid") !== match); return !noData || noData !== true && elem.getAttribute("classid") === noData;
}
}
return true;
} }
}); });

View File

@ -347,14 +347,18 @@ jQuery.fn.extend({
if ( scripts.length ) { if ( scripts.length ) {
jQuery.each( scripts, function( i, elem ) { jQuery.each( scripts, function( i, elem ) {
if ( elem.src ) { if ( elem.src ) {
jQuery.ajax ? jQuery.ajax({ if ( jQuery.ajax ) {
jQuery.ajax({
url: elem.src, url: elem.src,
type: "GET", type: "GET",
dataType: "script", dataType: "script",
async: false, async: false,
global: false, global: false,
throws: true throws: true
}) : jQuery.error( "no ajax" ); });
} else {
jQuery.error("no ajax");
}
} else { } else {
jQuery.globalEval( ( elem.text || elem.textContent || elem.innerHTML || "" ).replace( rcleanScript, "" ) ); jQuery.globalEval( ( elem.text || elem.textContent || elem.innerHTML || "" ).replace( rcleanScript, "" ) );
} }
@ -760,24 +764,24 @@ jQuery.extend({
return ret; return ret;
}, },
cleanData: function( elems ) { cleanData: function( elems, /* internal */ acceptData ) {
var data, id, var data, id, elem, type,
i = 0,
internalKey = jQuery.expando,
cache = jQuery.cache, cache = jQuery.cache,
special = jQuery.event.special, deleteExpando = jQuery.support.deleteExpando,
deleteExpando = jQuery.support.deleteExpando; special = jQuery.event.special;
for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) { for ( ; (elem = elems[i]) != null; i++ ) {
if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) {
continue;
}
id = elem[ jQuery.expando ]; if ( acceptData || jQuery.acceptData( elem ) ) {
if ( id ) { id = elem[ internalKey ];
data = cache[ id ]; data = id && cache[ id ];
if ( data && data.events ) { if ( data ) {
for ( var type in data.events ) { if ( data.events ) {
for ( type in data.events ) {
if ( special[ type ] ) { if ( special[ type ] ) {
jQuery.event.remove( elem, type ); jQuery.event.remove( elem, type );
@ -788,17 +792,26 @@ jQuery.extend({
} }
} }
// Remove cache only if jQuery.event.remove was not removed it before // Remove cache only if it was not already removed by jQuery.event.remove
if ( cache[ id ] ) { if ( cache[ id ] ) {
delete cache[ id ];
// IE does not allow us to delete expando properties from nodes,
// nor does it have a removeAttribute function on Document nodes;
// we must handle all of these cases
if ( deleteExpando ) { if ( deleteExpando ) {
delete elem[ jQuery.expando ]; delete elem[ internalKey ];
} else if ( elem.removeAttribute ) { } else if ( elem.removeAttribute ) {
elem.removeAttribute( jQuery.expando ); elem.removeAttribute( internalKey );
} else {
elem[ internalKey ] = null;
} }
jQuery.deletedIds.push( id ); jQuery.deletedIds.push( id );
delete cache[ id ]; }
} }
} }
} }