diff options
author | jeresig <jeresig@gmail.com> | 2009-12-03 11:14:10 -0500 |
---|---|---|
committer | jeresig <jeresig@gmail.com> | 2009-12-03 11:14:10 -0500 |
commit | 24e96ba22ca86e54f2442b88ffd2b7ea3f60f0bf (patch) | |
tree | e603e46190a4d6b5c87b910bfe9825b1af93af76 /src/traversing.js | |
parent | 525bb27bc0ccfcf384a209773eb93dc1b5a8cc09 (diff) | |
download | jquery-24e96ba22ca86e54f2442b88ffd2b7ea3f60f0bf.tar.gz jquery-24e96ba22ca86e54f2442b88ffd2b7ea3f60f0bf.zip |
Re-worked the closest no-duplicate-selector code again to be more efficient.
Diffstat (limited to 'src/traversing.js')
-rw-r--r-- | src/traversing.js | 27 |
1 files changed, 12 insertions, 15 deletions
diff --git a/src/traversing.js b/src/traversing.js index 609e2ebb0..5836ab578 100644 --- a/src/traversing.js +++ b/src/traversing.js @@ -56,29 +56,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; |