Skip to content
Permalink
Browse files

Re-worked the closest no-duplicate-selector code again to be more eff…

…icient.
  • Loading branch information
jeresig committed Dec 3, 2009
1 parent 525bb27 commit 24e96ba22ca86e54f2442b88ffd2b7ea3f60f0bf
Showing with 12 additions and 15 deletions.
  1. +12 −15 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;

0 comments on commit 24e96ba

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