Recognize nodes from other realms via realm-safe type checks - #156
Open
lizarusi wants to merge 2 commits into
Open
Recognize nodes from other realms via realm-safe type checks#156lizarusi wants to merge 2 commits into
lizarusi wants to merge 2 commits into
Conversation
… behaviors One test per realm-sensitive check: entry normalization (crash), id-based element preservation (detached and SlicedParentNode paths), template content morphing, input/option/textarea value syncing, head handling, and Document normalization.
Nodes created in another JS realm (e.g. an iframe's document) fail `instanceof` checks, even after being adopted into this document, because each realm has its own constructors. This made morph() throw "TypeError: newContent is not iterable" when given a cross-realm node, and silently skip template handling, id-based matching, input/option/textarea value syncing, head handling, and Document normalization for cross-realm nodes. Replace realm-sensitive `instanceof` checks with helpers that duck-type via `nodeType` and `localName` (the approach morphdom uses). The `document.activeElement` checks keep `instanceof`, since the active element always belongs to this document. Each converted check has a dedicated test that fails if that single check is reverted to `instanceof`.
lizarusi
force-pushed
the
fix-cross-realm-newcontent
branch
from
August 13, 2026 13:10
c9da9c7 to
a52cd2f
Compare
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.
Hi!
While using idiomorph at Walnut, we hit a bug that we've been carrying as a local patch — contributing the fix upstream.
The bug
Passing a node created in another JS realm (e.g. an iframe's
contentDocument) asnewContentthrows:Repro:
Every realm has its own constructors, so cross-realm nodes fail
instanceof NodeinnormalizeParentand fall through to the array/HTMLCollection branch, which blows up on the spread.The crash is only the visible part.
instanceofstays false for these nodes even after they are adopted into this document, so once past normalization they also silently fail every other realm-sensitiveinstanceofcheck: template content handling, id-based matching of new children (newChild instanceof Element), andsyncInputValue— meaning input/option/textarea values would silently not sync from cross-realm content. The second test demonstrates that.We hit this in production at Walnut, where we morph DOM captured from customer apps across document boundaries — we've been carrying a fix as a local patch since 0.7.2.
The fix
Replace realm-sensitive
instanceofchecks with tiny duck-typing helpers (nodeType/localName) — the same approach morphdom uses. The helpers carry JSDoc type predicates, so TypeScript narrows exactly likeinstanceofdid andnpm run typecheckstays green. Thedocument.activeElementchecks in the focus-preservation code keepinstanceof, since the active element always belongs to this document.First commit adds the failing tests, second commit makes them pass. There is a dedicated test per converted check — entry normalization, id-based element preservation (both the dummy-parent and
SlicedParentNodepaths), template content morphing, input/option/textarea value syncing, head handling, and Document normalization — and each test was verified to fail if its single check alone is reverted toinstanceof.npm run typecheck,npm run format:check, and the full suite pass, with coverage at 100%.Relates to #103 — whichever direction the
newContenttype-narrowing goes, cross-realm nodes currently crash rather than being handled or rejected cleanly, so this seems worth fixing today.