Skip to content

Commit 560c0c6

Browse files
committed
Traversing: Let .not(arraylike) pass non-element nodes
Fixes gh-3226 Closes gh-3261
1 parent cca2aa2 commit 560c0c6

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/traversing/findFilter.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,29 @@ function winnow( elements, qualifier, not ) {
1515
return jQuery.grep( elements, function( elem, i ) {
1616
return !!qualifier.call( elem, i, elem ) !== not;
1717
} );
18-
1918
}
2019

20+
// Single element
2121
if ( qualifier.nodeType ) {
2222
return jQuery.grep( elements, function( elem ) {
2323
return ( elem === qualifier ) !== not;
2424
} );
25-
2625
}
2726

28-
if ( typeof qualifier === "string" ) {
29-
if ( risSimple.test( qualifier ) ) {
30-
return jQuery.filter( qualifier, elements, not );
31-
}
27+
// Arraylike of elements (jQuery, arguments, Array)
28+
if ( typeof qualifier !== "string" ) {
29+
return jQuery.grep( elements, function( elem ) {
30+
return ( indexOf.call( qualifier, elem ) > -1 ) !== not;
31+
} );
32+
}
3233

33-
qualifier = jQuery.filter( qualifier, elements );
34+
// Simple selector that can be filtered directly, removing non-Elements
35+
if ( risSimple.test( qualifier ) ) {
36+
return jQuery.filter( qualifier, elements, not );
3437
}
3538

39+
// Complex selector, compare the two sets, removing non-Elements
40+
qualifier = jQuery.filter( qualifier, elements );
3641
return jQuery.grep( elements, function( elem ) {
3742
return ( indexOf.call( qualifier, elem ) > -1 ) !== not && elem.nodeType === 1;
3843
} );

test/unit/traversing.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,20 @@ QUnit.test( "not(Selector) excludes non-element nodes (gh-2808)", function( asse
481481
assert.deepEqual( mixedContents.not( "[id=a],*,[id=b]" ).get(), [], "not [id=a],*,[id=b]" );
482482
} );
483483

484+
QUnit.test( "not(arraylike) passes non-element nodes (gh-3226)", function( assert ) {
485+
assert.expect( 5 );
486+
487+
var mixedContents = jQuery( "<span id='nonnodesElement'>hi</span> there <!-- mon ami -->" ),
488+
mixedLength = mixedContents.length,
489+
firstElement = mixedContents.first();
490+
491+
assert.deepEqual( mixedContents.not( mixedContents ).get(), [], "not everything" );
492+
assert.deepEqual( mixedContents.not( firstElement ).length, mixedLength - 1, "not firstElement" );
493+
assert.deepEqual( mixedContents.not( [ firstElement[ 0 ].nextSibling ] ).length, mixedLength - 1, "not textnode array" );
494+
assert.deepEqual( mixedContents.not( firstElement[ 0 ].nextSibling ).length, mixedLength - 1, "not textnode" );
495+
assert.deepEqual( mixedContents.not( document.body ).get(), mixedContents.get(), "not with unmatched element" );
496+
} );
497+
484498
QUnit.test( "has(Element)", function( assert ) {
485499
assert.expect( 3 );
486500
var obj, detached, multipleParent;

0 commit comments

Comments
 (0)