Skip to content

Commit

Permalink
Traversing: Let .not(arraylike) pass non-element nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
dmethvin committed Aug 10, 2016
1 parent cca2aa2 commit 560c0c6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/traversing/findFilter.js
Expand Up @@ -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;
} );
Expand Down
14 changes: 14 additions & 0 deletions test/unit/traversing.js
Expand Up @@ -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;
Expand Down

0 comments on commit 560c0c6

Please sign in to comment.