From f7a378e6b7b4599930a9bd97bd30ff4855498a5f Mon Sep 17 00:00:00 2001 From: Dave Methvin Date: Fri, 5 Apr 2013 08:57:01 -0400 Subject: [PATCH] Fix #13721. Filter before .remove() loop so positionals work. Close gh-1221. (Cherry picked from 1b610266502490eab42a0b9ddfac2f93da0b0fe1) --- src/manipulation.js | 9 +++++---- test/unit/manipulation.js | 19 ++++++++++++++++--- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/manipulation.js b/src/manipulation.js index 3a4f9a688..196794f83 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -160,10 +160,12 @@ jQuery.fn.extend({ // keepData is for internal use only--do not document remove: function( selector, keepData ) { var elem, - i = 0; + elems = selector ? jQuery.filter( selector, this ) : this, + i = elems.length; + + while ( i-- ) { + elem = elems[ i ]; - for ( ; (elem = this[i]) != null; i++ ) { - if ( !selector || jQuery.filter( selector, [ elem ] ).length > 0 ) { if ( !keepData && elem.nodeType === 1 ) { jQuery.cleanData( getAll( elem ) ); } @@ -175,7 +177,6 @@ jQuery.fn.extend({ elem.parentNode.removeChild( elem ); } } - } return this; }, diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js index f06e51098..c3c83b4f5 100644 --- a/test/unit/manipulation.js +++ b/test/unit/manipulation.js @@ -1741,7 +1741,8 @@ test( "clone()/html() don't expose jQuery/Sizzle expandos (#12858)", function() }); var testRemove = function( method ) { - var first = jQuery("#ap").children().first(); + var markup, div, + first = jQuery("#ap").children().first(); first.data("foo", "bar"); @@ -1759,6 +1760,18 @@ var testRemove = function( method ) { jQuery("#ap").children()[ method ]("a, code"); equal( jQuery("#ap").children().length, 0, "Check multi-filtered remove" ); + // Positional and relative selectors + markup = "
1234
"; + div = jQuery( markup ); + div.children().remove("span:nth-child(2n)"); + equal( div.text(), "13", "relative selector in " + method ); + div = jQuery( markup ); + div.children().remove("span:first"); + equal( div.text(), "234", "positional selector in " + method ); + div = jQuery( markup ); + div.children().remove("span:last"); + equal( div.text(), "123", "positional selector in " + method ); + // using contents will get comments regular, text, and comment nodes // Handle the case where no comment is in the document ok( jQuery("#nonnodes").contents().length >= 2, "Check node,textnode,comment remove works" ); @@ -1771,7 +1784,7 @@ var testRemove = function( method ) { } }; -test( "remove()", 8, function() { +test( "remove()", 11, function() { testRemove("remove"); }); @@ -1790,7 +1803,7 @@ test( "remove() event cleaning ", 1, function() { cleanUp.remove(); }); -test( "detach()", 8, function() { +test( "detach()", 11, function() { testRemove("detach"); }); -- 2.39.5