diff --git a/docs/output.md b/docs/output.md index eaa319a..3d6e629 100644 --- a/docs/output.md +++ b/docs/output.md @@ -161,14 +161,20 @@ computed: 2 findings; dom identical on 2 of 3 route(s), differs /pricing (1), so doesn't misalign everything after it. - An inserted sibling of the same tag and classes still shifts the `:nth-of-type` of the ones after it. - - A class renamed on an ancestor unmatches its whole subtree. + - A class added to, removed from or renamed on a wrapper re-keys its whole subtree. A + style change under it then shows only as `dom differs` and `unmatched`, not as a + finding. - One change high in the tree is one finding. An inherited property (`font-*`, `line-height`, `letter-spacing`, `text-*`, `white-space`, `color`, `visibility`) is dropped where the parent has the same change, so a `body` font change is one line, not one per element. A `width` is dropped inside a parent whose width changed, and a `height` around a child whose height changed, so the box that caused it is what's left. + A box whose own width or height is set in px on either side is kept (so a child widened + inside an `inline-block` is, at the cost of the odd fixed-size child a parent squeezed), + and a `height: 100%` child is dropped instead of the px-height parent it follows. - An element shown on one side and `display: none` on the other is one `display` finding; - its subtree counts neither as unmatched nor against `dom identical`. + its subtree and its `::before`/`::after` count neither as findings, unmatched nor against + `dom identical`. - A `::before`/`::after` that only one side generates shows as `content` against `none`. That is how a removed rule that drew an icon shows up. - `dom identical` means that on every width, each walked element's tag, attributes and own @@ -180,7 +186,8 @@ computed: 2 findings; dom identical on 2 of 3 route(s), differs /pricing (1), so - Hash-shaped tokens only: 8 or more letters and digits that alternate, between separators in a file name. `team_photo_2024.jpg` and `/p/abcdef123456` count as changes. - `dom differs (N)` counts the elements that differ or that one side lacks. Treat that - route's findings as hints. + route's findings as hints. A re-keyed element counts once on each side, so one renamed + wrapper with three descendants is `dom differs (8)`. - `unmatched` lists the outermost elements only one side has: `+` left, `-` right. The summary shows five routes, five each. - The HTML report shows the same block and puts `dom identical` / `dom differs (N)` on each diff --git a/src/computed.ts b/src/computed.ts index 62a6170..abe9cc0 100644 --- a/src/computed.ts +++ b/src/computed.ts @@ -4,8 +4,11 @@ import type { ComparedResult, RouteResult } from "./report.ts"; /** `--computed`: every rendered element's computed style on both sides, diffed by a stable key. */ -/** Selector path -> its opening tag with own text, and `PROPS` then `content`, computed. */ -export type Snapshot = Record; +/** + * Selector path -> its opening tag with own text, `PROPS` then `content` computed, and its + * specified width and height (`auto`, `100%`, `200px`) where the browser says. + */ +export type Snapshot = Record; export interface StyleChange { /** Selector path from `body`, e.g. `body > div.page > nav.chips > a[href="#blog"]`; `::before` on a pseudo-element. */ @@ -74,19 +77,37 @@ export const normalizeAssets = (html: string) => return ` ${n}="${u}"`; }); +/** A page's throw is its text after the first `Error: `, without the stack page.evaluate appends. */ +export const pageError = (e: unknown) => { + const m = String(e instanceof Error ? e.message : e); + const i = m.indexOf("Error: "); + return i < 0 ? m : m.slice(i + 7).split("\n")[0]!; +}; + /** * Drops what a change elsewhere explains: an inherited property's change the parent has too, * a width inside a parent whose width changed, a height around a child whose height changed. + * With the snapshots, a box whose own size is set in px on either side is kept, and a + * percentage height inside a px-height parent whose height changed is the one that follows. */ -export function collapseInherited(changes: StyleChange[]): StyleChange[] { +export function collapseInherited(changes: StyleChange[], snaps: Snapshot[] = []): StyleChange[] { const raw = new Set(changes.map((c) => `${c.key}\0${c.prop}\0${c.values.join("\0")}`)); const wide = new Set(changes.filter((c) => c.prop === "width").map((c) => c.key)); - const tallKid = new Set(changes.filter((c) => c.prop === "height").map((c) => parentOf(c.key))); + const tall = new Set(changes.filter((c) => c.prop === "height").map((c) => c.key)); + const spec = (key: string, i: 0 | 1) => snaps.map((s) => s[key]?.size?.[i] ?? "auto"); + const set = (key: string, i: 0 | 1) => spec(key, i).some((v) => v.endsWith("px")); + // A percentage of an auto height is auto, so only a px-height parent sizes it. + const follows = (key: string) => { + const vs = spec(key, 1); + const p = parentOf(key); + return tall.has(p) && set(p, 1) && vs.length > 0 && vs.every((v) => v.includes("%")); + }; + const tallKid = new Set([...tall].filter((k) => !follows(k)).map(parentOf)); return changes.filter((c) => { const p = parentOf(c.key); if (INHERITED.has(c.prop)) return !raw.has(`${p}\0${c.prop}\0${c.values.join("\0")}`); - if (c.prop === "width") return !wide.has(p); - if (c.prop === "height") return !tallKid.has(c.key); + if (c.prop === "width") return !wide.has(p) || set(c.key, 0); + if (c.prop === "height") return (!tallKid.has(c.key) || set(c.key, 1)) && !follows(c.key); return true; }); } @@ -125,7 +146,10 @@ export function diffSnapshots(left: Snapshot, right: Snapshot): ComputedDiff { if (l.styles[i] !== r.styles[i]) changes.push({ key, prop, values: [l.styles[i]!, r.styles[i]!] }); }); - } else if (isPseudo(key) && left[parentOf(key)] && right[parentOf(key)]) { + } else if ( + isPseudo(key) && + [left, right].every((s) => s[parentOf(key)] && s[parentOf(key)]!.styles[DISPLAY] !== "none") + ) { // A pseudo-element one side no longer generates: the removed rule's own finding. changes.push({ key, @@ -140,7 +164,7 @@ export function diffSnapshots(left: Snapshot, right: Snapshot): ComputedDiff { const set = new Set(ks); return ks.filter((k) => !set.has(parentOf(k))).slice(0, MAX_UNMATCHED); }; - const kept = collapseInherited(changes); + const kept = collapseInherited(changes, [left, right]); return { dom, changes: kept.slice(0, MAX_CHANGES), diff --git a/src/locate.ts b/src/locate.ts index 5c89c66..2723ac2 100644 --- a/src/locate.ts +++ b/src/locate.ts @@ -252,7 +252,7 @@ function probe({ boxes, find, masked, props, perSpot, walk, root }: ProbeArgs) { a.cancel(); } } - const snap: Record = {}; + const snap: Record = {}; const SKIP = new Set(["script", "style", "link", "meta", "template", "noscript"]); let n = 0; const end = performance.now() + walk; @@ -279,7 +279,13 @@ function probe({ boxes, find, masked, props, perSpot, walk, root }: ProbeArgs) { if (++n > MAX_SCAN) throw new Error(`more than ${MAX_SCAN} elements`); if (performance.now() > end) throw new Error(`took over ${walk}ms`); const cs = getComputedStyle(e); - snap[key] = { html: html(e), styles: props.map((p) => cs.getPropertyValue(p)) }; + // Specified, not used: `auto`, `100%` or `200px` says which box sizes which. + const m = e.computedStyleMap?.(); + snap[key] = { + html: html(e), + styles: props.map((p) => cs.getPropertyValue(p)), + ...(m ? { size: [String(m.get("width")), String(m.get("height"))] } : {}), + }; if (cs.display === "none") return; for (const pseudo of ["::before", "::after"]) { const ps = generated(e, pseudo); diff --git a/src/run.ts b/src/run.ts index e9e6e77..7e1082c 100644 --- a/src/run.ts +++ b/src/run.ts @@ -8,7 +8,7 @@ import { applyAccepted, readAccepted, updateAccepted } from "./accept.ts"; import { CACHE_DIR, cacheFile, replayable, REPLAY_TTL, saveRecording, shotId } from "./cache.ts"; import { cancelUnstable, capture, HookError, prepare, type Shot } from "./capture.ts"; import { compare, crops, type Band, type Region } from "./compare.ts"; -import { diffSnapshots, fmtComputed } from "./computed.ts"; +import { diffSnapshots, fmtComputed, pageError } from "./computed.ts"; import { locate, snapshot, type ElementDiff } from "./locate.ts"; import { CONTEXT_OPTIONS, @@ -349,10 +349,7 @@ export async function run({ COMPUTED_TIMEOUT, route.selector ? page.locator(route.selector[side]) : null, ).catch((e: unknown) => { - // page.evaluate prefixes the page's own error: keep only its message. - throw new Error( - String(e instanceof Error ? e.message : e).replace(/^[\s\S]*Error: /, ""), - ); + throw new Error(pageError(e)); }); const computed = cfg.computed ? await Promise.race([ diff --git a/tests/unit/computed.test.ts b/tests/unit/computed.test.ts index 2aacb83..491d794 100644 --- a/tests/unit/computed.test.ts +++ b/tests/unit/computed.test.ts @@ -10,6 +10,7 @@ import { fmtComputed, groupFindings, normalizeAssets, + pageError, type Snapshot, } from "../../src/computed.ts"; import { PROPS } from "../../src/locate.ts"; @@ -72,6 +73,57 @@ describe("diffSnapshots", () => { ); }); + test("a child whose own width changed inside a shrink-to-fit parent is kept", () => { + // `.inner { width: 100px → 200px }` inside an inline-block span that follows it. + const snap = (w: string): Snapshot => ({ + "body > span": el("", { display: "inline-block", width: w }), + "body > span > div.inner": { ...el("
", { width: w }), size: [w, "auto"] }, + }); + const d = diffSnapshots(snap("100px"), snap("200px")); + assert.deepEqual( + d.changes.map((c) => c.key), + ["body > span", "body > span > div.inner"], + ); + }); + + test("a parent's own height change is kept over a height: 100% child that follows it", () => { + const snap = (h: string): Snapshot => ({ + "body > div.box": { ...el("
", { height: h }), size: ["auto", h] }, + "body > div.box > div.fill": { ...el("
", { height: h }), size: ["auto", "100%"] }, + }); + const d = diffSnapshots(snap("50px"), snap("80px")); + assert.deepEqual( + d.changes.map((c) => c.key), + ["body > div.box"], + ); + }); + + test("a height: 100% child in an auto-height parent sizes it, so the child is kept", () => { + const snap = (h: string): Snapshot => ({ + "body > div.card": { ...el("
", { height: h }), size: ["auto", "auto"] }, + "body > div.card > div.inner": { ...el("
", { height: h }), size: ["auto", "100%"] }, + }); + assert.deepEqual( + diffSnapshots(snap("50px"), snap("80px")).changes.map((c) => c.key), + ["body > div.card > div.inner"], + ); + }); + + test("an element going display: none is one finding, not its pseudo-elements too", () => { + const left: Snapshot = { + body: el(""), + "body > p.tip": el("

Tip", { display: "none" }), + }; + const right: Snapshot = { + body: el(""), + "body > p.tip": el("

Tip", { display: "block" }), + "body > p.tip::before": el("", { content: '"→"' }), + }; + assert.deepEqual(diffSnapshots(left, right).changes, [ + { key: "body > p.tip", prop: "display", values: ["none", "block"] }, + ]); + }); + test("an element one side hides is a display change, not a markup difference", () => { const left: Snapshot = { body: el(""), @@ -229,3 +281,16 @@ describe("groupFindings", () => { assert.deepEqual(fmtComputed([shot("/a", 1440, {})], ["/a"]), []); }); }); + +test("a snapshot error keeps the page's message, not page.evaluate's prefix or stack", () => { + // As Playwright reported a throw inside the walk, message text included. + const raw = + "page.evaluate: Error: more than 3 elements: Error: nested\n" + + " at visit (eval at evaluate (:311:30), :1:66)\n" + + " at probe (eval at evaluate (:311:30), :1:133)\n" + + " at UtilityScript.evaluate (:313:16)"; + assert.equal(pageError(new Error(raw)), "more than 3 elements: Error: nested"); + const timeout = + "locator.evaluate: Timeout 30000ms exceeded.\nCall log:\n - waiting for locator('.x')"; + assert.equal(pageError(new Error(timeout)), timeout); +});