Fix #12009. $().find( DOMElement ) should pushStack properly. Close gh-927.

This commit is contained in:
Mike Sherov 2012-10-30 13:32:53 -04:00 committed by Dave Methvin
parent c78a3ba657
commit e8f91514a6
2 changed files with 36 additions and 27 deletions

View File

@ -12,29 +12,34 @@ var runtil = /Until$/,
jQuery.fn.extend({ jQuery.fn.extend({
find: function( selector ) { find: function( selector ) {
var i, l, length, n, r, ret, var prevLength, n, r, ret,
self = this; i = 0,
self = this,
selfLength = this.length;
if ( typeof selector !== "string" ) { if ( typeof selector !== "string" ) {
return jQuery( selector ).filter(function() {
for ( i = 0, l = self.length; i < l; i++ ) { ret = jQuery( selector ).filter(function() {
for ( ; i < selfLength; i++ ) {
if ( jQuery.contains( self[ i ], this ) ) { if ( jQuery.contains( self[ i ], this ) ) {
return true; return true;
} }
} }
}); });
}
ret = this.pushStack( [] ); } else {
for ( i = 0, l = this.length; i < l; i++ ) { ret = [];
length = ret.length; for ( ; i < selfLength; i++ ) {
prevLength = ret.length;
jQuery.find( selector, this[ i ], ret ); jQuery.find( selector, this[ i ], ret );
if ( i > 0 ) { if ( i > 0 ) {
// Make sure that the results are unique // Make sure that the results are unique
for ( n = length; n < ret.length; n++ ) { // by comparing the newly added elements on the ith
for ( r = 0; r < length; r++ ) { // 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 ] ) { if ( ret[ r ] === ret[ n ] ) {
ret.splice( n--, 1 ); ret.splice( n--, 1 );
break; break;
@ -43,8 +48,10 @@ jQuery.fn.extend({
} }
} }
} }
}
// Needed because $( "selector", context ) becomes $( context ).find( "selector" ) // Needed because $( selector, context ) becomes $( context ).find( selector )
ret = this.pushStack( ret );
ret.selector = ( this.selector ? this.selector + " " : "" ) + selector; ret.selector = ( this.selector ? this.selector + " " : "" ) + selector;
return ret; return ret;
}, },

View File

@ -1,12 +1,13 @@
module("traversing", { teardown: moduleTeardown }); module("traversing", { teardown: moduleTeardown });
test( "find(String)", function() { test( "find(String)", function() {
expect(5); expect( 6 );
equal( "Yahoo", jQuery("#foo").find(".blogTest").text(), "Check for find" ); equal( "Yahoo", jQuery("#foo").find(".blogTest").text(), "Check for find" );
// using contents will get comments regular, text, and comment nodes // using contents will get comments regular, text, and comment nodes
var j = jQuery("#nonnodes").contents(); var j = jQuery("#nonnodes").contents();
equal( j.find("div").length, 0, "Check node,textnode,comment to find zero divs" ); equal( j.find("div").length, 0, "Check node,textnode,comment to find zero divs" );
equal( j.find("div").andSelf().length, 3, "Check node,textnode,comment to find zero divs, but preserves pushStack" );
deepEqual( jQuery("#qunit-fixture").find("> div").get(), q( "foo", "moretests", "tabindex-tests", "liveHandlerOrder", "siblingTest", "fx-test-group" ), "find child elements" ); deepEqual( jQuery("#qunit-fixture").find("> div").get(), q( "foo", "moretests", "tabindex-tests", "liveHandlerOrder", "siblingTest", "fx-test-group" ), "find child elements" );
deepEqual( jQuery("#qunit-fixture").find("> #foo, > #moretests").get(), q( "foo", "moretests" ), "find child elements" ); deepEqual( jQuery("#qunit-fixture").find("> #foo, > #moretests").get(), q( "foo", "moretests" ), "find child elements" );
@ -14,7 +15,7 @@ test("find(String)", function() {
}); });
test( "find(node|jQuery object)", function() { test( "find(node|jQuery object)", function() {
expect( 11 ); expect( 12 );
var $foo = jQuery("#foo"), var $foo = jQuery("#foo"),
$blog = jQuery(".blogTest"), $blog = jQuery(".blogTest"),
@ -35,6 +36,7 @@ test("find(node|jQuery object)", function() {
equal( $two.find( $first ).length, 0, "first is in the collection and not within two" ); equal( $two.find( $first ).length, 0, "first is in the collection and not within two" );
equal( $two.find( $first ).length, 0, "first is in the collection and not within two(node)" ); equal( $two.find( $first ).length, 0, "first is in the collection and not within two(node)" );
equal( $two.find( $foo[ 0 ] ).andSelf().length, 2, "find preserves the pushStack, see #12009" );
}); });
test("is(String|undefined)", function() { test("is(String|undefined)", function() {