diff --git a/docs/how-it-works.md b/docs/how-it-works.md index 25ffa32..49b02b2 100644 --- a/docs/how-it-works.md +++ b/docs/how-it-works.md @@ -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 diff --git a/src/capture.ts b/src/capture.ts index 9e14bc9..b222239 100644 --- a/src/capture.ts +++ b/src/capture.ts @@ -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; + 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); + }; + } + } }); } diff --git a/src/config.ts b/src/config.ts index fca9645..7aaff94 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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) => { diff --git a/tests/fixtures/pages/README.md b/tests/fixtures/pages/README.md index d89501a..2e12405 100644 --- a/tests/fixtures/pages/README.md +++ b/tests/fixtures/pages/README.md @@ -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 @@ -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 diff --git a/tests/fixtures/pages/smooth-scroll/left.html b/tests/fixtures/pages/smooth-scroll/left.html new file mode 100644 index 0000000..09d7e92 --- /dev/null +++ b/tests/fixtures/pages/smooth-scroll/left.html @@ -0,0 +1,35 @@ + + + + +Smooth scroll + + + + +
+

Smooth scroll

+
+

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.

+
+

The rows are numbered, so a scroller stopped at another offset shows other numbers.

+
+
+
+ + + diff --git a/tests/fixtures/pages/smooth-scroll/right.html b/tests/fixtures/pages/smooth-scroll/right.html new file mode 100644 index 0000000..0e3dff0 --- /dev/null +++ b/tests/fixtures/pages/smooth-scroll/right.html @@ -0,0 +1,35 @@ + + + + +Smooth scroll + + + + +
+

Smooth scroll

+
+

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.

+
+

The rows are numbered, so a scroller stopped at another offset shows other numbers.

+
+
+
+ + + diff --git a/tests/helpers/corpus.ts b/tests/helpers/corpus.ts index afa25ba..f822c5a 100644 --- a/tests/helpers/corpus.ts +++ b/tests/helpers/corpus.ts @@ -67,6 +67,7 @@ export const GUARDRAILS = [ "bistable", "offcanvas", "appshell", + "smooth-scroll", ] as const; export const ALL: string[] = [...EQUIVALENCE, ...CONTROLS, ...GUARDRAILS]; diff --git a/tests/visual/guardrails.test.ts b/tests/visual/guardrails.test.ts index 31904bf..7c66121 100644 --- a/tests/visual/guardrails.test.ts +++ b/tests/visual/guardrails.test.ts @@ -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"); } }] }',