Skip to content

Commit cae93c3

Browse files
committed
Implemented support for .context limited .closest() calls. Fixes #4072.
1 parent 343b093 commit cae93c3

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/traversing.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,12 @@ jQuery.fn.extend({
5353

5454
closest: function( selector ) {
5555
var pos = jQuery.expr.match.POS.test( selector ) ? jQuery(selector) : null,
56-
closer = 0;
56+
closer = 0,
57+
context = this.context;
5758

5859
return this.map(function(){
5960
var cur = this;
60-
while ( cur && cur.ownerDocument ) {
61+
while ( cur && cur.ownerDocument && cur !== context ) {
6162
if ( pos ? pos.index(cur) > -1 : jQuery(cur).is(selector) ) {
6263
jQuery.data(cur, "closest", closer);
6364
return cur;
@@ -120,4 +121,4 @@ jQuery.each({
120121

121122
return this.pushStack( jQuery.unique( ret ), name, selector );
122123
};
123-
});
124+
});

test/unit/traversing.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,20 @@ test("filter(jQuery)", function() {
9191
})
9292

9393
test("closest()", function() {
94-
expect(6);
94+
expect(9);
9595
isSet( jQuery("body").closest("body").get(), q("body"), "closest(body)" );
9696
isSet( jQuery("body").closest("html").get(), q("html"), "closest(html)" );
9797
isSet( jQuery("body").closest("div").get(), [], "closest(div)" );
9898
isSet( jQuery("#main").closest("span,#html").get(), q("html"), "closest(span,#html)" );
9999

100100
isSet( jQuery("div:eq(1)").closest("div:first").get(), [], "closest(div:first)" );
101101
isSet( jQuery("div").closest("body:first div:last").get(), q("fx-tests"), "closest(body:first div:last)" );
102+
103+
// Test .closest() limited by the context
104+
var jq = jQuery("#nothiddendivchild", document.body);
105+
isSet( jq.closest("html").get(), [], "Context limited." );
106+
isSet( jq.closest("body").get(), [], "Context limited." );
107+
isSet( jq.closest("#nothiddendiv").get(), q("nothiddendiv"), "Context not reached." );
102108
});
103109

104110
test("not(Selector)", function() {

0 commit comments

Comments
 (0)