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];
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;
});
});
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])" );