Expose cleanData, make sure that all bound event handlers are properly cleaned up after html/empty/remove. Fixes #5856 and #5906.
This commit is contained in:
parent
4f9aa62a85
commit
f06e0e5575
2 changed files with 81 additions and 12 deletions
|
@ -193,7 +193,7 @@ jQuery.fn.extend({
|
|||
for ( var i = 0, l = this.length; i < l; i++ ) {
|
||||
// Remove element nodes and prevent memory leaks
|
||||
if ( this[i].nodeType === 1 ) {
|
||||
cleanData( this[i].getElementsByTagName("*") );
|
||||
jQuery.cleanData( this[i].getElementsByTagName("*") );
|
||||
this[i].innerHTML = value;
|
||||
}
|
||||
}
|
||||
|
@ -373,8 +373,8 @@ jQuery.each({
|
|||
remove: function( selector, keepData ) {
|
||||
if ( !selector || jQuery.filter( selector, [ this ] ).length ) {
|
||||
if ( !keepData && this.nodeType === 1 ) {
|
||||
cleanData( this.getElementsByTagName("*") );
|
||||
cleanData( [ this ] );
|
||||
jQuery.cleanData( this.getElementsByTagName("*") );
|
||||
jQuery.cleanData( [ this ] );
|
||||
}
|
||||
|
||||
if ( this.parentNode ) {
|
||||
|
@ -386,7 +386,7 @@ jQuery.each({
|
|||
empty: function() {
|
||||
// Remove element nodes and prevent memory leaks
|
||||
if ( this.nodeType === 1 ) {
|
||||
cleanData( this.getElementsByTagName("*") );
|
||||
jQuery.cleanData( this.getElementsByTagName("*") );
|
||||
}
|
||||
|
||||
// Remove any remaining nodes
|
||||
|
@ -493,13 +493,12 @@ jQuery.extend({
|
|||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
});
|
||||
|
||||
function cleanData( elems ) {
|
||||
for ( var i = 0, elem, id; (elem = elems[i]) != null; i++ ) {
|
||||
if ( !jQuery.noData[elem.nodeName.toLowerCase()] && (id = elem[expando]) ) {
|
||||
delete jQuery.cache[ id ];
|
||||
},
|
||||
|
||||
cleanData: function( elems ) {
|
||||
for ( var i = 0, elem, id; (elem = elems[i]) != null; i++ ) {
|
||||
jQuery.event.remove( elem );
|
||||
jQuery.removeData( elem );
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
|
@ -971,3 +971,73 @@ test("empty()", function() {
|
|||
equals( j.html(), "", "Check node,textnode,comment empty works" );
|
||||
});
|
||||
|
||||
test("jQuery.cleanData", function() {
|
||||
expect(10);
|
||||
|
||||
var type, pos, div, child;
|
||||
|
||||
type = "remove";
|
||||
|
||||
// Should trigger 4 remove event
|
||||
div = getDiv().remove();
|
||||
|
||||
// Should both do nothing
|
||||
pos = "Outer";
|
||||
div.trigger("click");
|
||||
|
||||
pos = "Inner";
|
||||
div.children().trigger("click");
|
||||
|
||||
type = "empty";
|
||||
div = getDiv();
|
||||
child = div.children();
|
||||
|
||||
// Should trigger 2 remove event
|
||||
div.empty();
|
||||
|
||||
// Should trigger 1
|
||||
pos = "Outer";
|
||||
div.trigger("click");
|
||||
|
||||
// Should do nothing
|
||||
pos = "Inner";
|
||||
child.trigger("click");
|
||||
|
||||
type = "html";
|
||||
|
||||
div = getDiv();
|
||||
child = div.children();
|
||||
|
||||
// Should trigger 2 remove event
|
||||
div.html("<div></div>");
|
||||
|
||||
// Should trigger 1
|
||||
pos = "Outer";
|
||||
div.trigger("click");
|
||||
|
||||
// Should do nothing
|
||||
pos = "Inner";
|
||||
child.trigger("click");
|
||||
|
||||
function getDiv() {
|
||||
var div = jQuery("<div class='outer'><div class='inner'></div></div>").click(function(){
|
||||
ok( true, type + " " + pos + " Click event fired." );
|
||||
}).focus(function(){
|
||||
ok( true, type + " " + pos + " Focus event fired." );
|
||||
}).find("div").click(function(){
|
||||
ok( false, type + " " + pos + " Click event fired." );
|
||||
}).focus(function(){
|
||||
ok( false, type + " " + pos + " Focus event fired." );
|
||||
}).end().appendTo("body");
|
||||
|
||||
div[0].detachEvent = div[0].removeEventListener = function(t){
|
||||
ok( true, type + " Outer " + t + " event unbound" );
|
||||
};
|
||||
|
||||
div[0].firstChild.detachEvent = div[0].firstChild.removeEventListener = function(t){
|
||||
ok( true, type + " Inner " + t + " event unbound" );
|
||||
};
|
||||
|
||||
return div;
|
||||
}
|
||||
});
|
Loading…
Reference in a new issue