Minor syntax changes to the winnow function.
[jquery.git] / src / traversing.js
index 609e2eb..388cd35 100644 (file)
@@ -1,16 +1,21 @@
+// Implement the identical functionality for filter and not
 var winnow = function( elements, qualifier, keep ) {
-       if(jQuery.isFunction( qualifier )) {
+       if ( jQuery.isFunction( qualifier ) ) {
                return jQuery.grep(elements, function(elem, i) {
                        return !!qualifier.call( elem, i ) === keep;
                });
-       } else if( qualifier.nodeType ) {
+
+       } else if ( qualifier.nodeType ) {
                return jQuery.grep(elements, function(elem, i) {
                        return (elem === qualifier) === keep;
                });
-       } else if( typeof qualifier === "string" ) {
-               var filtered = jQuery.grep(elements, function(elem) { return elem.nodeType === 1; });
 
-               if(isSimple.test( qualifier )) {
+       } else if ( typeof qualifier === "string" ) {
+               var filtered = jQuery.grep(elements, function(elem) {
+                       return elem.nodeType === 1;
+               });
+
+               if ( isSimple.test( qualifier ) ) {
                        return jQuery.filter(qualifier, filtered, !keep);
                } else {
                        qualifier = jQuery.filter( qualifier, elements );
@@ -56,29 +61,26 @@ jQuery.fn.extend({
 
        closest: function( selectors, context ) {
                if ( jQuery.isArray( selectors ) ) {
-                       var ret = [], cur = this[0], match, selector, done;
+                       var ret = [], cur = this[0], match, matches = {}, selector;
 
                        if ( cur && selectors.length ) {
                                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];
+                                       selector = selectors[i];
+
+                                       if ( !matches[selector] ) {
+                                               matches[selector] = jQuery.expr.match.POS.test( selector ) ? 
+                                                       jQuery( selector, context || this.context ) :
+                                                       selector;
+                                       }
                                }
 
                                while ( cur && cur.ownerDocument && cur !== context ) {
-                                       for ( var i = 0; i < selectors.length; i++ ) {
-                                               match = selectors[i];
-                                               selector = match.selector || match;
-
-                                               // Get rid of duplicate selectors
-                                               if ( selector === done ) {
-                                                       selectors.splice(i--, 1);
+                                       for ( selector in matches ) {
+                                               match = matches[selector];
 
-                                               // See if we have a match
-                                               } else if ( match.jquery ? match.index(cur) > -1 : jQuery(cur).is(match) ) {
+                                               if ( match.jquery ? match.index(cur) > -1 : jQuery(cur).is(match) ) {
                                                        ret.push({ selector: selector, elem: cur });
-                                                       selectors.splice(i--, 1);
-                                                       done = selector;
+                                                       delete matches[selector];
                                                }
                                        }
                                        cur = cur.parentNode;