Implement getElementById on Document and DocumentFragment#618
Closed
airhorns wants to merge 1 commit into
Closed
Conversation
The polyfill gave ParentNode querySelector and querySelectorAll, but the
NonElementParentNode.getElementById lookup was missing, so
document.getElementById(id) threw "document.getElementById is not a function"
inside a remote environment. Host code is typically type-checked against
lib.dom, which does declare the method, so the gap was invisible to every
static check and surfaced only at runtime in the browser.
Return the first element in tree order whose id attribute matches. Match that
attribute literally rather than delegating to querySelector('#' + id): an id is
a string, not a selector, and the selector tokenizer has no escape handling, so
an id containing '.', ':', '[', or whitespace would parse as a different
selector.
Co-Authored-By: Claude <noreply@anthropic.com>
Author
|
closing in favour of #620 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
@remote-dom/polyfillgivesParentNodebothquerySelector()andquerySelectorAll(), but theNonElementParentNode.getElementById()lookup is missing. Inside a remote environment,document.getElementById(id)throws:This is a nasty failure mode for host code, because remote code is normally type-checked against
lib.dom, which does declaregetElementById. So the call is type-clean, passes every static check, and only blows up at runtime in the sandbox. We hit exactly this in Shopify's admin: a script handler in a remote view looked up an element by id to call an imperative method on it, typechecked green, and threw as soon as the handler ran.Fix
Implement
getElementById()onDocumentandDocumentFragment(the twoNonElementParentNodehosts per the DOM spec — deliberately not onElement), delegating to a shared helper inshared.ts. This mirrors how the existingquerySelector/querySelectorAlldelegate toselectors.ts.The helper returns the first element in tree order whose
idattribute matches, walking the existingCHILD/NEXTlinks.It matches the
idattribute literally rather than delegating toquerySelector('#' + id). An id is a string, not a selector, and the selector tokenizer inselectors.tshas no escape handling — so an id containing.,:,[, or whitespace would parse as a different selector entirely and silently return the wrong element (or nothing).getElementById('a.b')must find<div id="a.b">, not an<a>with classb.