Closed Bug 1007649 Opened 10 years ago Closed 10 years ago

querySelector for id on shadowRoot returns null

Categories

(Core :: DOM: Core & HTML, defect)

x86
macOS
defect
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla32

People

(Reporter: wilsonpage, Assigned: wchen)

References

Details

Attachments

(1 file)

myElement.shadowRoot.querySelector('#some-id'); //=> null
myElement.shadowRoot.getElementById('some-id'); //=> HTMLElement
This is because querySelector makes certain optimizations for ID selectors when the node is in a document (mapping to document.getElementById in that case).  And shadow roots lie about being in a document: see bug 992521.

That said, we should perhaps disable that querySelector optimization when aRoot is anonymous!

Wilson, can you please attach the testcase you were using to reproduce this bug?
Depends on: 992521
Flags: needinfo?(wilsonpage)
OK, this stuff is just totally busted.  Clearly no one has ever written a test trying to querySelector on a shadowroot, since it immediately fails an assertion in FindMatchingElements:

  NS_ASSERTION(aRoot->IsElement() || aRoot->IsNodeOfType(nsINode::eDOCUMENT) ||
               !aRoot->IsInDoc(),
               "The optimization below to check ContentIsDescendantOf only for "
               "elements depends on aRoot being either an element or a "
               "document if it's in the document.");

Furthermore, the shadow root does not claim to be in an anonymous subtree, so the obvious fix I was going to try won't even work.

We really need to fix bug 992521.
Of course this doesn't fix the bug, because ShadowRoot is just completely fucked up
Boris, would this have to do with the crash seen here: 
https://crash-stats.mozilla.com/report/index/f834ff19-e794-4eed-a9e3-4266a2140617
We only see the crash on the 6/11 build.
No.  In fact, I would expect this bug to be fixed now that bug 992521 is fixed.  Wilson, can you confirm?

That crash is some other issue that you should probably file a separate bug on.
Flags: needinfo?(wilsonpage)
Confirmed. Original test case in comment 2 is now working as expected.
Status: NEW → RESOLVED
Closed: 10 years ago
Flags: needinfo?(wilsonpage)
Resolution: --- → FIXED
Assignee: nobody → wchen
Target Milestone: --- → mozilla32
Thanks Boris.  I'll file it.
Component: DOM → DOM: Core & HTML
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: