Fix #12816. Ensure .find() result are DOM ordered.

This commit is contained in:
Dave Methvin 2012-10-31 22:43:04 -04:00
parent e8cf41a051
commit 5ed0450dc4

View File

@ -12,46 +12,26 @@ var runtil = /Until$/,
jQuery.fn.extend({ jQuery.fn.extend({
find: function( selector ) { find: function( selector ) {
var prevLength, n, r, ret, var i, ret, self;
i = 0,
self = this,
selfLength = this.length;
if ( typeof selector !== "string" ) { if ( typeof selector !== "string" ) {
self = this;
ret = jQuery( selector ).filter(function() { return this.pushStack( jQuery( selector ).filter(function() {
for ( ; i < selfLength; i++ ) { for ( i = 0; i < self.length; i++ ) {
if ( jQuery.contains( self[ i ], this ) ) { if ( jQuery.contains( self[ i ], this ) ) {
return true; return true;
} }
} }
}); }) );
}
} else { ret = [];
for ( i = 0; i < this.length; i++ ) {
ret = []; jQuery.find( selector, this[ i ], 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;
}
}
}
}
}
} }
// Needed because $( selector, context ) becomes $( context ).find( selector ) // 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; ret.selector = ( this.selector ? this.selector + " " : "" ) + selector;
return ret; return ret;
}, },