Adds detach()

This commit is contained in:
Yehuda Katz 2009-07-21 09:17:33 +00:00
parent 29ff710c9b
commit 7a67f8897d
2 changed files with 66 additions and 41 deletions

View file

@ -249,18 +249,21 @@ jQuery.each({
}); });
jQuery.each({ jQuery.each({
remove: function( selector ) { // keepData is for internal use only--do not document
remove: function( selector, keepData ) {
if ( !selector || jQuery.multiFilter( selector, [ this ] ).length ) { if ( !selector || jQuery.multiFilter( selector, [ this ] ).length ) {
if ( this.nodeType === 1 ) { if ( !keepData && this.nodeType === 1 ) {
cleanData( jQuery("*", this).add(this) ); cleanData( jQuery("*", this).add(this) );
} }
if ( this.parentNode ) { this.parentNode && this.parentNode.removeChild( this );
this.parentNode.removeChild( this );
}
} }
}, },
detach: function( selector ) {
jQuery( this ).remove( selector, true )
},
empty: function() { empty: function() {
// Remove element nodes and prevent memory leaks // Remove element nodes and prevent memory leaks
if ( this.nodeType === 1 ) { if ( this.nodeType === 1 ) {

View file

@ -677,24 +677,46 @@ test("text(Function)", function() {
testText(functionReturningObj); testText(functionReturningObj);
}) })
test("remove()", function() { var testRemove = function(method) {
expect(7); expect(9);
jQuery("#ap").children().remove();
var first = jQuery("#ap").children(":first");
first.data("foo", "bar");
jQuery("#ap").children()[method]();
ok( jQuery("#ap").text().length > 10, "Check text is not removed" ); ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
equals( jQuery("#ap").children().length, 0, "Check remove" ); equals( jQuery("#ap").children().length, 0, "Check remove" );
equals( first.data("foo"), method == "remove" ? null : "bar" );
reset(); reset();
jQuery("#ap").children().remove("a"); jQuery("#ap").children()[method]("a");
ok( jQuery("#ap").text().length > 10, "Check text is not removed" ); ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
equals( jQuery("#ap").children().length, 1, "Check filtered remove" ); equals( jQuery("#ap").children().length, 1, "Check filtered remove" );
jQuery("#ap").children().remove("a, code"); jQuery("#ap").children()[method]("a, code");
equals( jQuery("#ap").children().length, 0, "Check multi-filtered remove" ); equals( jQuery("#ap").children().length, 0, "Check multi-filtered remove" );
// using contents will get comments regular, text, and comment nodes // using contents will get comments regular, text, and comment nodes
equals( jQuery("#nonnodes").contents().length, 3, "Check node,textnode,comment remove works" ); equals( jQuery("#nonnodes").contents().length, 3, "Check node,textnode,comment remove works" );
jQuery("#nonnodes").contents().remove(); jQuery("#nonnodes").contents()[method]();
equals( jQuery("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works" ); equals( jQuery("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works" );
reset();
var count = 0;
var first = jQuery("#ap").children(":first");
first.click(function() { count++ })[method]().appendTo("body").click();
equals( method == "remove" ? 0 : 1, count );
};
test("remove()", function() {
testRemove("remove");
});
test("detach()", function() {
testRemove("detach");
}); });
test("empty()", function() { test("empty()", function() {