Skip to content
Permalink
Browse files
Fix #12816. Ensure .find() result are DOM ordered.
  • Loading branch information
dmethvin committed Nov 1, 2012
1 parent e8cf41a commit 5ed0450
Showing 1 changed file with 10 additions and 30 deletions.
@@ -12,46 +12,26 @@ var runtil = /Until$/,

jQuery.fn.extend({
find: function( selector ) {
var prevLength, n, r, ret,
i = 0,
self = this,
selfLength = this.length;
var i, ret, self;

if ( typeof selector !== "string" ) {

ret = jQuery( selector ).filter(function() {
for ( ; i < selfLength; i++ ) {
self = this;
return this.pushStack( jQuery( selector ).filter(function() {
for ( i = 0; i < self.length; i++ ) {
if ( jQuery.contains( self[ i ], this ) ) {
return true;
}
}
});

} else {
}) );
}

ret = [];
for ( ; i < selfLength; i++ ) {
prevLength = ret.length;
jQuery.find( selector, this[ i ], ret );

if ( i > 0 ) {
// Make sure that the results are unique
// by comparing the newly added elements on the ith
// iteration to the elements added by the previous iterations
for ( n = prevLength; n < ret.length; n++ ) {
for ( r = 0; r < prevLength; r++ ) {
if ( ret[ r ] === ret[ n ] ) {
ret.splice( n--, 1 );
break;
}
}
}
}
}
ret = [];
for ( i = 0; i < this.length; i++ ) {
jQuery.find( selector, this[ i ], ret );
}

// Needed because $( selector, context ) becomes $( context ).find( selector )
ret = this.pushStack( ret );
ret = this.pushStack( jQuery.unique( ret ) );
ret.selector = ( this.selector ? this.selector + " " : "" ) + selector;
return ret;
},

1 comment on commit 5ed0450

@dmethvin
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn, forgot to add another test!

Please sign in to comment.