Repository navigation
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.
-
In: the baseline's two runs and the engine's two runs, as trees (
Prepturns a capture into aTreeofNodes 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.
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.
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.
MatchTrees pairs every baseline element with one engine element, sibling list by sibling list:
- an LCS over identity tokens (tag, id, test id, role, classes, name,
href/srckey) anchors the pairs; - the leftovers between anchors are paired in order by tag + class, then by tag alone (which rescues dynamic ids and changed text);
- 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.
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).
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-movedcause 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-differcause 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.)
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.
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).
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.
| File | Holds |
|---|---|
match.go |
tolerances, Node, Tree, Prep, selectors, MatchTrees
|
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 |
- Unit tests on hand-built trees (
align_test.go): anchors and leftovers, deltas, width flowing down, the moved-kids rule, only-in andchildren-differ, table columns, the dynamic mask, run pairing, warnings. -
Golden parity (
TestGoldenAlignParity): on the spike's captures intestdata/golden/planted, the Go port finds the same causes as the spike.TestGoldenRealWarningsandTestGoldenRealPairingMasksrun on local real-site goldens when present (they are third-party content and not committed).
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.
-
internal/layoutdiff/match.go: the tree,Node, and matching -
internal/layoutdiff/analyze.go:Analyze: causes and symptoms -
internal/layoutdiff/deltas.go: per-element deltas -
internal/layoutdiff/pairing.go:PairRuns -
internal/layoutdiff/intent.go: intent checks against a third engine -
internal/layoutdiff/lineinfo.go: line information -
internal/layoutdiff/pitch.go: line pitch -
internal/layoutdiff/masks.go:DynamicMask: what changes between two runs of the same engine -
internal/layoutdiff/tables.go: table columns -
internal/layoutdiff/severity.go: severity weights -
internal/layoutdiff/warnings.go: pair warnings
This wiki is published from docs/internals/ on main. Edits made here are overwritten on the next publish: change the pages there, in a pull request. The specs are normative; where a page and a spec disagree, the page says what the code does.
Start
Contracts
Capture side
Analysis side
Output
Quality