From 72dd381cf908d0d139ee50f27732b134a7b29858 Mon Sep 17 00:00:00 2001 From: Sluijsens Date: Mon, 16 Nov 2026 16:48:55 +0100 Subject: [PATCH] cms-client: hero uit het datablok #cms-data (parseHero, doelcontrole, titelregels en afgeleide kop, useCmsHero) met tests Co-Authored-By: Claude Sonnet 5.5 --- .../hero/__tests__/parseHero.test.ts | 66 +++++++++++++++++++ .../hero/__tests__/readHero.test.ts | 33 ++++++++++ src/cms-client/hero/__tests__/target.test.ts | 64 ++++++++++++++++++ src/cms-client/hero/__tests__/title.test.ts | 47 +++++++++++++ src/cms-client/hero/index.ts | 6 ++ src/cms-client/hero/parseHero.ts | 41 ++++++++++++ src/cms-client/hero/readHero.ts | 25 +++++++ src/cms-client/hero/target.ts | 31 +++++++++ src/cms-client/hero/title.ts | 17 +++++ src/cms-client/hero/types.ts | 19 ++++++ src/cms-client/hero/useCmsHero.ts | 15 +++++ 11 files changed, 364 insertions(+) create mode 100644 src/cms-client/hero/__tests__/parseHero.test.ts create mode 100644 src/cms-client/hero/__tests__/readHero.test.ts create mode 100644 src/cms-client/hero/__tests__/target.test.ts create mode 100644 src/cms-client/hero/__tests__/title.test.ts create mode 100644 src/cms-client/hero/index.ts create mode 100644 src/cms-client/hero/parseHero.ts create mode 100644 src/cms-client/hero/readHero.ts create mode 100644 src/cms-client/hero/target.ts create mode 100644 src/cms-client/hero/title.ts create mode 100644 src/cms-client/hero/types.ts create mode 100644 src/cms-client/hero/useCmsHero.ts diff --git a/src/cms-client/hero/__tests__/parseHero.test.ts b/src/cms-client/hero/__tests__/parseHero.test.ts new file mode 100644 index 0000000..48363e3 --- /dev/null +++ b/src/cms-client/hero/__tests__/parseHero.test.ts @@ -0,0 +1,66 @@ +import { describe, expect, it } from 'vitest'; +import { parseHero } from '../parseHero'; + +// The seed of the SLP Software hero in the CMS (scripts/hero-sls.json of the CMS repository). +const seed = { + display: 'single', + title: 'Websites,\napps\n& meer.', + tagline: null, + lead: 'Bij SLP Software kun je terecht.', + buttons: [ + { label: 'Bekijk pakketten', target: '#pakketten' }, + { label: 'Plan een gesprek', target: '#contact' }, + ], + slides: [], +}; + +describe('parseHero', () => { + it('reads the seed hero of the CMS', () => { + expect(parseHero(seed)).toEqual({ + title: 'Websites,\napps\n& meer.', + lead: 'Bij SLP Software kun je terecht.', + buttons: seed.buttons, + }); + }); + + it('returns null (baked hero) without a usable hero', () => { + for (const value of [null, undefined, 'x', 5, [], {}, { title: '' }, { title: ' \n ' }, { title: 7 }]) { + expect(parseHero(value)).toBeNull(); + } + }); + + it('ignores tagline and slides: a text-only site', () => { + const hero = parseHero({ ...seed, tagline: 'Label', slides: [{ id: 'x' }] }); + expect(hero).not.toBeNull(); + expect(hero).not.toHaveProperty('tagline'); + expect(hero).not.toHaveProperty('slides'); + }); + + it('turns an empty lead into null and trims texts', () => { + expect(parseHero({ ...seed, lead: ' ' })?.lead).toBeNull(); + expect(parseHero({ ...seed, lead: null })?.lead).toBeNull(); + expect(parseHero({ ...seed, lead: ' Hallo ' })?.lead).toBe('Hallo'); + }); + + it('skips buttons with an unsafe target or an empty label, never repairs them', () => { + const hero = parseHero({ + ...seed, + buttons: [ + { label: 'Slecht', target: 'javascript:alert(1)' }, + { label: ' ', target: '#contact' }, + { label: 'Goed', target: '/over' }, + null, + 'x', + ], + }); + expect(hero?.buttons).toEqual([{ label: 'Goed', target: '/over' }]); + }); + + it('keeps at most two buttons and accepts zero or a missing list', () => { + const three = [1, 2, 3].map((n) => ({ label: `B${n}`, target: `#b${n}` })); + expect(parseHero({ ...seed, buttons: three })?.buttons).toHaveLength(2); + expect(parseHero({ ...seed, buttons: [] })?.buttons).toEqual([]); + expect(parseHero({ title: 'x' })?.buttons).toEqual([]); + expect(parseHero({ title: 'x', buttons: 'nope' })?.buttons).toEqual([]); + }); +}); diff --git a/src/cms-client/hero/__tests__/readHero.test.ts b/src/cms-client/hero/__tests__/readHero.test.ts new file mode 100644 index 0000000..cabc0cb --- /dev/null +++ b/src/cms-client/hero/__tests__/readHero.test.ts @@ -0,0 +1,33 @@ +import { afterEach, describe, expect, it } from 'vitest'; +import { readHeroFromDocument } from '../readHero'; + +function setBlock(text: string | null) { + document.body.innerHTML = text === null ? '' : ''; + if (text !== null) { + document.getElementById('cms-data')!.textContent = text; + } +} + +afterEach(() => { + document.body.innerHTML = ''; +}); + +describe('readHeroFromDocument', () => { + it('reads the hero key of the data block', () => { + setBlock(JSON.stringify({ siteUrl: 'https://x', offerings: [], hero: { title: 'A\nB', buttons: [] } })); + expect(readHeroFromDocument(document)).toEqual({ title: 'A\nB', lead: null, buttons: [] }); + }); + + it.each([ + ['no block', null], + ['an empty block', ''], + ['the baked block', '{}'], + ['hero null', '{"hero":null}'], + ['invalid JSON', '{"hero": '], + ['a block that is not an object', '"hero"'], + ['a hero without title', '{"hero":{"title":" "}}'], + ])('returns null for %s (baked hero stays)', (_name, text) => { + setBlock(text); + expect(readHeroFromDocument(document)).toBeNull(); + }); +}); diff --git a/src/cms-client/hero/__tests__/target.test.ts b/src/cms-client/hero/__tests__/target.test.ts new file mode 100644 index 0000000..47b8e85 --- /dev/null +++ b/src/cms-client/hero/__tests__/target.test.ts @@ -0,0 +1,64 @@ +import fc from 'fast-check'; +import { describe, expect, it } from 'vitest'; +import { isSafeTarget } from '../target'; + +describe('isSafeTarget', () => { + it.each([ + '#pakketten', + '#contact', + '#a-b_c1', + '/', + '/over-mij', + '/a?b=1&c=2', + 'https://example.com', + 'https://example.com/x?y=1#z', + ])('accepts %s', (target) => { + expect(isSafeTarget(target)).toBe(true); + }); + + it.each([ + '', + '#', + '# x', + '#a b', + '#a.b', + '//evil.example/x', + '/\\evil', + '/a b', + 'javascript:alert(1)', + 'JAVASCRIPT:alert(1)', + 'data:text/html,x', + 'http://example.com', + 'https://', + 'https://user:pw@example.com', + 'https://user@example.com', + 'ftp://example.com', + 'mailto:a@b.nl', + 'pakketten', + 'https://exa mple.com', + '/a\nb', + ])('refuses %j', (target) => { + expect(isSafeTarget(target)).toBe(false); + }); + + it('refuses anything that is not a string', () => { + for (const value of [null, undefined, 1, {}, [], true]) { + expect(isSafeTarget(value)).toBe(false); + } + }); + + it('property: whatever is accepted has one of the three allowed forms and no unsafe characters', () => { + fc.assert( + fc.property(fc.string(), (value) => { + if (!isSafeTarget(value)) return true; + const allowedForm = + /^#[A-Za-z0-9_-]+$/.test(value) || + (value.startsWith('/') && !value.startsWith('//')) || + value.startsWith('https://'); + // eslint-disable-next-line no-control-regex + return allowedForm && !/[\u0000- \u007f\\]/.test(value); + }), + { numRuns: 500 }, + ); + }); +}); diff --git a/src/cms-client/hero/__tests__/title.test.ts b/src/cms-client/hero/__tests__/title.test.ts new file mode 100644 index 0000000..e74176e --- /dev/null +++ b/src/cms-client/hero/__tests__/title.test.ts @@ -0,0 +1,47 @@ +import fc from 'fast-check'; +import { describe, expect, it } from 'vitest'; +import { headingFromLines, titleLines } from '../title'; + +describe('titleLines', () => { + it('splits on line breaks and trims', () => { + expect(titleLines('Websites,\napps\n& meer.')).toEqual(['Websites,', 'apps', '& meer.']); + expect(titleLines(' a \r\n b\r c ')).toEqual(['a', 'b', 'c']); + }); + + it('drops empty lines', () => { + expect(titleLines('\n\na\n\n b \n')).toEqual(['a', 'b']); + expect(titleLines(' \n ')).toEqual([]); + }); + + it('property: no line is empty, has line breaks or has outer spaces', () => { + fc.assert( + fc.property(fc.string(), (title) => + titleLines(title).every( + (line) => line !== '' && line === line.trim() && !/[\r\n]/.test(line), + ), + ), + ); + }); +}); + +describe('headingFromLines (screen-reader and search-engine heading)', () => { + it('puts the brand prefix before the lines joined with single spaces (the seed title)', () => { + expect(headingFromLines(['Websites,', 'apps', '& meer.'], 'SLP Software:')).toBe( + 'SLP Software: Websites, apps & meer.', + ); + }); + + it('works without a prefix and with a single line', () => { + expect(headingFromLines(['Alleen'])).toBe('Alleen'); + expect(headingFromLines(['Alleen'], ' ')).toBe('Alleen'); + }); + + it('property: the heading is the prefix, a space and the lines joined by spaces', () => { + fc.assert( + fc.property( + fc.array(fc.string({ minLength: 1 }), { minLength: 1 }), + (lines) => headingFromLines(lines, 'SLP Software:') === `SLP Software: ${lines.join(' ')}`, + ), + ); + }); +}); diff --git a/src/cms-client/hero/index.ts b/src/cms-client/hero/index.ts new file mode 100644 index 0000000..d9e57b2 --- /dev/null +++ b/src/cms-client/hero/index.ts @@ -0,0 +1,6 @@ +export { CMS_DATA_ID, readHeroFromDocument } from './readHero'; +export { parseHero } from './parseHero'; +export { isSafeTarget } from './target'; +export { headingFromLines, titleLines } from './title'; +export { useCmsHero } from './useCmsHero'; +export type { CmsHero, CmsHeroButton } from './types'; diff --git a/src/cms-client/hero/parseHero.ts b/src/cms-client/hero/parseHero.ts new file mode 100644 index 0000000..49c76b8 --- /dev/null +++ b/src/cms-client/hero/parseHero.ts @@ -0,0 +1,41 @@ +import { isSafeTarget } from './target'; +import { titleLines } from './title'; +import type { CmsHero, CmsHeroButton } from './types'; + +const MAX_BUTTONS = 2; + +function text(value: unknown): string { + return typeof value === 'string' ? value.trim() : ''; +} + +/** + * Turns the untrusted `hero` value of the data block (or the API) into a `CmsHero`, or null when nothing usable is + * there: no object, no title, or a title without any text. Fail-open: null means "show the baked hero". Buttons with + * an empty label or an unsafe target are skipped (never repaired); at most two are kept. + */ +export function parseHero(value: unknown): CmsHero | null { + if (value === null || typeof value !== 'object' || Array.isArray(value)) { + return null; + } + const raw = value as Record; + + const title = typeof raw.title === 'string' ? titleLines(raw.title).join('\n') : ''; + if (title === '') { + return null; + } + + const buttons: CmsHeroButton[] = []; + if (Array.isArray(raw.buttons)) { + for (const item of raw.buttons) { + if (buttons.length >= MAX_BUTTONS) break; + if (item === null || typeof item !== 'object') continue; + const { label, target } = item as Record; + const cleanLabel = text(label); + if (cleanLabel !== '' && isSafeTarget(target)) { + buttons.push({ label: cleanLabel, target }); + } + } + } + + return { title, lead: text(raw.lead) || null, buttons }; +} diff --git a/src/cms-client/hero/readHero.ts b/src/cms-client/hero/readHero.ts new file mode 100644 index 0000000..1195139 --- /dev/null +++ b/src/cms-client/hero/readHero.ts @@ -0,0 +1,25 @@ +import { parseHero } from './parseHero'; +import type { CmsHero } from './types'; + +/** Id of the data block the CMS host writes into the page (`