From 17a26f5bd9b14225248942b876e96545b4c21fb6 Mon Sep 17 00:00:00 2001 From: Timmy Willison Date: Wed, 25 Jul 2012 16:05:48 -0400 Subject: [PATCH] Fail silently if closest is somehow called on a document. Fixes #10726. --- src/traversing.js | 5 +---- test/unit/traversing.js | 5 ++++- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/traversing.js b/src/traversing.js index 321c25197..baac0399e 100644 --- a/src/traversing.js +++ b/src/traversing.js @@ -92,16 +92,13 @@ jQuery.fn.extend({ for ( ; i < l; 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) ) { ret.push( cur ); break; } else { cur = cur.parentNode; - if ( !cur || !cur.ownerDocument || cur === context || cur.nodeType === 11 ) { - break; - } } } } diff --git a/test/unit/traversing.js b/test/unit/traversing.js index 2712e7f2f..7f9678307 100644 --- a/test/unit/traversing.js +++ b/test/unit/traversing.js @@ -275,7 +275,8 @@ test("filter() with positional selectors", function() { }); test("closest()", function() { - expect(13); + expect( 14 ); + deepEqual( jQuery("body").closest("body").get(), q("body"), "closest(body)" ); deepEqual( jQuery("body").closest("html").get(), q("html"), "closest(html)" ); deepEqual( jQuery("body").closest("div").get(), [], "closest(div)" ); @@ -299,6 +300,8 @@ test("closest()", function() { // Bug #7369 equal( jQuery("
").closest("[foo]").length, 1, "Disconnected nodes with attribute selector" ); equal( jQuery("
text
").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() { -- 2.39.5