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: 2 additions & 1 deletion docs/how-it-works.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ honestly. This is what renderdiff does to make sure of that.
seeded, timezone (UTC), locale (en-US), colour scheme (light) and
`prefers-reduced-motion` are fixed, and videos are kept from playing and, once their metadata loads, rewound to frame 0 (one that never loads shows its poster) with video.js captions and control bars hidden (`--no-freeze-video` opts out); with `lottieFrame`, every `lottie-player` is stopped on that frame once it loads (waited for up to 2s). A clock that shows
seconds can still tick between the two sides and may need a mask.
- **Kills animation, transitions and caret blink**, and hides common consent banners and
- **Kills animation, transitions, caret blink and smooth scrolling** (CSS `scroll-behavior` and
`behavior: "smooth"` in script scrolls, from before any hook), and hides common consent banners and
dev toolbars. Animated images (GIF, WebP, APNG) and SVG animations show their first frame;
it is a Blink setting, so cross-origin and CSS background images freeze too.
- **Settles the page** — makes native lazy images eager, scrolls every viewport-height
Expand Down
18 changes: 18 additions & 0 deletions src/capture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,24 @@ export async function prepare(page: Page, cfg: ResolvedConfig, now: number): Pro
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
// Before any hook, unlike neutralizeCss, so a scroll a `before` hook starts is instant too.
const sheet = new CSSStyleSheet();
sheet.replaceSync("*,*::before,*::after{scroll-behavior:auto!important}");
document.adoptedStyleSheets = [...document.adoptedStyleSheets, sheet];
type Scroll = (this: unknown, o?: ScrollToOptions, ...r: unknown[]) => void;
// window itself, not Window.prototype: a global's methods live on the global.
for (const [target, names] of [
[window, ["scroll", "scrollTo", "scrollBy"]],
[Element.prototype, ["scroll", "scrollTo", "scrollBy", "scrollIntoView"]],
] as const) {
const t = target as unknown as Record<string, Scroll>;
for (const m of names) {
const f = t[m]!;
t[m] = function (o, ...r) {
return f.call(this, o?.behavior === "smooth" ? { ...o, behavior: "instant" } : o, ...r);
};
}
}
});
}

Expand Down
3 changes: 1 addition & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,7 @@ export const DEFAULT_HIDE_SELECTORS = [

/** Kills motion so two captures taken seconds apart agree. Always applied. */
const STILL_CSS = `
*,*::before,*::after{animation:none!important;transition:none!important;caret-color:transparent!important}
html{scroll-behavior:auto!important}
*,*::before,*::after{animation:none!important;transition:none!important;caret-color:transparent!important;scroll-behavior:auto!important}
`;

export const alive = async (origin: string) => {
Expand Down
7 changes: 4 additions & 3 deletions tests/fixtures/pages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Three groups:
elements each change names (`margin`, `color`, `nav`), and a lost `::before` line and a
re-parented pairing (`divider`).
- **Guard rails** — `blank`, `tall`, `animated`, `lazy`, `lazy-deep`, `lazy-js`, `lazy-grow`, `consent`,
`drifting`, `tracker`, `clock`, `ticker`, `unsettled`, `unsettled-once`, `sprite`, `short`, `tall-region`, `reorder`, `stripe`, `frameset`, `menu`, `scroll-spy`, `signed-in`, `fixed`, `inserted`, `labelled`, `broken-asset`, `fonts`, `media`, `long`, `video`, `video-seek`, `lottie`, `hang`, `origin`, `cooperate`, `mask-geometry`, `bistable`, `offcanvas`, `appshell`.
`drifting`, `tracker`, `clock`, `ticker`, `unsettled`, `unsettled-once`, `sprite`, `short`, `tall-region`, `reorder`, `stripe`, `frameset`, `menu`, `scroll-spy`, `signed-in`, `fixed`, `inserted`, `labelled`, `broken-asset`, `fonts`, `media`, `long`, `video`, `video-seek`, `lottie`, `hang`, `origin`, `cooperate`, `mask-geometry`, `bistable`, `offcanvas`, `appshell`, `smooth-scroll`.
One per defence in `src/capture.ts` / `src/config.ts`.

## Authoring rules
Expand Down Expand Up @@ -65,8 +65,9 @@ Breaking any of these produces a flaky suite, and the flake looks like a renderd
image-animation launch flag kill it, `ticker`, whose timer exists to be caught by the
stability check, `unsettled`, which recolours the whole page to trip the
unstable-pixel cap, `unsettled-once`, which does so on its first render only,
`mask-geometry`, whose box moves once after settle, and `lottie`, whose script-driven
player exists to be held on one frame.
`mask-geometry`, whose box moves once after settle, `lottie`, whose script-driven
player exists to be held on one frame, and `smooth-scroll`, whose left scroller scrolls
smoothly.

## Adding a route

Expand Down
35 changes: 35 additions & 0 deletions tests/fixtures/pages/smooth-scroll/left.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Smooth scroll</title>
<link rel="stylesheet" href="/shared/base.css">
<style>
/* GUARD RAIL for smooth scrolling. Only the left scroller scrolls smoothly, so the pair
differs only if a scroll a hook starts is still moving at the shot. */
#rows { width: var(--content); height: 320px; overflow: auto; scroll-behavior: smooth; }
.row { height: 40px; border-bottom: 1px solid var(--line); }
.tail { height: 2400px; }
</style>
</head>
<body>
<div class="page">
<h1>Smooth scroll</h1>
<div id="rows"></div>
<p class="lede">A page with an inner scroller and a tall tail. A hook scrolls the scroller or the window; the shot must catch neither mid-way.</p>
<section class="notes">
<p>The rows are numbered, so a scroller stopped at another offset shows other numbers.</p>
</section>
<div class="tail"></div>
</div>
<script>
const rows = document.getElementById("rows");
for (let i = 1; i <= 100; i++) {
const row = document.createElement("div");
row.className = "row";
row.textContent = "Row " + i;
rows.append(row);
}
</script>
</body>
</html>
35 changes: 35 additions & 0 deletions tests/fixtures/pages/smooth-scroll/right.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Smooth scroll</title>
<link rel="stylesheet" href="/shared/base.css">
<style>
/* GUARD RAIL for smooth scrolling. Only the left scroller scrolls smoothly, so the pair
differs only if a scroll a hook starts is still moving at the shot. */
#rows { width: var(--content); height: 320px; overflow: auto; }
.row { height: 40px; border-bottom: 1px solid var(--line); }
.tail { height: 2400px; }
</style>
</head>
<body>
<div class="page">
<h1>Smooth scroll</h1>
<div id="rows"></div>
<p class="lede">A page with an inner scroller and a tall tail. A hook scrolls the scroller or the window; the shot must catch neither mid-way.</p>
<section class="notes">
<p>The rows are numbered, so a scroller stopped at another offset shows other numbers.</p>
</section>
<div class="tail"></div>
</div>
<script>
const rows = document.getElementById("rows");
for (let i = 1; i <= 100; i++) {
const row = document.createElement("div");
row.className = "row";
row.textContent = "Row " + i;
rows.append(row);
}
</script>
</body>
</html>
1 change: 1 addition & 0 deletions tests/helpers/corpus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const GUARDRAILS = [
"bistable",
"offcanvas",
"appshell",
"smooth-scroll",
] as const;

export const ALL: string[] = [...EQUIVALENCE, ...CONTROLS, ...GUARDRAILS];
Expand Down
34 changes: 34 additions & 0 deletions tests/visual/guardrails.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,40 @@ describe("before hooks", () => {
assert.equal(r.hook, true);
});

test("a smooth scroller a before hook scrolls has stopped by the shot", async () => {
const run = await hooked(
'{ before: async (page) => { await page.evaluate(() => { document.getElementById("rows").scrollTop = 2000; }); } }',
["/smooth-scroll"],
);
assert.equal(run.code, 0, context(run));
const r = result(results(run.outDir), "/smooth-scroll", 1440);
assert.equal(r.diffPixels, 0, context(run));
assert.equal(r.unstablePixels, 0, context(run));
});

test("a smooth window scroll a beforeShot starts is instant", async () => {
// The hook writes where the window got to, so a scroll still moving shows as text.
const run = await hooked(
'{ beforeShot: async (page, { side }) => { await page.evaluate(async (behavior) => { window.scrollTo({ top: 2000, behavior }); await new Promise((r) => setTimeout(r, 50)); document.querySelector("h1").textContent = "At " + Math.round(scrollY); }, side === "left" ? "smooth" : "instant"); } }',
["/smooth-scroll"],
);
assert.equal(run.code, 0, context(run));
const r = result(results(run.outDir), "/smooth-scroll", 1440);
assert.equal(r.diffPixels, 0, context(run));
assert.equal(r.unstablePixels, 0, context(run));
});

test("a smooth scrollIntoView a beforeShot starts is instant", async () => {
const run = await hooked(
'{ beforeShot: async (page, { side }) => { await page.evaluate(async (behavior) => { document.querySelector(".tail").scrollIntoView({ behavior }); await new Promise((r) => setTimeout(r, 50)); document.querySelector("h1").textContent = "At " + Math.round(scrollY); }, side === "left" ? "smooth" : "instant"); } }',
["/smooth-scroll"],
);
assert.equal(run.code, 0, context(run));
const r = result(results(run.outDir), "/smooth-scroll", 1440);
assert.equal(r.diffPixels, 0, context(run));
assert.equal(r.unstablePixels, 0, context(run));
});

test("a route's beforeShot runs after the scroll pass on both sides, over the top-level one, and on the recheck", async () => {
const run = await hooked(
'{ beforeShot: async () => { throw new Error("top-level ran"); }, routes: [{ left: "/scroll-spy", beforeShot: async (page) => { await page.click("#soon"); } }] }',
Expand Down
Loading