diff --git a/docs/output.md b/docs/output.md index f1e4ed6..baab7af 100644 --- a/docs/output.md +++ b/docs/output.md @@ -203,6 +203,9 @@ computed: 2 findings; dom identical on 2 of 3 route(s), differs /pricing (1), so - An element shown on one side and `display: none` on the other is one `display` finding; its subtree and its `::before`/`::after` count neither as findings, unmatched nor against `dom identical`. +- An `auto` margin reads as `auto`, not the px Chrome resolved it to, since that number + changes with the layout (a grid item's reads `0px`). Centring swapped for a fixed margin + is `margin-left auto→60px`. - 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 diff --git a/skills/renderdiff/reference.md b/skills/renderdiff/reference.md index f70bd7a..801bfee 100644 --- a/skills/renderdiff/reference.md +++ b/skills/renderdiff/reference.md @@ -162,7 +162,8 @@ computed: 2 findings; dom identical on 2 of 3 route(s), differs /pricing (1), so dropped, and so is a width inside a parent whose width changed, or a height around a child whose height changed (unless set in px). Shown on one side and `display: none` on the other is one `display` finding. A pseudo-element one side no longer generates is `content` against - `none`. + `none`. An `auto` margin reads as `auto`, not the px it resolved to, which varies with + layout. - A finding on a clean route is a change you can't see in this state: a hidden element (closed menu), another breakpoint, `:hover`/`:focus` (not snapshotted). Decide it; a `before` hook can open the state. diff --git a/src/locate.ts b/src/locate.ts index 2723ac2..af8fe74 100644 --- a/src/locate.ts +++ b/src/locate.ts @@ -183,6 +183,12 @@ function probe({ boxes, find, masked, props, perSpot, walk, root }: ProbeArgs) { return path; }; + // A resolved auto margin's px depends on the layout path: a grid item's reads 0px. + const styles = (e: Element, cs: CSSStyleDeclaration, m = e.computedStyleMap?.()) => + props.map((p) => + p.startsWith("margin-") && String(m?.get(p)) === "auto" ? "auto" : cs.getPropertyValue(p), + ); + const seen = (e: Element) => { const r = e.getBoundingClientRect(); const cs = getComputedStyle(e); @@ -196,7 +202,7 @@ function probe({ boxes, find, masked, props, perSpot, walk, root }: ProbeArgs) { w: Math.round(r.width), h: Math.round(r.height), }, - styles: props.map((p) => cs.getPropertyValue(p)), + styles: styles(e, cs), up: (() => { const tags: string[] = []; for (let n = e.parentElement; n && tags.length < 2; n = n.parentElement) @@ -283,7 +289,7 @@ function probe({ boxes, find, masked, props, perSpot, walk, root }: ProbeArgs) { const m = e.computedStyleMap?.(); snap[key] = { html: html(e), - styles: props.map((p) => cs.getPropertyValue(p)), + styles: styles(e, cs, m), ...(m ? { size: [String(m.get("width")), String(m.get("height"))] } : {}), }; if (cs.display === "none") return; diff --git a/tests/visual/computed.test.ts b/tests/visual/computed.test.ts index 91fb912..86305a0 100644 --- a/tests/visual/computed.test.ts +++ b/tests/visual/computed.test.ts @@ -7,7 +7,7 @@ import assert from "node:assert/strict"; import { after, before, test } from "node:test"; import { chromium, type Browser, type Page } from "playwright"; import { diffSnapshots } from "../../src/computed.ts"; -import { locate, snapshot } from "../../src/locate.ts"; +import { locate, PROPS, snapshot } from "../../src/locate.ts"; import type { Shot } from "../../src/capture.ts"; import type { Region } from "../../src/compare.ts"; @@ -88,6 +88,22 @@ test("a removed hiding rule is a display change, and the dom stays identical", a ); }); +test("an auto margin reads as auto, whatever px the layout resolves it to", async () => { + const body = `
x
`; + const css = "body { margin: 0 } .c { width: 100px; margin: 0 auto }"; + const margins = (d: Awaited>["d"]) => + d.changes.filter((c) => c.prop.startsWith("margin-")).map((c) => [c.key, c.prop, c.values]); + // A grid item's auto margins resolve to 0px, a block's to the centring px. + const centred = await diff(body, body, [css, `${css} body { display: grid }`]); + assert.deepEqual(margins(centred.d), []); + assert.equal(centred.snaps[0]["body > div.c"]!.styles[PROPS.indexOf("margin-left")], "auto"); + const fixed = await diff(body, body, [css, `${css} .c { margin: 0 60px }`]); + assert.deepEqual(margins(fixed.d), [ + ["body > div.c", "margin-right", ["auto", "60px"]], + ["body > div.c", "margin-left", ["auto", "60px"]], + ]); +}); + test("with a selector the walk covers only that subtree, keyed from :scope", async () => { const p = await page(`
Home
x
`); const snap = await snapshot(p, [], BUDGET, p.locator(".card"));