Skip to content

Commit

Permalink
Make sure closest works on disconnected DOM nodes. Fixes #7142.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeresig committed Oct 11, 2010
1 parent 2c04035 commit 385ca2a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/traversing.js
Expand Up @@ -108,7 +108,7 @@ jQuery.fn.extend({

} else {
cur = cur.parentNode;
if ( !cur.ownerDocument || cur === context ) {
if ( !cur || !cur.ownerDocument || cur === context ) {
break;
}
}
Expand Down
6 changes: 4 additions & 2 deletions test/unit/traversing.js
Expand Up @@ -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)" );
Expand All @@ -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("<div><p></p></div>").find("p").closest("table").length, 0, "Make sure disconnected closest work." );
});

test("closest(Array)", function() {
Expand Down

0 comments on commit 385ca2a

Please sign in to comment.