From 13f78c48cd624c8d868e3dcb74dab5850558fd90 Mon Sep 17 00:00:00 2001 From: Sluijsens Date: Mon, 16 Nov 2026 14:44:30 +0100 Subject: [PATCH 1/6] Adds the hero contract: golden files for the picture and the preload links, mapping and seed Model B (wide 3:2 and tall 4:5 variants, focus point), agreed with the CMS session. Pure functions mapHero, heroHtml and heroPreloadHtml, golden files in web/contract/host-injection/hero with a README, and a seed with a first focus estimate per photo. No site change yet. Co-Authored-By: Claude Sonnet 5.5 --- web/.prettierignore | 1 + web/contract/host-injection/hero/README.md | 33 +++ .../hero/hero-hostile.expected-preload.html | 1 + .../hero/hero-hostile.expected.html | 1 + .../hero/hero-hostile.input.json | 58 ++++ .../hero/hero-minimal.expected-preload.html | 1 + .../hero/hero-minimal.expected.html | 1 + .../hero/hero-minimal.input.json | 17 ++ .../hero/hero-no-alt.expected-preload.html | 1 + .../hero/hero-no-alt.expected.html | 1 + .../hero/hero-no-alt.input.json | 13 + .../hero/hero-none.expected-preload.html | 1 + .../hero/hero-none.expected.html | 1 + .../host-injection/hero/hero-none.input.json | 1 + .../hero/hero-standard.expected-preload.html | 1 + .../hero/hero-standard.expected.html | 1 + .../hero/hero-standard.input.json | 76 ++++++ .../hero/hero-two-lines.expected-preload.html | 1 + .../hero/hero-two-lines.expected.html | 1 + .../hero/hero-two-lines.input.json | 23 ++ web/contract/host-injection/hero/seed.json | 73 ++++++ web/src/lib/hero.ts | 139 ++++++++++ web/src/lib/heroHtml.test.ts | 248 ++++++++++++++++++ web/src/lib/heroHtml.ts | 99 +++++++ 24 files changed, 793 insertions(+) create mode 100644 web/contract/host-injection/hero/README.md create mode 100644 web/contract/host-injection/hero/hero-hostile.expected-preload.html create mode 100644 web/contract/host-injection/hero/hero-hostile.expected.html create mode 100644 web/contract/host-injection/hero/hero-hostile.input.json create mode 100644 web/contract/host-injection/hero/hero-minimal.expected-preload.html create mode 100644 web/contract/host-injection/hero/hero-minimal.expected.html create mode 100644 web/contract/host-injection/hero/hero-minimal.input.json create mode 100644 web/contract/host-injection/hero/hero-no-alt.expected-preload.html create mode 100644 web/contract/host-injection/hero/hero-no-alt.expected.html create mode 100644 web/contract/host-injection/hero/hero-no-alt.input.json create mode 100644 web/contract/host-injection/hero/hero-none.expected-preload.html create mode 100644 web/contract/host-injection/hero/hero-none.expected.html create mode 100644 web/contract/host-injection/hero/hero-none.input.json create mode 100644 web/contract/host-injection/hero/hero-standard.expected-preload.html create mode 100644 web/contract/host-injection/hero/hero-standard.expected.html create mode 100644 web/contract/host-injection/hero/hero-standard.input.json create mode 100644 web/contract/host-injection/hero/hero-two-lines.expected-preload.html create mode 100644 web/contract/host-injection/hero/hero-two-lines.expected.html create mode 100644 web/contract/host-injection/hero/hero-two-lines.input.json create mode 100644 web/contract/host-injection/hero/seed.json create mode 100644 web/src/lib/hero.ts create mode 100644 web/src/lib/heroHtml.test.ts create mode 100644 web/src/lib/heroHtml.ts diff --git a/web/.prettierignore b/web/.prettierignore index 78aa15e..107652a 100644 --- a/web/.prettierignore +++ b/web/.prettierignore @@ -17,3 +17,4 @@ src/cms-client # Golden files of the host injection contract: exact text, never reformatted contract/host-injection/*.expected.html +contract/host-injection/hero/*.html diff --git a/web/contract/host-injection/hero/README.md b/web/contract/host-injection/hero/README.md new file mode 100644 index 0000000..43a989e --- /dev/null +++ b/web/contract/host-injection/hero/README.md @@ -0,0 +1,33 @@ +# Hero contract (ASF site and CMS host) + +Golden files for the hero regions that the CMS host fills, model B (decided 2026-11-16): the host cuts a `wide` (3:2) and a `tall` (4:5) variant per photo around the focus point, and the site uses ``. Same idea as the packages (`../README.md`): for every `NAME.input.json` the host's template must give exactly `NAME.expected.html` (between `` and ``) and `NAME.expected-preload.html` (between `` and `` in the head). The file ends with one newline that is not part of the HTML. The ASF repo tests its own implementation (`src/lib/hero.ts`, `src/lib/heroHtml.ts`) against the same files; to change the contract, change the code and run `UPDATE_GOLDEN=1 pnpm test src/lib/heroHtml.test.ts`, then send the new files to the CMS session. The ASF commit is in `git log -1 -- web/contract/host-injection/hero`. + +## Input + +The answer of `GET /api/v1/hero` (an object). A 204 (no hero) is `null`: the output is empty and the baked hero stays. + +## Rules + +1. Usable hero: an object with a non-empty `title` (trimmed) and at least one usable slide; otherwise both outputs are empty strings (fail open: the baked page stays). +2. A slide is kept when: `id` is non-empty; `image.src` is a path of our host (starts with one `/`, not `//`, no backslash); `image.width` and `image.height` are whole numbers above 0; and it has an `alt` (non-empty after trimming) or `decorative` is `true`. Other slides are skipped. Order is the given order. Only the FIRST kept slide is written; the client takes the rest from the data block. +3. A decorative slide writes `alt=""`. +4. Variants: kept when `ratio`, `type` and `url` are non-empty, `width` is a whole number above 0 and `url` is a path of our host. Only `image/webp` variants are used here (a preload cannot say "AVIF if supported", so a second format would be fetched twice). `wide` variants make the `img` `srcset`, `tall` variants the `source` `srcset`; sorted by width ascending, written `URL WIDTHw`, joined by `, `. +5. `focus` (`x` and `y` both between 0 and 1) is written as `style="object-position:X% Y%"` on the `img` (percentages without trailing zeros, at most two decimals); without a valid focus there is no `style`. +6. Buttons: at most 2 (the first two valid ones). A button is valid with a non-empty `label` and a `target` that is an anchor (`#name`), a path (one leading `/`, not `//`, no backslash) or an `https://` address that parses. `lead` and `display` are not written in the HTML. +7. Escape every text and attribute (`&` `&`, `<` `<`, `>` `>`, `"` `"`, `'` `'`). A new line in the title becomes `
` (after escaping). + +## Output, HERO-HTML + +`` + (when a `tall` webp variant exists: ``) + `ALT` + `` + `

` + (tagline: `TAGLINE `) + `TITLE

` + (when there are buttons: `

` + `LABEL`... + `

`). + +`src` is `image.src`; `W` and `H` are `image.width` and `image.height`. Selection is by orientation, not width, because the hero is as high as the screen. + +## Output, HERO-PRELOAD + +With a `tall` variant: `` followed by the landscape link with `media="(orientation: landscape)"`. The landscape link has `imagesrcset="WIDE" imagesizes="100vw"`, or `href="SRC"` when there is no wide set. Without a tall variant there is one link without `media`. The `srcset`, `sizes` and `media` values are byte for byte the same as in the picture; the golden files check this. + +## Files + +`hero-standard` (two slides, wide and tall, AVIF ignored), `hero-minimal` (no variants, no tagline, no button), `hero-two-lines` (line break, anchor, https and a third button that is cut, decorative slide), `hero-hostile` (escaping, unsafe buttons, unusable slides skipped), `hero-no-alt` and `hero-none` (empty). `seed.json` holds the seed for the ASF test CMS with a first focus estimate per photo. + +URL scheme suggestion for the variants (the CMS decides): `/media/hero//wide-1600.webp`, `/media/hero//tall-960.avif`. diff --git a/web/contract/host-injection/hero/hero-hostile.expected-preload.html b/web/contract/host-injection/hero/hero-hostile.expected-preload.html new file mode 100644 index 0000000..885e97e --- /dev/null +++ b/web/contract/host-injection/hero/hero-hostile.expected-preload.html @@ -0,0 +1 @@ + diff --git a/web/contract/host-injection/hero/hero-hostile.expected.html b/web/contract/host-injection/hero/hero-hostile.expected.html new file mode 100644 index 0000000..73166d9 --- /dev/null +++ b/web/contract/host-injection/hero/hero-hostile.expected.html @@ -0,0 +1 @@ +Een "foto" <script>

<i>x</i> <b>"Andrea" & 'Slop'</b>

<Goed>

diff --git a/web/contract/host-injection/hero/hero-hostile.input.json b/web/contract/host-injection/hero/hero-hostile.input.json new file mode 100644 index 0000000..497bbbf --- /dev/null +++ b/web/contract/host-injection/hero/hero-hostile.input.json @@ -0,0 +1,58 @@ +{ + "display": "slideshow", + "title": "\"Andrea\" & 'Slop'", + "tagline": "x", + "buttons": [ + { "label": "javascript", "target": "javascript:alert(1)" }, + { "label": "Protocolvrij", "target": "//evil.example/x" }, + { "label": "", "target": "/a?b=1&c=\"2\"" } + ], + "slides": [ + { + "id": "skip1", + "alt": "", + "decorative": false, + "image": { "width": 10, "height": 10, "src": "/media/x.webp" } + }, + { + "id": "skip2", + "alt": "extern", + "decorative": false, + "image": { + "width": 10, + "height": 10, + "src": "https://evil.example/x.webp" + } + }, + { + "id": "skip3", + "alt": "geen afmeting", + "decorative": false, + "image": { "width": 0, "height": 10, "src": "/media/x.webp" } + }, + { + "id": "a1", + "alt": "Een \"foto\" ', + ), + { ...live([]), hero }, + ); + expect(out).toContain('

'); + expect(out).toContain( + ' { + for (const data of [live([]), { ...live([]), hero: { title: 'x' } }]) { + const out = run(page(regions), data); + expect(out).toBe(page(regions)); + } + }); +}); + describe('injectCms data block', () => { const baked = ''; diff --git a/web/config/hostInjector.ts b/web/config/hostInjector.ts index ac17832..fa50b2e 100644 --- a/web/config/hostInjector.ts +++ b/web/config/hostInjector.ts @@ -9,6 +9,8 @@ // matches that code. import { bannerHtml, offeringsHtml } from '../src/lib/cmsHtml.ts'; import { scriptJson } from '../src/lib/cmsData.ts'; +import { mapHero } from '../src/lib/hero.ts'; +import { heroHtml, heroPreloadHtml } from '../src/lib/heroHtml.ts'; import { formatPrice } from '../src/lib/formatPrice.ts'; import { mapBanner, mapOfferings, mapTopBanner } from '../src/lib/offerings.ts'; @@ -16,6 +18,8 @@ export interface LiveData { // The answers as the public API gives them offerings: unknown; banners: { top: unknown; bottom: unknown }; + // The answer of GET /api/v1/hero; absent or unusable leaves the baked hero + hero?: unknown; } export interface InjectOptions { @@ -33,7 +37,7 @@ const jsonLd = (value: unknown) => .replace(/&/g, '\\u0026'); const REGION = - /[\s\S]*?/g; + /[\s\S]*?/g; const DATA_BLOCK = /(?:`, ) .replace(OFFER_JSON_LD, (match, slug: string, json: string) => { diff --git a/web/config/normalizeInjected.ts b/web/config/normalizeInjected.ts index 416fd21..c43a281 100644 --- a/web/config/normalizeInjected.ts +++ b/web/config/normalizeInjected.ts @@ -11,7 +11,7 @@ import { readFileSync, writeFileSync } from 'node:fs'; const REGION = - /()[\s\S]*?()/g; + /()[\s\S]*?()/g; const DATA_BLOCK = /(?:)?