Skip to content

Commit 17a26f5

Browse files
committed
Fail silently if closest is somehow called on a document. Fixes #10726.
1 parent 3478cbb commit 17a26f5

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/traversing.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,13 @@ jQuery.fn.extend({
9292
for ( ; i < l; i++ ) {
9393
cur = this[i];
9494

95-
while ( cur ) {
95+
while ( cur && cur.ownerDocument && cur !== context && cur.nodeType !== 11 ) {
9696
if ( pos ? pos.index(cur) > -1 : jQuery.find.matchesSelector(cur, selectors) ) {
9797
ret.push( cur );
9898
break;
9999

100100
} else {
101101
cur = cur.parentNode;
102-
if ( !cur || !cur.ownerDocument || cur === context || cur.nodeType === 11 ) {
103-
break;
104-
}
105102
}
106103
}
107104
}

test/unit/traversing.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ test("filter() with positional selectors", function() {
275275
});
276276

277277
test("closest()", function() {
278-
expect(13);
278+
expect( 14 );
279+
279280
deepEqual( jQuery("body").closest("body").get(), q("body"), "closest(body)" );
280281
deepEqual( jQuery("body").closest("html").get(), q("html"), "closest(html)" );
281282
deepEqual( jQuery("body").closest("div").get(), [], "closest(div)" );
@@ -299,6 +300,8 @@ test("closest()", function() {
299300
// Bug #7369
300301
equal( jQuery("<div foo='bar'></div>").closest("[foo]").length, 1, "Disconnected nodes with attribute selector" );
301302
equal( jQuery("<div>text</div>").closest("[lang]").length, 0, "Disconnected nodes with text and non-existent attribute selector" );
303+
304+
ok( !jQuery(document).closest("#foo").length, "Calling closest on a document fails silently" );
302305
});
303306

304307
test("closest(jQuery)", function() {

0 commit comments

Comments
 (0)