From 385ca2aa899d2ed953d9e18c95387e72e330b42c Mon Sep 17 00:00:00 2001 From: John Resig Date: Mon, 11 Oct 2010 07:45:15 -0400 Subject: [PATCH] Make sure closest works on disconnected DOM nodes. Fixes #7142. --- src/traversing.js | 2 +- test/unit/traversing.js | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/traversing.js b/src/traversing.js index de250e6c6..5a479f2ef 100644 --- a/src/traversing.js +++ b/src/traversing.js @@ -108,7 +108,7 @@ jQuery.fn.extend({ } else { cur = cur.parentNode; - if ( !cur.ownerDocument || cur === context ) { + if ( !cur || !cur.ownerDocument || cur === context ) { break; } } diff --git a/test/unit/traversing.js b/test/unit/traversing.js index 0636f0c33..f9e79372a 100644 --- a/test/unit/traversing.js +++ b/test/unit/traversing.js @@ -122,7 +122,7 @@ test("filter(jQuery)", function() { }) test("closest()", function() { - expect(10); + expect(11); same( jQuery("body").closest("body").get(), q("body"), "closest(body)" ); same( jQuery("body").closest("html").get(), q("html"), "closest(html)" ); same( jQuery("body").closest("div").get(), [], "closest(div)" ); @@ -139,7 +139,9 @@ test("closest()", function() { //Test that .closest() returns unique'd set equals( jQuery('#main p').closest('#main').length, 1, "Closest should return a unique set" ); - + + // Test on disconnected node + equals( jQuery("

").find("p").closest("table").length, 0, "Make sure disconnected closest work." ); }); test("closest(Array)", function() { -- 2.39.5