mirror of
https://github.com/jquery/jquery.git
synced 2024-11-23 02:54:22 +00:00
Traversing: Never let .closest() match positional selectors
Fixes gh-2796 Close gh-2818
This commit is contained in:
parent
0e2f8f9eff
commit
a268f5225c
@ -39,16 +39,16 @@ jQuery.fn.extend( {
|
|||||||
i = 0,
|
i = 0,
|
||||||
l = this.length,
|
l = this.length,
|
||||||
matched = [],
|
matched = [],
|
||||||
pos = rneedsContext.test( selectors ) || typeof selectors !== "string" ?
|
targets = typeof selectors !== "string" && jQuery( selectors );
|
||||||
jQuery( selectors, context || this.context ) :
|
|
||||||
0;
|
|
||||||
|
|
||||||
|
// Positional selectors never match, since there's no _selection_ context
|
||||||
|
if ( !rneedsContext.test( selectors ) ) {
|
||||||
for ( ; i < l; i++ ) {
|
for ( ; i < l; i++ ) {
|
||||||
for ( cur = this[ i ]; cur && cur !== context; cur = cur.parentNode ) {
|
for ( cur = this[ i ]; cur && cur !== context; cur = cur.parentNode ) {
|
||||||
|
|
||||||
// Always skip document fragments
|
// Always skip document fragments
|
||||||
if ( cur.nodeType < 11 && ( pos ?
|
if ( cur.nodeType < 11 && ( targets ?
|
||||||
pos.index( cur ) > -1 :
|
targets.index( cur ) > -1 :
|
||||||
|
|
||||||
// Don't pass non-elements to Sizzle
|
// Don't pass non-elements to Sizzle
|
||||||
cur.nodeType === 1 &&
|
cur.nodeType === 1 &&
|
||||||
@ -59,6 +59,7 @@ jQuery.fn.extend( {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return this.pushStack( matched.length > 1 ? jQuery.uniqueSort( matched ) : matched );
|
return this.pushStack( matched.length > 1 ? jQuery.uniqueSort( matched ) : matched );
|
||||||
},
|
},
|
||||||
|
@ -323,7 +323,7 @@ QUnit[ jQuery.find.compile ? "test" : "skip" ]( "filter() with positional select
|
|||||||
} );
|
} );
|
||||||
|
|
||||||
QUnit.test( "closest()", function( assert ) {
|
QUnit.test( "closest()", function( assert ) {
|
||||||
assert.expect( 13 );
|
assert.expect( 14 );
|
||||||
|
|
||||||
var jq;
|
var jq;
|
||||||
|
|
||||||
@ -344,6 +344,12 @@ QUnit.test( "closest()", function( assert ) {
|
|||||||
// Test on disconnected node
|
// Test on disconnected node
|
||||||
assert.equal( jQuery( "<div><p></p></div>" ).find( "p" ).closest( "table" ).length, 0, "Make sure disconnected closest work." );
|
assert.equal( jQuery( "<div><p></p></div>" ).find( "p" ).closest( "table" ).length, 0, "Make sure disconnected closest work." );
|
||||||
|
|
||||||
|
assert.deepEqual(
|
||||||
|
jQuery( "#firstp" ).closest( q( "qunit-fixture" ) ).get(),
|
||||||
|
q( "qunit-fixture" ),
|
||||||
|
"Non-string match target"
|
||||||
|
);
|
||||||
|
|
||||||
// Bug #7369
|
// Bug #7369
|
||||||
assert.equal( jQuery( "<div foo='bar'></div>" ).closest( "[foo]" ).length, 1, "Disconnected nodes with attribute selector" );
|
assert.equal( jQuery( "<div foo='bar'></div>" ).closest( "[foo]" ).length, 1, "Disconnected nodes with attribute selector" );
|
||||||
assert.equal( jQuery( "<div>text</div>" ).closest( "[lang]" ).length, 0, "Disconnected nodes with text and non-existent attribute selector" );
|
assert.equal( jQuery( "<div>text</div>" ).closest( "[lang]" ).length, 0, "Disconnected nodes with text and non-existent attribute selector" );
|
||||||
@ -355,10 +361,17 @@ QUnit.test( "closest()", function( assert ) {
|
|||||||
} );
|
} );
|
||||||
|
|
||||||
QUnit[ jQuery.find.compile ? "test" : "skip" ]( "closest() with positional selectors", function( assert ) {
|
QUnit[ jQuery.find.compile ? "test" : "skip" ]( "closest() with positional selectors", function( assert ) {
|
||||||
assert.expect( 2 );
|
assert.expect( 3 );
|
||||||
|
|
||||||
assert.deepEqual( jQuery( "#qunit-fixture" ).closest( "div:first" ).get(), [], "closest(div:first)" );
|
assert.deepEqual( jQuery( "#qunit-fixture" ).closest( "div:first" ).get(), [],
|
||||||
assert.deepEqual( jQuery( "#qunit-fixture div" ).closest( "body:first div:last" ).get(), q( "fx-tests" ), "closest(body:first div:last)" );
|
"closest(div:first)" );
|
||||||
|
assert.deepEqual( jQuery( "#qunit-fixture div" ).closest( "body:first div:last" ).get(), [],
|
||||||
|
"closest(body:first div:last)" );
|
||||||
|
assert.deepEqual(
|
||||||
|
jQuery( "#qunit-fixture div" ).closest( "body:first div:last", document ).get(),
|
||||||
|
[],
|
||||||
|
"closest(body:first div:last, document)"
|
||||||
|
);
|
||||||
} );
|
} );
|
||||||
|
|
||||||
QUnit.test( "closest(jQuery)", function( assert ) {
|
QUnit.test( "closest(jQuery)", function( assert ) {
|
||||||
|
Loading…
Reference in New Issue
Block a user