Traversing: Let .not(arraylike) pass non-element nodes

Fixes gh-3226
Closes gh-3261
This commit is contained in:
Dave Methvin 2016-08-02 16:41:34 -04:00
parent cca2aa2254
commit 560c0c6f99
2 changed files with 27 additions and 8 deletions

View File

@ -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 );
}
qualifier = jQuery.filter( qualifier, elements );
// Arraylike of elements (jQuery, arguments, Array)
if ( typeof qualifier !== "string" ) {
return jQuery.grep( elements, function( elem ) {
return ( indexOf.call( qualifier, elem ) > -1 ) !== not;
} );
}
// 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;
} );

View File

@ -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( "<span id='nonnodesElement'>hi</span> there <!-- mon ami -->" ),
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;