Skip to content

dom-shim, page-script: document identity, dialog focus, frame windows, style preloads, and viewport meta - #207

Open
BernardoPe wants to merge 9 commits into
bpereira/compat-engine-resources-historyfrom
bpereira/compat-dom-shim-page-script
Open

BernardoPe wants to merge 9 commits into
bpereira/compat-engine-resources-historyfrom
bpereira/compat-dom-shim-page-script

Conversation

@BernardoPe

Copy link
Copy Markdown
Collaborator

Part 2 of 5 in a stacked compatibility series (stacked on #206).

Changes

  • Document identity, modal focus, download anchors, and structuredClone (dom-shim, page-script):
    • Point document.constructor at Document so element instanceof document.constructor is false (fixes Bootstrap 4 tooltip guard throwing on dominos.com).
    • Run HTML's dialog focusing steps in showModal() so opened modals receive keyboard focus (fixes Escape dismissal on excalidraw.com).
    • Keep the current document for download anchors targeting blob:, data:, or same-origin URLs (fixes feathericons.com).
    • Reject DOM nodes with DataCloneError in structuredClone instead of cloning them into plain objects (fixes docs.stripe.com).
  • target="_blank" anchors and SVG timing API (page-script):
    • Keep the current tab in place for links targeting _blank or non-reserved browsing context names (fixes cvc.com.br).
    • Add pauseAnimations()/unpauseAnimations() on SVG elements with receiver checks and TODO for SMIL animation playback, and fix SVG constructor prototype inheritance (fixes soundcloud.com).
  • Frame-document inline .style and inserted-script budget (dom-shim, page-script):
    • Expose .style on ParsedElement by reusing Element's inline style declaration (fixes w3schools.com).
    • Cap inserted scripts at 8 MiB per script and 32 MiB total instead of a single 8 MiB cumulative cap (fixes jotform.com).
  • Same-origin frame contentWindow constructors (page-script): Copy every realm constructor (read via property descriptors so accessors are not invoked) onto same-origin frame windows alongside existing window helpers, while keeping cross-origin facades restricted (fixes Osano HTMLScriptElement.prototype reads on life360.com and rentgrata.com).
  • Parser-inserted style preloads (page-script): Fetch parser-inserted <link rel="preload" as="style"> and dispatch load/error so onload handlers that swap rel to "stylesheet" run (fixes blogs.com).
  • Numeric width in <meta name="viewport"> (page-script): Widen narrow mobile viewports to max(deviceWidth, metaWidth) with proportional height scaling while leaving width=device-width and wider desktop viewports unchanged (fixes wikidata.org).

Verification

  • packages/engine/test/e2e/document-constructor-identity.e2e.test.ts
  • packages/engine/test/e2e/dialog-modal-focus.e2e.test.ts
  • packages/engine/test/e2e/download-anchor-no-navigation.e2e.test.ts
  • packages/engine/test/e2e/structured-clone-node-rejection.e2e.test.ts
  • packages/engine/test/e2e/anchor-target-blank-no-navigation.e2e.test.ts
  • packages/engine/test/e2e/svg-animation-methods.e2e.test.ts
  • packages/engine/test/e2e/frame-document-inline-style.e2e.test.ts
  • packages/engine/test/e2e/iframe-window-constructors.e2e.test.ts
  • packages/engine/test/e2e/preload-style-link.e2e.test.ts
  • packages/engine/test/e2e/meta-viewport-width.e2e.test.ts

@BernardoPe
BernardoPe added this pull request to stack #211 November 23, 2026 12:25
@BernardoPe
BernardoPe force-pushed the bpereira/compat-dom-shim-page-script branch from 33a6b49 to 450ea31 Compare November 23, 2026 12:48
@BernardoPe
BernardoPe force-pushed the bpereira/compat-dom-shim-page-script branch from 450ea31 to 53bde43 Compare November 23, 2026 13:43
@github-actions github-actions Bot added the vouch:trusted Author is vouched, a collaborator, or a bot. label Nov 24, 2026
Comment thread packages/page-script/src/index.ts Resolved
Comment thread packages/page-script/src/index.ts Resolved
Comment thread packages/page-script/src/index.ts Resolved
Comment thread packages/page-script/src/globals.ts Resolved
@BernardoPe
BernardoPe force-pushed the bpereira/compat-dom-shim-page-script branch from 53bde43 to d16ac9e Compare November 24, 2026 18:51
@BernardoPe
BernardoPe force-pushed the bpereira/compat-dom-shim-page-script branch from d16ac9e to 90ac50e Compare November 24, 2026 22:20
@BernardoPe
BernardoPe force-pushed the bpereira/compat-dom-shim-page-script branch from 90ac50e to 7d2050c Compare November 25, 2026 22:16

@ruifigueira ruifigueira left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beep-boop-rui-agent-🤖

The blocking findings are fixed. Approving.

@BernardoPe
BernardoPe force-pushed the bpereira/compat-dom-shim-page-script branch 2 times, most recently from ac19aaa to d1757c5 Compare November 26, 2026 17:47
Four web-platform behavior gaps:

`document.constructor` was the live DocumentImpl, whose inherited
hasInstance accepted any node. Bootstrap 4's tooltip guard compares
against it, so `span instanceof document.constructor` was true and the
guard threw on every element on dominos.com. The constructor now points
at the Document interface.

showModal() left focus on the page, so a modal could not be operated by
keyboard. It now runs the HTML dialog focusing steps: the autofocus
element, else the first focusable descendant, else the dialog.

A `download` anchor followed a blob: or data: target as a navigation and
replaced the document. It now keeps the current document per HTML's
hyperlink download rules.

structuredClone copied a DOM node into a broken plain object instead of
throwing DataCloneError, so a page that cloned a node saw a silent
success.

Signed-off-by: bpereira <bpereira@cloudflare.com>
A link with target="_blank" (or any non-reserved target name) opened in
another browsing context on the web; Kitesurf navigated the current tab
instead, losing the page the visitor was on (cvc.com.br's Promoções link).

SVG elements also had no timing API, so a page that calls
pauseAnimations()/unpauseAnimations() unconditionally threw a TypeError
and aborted its script (soundcloud.com). The methods now exist on
Element.prototype with a receiver check, since SVG elements are plain
Element instances. Nothing animates yet; the TODO records that.

The SVG constructor stubs now inherit from SVGElement, which inherits
from Element, so SVGSVGElement.prototype resolves those members the way
the real interface hierarchy does.

Signed-off-by: bpereira <bpereira@cloudflare.com>
Elements created from an iframe's document are ParsedElements, which had no
style property. A page that set `el.style.cssText` on one threw
"Cannot set properties of undefined" (w3schools.com's fast-cmp listener).

The inline style declaration only needs getAttribute and setAttribute, so
ParsedElement reuses Element's implementation through a named export rather
than duplicating it.

page-script: cap inserted scripts per script, not only in total

The 8 MiB budget was cumulative, so a page whose inserted scripts add up past
it silently lost the rest (jotform.com skipped a 6.7 MB embedded agent). One
script is still capped at 8 MiB, which bounds what boa buffers and parses; the
page total is now 32 MiB.

Signed-off-by: bpereira <bpereira@cloudflare.com>
…dows

The iframe contentWindow facade carried a hand-kept list of constructors, so
`frame.contentWindow.HTMLScriptElement` was undefined. Osano's bundle reads
`HTMLScriptElement.prototype.src` through that window and threw
"Cannot read properties of undefined (reading 'prototype')" on life360.com and
rentgrata.com.

The decorator now copies every global that is a constructor (a function with a
prototype), read from its descriptor so accessors such as `document` are not
invoked, alongside the non-constructor window helpers. Cross-origin facades are
built elsewhere and still hide them.

Signed-off-by: bpereira <bpereira@cloudflare.com>
A parser-inserted `<link rel="preload" as="style">` with an `onload` handler
(the standard non-blocking CSS pattern that swaps `rel` to `"stylesheet"` on
load) never fetched and never fired `load`, leaving dialogs and sections
unstyled on sites like blogs.com.

Start the preload fetch when the initial document is parsed and dispatch
`load` or `error` on the `<link>` element per HTML's preload link processing.

Signed-off-by: bpereira <bpereira@cloudflare.com>
Pages that declare a fixed layout viewport width such as
`<meta name="viewport" content="width=1120">` (e.g. wikidata.org) were still
laid out at the narrower device viewport width (375px), causing horizontal
overflow and a mismatched `window.innerWidth` / `clientWidth`.

Parse explicit numeric `width=<px>` from `<meta name="viewport">` in
`resolveLayoutViewport`, widening the layout viewport to `max(deviceWidth,
metaWidth)` and scaling height proportionally while leaving `width=device-width`
and wider desktop viewports unchanged.

https://drafts.csswg.org/css-viewport/#viewport-meta
Signed-off-by: bpereira <bpereira@cloudflare.com>
…erfaces

Copying every function with a prototype from `globalThis` onto same-origin
iframe facades leaked non-element constructors such as `BroadcastChannel` and
`EventTarget` across realms, causing cross-realm WPT tests (`abort-signal-timeout`,
`reason-constructor`, `detached-iframe`) to fail or time out.

Restore the explicit standard global list and copy only `HTML*Element`,
`SVG*Element`, and `MathML*Element` constructors dynamically.

Signed-off-by: bpereira <bpereira@cloudflare.com>
Signed-off-by: bpereira <bpereira@cloudflare.com>
…oad, and structuredClone review findings

Gate meta[name=viewport] numeric width on mobile emulation (passed from
Emulation.setDeviceMetricsOverride through NavigateOptions, setViewport,
and Page.startScreencast) and clamp the layout viewport to engine side
and pixel caps.

Keep _blank and unmatched named-target anchors navigating the current tab
until popup targets exist, route matching named targets to their
<iframe name> via querySelectorAll, and restrict download suppression to
blob:/data: URLs.

Replace duck-typed structuredClone node detection with a prototype-backed
isDomNode check across NodeBase/Attr/Detached/Parsed nodes, traversing
arrays without Object.keys and inspecting Map/Set entries.

Pass crossorigin mode and non-load-blocking mode on parser style
preloads, cache element interface names for same-origin frame decoration,
and require autofocus candidates in showModal() to be focusable.

Signed-off-by: bpereira <bpereira@cloudflare.com>
@BernardoPe
BernardoPe force-pushed the bpereira/compat-dom-shim-page-script branch from d1757c5 to 4984e0a Compare November 27, 2026 19:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

vouch:trusted Author is vouched, a collaborator, or a bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants