Skip to content
Permalink
Browse files

Removing unique check on closest(Array) (it wasn't appropriate there …

…and broke functionality elsewhere) - follow-up to commit 8c41325. Also added another test for closest(Array) after a discussion with @jitter in 26b0e91.
  • Loading branch information
jeresig committed Sep 28, 2010
1 parent 8c41325 commit 7be11207b9267592a639eb739dc8d719c7de56a1
Showing with 9 additions and 2 deletions.
  1. +7 −1 src/traversing.js
  2. +2 −1 test/unit/traversing.js
@@ -56,9 +56,11 @@ jQuery.fn.extend({

closest: function( selectors, context ) {
var ret;

if ( jQuery.isArray( selectors ) ) {
var cur = this[0], match, matches = {}, selector, level = 1;
ret = [];

if ( cur && selectors.length ) {
for ( var i = 0, l = selectors.length; i < l; i++ ) {
selector = selectors[i];
@@ -78,23 +80,27 @@ jQuery.fn.extend({
ret.push({ selector: selector, elem: cur, level: level });
}
}

cur = cur.parentNode;
level++;
}
}

return ret.length > 1 ? jQuery.unique(ret) : ret;
return ret;
}

var pos = jQuery.expr.match.POS.test( selectors ) ?
jQuery( selectors, context || this.context ) : null;

ret = jQuery.map(this.get(),function( cur,i ) {
while ( cur && cur.ownerDocument && cur !== context ) {
if ( pos ? pos.index(cur) > -1 : jQuery(cur).is(selectors) ) {
return cur;
}

cur = cur.parentNode;
}

return null;
});

@@ -141,10 +141,11 @@ test("closest()", function() {
});

test("closest(Array)", function() {
expect(6);
expect(7);
same( jQuery("body").closest(["body"]), [{selector:"body", elem:document.body, level:1}], "closest([body])" );
same( jQuery("body").closest(["html"]), [{selector:"html", elem:document.documentElement, level:2}], "closest([html])" );
same( jQuery("body").closest(["div"]), [], "closest([div])" );
same( jQuery("#yahoo").closest(["div"]), [{"selector":"div", "elem": document.getElementById("foo"), "level": 3}, { "selector": "div", "elem": document.getElementById("main"), "level": 4 }], "closest([div])" );
same( jQuery("#main").closest(["span,#html"]), [{selector:"span,#html", elem:document.documentElement, level:4}], "closest([span,#html])" );

same( jQuery("body").closest(["body","html"]), [{selector:"body", elem:document.body, level:1}, {selector:"html", elem:document.documentElement, level:2}], "closest([body, html])" );

0 comments on commit 7be1120

Please sign in to comment.
You can’t perform that action at this time.