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 <noreply@anthropic.com>
This commit is contained in:
@@ -17,3 +17,4 @@ src/cms-client
|
|||||||
|
|
||||||
# Golden files of the host injection contract: exact text, never reformatted
|
# Golden files of the host injection contract: exact text, never reformatted
|
||||||
contract/host-injection/*.expected.html
|
contract/host-injection/*.expected.html
|
||||||
|
contract/host-injection/hero/*.html
|
||||||
|
|||||||
@@ -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 `<picture>`. Same idea as the packages (`../README.md`): for every `NAME.input.json` the host's template must give exactly `NAME.expected.html` (between `<!--CMS:HERO-HTML-->` and `<!--/CMS:HERO-HTML-->`) and `NAME.expected-preload.html` (between `<!--CMS:HERO-PRELOAD-->` and `<!--/CMS:HERO-PRELOAD-->` 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 `<br>` (after escaping).
|
||||||
|
|
||||||
|
## Output, HERO-HTML
|
||||||
|
|
||||||
|
`<picture class="cms-hero-picture">` + (when a `tall` webp variant exists: `<source media="(orientation: portrait)" srcset="TALL" sizes="100vw" type="image/webp">`) + `<img class="cms-hero-image" src="SRC"` + (when a `wide` webp variant exists: ` srcset="WIDE" sizes="100vw"`) + ` width="W" height="H" alt="ALT"` + (focus: ` style="object-position:X% Y%"`) + ` fetchpriority="high">` + `</picture>` + `<h1 class="cms-hero-title">` + (tagline: `<span class="cms-hero-tagline">TAGLINE</span> `) + `<span class="cms-hero-name">TITLE</span></h1>` + (when there are buttons: `<p class="cms-hero-buttons">` + `<a class="cms-hero-button" href="TARGET">LABEL</a>`... + `</p>`).
|
||||||
|
|
||||||
|
`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: `<link rel="preload" as="image" imagesrcset="TALL" imagesizes="100vw" media="(orientation: portrait)" fetchpriority="high">` 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/<hash>/wide-1600.webp`, `/media/hero/<hash>/tall-960.avif`.
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
<link rel="preload" as="image" imagesrcset="/media/hero/a"b/w800.webp 800w" imagesizes="100vw" fetchpriority="high">
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
<picture class="cms-hero-picture"><img class="cms-hero-image" src="/media/hero/a"b/w1600.webp" srcset="/media/hero/a"b/w800.webp 800w" sizes="100vw" width="1600" height="1067" alt="Een "foto" <script>" fetchpriority="high"></picture><h1 class="cms-hero-title"><span class="cms-hero-tagline"><i>x</i></span> <span class="cms-hero-name"><b>"Andrea" & 'Slop'</b></span></h1><p class="cms-hero-buttons"><a class="cms-hero-button" href="/a?b=1&c="2""><Goed></a></p>
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
{
|
||||||
|
"display": "slideshow",
|
||||||
|
"title": "<b>\"Andrea\" & 'Slop'</b>",
|
||||||
|
"tagline": "<i>x</i>",
|
||||||
|
"buttons": [
|
||||||
|
{ "label": "javascript", "target": "javascript:alert(1)" },
|
||||||
|
{ "label": "Protocolvrij", "target": "//evil.example/x" },
|
||||||
|
{ "label": "<Goed>", "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\" <script>",
|
||||||
|
"decorative": false,
|
||||||
|
"image": {
|
||||||
|
"width": 1600,
|
||||||
|
"height": 1067,
|
||||||
|
"src": "/media/hero/a\"b/w1600.webp",
|
||||||
|
"variants": [
|
||||||
|
{
|
||||||
|
"ratio": "wide",
|
||||||
|
"width": 800,
|
||||||
|
"type": "image/webp",
|
||||||
|
"url": "/media/hero/a\"b/w800.webp"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ratio": "wide",
|
||||||
|
"width": 400,
|
||||||
|
"type": "image/webp",
|
||||||
|
"url": "https://evil.example/w400.webp"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
<link rel="preload" as="image" href="/media/hero/abc123/w2560.webp" fetchpriority="high">
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
<picture class="cms-hero-picture"><img class="cms-hero-image" src="/media/hero/abc123/w2560.webp" width="2560" height="1702" alt="Lifestyle fotografie" fetchpriority="high"></picture><h1 class="cms-hero-title"><span class="cms-hero-name">Andrea Slop</span></h1>
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"display": "single",
|
||||||
|
"title": "Andrea Slop",
|
||||||
|
"buttons": [],
|
||||||
|
"slides": [
|
||||||
|
{
|
||||||
|
"id": "a1",
|
||||||
|
"alt": "Lifestyle fotografie",
|
||||||
|
"decorative": false,
|
||||||
|
"image": {
|
||||||
|
"width": 2560,
|
||||||
|
"height": 1702,
|
||||||
|
"src": "/media/hero/abc123/w2560.webp"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"display": "slideshow",
|
||||||
|
"title": "Titel",
|
||||||
|
"buttons": [],
|
||||||
|
"slides": [
|
||||||
|
{
|
||||||
|
"id": "x",
|
||||||
|
"alt": "",
|
||||||
|
"decorative": false,
|
||||||
|
"image": { "width": 1, "height": 1, "src": "/m.webp" }
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
null
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
<link rel="preload" as="image" imagesrcset="/media/hero/3f9a1c/tall-640.webp 640w, /media/hero/3f9a1c/tall-960.webp 960w" imagesizes="100vw" media="(orientation: portrait)" fetchpriority="high"><link rel="preload" as="image" imagesrcset="/media/hero/3f9a1c/wide-960.webp 960w, /media/hero/3f9a1c/wide-1600.webp 1600w" imagesizes="100vw" media="(orientation: landscape)" fetchpriority="high">
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
<picture class="cms-hero-picture"><source media="(orientation: portrait)" srcset="/media/hero/3f9a1c/tall-640.webp 640w, /media/hero/3f9a1c/tall-960.webp 960w" sizes="100vw" type="image/webp"><img class="cms-hero-image" src="/media/hero/3f9a1c/wide-1600.webp" srcset="/media/hero/3f9a1c/wide-960.webp 960w, /media/hero/3f9a1c/wide-1600.webp 1600w" sizes="100vw" width="1600" height="1067" alt="Zwangerschapsfoto in de duinen" style="object-position:50% 55%" fetchpriority="high"></picture><h1 class="cms-hero-title"><span class="cms-hero-tagline">Lifestyle fotograaf · Huizen</span> <span class="cms-hero-name">Andrea Slop</span></h1><p class="cms-hero-buttons"><a class="cms-hero-button" href="/over-mij">Lees meer</a></p>
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
{
|
||||||
|
"display": "slideshow",
|
||||||
|
"title": "Andrea Slop",
|
||||||
|
"tagline": "Lifestyle fotograaf · Huizen",
|
||||||
|
"lead": null,
|
||||||
|
"buttons": [{ "label": "Lees meer", "target": "/over-mij" }],
|
||||||
|
"slides": [
|
||||||
|
{
|
||||||
|
"id": "11111111-1111-1111-1111-111111111111",
|
||||||
|
"alt": "Zwangerschapsfoto in de duinen",
|
||||||
|
"decorative": false,
|
||||||
|
"image": {
|
||||||
|
"width": 1600,
|
||||||
|
"height": 1067,
|
||||||
|
"focus": { "x": 0.5, "y": 0.55 },
|
||||||
|
"src": "/media/hero/3f9a1c/wide-1600.webp",
|
||||||
|
"variants": [
|
||||||
|
{
|
||||||
|
"ratio": "wide",
|
||||||
|
"width": 1600,
|
||||||
|
"type": "image/webp",
|
||||||
|
"url": "/media/hero/3f9a1c/wide-1600.webp"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ratio": "wide",
|
||||||
|
"width": 960,
|
||||||
|
"type": "image/webp",
|
||||||
|
"url": "/media/hero/3f9a1c/wide-960.webp"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ratio": "wide",
|
||||||
|
"width": 1600,
|
||||||
|
"type": "image/avif",
|
||||||
|
"url": "/media/hero/3f9a1c/wide-1600.avif"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ratio": "wide",
|
||||||
|
"width": 960,
|
||||||
|
"type": "image/avif",
|
||||||
|
"url": "/media/hero/3f9a1c/wide-960.avif"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ratio": "tall",
|
||||||
|
"width": 960,
|
||||||
|
"type": "image/webp",
|
||||||
|
"url": "/media/hero/3f9a1c/tall-960.webp"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ratio": "tall",
|
||||||
|
"width": 640,
|
||||||
|
"type": "image/webp",
|
||||||
|
"url": "/media/hero/3f9a1c/tall-640.webp"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ratio": "tall",
|
||||||
|
"width": 960,
|
||||||
|
"type": "image/avif",
|
||||||
|
"url": "/media/hero/3f9a1c/tall-960.avif"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "22222222-2222-2222-2222-222222222222",
|
||||||
|
"alt": "Knusse newbornfoto met de familie",
|
||||||
|
"decorative": false,
|
||||||
|
"image": {
|
||||||
|
"width": 1600,
|
||||||
|
"height": 1067,
|
||||||
|
"focus": { "x": 0.45, "y": 0.5 },
|
||||||
|
"src": "/media/hero/7b2e90/wide-1600.webp",
|
||||||
|
"variants": []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
<link rel="preload" as="image" href="/media/hero/abc123/w1600.webp" fetchpriority="high">
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
<picture class="cms-hero-picture"><img class="cms-hero-image" src="/media/hero/abc123/w1600.webp" width="1600" height="1067" alt="" fetchpriority="high"></picture><h1 class="cms-hero-title"><span class="cms-hero-tagline">Fotografie</span> <span class="cms-hero-name">Andrea<br>Slop</span></h1><p class="cms-hero-buttons"><a class="cms-hero-button" href="#contact">Boek een shoot</a><a class="cms-hero-button" href="https://andreaslop.nl/portfolio">Portfolio</a></p>
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
{
|
||||||
|
"display": "slideshow",
|
||||||
|
"title": "Andrea\nSlop",
|
||||||
|
"tagline": "Fotografie",
|
||||||
|
"buttons": [
|
||||||
|
{ "label": "Boek een shoot", "target": "#contact" },
|
||||||
|
{ "label": "Portfolio", "target": "https://andreaslop.nl/portfolio" },
|
||||||
|
{ "label": "Derde knop", "target": "/derde" }
|
||||||
|
],
|
||||||
|
"slides": [
|
||||||
|
{
|
||||||
|
"id": "a1",
|
||||||
|
"alt": "",
|
||||||
|
"decorative": true,
|
||||||
|
"image": {
|
||||||
|
"width": 1600,
|
||||||
|
"height": 1067,
|
||||||
|
"src": "/media/hero/abc123/w1600.webp",
|
||||||
|
"variants": []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
{
|
||||||
|
"note": "Seed for the ASF hero on the test CMS. Focus points are a first estimate from looking at the photos (x and y between 0 and 1, from the top left); Bryan adjusts them in the admin. Sources are the existing exports in web/src/assets (not camera originals; never upscale).",
|
||||||
|
"display": "slideshow",
|
||||||
|
"title": "Andrea Slop",
|
||||||
|
"tagline": "Lifestyle fotograaf · Huizen",
|
||||||
|
"buttons": [{ "label": "Lees meer", "target": "/over-mij" }],
|
||||||
|
"slides": [
|
||||||
|
{
|
||||||
|
"file": "hero-8.jpg",
|
||||||
|
"width": 1600,
|
||||||
|
"height": 1067,
|
||||||
|
"alt": "Zwangerschapsfoto in de duinen",
|
||||||
|
"focus": { "x": 0.5, "y": 0.55 },
|
||||||
|
"subject": "belly and hands"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"file": "hero-7.jpg",
|
||||||
|
"width": 1600,
|
||||||
|
"height": 1067,
|
||||||
|
"alt": "Knusse newbornfoto met de familie",
|
||||||
|
"focus": { "x": 0.45, "y": 0.5 },
|
||||||
|
"subject": "the kiss on the baby"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"file": "hero-1.jpg",
|
||||||
|
"width": 2560,
|
||||||
|
"height": 1702,
|
||||||
|
"alt": "Lifestyle fotografie",
|
||||||
|
"focus": { "x": 0.55, "y": 0.6 },
|
||||||
|
"subject": "tattoo and heather"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"file": "hero-2.jpg",
|
||||||
|
"width": 1600,
|
||||||
|
"height": 1067,
|
||||||
|
"alt": "Zwangerschapsshoot in de buitenlucht",
|
||||||
|
"focus": { "x": 0.45, "y": 0.45 },
|
||||||
|
"subject": "the couple"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"file": "hero-3.jpg",
|
||||||
|
"width": 1600,
|
||||||
|
"height": 1067,
|
||||||
|
"alt": "Newbornshoot van een pasgeboren baby",
|
||||||
|
"focus": { "x": 0.45, "y": 0.4 },
|
||||||
|
"subject": "the baby's face"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"file": "hero-4.jpg",
|
||||||
|
"width": 2048,
|
||||||
|
"height": 1362,
|
||||||
|
"alt": "Spontaan portret buiten",
|
||||||
|
"focus": { "x": 0.45, "y": 0.35 },
|
||||||
|
"subject": "the face"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"file": "hero-5.jpg",
|
||||||
|
"width": 1600,
|
||||||
|
"height": 1067,
|
||||||
|
"alt": "Natuurlijke lifestylefoto",
|
||||||
|
"focus": { "x": 0.5, "y": 0.55 },
|
||||||
|
"subject": "the hands with the ring"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"file": "hero-6.jpg",
|
||||||
|
"width": 2048,
|
||||||
|
"height": 1362,
|
||||||
|
"alt": "Portret in de natuur",
|
||||||
|
"focus": { "x": 0.5, "y": 0.35 },
|
||||||
|
"subject": "the face"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,139 @@
|
|||||||
|
import { isSafeBannerUrl } from './offerings.ts';
|
||||||
|
|
||||||
|
// Turns the answer of GET /api/v1/hero (the CMS) into what the site uses. Pure
|
||||||
|
// functions, tested without a network. Anything that is not clearly valid is
|
||||||
|
// dropped; an answer without a usable slide gives null, so the caller keeps
|
||||||
|
// the hero that was baked into the page (fail open).
|
||||||
|
|
||||||
|
export interface HeroButton {
|
||||||
|
label: string;
|
||||||
|
target: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface HeroImageVariant {
|
||||||
|
ratio: string;
|
||||||
|
width: number;
|
||||||
|
type: string;
|
||||||
|
url: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface HeroSlide {
|
||||||
|
id: string;
|
||||||
|
alt: string;
|
||||||
|
decorative: boolean;
|
||||||
|
image: {
|
||||||
|
width: number;
|
||||||
|
height: number;
|
||||||
|
// Where the subject is, 0..1; the site uses it as object-position
|
||||||
|
focus: { x: number; y: number } | null;
|
||||||
|
src: string;
|
||||||
|
variants: HeroImageVariant[];
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface HeroData {
|
||||||
|
display: 'single' | 'slideshow';
|
||||||
|
title: string;
|
||||||
|
tagline?: string;
|
||||||
|
buttons: HeroButton[];
|
||||||
|
slides: HeroSlide[];
|
||||||
|
}
|
||||||
|
|
||||||
|
type Raw = Record<string, unknown>;
|
||||||
|
|
||||||
|
const isObject = (value: unknown): value is Raw =>
|
||||||
|
typeof value === 'object' && value !== null && !Array.isArray(value);
|
||||||
|
|
||||||
|
const asText = (value: unknown): string | undefined =>
|
||||||
|
typeof value === 'string' && value.trim() !== '' ? value.trim() : undefined;
|
||||||
|
|
||||||
|
const asSize = (value: unknown): number | undefined =>
|
||||||
|
typeof value === 'number' && Number.isInteger(value) && value > 0
|
||||||
|
? value
|
||||||
|
: undefined;
|
||||||
|
|
||||||
|
const BACKSLASH = String.fromCharCode(92);
|
||||||
|
|
||||||
|
// An image address of our own host: a path with one leading slash
|
||||||
|
const isOwnPath = (url: string): boolean =>
|
||||||
|
url.startsWith('/') && !url.startsWith('//') && !url.includes(BACKSLASH);
|
||||||
|
|
||||||
|
// A button goes to an anchor, a path or an https address
|
||||||
|
const isSafeTarget = (target: string): boolean =>
|
||||||
|
/^#[A-Za-z][\w-]*$/.test(target) || isSafeBannerUrl(target);
|
||||||
|
|
||||||
|
function toButton(raw: unknown): HeroButton | null {
|
||||||
|
if (!isObject(raw)) return null;
|
||||||
|
const label = asText(raw.label);
|
||||||
|
const target = asText(raw.target);
|
||||||
|
return label && target && isSafeTarget(target) ? { label, target } : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function toVariant(raw: unknown): HeroImageVariant | null {
|
||||||
|
if (!isObject(raw)) return null;
|
||||||
|
const ratio = asText(raw.ratio);
|
||||||
|
const type = asText(raw.type);
|
||||||
|
const url = asText(raw.url);
|
||||||
|
const width = asSize(raw.width);
|
||||||
|
return ratio && type && url && width && isOwnPath(url)
|
||||||
|
? { ratio, width, type, url }
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const inUnit = (value: unknown): value is number =>
|
||||||
|
typeof value === 'number' && value >= 0 && value <= 1;
|
||||||
|
|
||||||
|
function toFocus(raw: unknown): { x: number; y: number } | null {
|
||||||
|
if (!isObject(raw) || !inUnit(raw.x) || !inUnit(raw.y)) return null;
|
||||||
|
return { x: raw.x, y: raw.y };
|
||||||
|
}
|
||||||
|
|
||||||
|
function toSlide(raw: unknown): HeroSlide | null {
|
||||||
|
if (!isObject(raw) || !isObject(raw.image)) return null;
|
||||||
|
const id = asText(raw.id);
|
||||||
|
const decorative = raw.decorative === true;
|
||||||
|
// A photo that says something needs its alt text; a decorative one has none
|
||||||
|
const alt = decorative ? '' : asText(raw.alt);
|
||||||
|
const src = asText(raw.image.src);
|
||||||
|
const width = asSize(raw.image.width);
|
||||||
|
const height = asSize(raw.image.height);
|
||||||
|
if (!id || !src || !isOwnPath(src) || !width || !height) return null;
|
||||||
|
if (alt === undefined) return null;
|
||||||
|
const variants = Array.isArray(raw.image.variants)
|
||||||
|
? raw.image.variants.map(toVariant).filter((v) => v !== null)
|
||||||
|
: [];
|
||||||
|
return {
|
||||||
|
id,
|
||||||
|
alt,
|
||||||
|
decorative,
|
||||||
|
image: {
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
focus: toFocus(raw.image.focus),
|
||||||
|
src,
|
||||||
|
variants,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function mapHero(answer: unknown): HeroData | null {
|
||||||
|
if (!isObject(answer)) return null;
|
||||||
|
const title = asText(answer.title);
|
||||||
|
const slides = Array.isArray(answer.slides)
|
||||||
|
? answer.slides.map(toSlide).filter((slide) => slide !== null)
|
||||||
|
: [];
|
||||||
|
if (!title || slides.length === 0) return null;
|
||||||
|
const buttons = Array.isArray(answer.buttons)
|
||||||
|
? answer.buttons
|
||||||
|
.map(toButton)
|
||||||
|
.filter((button) => button !== null)
|
||||||
|
.slice(0, 2)
|
||||||
|
: [];
|
||||||
|
return {
|
||||||
|
display: answer.display === 'single' ? 'single' : 'slideshow',
|
||||||
|
title,
|
||||||
|
tagline: asText(answer.tagline),
|
||||||
|
buttons,
|
||||||
|
slides,
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,248 @@
|
|||||||
|
import { readdirSync, readFileSync, writeFileSync } from 'node:fs';
|
||||||
|
import { join } from 'node:path';
|
||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
import { mapHero } from './hero';
|
||||||
|
import { heroHtml, heroPreloadHtml } from './heroHtml';
|
||||||
|
|
||||||
|
// The contract with the CMS host: for every hero-*.input.json in
|
||||||
|
// contract/host-injection/hero there is an *.expected.html (the region between
|
||||||
|
// <!--CMS:HERO-HTML--> markers) and an *.expected-preload.html (the links
|
||||||
|
// between <!--CMS:HERO-PRELOAD--> markers). Regenerate after a deliberate change
|
||||||
|
// with UPDATE_GOLDEN=1 and send the files to the CMS session.
|
||||||
|
const DIR = join(process.cwd(), 'contract', 'host-injection', 'hero');
|
||||||
|
const inputs = readdirSync(DIR).filter((name) => name.endsWith('.input.json'));
|
||||||
|
|
||||||
|
describe('hero golden files', () => {
|
||||||
|
it('has inputs', () => {
|
||||||
|
expect(inputs.length).toBeGreaterThanOrEqual(6);
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each(inputs)('%s gives the expected HTML and preload', (file) => {
|
||||||
|
const name = file.replace('.input.json', '');
|
||||||
|
const hero = mapHero(JSON.parse(readFileSync(join(DIR, file), 'utf8')));
|
||||||
|
const outputs = [
|
||||||
|
[`${name}.expected.html`, heroHtml(hero)],
|
||||||
|
[`${name}.expected-preload.html`, heroPreloadHtml(hero)],
|
||||||
|
];
|
||||||
|
for (const [expectedName, html] of outputs) {
|
||||||
|
const path = join(DIR, expectedName);
|
||||||
|
if (process.env.UPDATE_GOLDEN === '1') {
|
||||||
|
writeFileSync(path, html + '\n');
|
||||||
|
}
|
||||||
|
expect(html + '\n').toBe(readFileSync(path, 'utf8'));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const slide = (image: object, extra: object = {}) => ({
|
||||||
|
id: 's',
|
||||||
|
alt: 'Alt',
|
||||||
|
decorative: false,
|
||||||
|
image: { width: 10, height: 8, src: '/m/a.webp', ...image },
|
||||||
|
...extra,
|
||||||
|
});
|
||||||
|
|
||||||
|
const answer = (slides: unknown[], extra: object = {}) => ({
|
||||||
|
title: 'T',
|
||||||
|
slides,
|
||||||
|
...extra,
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('mapHero', () => {
|
||||||
|
it('rejects what is not a usable hero', () => {
|
||||||
|
expect(mapHero(null)).toBeNull();
|
||||||
|
expect(mapHero([])).toBeNull();
|
||||||
|
expect(mapHero({ title: 'T' })).toBeNull();
|
||||||
|
expect(mapHero(answer([]))).toBeNull();
|
||||||
|
expect(mapHero({ slides: [slide({})] })).toBeNull();
|
||||||
|
expect(mapHero(answer([slide({})], { title: ' ' }))).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('drops slides that cannot be used', () => {
|
||||||
|
const bad = [
|
||||||
|
null,
|
||||||
|
{ id: 's' },
|
||||||
|
slide({}, { id: '' }),
|
||||||
|
slide({ src: '' }),
|
||||||
|
slide({ src: 'https://evil.example/a.webp' }),
|
||||||
|
slide({ src: '//evil.example/a.webp' }),
|
||||||
|
slide({ src: '/a\\b.webp' }),
|
||||||
|
slide({ width: 0 }),
|
||||||
|
slide({ height: 1.5 }),
|
||||||
|
slide({}, { alt: ' ' }),
|
||||||
|
];
|
||||||
|
expect(mapHero(answer(bad))).toBeNull();
|
||||||
|
expect(mapHero(answer([...bad, slide({})]))?.slides).toHaveLength(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps a decorative slide with an empty alt', () => {
|
||||||
|
const hero = mapHero(
|
||||||
|
answer([slide({}, { decorative: true, alt: 'ignored' })]),
|
||||||
|
);
|
||||||
|
expect(hero?.slides[0].alt).toBe('');
|
||||||
|
expect(hero?.slides[0].decorative).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('reads the display, the tagline and at most two safe buttons', () => {
|
||||||
|
const hero = mapHero(
|
||||||
|
answer([slide({})], {
|
||||||
|
display: 'single',
|
||||||
|
tagline: ' Label ',
|
||||||
|
buttons: [
|
||||||
|
null,
|
||||||
|
{ label: 'x', target: 'javascript:1' },
|
||||||
|
{ label: '', target: '/a' },
|
||||||
|
{ label: 'A', target: '#contact' },
|
||||||
|
{ label: 'B', target: '/b' },
|
||||||
|
{ label: 'C', target: 'https://a.example/c' },
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
expect(hero?.display).toBe('single');
|
||||||
|
expect(hero?.tagline).toBe('Label');
|
||||||
|
expect(hero?.buttons.map((b) => b.label)).toEqual(['A', 'B']);
|
||||||
|
expect(mapHero(answer([slide({})]))?.display).toBe('slideshow');
|
||||||
|
expect(
|
||||||
|
mapHero(answer([slide({})], { buttons: 'nope' }))?.buttons,
|
||||||
|
).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps only valid variants and a focus point inside the picture', () => {
|
||||||
|
const hero = mapHero(
|
||||||
|
answer([
|
||||||
|
slide({
|
||||||
|
focus: { x: 0.25, y: 1 },
|
||||||
|
variants: [
|
||||||
|
null,
|
||||||
|
{
|
||||||
|
ratio: 'wide',
|
||||||
|
width: 100,
|
||||||
|
type: 'image/webp',
|
||||||
|
url: '/a.webp',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ratio: 'wide',
|
||||||
|
width: 0,
|
||||||
|
type: 'image/webp',
|
||||||
|
url: '/b.webp',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ratio: 'wide',
|
||||||
|
width: 100,
|
||||||
|
type: 'image/webp',
|
||||||
|
url: 'https://x.example/c.webp',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
slide({ focus: { x: 2, y: 0 } }),
|
||||||
|
slide({ focus: 'middle', variants: 'none' }),
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
expect(hero?.slides[0].image.variants).toHaveLength(1);
|
||||||
|
expect(hero?.slides[0].image.focus).toEqual({ x: 0.25, y: 1 });
|
||||||
|
expect(hero?.slides[1].image.focus).toBeNull();
|
||||||
|
expect(hero?.slides[2].image.focus).toBeNull();
|
||||||
|
expect(hero?.slides[2].image.variants).toEqual([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('heroHtml and heroPreloadHtml', () => {
|
||||||
|
it('write nothing for no hero', () => {
|
||||||
|
expect(heroHtml(null)).toBe('');
|
||||||
|
expect(heroPreloadHtml(null)).toBe('');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('leave out what is not there', () => {
|
||||||
|
const hero = mapHero(answer([slide({})]));
|
||||||
|
const html = heroHtml(hero);
|
||||||
|
expect(html).not.toContain('<source');
|
||||||
|
expect(html).not.toContain('srcset');
|
||||||
|
expect(html).not.toContain('style=');
|
||||||
|
expect(html).not.toContain('cms-hero-buttons');
|
||||||
|
expect(html).not.toContain('cms-hero-tagline');
|
||||||
|
expect(heroPreloadHtml(hero)).toBe(
|
||||||
|
'<link rel="preload" as="image" href="/m/a.webp" fetchpriority="high">',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('uses the same srcset, sizes and media in the picture and the preload', () => {
|
||||||
|
const hero = mapHero(
|
||||||
|
answer([
|
||||||
|
slide({
|
||||||
|
variants: [
|
||||||
|
{
|
||||||
|
ratio: 'wide',
|
||||||
|
width: 20,
|
||||||
|
type: 'image/webp',
|
||||||
|
url: '/w20.webp',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ratio: 'tall',
|
||||||
|
width: 8,
|
||||||
|
type: 'image/webp',
|
||||||
|
url: '/t8.webp',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
const html = heroHtml(hero);
|
||||||
|
const preload = heroPreloadHtml(hero);
|
||||||
|
for (const part of [
|
||||||
|
'srcset="/t8.webp 8w"',
|
||||||
|
'srcset="/w20.webp 20w"',
|
||||||
|
'media="(orientation: portrait)"',
|
||||||
|
]) {
|
||||||
|
expect(html).toContain(part);
|
||||||
|
}
|
||||||
|
expect(preload).toContain('imagesrcset="/t8.webp 8w"');
|
||||||
|
expect(preload).toContain('imagesrcset="/w20.webp 20w"');
|
||||||
|
expect(preload).toContain('media="(orientation: portrait)"');
|
||||||
|
expect(preload).toContain('media="(orientation: landscape)"');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('preloads the plain image for landscape when only a tall variant has a set', () => {
|
||||||
|
const hero = mapHero(
|
||||||
|
answer([
|
||||||
|
slide({
|
||||||
|
variants: [
|
||||||
|
{
|
||||||
|
ratio: 'tall',
|
||||||
|
width: 8,
|
||||||
|
type: 'image/webp',
|
||||||
|
url: '/t8.webp',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
expect(heroPreloadHtml(hero)).toContain(
|
||||||
|
'href="/m/a.webp" media="(orientation: landscape)"',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps one preload without media when there is no tall variant', () => {
|
||||||
|
const hero = mapHero(
|
||||||
|
answer([
|
||||||
|
slide({
|
||||||
|
variants: [
|
||||||
|
{
|
||||||
|
ratio: 'wide',
|
||||||
|
width: 20,
|
||||||
|
type: 'image/webp',
|
||||||
|
url: '/w20.webp',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
expect(heroPreloadHtml(hero)).toBe(
|
||||||
|
'<link rel="preload" as="image" imagesrcset="/w20.webp 20w" imagesizes="100vw" fetchpriority="high">',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('writes the focus point as object-position', () => {
|
||||||
|
const hero = mapHero(answer([slide({ focus: { x: 0.333333, y: 0 } })]));
|
||||||
|
expect(heroHtml(hero)).toContain('style="object-position:33.33% 0%"');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
import { escapeHtml } from './cmsHtml.ts';
|
||||||
|
import type { HeroData, HeroImageVariant, HeroSlide } from './hero.ts';
|
||||||
|
|
||||||
|
// The neutral HTML of the hero between the CMS markers, and the preload links
|
||||||
|
// in the head. The host (SLP Modular CMS) writes the same markup from live
|
||||||
|
// data; the golden files in contract/host-injection/hero are the contract.
|
||||||
|
//
|
||||||
|
// Only the FIRST slide is written (it is the LCP image); the client takes the
|
||||||
|
// rest from the data block. The picture holds one <source> for portrait
|
||||||
|
// screens (ratio "tall") and the <img> itself is the landscape one (ratio
|
||||||
|
// "wide"). Selection is by orientation, because the hero is as high as the
|
||||||
|
// screen. The preload links repeat exactly the same srcset, sizes and media,
|
||||||
|
// so the browser starts the very file the picture will pick.
|
||||||
|
//
|
||||||
|
// Only WebP is used here: a preload cannot say "AVIF if supported", so a
|
||||||
|
// second format would be fetched twice. Later slides may use AVIF.
|
||||||
|
|
||||||
|
const PORTRAIT = '(orientation: portrait)';
|
||||||
|
const LANDSCAPE = '(orientation: landscape)';
|
||||||
|
const SIZES = '100vw';
|
||||||
|
|
||||||
|
const srcsetOf = (variants: HeroImageVariant[], ratio: string): string =>
|
||||||
|
variants
|
||||||
|
.filter((v) => v.ratio === ratio && v.type === 'image/webp')
|
||||||
|
.sort((a, b) => a.width - b.width)
|
||||||
|
.map((v) => `${escapeHtml(v.url)} ${v.width}w`)
|
||||||
|
.join(', ');
|
||||||
|
|
||||||
|
const percent = (unit: number): string => `${Number((unit * 100).toFixed(2))}%`;
|
||||||
|
|
||||||
|
const focusStyle = (slide: HeroSlide): string => {
|
||||||
|
const { focus } = slide.image;
|
||||||
|
return focus
|
||||||
|
? ` style="object-position:${percent(focus.x)} ${percent(focus.y)}"`
|
||||||
|
: '';
|
||||||
|
};
|
||||||
|
|
||||||
|
function pictureHtml(slide: HeroSlide): string {
|
||||||
|
const { image } = slide;
|
||||||
|
const tall = srcsetOf(image.variants, 'tall');
|
||||||
|
const wide = srcsetOf(image.variants, 'wide');
|
||||||
|
const source = tall
|
||||||
|
? `<source media="${PORTRAIT}" srcset="${tall}" sizes="${SIZES}" type="image/webp">`
|
||||||
|
: '';
|
||||||
|
const img =
|
||||||
|
`<img class="cms-hero-image" src="${escapeHtml(image.src)}"` +
|
||||||
|
(wide ? ` srcset="${wide}" sizes="${SIZES}"` : '') +
|
||||||
|
` width="${image.width}" height="${image.height}"` +
|
||||||
|
` alt="${escapeHtml(slide.alt)}"${focusStyle(slide)}` +
|
||||||
|
' fetchpriority="high">';
|
||||||
|
return `<picture class="cms-hero-picture">${source}${img}</picture>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function headingHtml(hero: HeroData): string {
|
||||||
|
const tagline = hero.tagline
|
||||||
|
? `<span class="cms-hero-tagline">${escapeHtml(hero.tagline)}</span> `
|
||||||
|
: '';
|
||||||
|
// A new line in the title is a line break
|
||||||
|
const name = escapeHtml(hero.title).replace(/\r?\n/g, '<br>');
|
||||||
|
return `<h1 class="cms-hero-title">${tagline}<span class="cms-hero-name">${name}</span></h1>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function buttonsHtml(hero: HeroData): string {
|
||||||
|
if (hero.buttons.length === 0) return '';
|
||||||
|
const links = hero.buttons
|
||||||
|
.map(
|
||||||
|
(button) =>
|
||||||
|
`<a class="cms-hero-button" href="${escapeHtml(button.target)}">${escapeHtml(button.label)}</a>`,
|
||||||
|
)
|
||||||
|
.join('');
|
||||||
|
return `<p class="cms-hero-buttons">${links}</p>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Between <!--CMS:HERO-HTML--> and <!--/CMS:HERO-HTML-->; nothing for no hero
|
||||||
|
export function heroHtml(hero: HeroData | null): string {
|
||||||
|
if (!hero) return '';
|
||||||
|
return pictureHtml(hero.slides[0]) + headingHtml(hero) + buttonsHtml(hero);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Between <!--CMS:HERO-PRELOAD--> and <!--/CMS:HERO-PRELOAD--> in the head: one
|
||||||
|
// link for portrait screens (when a tall variant exists) and one for the
|
||||||
|
// others. Without a tall variant a single link without media is enough.
|
||||||
|
export function heroPreloadHtml(hero: HeroData | null): string {
|
||||||
|
if (!hero) return '';
|
||||||
|
const { image } = hero.slides[0];
|
||||||
|
const tall = srcsetOf(image.variants, 'tall');
|
||||||
|
const wide = srcsetOf(image.variants, 'wide');
|
||||||
|
const link = (target: string, media: string): string =>
|
||||||
|
`<link rel="preload" as="image" ${target}` +
|
||||||
|
`${media ? ` media="${media}"` : ''} fetchpriority="high">`;
|
||||||
|
const wideTarget = wide
|
||||||
|
? `imagesrcset="${wide}" imagesizes="${SIZES}"`
|
||||||
|
: `href="${escapeHtml(image.src)}"`;
|
||||||
|
if (!tall) return link(wideTarget, '');
|
||||||
|
return (
|
||||||
|
link(`imagesrcset="${tall}" imagesizes="${SIZES}"`, PORTRAIT) +
|
||||||
|
link(wideTarget, LANDSCAPE)
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user