Bug Tracker


Ticket #12009 (closed bug: fixed)

Opened 22 months ago

Last modified 18 months ago

jQueryObject.find(element) corrupts the stack

Reported by: Olson.dev@… Owned by: mikesherov
Priority: low Milestone: 1.9
Component: traversing Version: 1.7.2
Keywords: Cc:
Blocking: Blocked by:


Version: 1.7.2

Browser: Chrome 21 dev, Firefox 14

OS: Windows 7 64-bit

Step-by-step:  http://jsfiddle.net/usY7W/

Expectation: I should be able to chain $(blah).find(element).andSelf().filter(element) and effectively check if blah or its descendants contain element.

Actually happens: $(blah).find(element).andSelf().filter(element).length is always 1 because .find(element) corrupts the stack and .andSelf().filter(element) is always a jQuery object only wrapping element

Change History

comment:1 Changed 22 months ago by anonymous

I fixed some logic and removed redundant CSS.  http://jsfiddle.net/usY7W/3/

comment:2 Changed 22 months ago by dmethvin

  • Status changed from new to closed
  • Resolution set to invalid

The "problem line" boils down to this:


That will either be 0 or 1 elements. Since the event has bubbled to this it would seem like event.target must be either this or one of its descendants so the result will always be e.target. So I have no idea what you're trying to do here, or what "corrupts the stack" means. Please ask for help on the forum.

comment:3 Changed 22 months ago by Olson.dev@…

Don't confuse yourself; this has nothing to do with the bubbling. See the jsFiddle below with an even simpler example (sorry for not reducing it even further in the first place).

The problem is the implementation of jQuery.fn.find, particularly this section:

if (typeof selector !== "string") {
    return jQuery(selector).filter(function () {
        for (i = 0, l = self.length; i < l; i++) {
            if (jQuery.contains(self[i], this)) {
                return true;

Internally jQuery keeps a context stack. Every traversal should push the previous context on the stack. That's what makes jQuery.fn.andSelf() work.

Because $(this).find(someElement) hits that block of code above, the stack becomes corrupted. That is, it effectively starts all over.

Thus, the next operation, .andSelf() is going to make return a jQuery object only wrapping someElement. Then when .filter(someElement) gets executed, it's obviously always going to return a jQuery object with a length of 1.

Here's a simpler demonstration of the problem:  http://jsfiddle.net/295X6/

It all has to do with WHAT you pass into jQuery.fn.find().

comment:4 Changed 22 months ago by Olson.dev@…

 http://jsfiddle.net/295X6/1/ made the output more intuitive...

comment:5 Changed 22 months ago by dmethvin

  • Status changed from closed to reopened
  • Resolution invalid deleted

Okay, that example makes a lot more sense.

comment:6 Changed 22 months ago by dmethvin

  • Status changed from reopened to open

comment:7 Changed 22 months ago by dmethvin

  • Component changed from unfiled to traversing

comment:8 Changed 20 months ago by mikesherov

  • Owner set to mikesherov
  • Priority changed from undecided to low
  • Status changed from open to assigned
  • Milestone changed from None to 1.8.2

comment:10 Changed 19 months ago by mikesherov

  • Milestone changed from 1.8.2 to 1.9

comment:11 Changed 18 months ago by Mike Sherov

  • Status changed from assigned to closed
  • Resolution set to fixed

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

Changeset: e8f91514a6f353e85d8117998087903dc7331210

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.


Add a comment

Modify Ticket

as closed

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.