Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/output.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion skills/renderdiff/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 8 additions & 2 deletions src/locate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
18 changes: 17 additions & 1 deletion tests/visual/computed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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 = `<div class="c">x</div>`;
const css = "body { margin: 0 } .c { width: 100px; margin: 0 auto }";
const margins = (d: Awaited<ReturnType<typeof diff>>["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(`<header><a href="/">Home</a></header><div class="card"><b>x</b></div>`);
const snap = await snapshot(p, [], BUDGET, p.locator(".card"));
Expand Down
Loading