fix(electron): measure toBeInViewport as Chromium clips the node

Closed
KD
3d ago
+318-7

What

expect(locator).toBeInViewport() on an Electron target measures a node the way Chromium draws it, in each of these cases. On main, the tests for them fail (see Verified): a clipped node read as in view, and the window's own node failed.

PageNow
A position: fixed box inside a transformed, filtered, or contained ancestor that clipsclipped by that ancestor, as the browser holds it
A scaled ancestor (transform: scale(2))clipped where and at the scale it is drawn
A slotted elementclipped by the slot it is drawn in
clip-path: inset/circle/ellipse/polygon(...) with lengths, %, or calc(), and an optional geometry box (content-box), on the node or an ancestorclipped by the bounds of the shape, against that box, at the box's scale
A CSS clip: rect(...) (the visually hidden pattern)out of view
overflow: clip with overflow-clip-margin, contain: paint, content-visibilityclips as drawn; the margin counts from the box it names, and widens paint containment and a clip on both axes, never a scroller
An inline element's overflow, a body whose root element sets overflow, a body or root element with containmentas the browser does
A modal dialog or open popover (top layer)clipped only by the window
A locator that matches the window itself, by its titlein view; toHaveCSS reads the root element

Why

The measurement walked parentElement and clipped by each ancestor's getBoundingClientRect() when its overflow was not visible. That missed every case in the table, so toBeInViewport gave a different answer from web() on pages that use these patterns.

How it works

  1. viewportIntersection starts from the node's box and narrows it by every ancestor that clips it, walking renderedParent (the assigned slot, the parent, or the shadow host) and stopping at the top layer (packages/electron/src/in-page/node-ops.ts:331-490).
  2. Whether an ancestor holds the node follows CSS containing blocks: any positioned ancestor for an absolute box, and for a fixed box only one with a transform, filter, containment, or will-change of those (holdsFixed, node-ops.ts:342).
  3. Each clipping ancestor's client box is scaled from its CSS size to the size it is drawn at (getBoundingClientRect over offsetWidth). overflow-clip-margin is added from the padding, content, or border box it names, as Chromium applies it: to paint containment or an overflow: clip on both axes, and not to a box that scrolls (hidden, scroll, auto). The body clips as any box does unless its overflow is the window's: the root element's overflow is visible and neither element has containment.
  4. clipPathOf reads inset(), circle(), ellipse(), and polygon() with lengths, percentages, and calc(), against the geometry box after the shape (padding, content, or margin box; the border box by default). It clips by the shape's bounding box: a circle sized in the box's own pixels, with a keyword radius measured as an absolute distance to the sides, then scaled on each axis. clipRectOf reads clip: rect() on an absolutely positioned box. A path(), a URL reference, a mirrored or rotated transform, and the exact outline of a polygon are not modelled.
  5. ElectronSurface answers viewportIntersection and readStyle for the window's own node (ROOT_ID): it is the viewport, and its styles are the root element's (packages/electron/src/surface.ts:556-580).
  6. packages/electron/tests/integration/node-ops.test.ts covers each row in Chromium; the testbed checks the window's own node.

Verified

Ran it locally: yes, on macOS (Apple Silicon) with Electron 45.5.0.

  • This branch passes the Electron package's unit and integration tests, the Electron testbed run through the built CLI (pnpm --filter @e2e-dev/electron-testbed test), and pnpm check, run at f2dbb32b0. After the review fixes, at 905e53d66: the unit tests, node-ops.test.ts, typecheck, and oxlint pass, and each new case fails with its fix reverted.
  • main against this branch: this branch's tests run against origin/main's packages/electron/src. These tests fail there and pass here:
SuiteTestmainthis PR
integrationthe window's own node is the viewport, and has its root element's stylesfailspasses
integrationnodeOp clips a box by its own clip-path, by a circle of the default radius, and by nothing above the top layerfailspasses
integrationnodeOp clips by content-visibility, and widens an overflow-clip-margin at the scale its box is drawnfailspasses
integrationnodeOp clips by the body when the root element has an overflow of its own, which keeps the body's from being the window'sfailspasses
integrationqueryAll reads computed styles, and how much of a box its scrollers and the window leave in viewfailspasses

Each failing test holds several cases; on main the first case that fails stops it.

  • Benchmark: pnpm bench:ab --suite electron-testbed (main against this branch) is still running; its table replaces this line when it finishes.
  • Video: none attached. The change is in what the engine reads and when it acts, which the tests above pin down; a recording would show the same window either way.
  • Local macOS runs, on both sides, started Electron with -ApplePersistenceIgnoreState YES from #2500, applied without committing it. Without it, an Electron the tests killed makes macOS ask to reopen its windows, and the next launch waits out its timeout. The Linux and Windows CI jobs do not need it.

Fresh-context review (Codex gpt-6.2-sol, read-only, on this branch's diff):

  • Fixed: overflow-clip-margin was always added to the padding box; it now counts from the box it names (70a18fd54, with a test).
  • Fixed: the reference now says what clips a node, and the changeset is added.
  • Dismissed: a polygon clips by its bounding box, so a child inside the bounds but outside the shape counts as in view. Measuring by the shape's bounds is the documented approach (clipPathOf); intersecting a polygon with a rectangle exactly is not modelled.
  • Dismissed: a mirrored ancestor (scaleX(-1)) with an inset() clip keeps the wrong side. The measurement models scale, not reflection or rotation, which would need the whole transform chain.

Review bots, on the previous head:

  • Fixed: a circle() or ellipse() centred outside its box took a negative keyword radius (d4ec31262), and a clip-path with a geometry box was skipped (bd39a9ce9).
  • Fixed: overflow-clip-margin now widens paint containment and no single-axis clip (2b383754a), and containment on the body or root element keeps the body's overflow its own (5bde8e6a7). Both checked against hit testing in Electron 45.
  • Fixed: the reference names the clip-path shapes it measures (905e53d66).
  • Dismissed: CodeQL's code-sanitization alert on a test that writes a constant page into a fixture app's source with JSON.stringify, the same pattern as the other tests in launch.test.ts.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Viewport visibility checks now account for clipping, containment, transforms, fixed-position elements, and content rendered through slots or shadow trees.
    • Electron window checks treat the window as the viewport and can read computed styles from the document’s root element.
  • Documentation
    • Updated Electron capability details to describe viewport clipping, supported clip-path behavior, and window locator behavior.