From: Dave Methvin Date: Tue, 2 Aug 2016 20:41:34 +0000 (-0400) Subject: Traversing: Let .not(arraylike) pass non-element nodes X-Git-Tag: 3.1.1~11 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=560c0c6f995a4f7584dac5be8fe5eea114a95f69;p=jquery.git Traversing: Let .not(arraylike) pass non-element nodes Fixes gh-3226 Closes gh-3261 --- diff --git a/src/traversing/findFilter.js b/src/traversing/findFilter.js index 92d6753b4..3bd036a18 100644 --- a/src/traversing/findFilter.js +++ b/src/traversing/findFilter.js @@ -15,24 +15,29 @@ function winnow( elements, qualifier, not ) { return jQuery.grep( elements, function( elem, i ) { return !!qualifier.call( elem, i, elem ) !== not; } ); - } + // Single element if ( qualifier.nodeType ) { return jQuery.grep( elements, function( elem ) { return ( elem === qualifier ) !== not; } ); - } - if ( typeof qualifier === "string" ) { - if ( risSimple.test( qualifier ) ) { - return jQuery.filter( qualifier, elements, not ); - } + // Arraylike of elements (jQuery, arguments, Array) + if ( typeof qualifier !== "string" ) { + return jQuery.grep( elements, function( elem ) { + return ( indexOf.call( qualifier, elem ) > -1 ) !== not; + } ); + } - qualifier = jQuery.filter( qualifier, elements ); + // Simple selector that can be filtered directly, removing non-Elements + if ( risSimple.test( qualifier ) ) { + return jQuery.filter( qualifier, elements, not ); } + // Complex selector, compare the two sets, removing non-Elements + qualifier = jQuery.filter( qualifier, elements ); return jQuery.grep( elements, function( elem ) { return ( indexOf.call( qualifier, elem ) > -1 ) !== not && elem.nodeType === 1; } ); diff --git a/test/unit/traversing.js b/test/unit/traversing.js index 74da6d973..c7ed01415 100644 --- a/test/unit/traversing.js +++ b/test/unit/traversing.js @@ -481,6 +481,20 @@ QUnit.test( "not(Selector) excludes non-element nodes (gh-2808)", function( asse assert.deepEqual( mixedContents.not( "[id=a],*,[id=b]" ).get(), [], "not [id=a],*,[id=b]" ); } ); +QUnit.test( "not(arraylike) passes non-element nodes (gh-3226)", function( assert ) { + assert.expect( 5 ); + + var mixedContents = jQuery( "hi there " ), + mixedLength = mixedContents.length, + firstElement = mixedContents.first(); + + assert.deepEqual( mixedContents.not( mixedContents ).get(), [], "not everything" ); + assert.deepEqual( mixedContents.not( firstElement ).length, mixedLength - 1, "not firstElement" ); + assert.deepEqual( mixedContents.not( [ firstElement[ 0 ].nextSibling ] ).length, mixedLength - 1, "not textnode array" ); + assert.deepEqual( mixedContents.not( firstElement[ 0 ].nextSibling ).length, mixedLength - 1, "not textnode" ); + assert.deepEqual( mixedContents.not( document.body ).get(), mixedContents.get(), "not with unmatched element" ); +} ); + QUnit.test( "has(Element)", function( assert ) { assert.expect( 3 ); var obj, detached, multipleParent;