]> source.dussan.org Git - jquery.git/commitdiff
Added in the new .closest(Array) method, will be used to improve the performance...
authorjeresig <jeresig@gmail.com>
Thu, 3 Dec 2009 00:05:51 +0000 (19:05 -0500)
committerjeresig <jeresig@gmail.com>
Thu, 3 Dec 2009 00:05:51 +0000 (19:05 -0500)
src/traversing.js
test/unit/traversing.js

index b5a5a2dee176a9fd68bb1f91fb88aba606cb30c8..737efa429b37199ff6827842a826654f31358545 100644 (file)
@@ -54,21 +54,43 @@ jQuery.fn.extend({
                return this.pushStack( winnow(this, selector, true), "filter", selector );
        },
 
-       closest: function( selector, context ) {
-               var pos = jQuery.expr.match.POS.test( selector ) ? 
-                       jQuery( selector, context || this.context ) : null;
+       closest: function( selectors, context ) {
+               if ( jQuery.isArray( selectors ) ) {
+                       var ret = [], cur = this[0], selector;
+
+                       if ( cur ) {
+                               for ( var i = 0, l = selectors.length; i < l; i++ ) {
+                                       selectors[i] = jQuery.expr.match.POS.test( selector ) ? 
+                                               jQuery( selector, context || this.context ) :
+                                               selectors[i];
+                               }
+
+                               while ( cur && cur.ownerDocument && cur !== context ) {
+                                       for ( var i = 0; i < selectors.length; i++ ) {
+                                               selector = selectors[i];
+                                               if ( selector.jquery ? selector.index(cur) > -1 : jQuery(cur).is(selector) ) {
+                                                       selector = selector.selector || selector;
+                                                       ret.push({ selector: selector, elem: cur });
+                                                       selectors.splice(i--, 1);
+                                               }
+                                       }
+                                       cur = cur.parentNode;
+                               }
+                       }
+
+                       return ret;
+               }
+
+               var pos = jQuery.expr.match.POS.test( selectors ) ? 
+                       jQuery( selectors, context || this.context ) : null;
 
-               return this.map(function(){
-                       var cur = this, closer = 0;
+               return this.map(function(i, cur){
                        while ( cur && cur.ownerDocument && cur !== context ) {
-                               if ( pos ? pos.index(cur) > -1 : jQuery(cur).is(selector) ) {
-                                       jQuery.lastCloser = closer;
+                               if ( pos ? pos.index(cur) > -1 : jQuery(cur).is(selectors) ) {
                                        return cur;
                                }
                                cur = cur.parentNode;
-                               closer++;
                        }
-                       jQuery.lastCloser = -1;
                        return null;
                });
        },
index 780d30f195b3e497141e054b9f440baac4523e65..22d48681255ba65dd006a1e2120faf7b941af9cd 100644 (file)
@@ -107,6 +107,17 @@ test("closest()", function() {
        same( jq.closest("#nothiddendiv", document.body).get(), q("nothiddendiv"), "Context not reached." );
 });
 
+test("closest(Array)", function() {
+       expect(6);
+       same( jQuery("body").closest(["body"]), [{selector:"body", elem:document.body}], "closest([body])" );
+       same( jQuery("body").closest(["html"]), [{selector:"html", elem:document.documentElement}], "closest([html])" );
+       same( jQuery("body").closest(["div"]), [], "closest([div])" );
+       same( jQuery("#main").closest(["span,#html"]), [{selector:"span,#html", elem:document.documentElement}], "closest([span,#html])" );
+
+       same( jQuery("body").closest(["body","html"]), [{selector:"body", elem:document.body}, {selector:"html", elem:document.documentElement}], "closest([body, html])" );
+       same( jQuery("body").closest(["span","html"]), [{selector:"html", elem:document.documentElement}], "closest([body, html])" );
+});
+
 test("not(Selector)", function() {
        expect(7);
        equals( jQuery("#main > p#ap > a").not("#google").length, 2, "not('selector')" );