Skip to content

Layout Diff

github-actions[bot] edited this page Nov 27, 2026 · 4 revisions

Layout diff

Role. Compare two engines' geometry trees and report each difference at the element where it starts, not at every element it pushes down the page. A pure function: captures in, causes out, no I/O. Module layout-diff, package internal/layoutdiff. It is run per page pair by pipeline.Run.

Inputs and outputs

  • In: the baseline's two runs and the engine's two runs, as trees (Prep turns a capture into a Tree of Nodes with uids, parents and derived values); optionally a third engine family's captures, for design intent.
  • Out: an Analysis: the delta of every matched element, the causes (Cause: the side, the node and its partner, kinds, the delta), the symptoms (overflows another element explains), and counts of explained and masked changes. Plus intent violations and warnings.

How it works

1. Pair the runs

Each engine has two runs. PairRuns compares the runs whose page states match best (a slideshow may be on a different slide in each engine): cost |ΔdocH| + 10·|Δnodes|, ties keep run a. The other run of each engine is kept for the mask.

2. Mask dynamic content

DynamicMask runs the whole analysis between the two runs of the same engine. Whatever changed there (ads, carousels, clocks) changes by itself, so it is masked with its subtree: left out of the causes and shaded in the viewer.

3. Match

MatchTrees pairs every baseline element with one engine element, sibling list by sibling list:

  1. an LCS over identity tokens (tag, id, test id, role, classes, name, href/src key) anchors the pairs;
  2. the leftovers between anchors are paired in order by tag + class, then by tag alone (which rescues dynamic ids and changed text);
  3. huge sibling lists fall back to a greedy in-order match with a 50-element lookahead (the dynamic-programming table would be 4M cells).

Unmatched elements are recorded only at the top: a missing subtree is one only-in-<engine> element, not one per descendant.

4. Measure deltas

Deltas compares each matched pair, with the tolerances in match.go:

Constant Value Meaning
TolAbs 2 px a size change below this is noise
TolRel 1% a size change must also exceed this fraction of the larger size to be reported (an absolute change still explains effects)
TolPos 2 px a position change relative to the parent
TolOvf 2 px content beyond the box before it counts as overflowing
TolOvfDelta 4 px an overflow must appear or disappear and change by more than this

Inline boxes: the height is ignored (the same font's content area differs by engine) and tops tolerate 20% of the box's height. Also measured: line count, a visibility flip, a truncation flip, and a text shift (the text inset changes by 3 px or more and by more than the text-width change + 1 px, with the same line count and the box width unchanged).

5. Causes versus effects

Analyze decides which changed elements are causes. The rules, from the spike's lib/align.mjs:

  • a child whose parent changed width is reflowing: an effect (width direction is tracked: it flows down from a resized parent, or up from content);
  • a box that grew because a child grew, wrapped or moved: an effect;
  • an item that moved because a sibling resized, or an earlier sibling moved the same way: an effect;
  • children that all moved with none resized: one children-moved cause on the parent (padding, gap, order, justify);
  • in flex and grid rows, a box stretched like its siblings: an effect;
  • in inline text flow, only the first changed box is a cause;
  • an overflow explained by a descendant's width or overflow: a symptom, not a cause;
  • an element present in one engine only: a cause. When the extras are on both sides, or more than 3 under one parent, the content itself differs: one children-differ cause on the parent, not N;
  • an element that takes no space, or is transparent and out of flow, is inert. A transparent element that still takes space is not: it pushes its siblings.

The cause kinds: only-in-<engine>, hidden-in-<engine>, truncated-in-<engine>, children-differ, children-moved, wrap, width, height, position, overflow-x, overflow-y, text-shifted, columns, and intent-*. (appearance is added by the Appearance diff.)

6. Table columns

TableColumns: when two or more cells of one table change width, the table keeps its width (within TolAbs), and the largest cell width change exceeds max(8, 3 × largest text-width change + 2) px, one columns cause on the table replaces the cell causes (a table-layout-type difference). Auto-layout columns that follow their widest text are left alone.

7. Design intent

Needs a third engine family. IntentViolations infers relations between two elements from the renders: shared edges and centres, equal width or height of repeated components (equal height only side by side), equal gaps between consecutive siblings, corresponding parts of repeated components (grid columns for stacked rows, shared lines for side-by-side cards), text columns, and no overlap. Constants in intent.go:

  • a relation is intended when at least two families keep it within 1 px;
  • a violation is one engine breaking it by 2 px or more (8% or more for sizes), and at most 800 px (more is a state or matching artifact);
  • pages with a match rate under 95% are skipped, with a warning;
  • at most the first 60 children of a parent are checked; in groups of more than 24, each child is compared with its next 6 siblings only.

Blame goes to the element that moved relative to its parent, against an engine that keeps the relation. Chrome can be the violator. The pipeline joins violations to the cause on the same element (or its child); several violating children of a children-moved parent merge into it; otherwise a violation becomes its own intent-align, intent-size, intent-gap, intent-column or intent-overlap cause (internal/pipeline/intent.go).

8. Warnings

WarningsOf adds, per comparison: the second run is missing (dynamic content not masked); the layout viewport differs (widths will differ everywhere); the node cap was hit; the page changed during capture; the engines rendered different page states versus the page changes over time (same-engine runs differ); web fonts failed differently; one side blocked requests the other loaded.

9. Selectors

Every cause, instance, intent violation and warning names its element with Selector, which calls the shared builder in internal/selector (the DOM audit calls it too). Up to five segments joined with >, from the nearest ancestor with an id (or body, or html) down to the element; a segment is the tag with #id, or with up to two .classes, and :nth-of-type(n) when the parent has other children of that tag.

  • n is the capture's nt: the DOM position among same-tag siblings, counting the display: none ones the capture leaves out (Data contracts). So an element one engine hides does not shift its later siblings, and both sides name the same element alike.
  • Without nt (captures from before 0.2.0, and the goldens) it counts the captured same-tag siblings, as the spike did. In a new capture a missing nt means the tag is unique among the DOM children, so the fallback adds no position either.
  • Tags, ids and classes are escaped as CSS.escape() does (Ident), so every selector is valid CSS. Anchored, document.querySelector returns the element; cut at five segments, the element is among its matches.
  • An element inside an open shadow root (below a node the capture marks sr) is named by the shadow host of its outermost shadow tree: document.querySelector cannot reach into a shadow tree, and the host is the nearest element it can select. Captures from before sr name it as if it were a light child.

Key files

File Holds
match.go tolerances, Node, Tree, Prep, Selector, MatchTrees
selector.go (package selector) the selector builder shared with the DOM audit: Path, Segment, Ident
deltas.go Delta, Deltas
analyze.go the cause and effect rules
tables.go columns
intent.go design intent
pairing.go, masks.go, warnings.go runs, masks, warnings
severity.go, lineinfo.go, pitch.go helpers triage uses: severity weight, line count and pitch

Tests

  • Unit tests on hand-built trees (align_test.go): anchors and leftovers, deltas, width flowing down, the moved-kids rule, only-in and children-differ, table columns, the dynamic mask, run pairing, warnings.
  • Selectors (selector_test.go): the hidden first card, one rendered p of two, an element hidden in one engine only (both sides get one selector), a capture without nt, a unique tag. internal/selector checks Ident against CSS.escape() values recorded from Chrome, the cut and anchor rules, and the shadow-host rule (an element inside a shadow root, nested roots, a host's light child).
  • Golden parity (TestGoldenAlignParity): on the spike's captures in testdata/golden/planted, the Go port finds the same causes as the spike. TestGoldenRealWarnings and TestGoldenRealPairingMasks run on local real-site goldens when present (they are third-party content and not committed).

Where the code differs from the spec

Nothing found: the tolerances, rules, kinds, table rule, intent thresholds and warnings match SPEC-layout-diff.md. The spec's open questions stand: an element moved to another parent shows as removed plus added, and iframes are not traversed.

Spec

specs/SPEC-layout-diff.md.

Source files

Clone this wiki locally