diff --git a/src/manipulation.js b/src/manipulation.js index 5cfc853be..f3c729fdf 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -72,23 +72,21 @@ jQuery.fn.extend({ // keepData is for internal use only--do not document remove: function( selector, keepData ) { var elem, - i = 0, - l = this.length; + elems = selector ? jQuery.filter( selector, this ) : this, + i = elems.length; - for ( ; i < l; i++ ) { - elem = this[ i ]; + while ( i-- ) { + elem = elems[ i ]; - if ( !selector || jQuery.filter( selector, [ elem ] ).length > 0 ) { - if ( !keepData && elem.nodeType === 1 ) { - jQuery.cleanData( getAll( elem ) ); - } - - if ( elem.parentNode ) { - if ( keepData && jQuery.contains( elem.ownerDocument, elem ) ) { - setGlobalEval( getAll( elem, "script" ) ); - } - elem.parentNode.removeChild( elem ); + if ( !keepData && elem.nodeType === 1 ) { + jQuery.cleanData( getAll( elem ) ); + } + + if ( elem.parentNode ) { + if ( keepData && jQuery.contains( elem.ownerDocument, elem ) ) { + setGlobalEval( getAll( elem, "script" ) ); } + elem.parentNode.removeChild( elem ); } } diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js index 012a0efc9..6093dbfb6 100644 --- a/test/unit/manipulation.js +++ b/test/unit/manipulation.js @@ -1713,7 +1713,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"); @@ -1731,6 +1732,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 = "