Bug Tracker

Ticket #14351 (closed bug: fixed)

Opened 16 months ago

Last modified 15 months ago

Exception thrown when running `find` in a non-attached DOM node

Reported by: jquery@… Owned by: gibson042
Priority: low Milestone: 1.11/2.1
Component: selector Version: 2.0.3
Keywords: Cc:
Blocking: Blocked by:

Description

tl;dr

The following snippet throws an exception in jQuery from version 1.8.3 and forward:

$('<div><a></a></div>').find(':not(:hidden) + c , d')

The exception reads:

Cannot read property '0' of undefined

When asking JSFiddle to use jQuery version 1.x/2.x, the message changes to:

Cannot read property 'length' of undefined

If appending the new element to another node, it works fine:

$('<div><a></a></div>').appendTo('<div>').find(':not(:hidden) + c , d')

More details

Testing in JSFiddle ( http://jsfiddle.net/cpxbb/), it worked in jQuery 1.7.2 and started failing in jQuery 1.8.3.

The error message changed nature between 1.9.1/2.0.2 and the 1.x/2.x versions (JSFiddle does not currently have any specific versions above 1.9.1/2.0.2).

I debugged a bit through the minified version of 1.8.3, and it fails at a point where it tries to find tags based on the parentNode of the root element, which is a document-fragment for an unattached element.

The call that returned undefined is:

i.find.TAG("*", h && a.parentNode || a)

where a is the top element (the div in the example code).


An immediate work-around is to attach the node to a dummy-element before running the find operation.

Change History

comment:1 Changed 16 months ago by gibson042

  • Owner set to gibson042
  • Status changed from new to assigned

comment:2 Changed 16 months ago by Richard Gibson

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

Fix #14351: Update Sizzle

Changeset: 1d57ffe5fff06928aac53813342d33ec6bddd531

comment:3 Changed 15 months ago by dmethvin

  • Milestone changed from None to 1.11/2.1

comment:4 Changed 15 months ago by dmethvin

  • Priority changed from undecided to low
  • Component changed from unfiled to selector
Note: See TracTickets for help on using tickets.