Commit 17a26f5b authored by Timmy Willison's avatar Timmy Willison

Fail silently if closest is somehow called on a document. Fixes #10726.

parent 3478cbb4
...@@ -92,16 +92,13 @@ jQuery.fn.extend({ ...@@ -92,16 +92,13 @@ jQuery.fn.extend({
for ( ; i < l; i++ ) { for ( ; i < l; i++ ) {
cur = this[i]; cur = this[i];
while ( cur ) { while ( cur && cur.ownerDocument && cur !== context && cur.nodeType !== 11 ) {
if ( pos ? pos.index(cur) > -1 : jQuery.find.matchesSelector(cur, selectors) ) { if ( pos ? pos.index(cur) > -1 : jQuery.find.matchesSelector(cur, selectors) ) {
ret.push( cur ); ret.push( cur );
break; break;
} else { } else {
cur = cur.parentNode; cur = cur.parentNode;
if ( !cur || !cur.ownerDocument || cur === context || cur.nodeType === 11 ) {
break;
}
} }
} }
} }
......
...@@ -275,7 +275,8 @@ test("filter() with positional selectors", function() { ...@@ -275,7 +275,8 @@ test("filter() with positional selectors", function() {
}); });
test("closest()", function() { test("closest()", function() {
expect(13); expect( 14 );
deepEqual( jQuery("body").closest("body").get(), q("body"), "closest(body)" ); deepEqual( jQuery("body").closest("body").get(), q("body"), "closest(body)" );
deepEqual( jQuery("body").closest("html").get(), q("html"), "closest(html)" ); deepEqual( jQuery("body").closest("html").get(), q("html"), "closest(html)" );
deepEqual( jQuery("body").closest("div").get(), [], "closest(div)" ); deepEqual( jQuery("body").closest("div").get(), [], "closest(div)" );
...@@ -299,6 +300,8 @@ test("closest()", function() { ...@@ -299,6 +300,8 @@ test("closest()", function() {
// Bug #7369 // Bug #7369
equal( jQuery("<div foo='bar'></div>").closest("[foo]").length, 1, "Disconnected nodes with attribute selector" ); equal( jQuery("<div foo='bar'></div>").closest("[foo]").length, 1, "Disconnected nodes with attribute selector" );
equal( jQuery("<div>text</div>").closest("[lang]").length, 0, "Disconnected nodes with text and non-existent attribute selector" ); equal( jQuery("<div>text</div>").closest("[lang]").length, 0, "Disconnected nodes with text and non-existent attribute selector" );
ok( !jQuery(document).closest("#foo").length, "Calling closest on a document fails silently" );
}); });
test("closest(jQuery)", function() { test("closest(jQuery)", function() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment