Skip to main content

Bug Tracker

Side navigation

#14351 closed bug (fixed)

Opened September 11, 2013 12:25PM UTC

Closed September 12, 2013 08:39PM UTC

Last modified September 20, 2013 12:12AM UTC

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

Reported by: jquery@fizkerinc.dk Owned by: gibson042
Priority: low Milestone: 1.11/2.1
Component: selector Version: 2.0.3
Keywords: Cc:
Blocked by: Blocking:
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.

Attachments (0)
Change History (4)

Changed September 11, 2013 09:29PM UTC by gibson042 comment:1

owner: → gibson042
status: newassigned

Changed September 12, 2013 08:39PM UTC by Richard Gibson comment:2

resolution: → fixed
status: assignedclosed

Fix #14351: Update Sizzle

Changeset: 1d57ffe5fff06928aac53813342d33ec6bddd531

Changed September 20, 2013 12:12AM UTC by dmethvin comment:3

milestone: None1.11/2.1

Changed September 20, 2013 12:12AM UTC by dmethvin comment:4

component: unfiledselector
priority: undecidedlow