From 47de196516ba085341c6015332fbbd6983738f36 Mon Sep 17 00:00:00 2001 From: Jakub Zych Date: Sat, 28 Nov 2026 13:01:27 +0100 Subject: [PATCH] test(embeds): add registry, pair-aware sanitizer, hydration tests, and README docs --- README.md | 64 ++++- app/utils/embedRegistry.ts | 4 +- tests/embedHydration.test.ts | 522 +++++++++++++++++++++++++++++++++++ tests/embeds.test.ts | 133 ++++++++- 4 files changed, 712 insertions(+), 11 deletions(-) create mode 100644 tests/embedHydration.test.ts diff --git a/README.md b/README.md index 05d33f1..9ad391b 100644 --- a/README.md +++ b/README.md @@ -33,28 +33,76 @@ Route paths are configuration, not files. The layer's page components live in `a - **Body links and images**: the CMS's phrase auto-links, the outbound link rules (nofollow or sponsored from the domain lists; new tab, `noopener` and a visually hidden hint while the CMS's links_new_tab is on) and lazy, async body images, applied at SSR on the sanitized body. - **Breadcrumbs and pagination**: `` at the top of every journal page (`journal.breadcrumbs`), and `JournalPagination` as `` with the journal's class names. - **HTML sanitizer**: post bodies and category content go through `sanitizeJournalHtml` before `v-html` and before they reach the SSR payload. It is [sanitize-html](https://github.com/apostrophecms/sanitize-html) with the Journal API's own allow-list: the usual content elements, `footer`, `class`, schema.org microdata, the `color`, `font-size`, `font-weight` and `text-align` inline styles, and `http`, `https`, `mailto` or relative URLs. Five rules keep the Journal Blog Editor's own markup, mirroring the API's policy: `data-embed` (`youtube`) and `data-id` (an 11-character video id) on `figure`, `alt` with any text on `img` (`alt=""` of a decorative image included), an integer `start` on `ol`, and a `rel` of only `nofollow`, `sponsored` and `ugc` on `a`. `target` is never kept; the outbound link rules add it. With `{ demoteH1: true }`, `h1` headings come out as `h2`. Anything else (`script`, `style`, `iframe`, `svg`, forms, event-handler attributes, `javascript:` URLs in any spelling) is removed and the rest of the value is kept, so a post never renders blank because of one stray tag. Server and browser produce the same string, so hydration matches. See `docs/decisions/0001-sanitize-html.md`. -- **Embeds**: YouTube videos added in the Journal Blog Editor render as privacy-enhanced iframes (see [Embeds](#embeds)). +- **Embeds**: built-in YouTube privacy-enhanced iframes plus an app-extensible embed provider registry (`app.config.ts` and Nuxt plugin helpers) with SSR static fallback preservation, CLS height reservation, `IntersectionObserver` lazy hydration, and `prefers-reduced-motion` support (see [Embeds](#embeds)). - **i18n**: English and Polish UI strings, registered only for the locales the app enables. ## Embeds -The Journal Blog Editor never stores an iframe. It stores a placeholder: +The Journal Blog Editor never stores an iframe or inline `' + + '' + + '' + + 'Safe' + + '
Caption
' + + '' + const cleaned = sanitizeJournalHtml(xss) + assert.doesNotMatch(cleaned, /Safe
Caption
', + ) +}) + +test('expandEmbeds preserves SSR fallback + adds classes/CLS styles for client-hydrated providers and allows youtube override', () => { + registerJournalEmbedProvider('viz', { + idPattern: /^[a-z0-9-]+$/, + mount: () => {}, + aspectRatio: '16/9', + minHeight: '360px', + }) + + const raw = + '
' + + 'Chart' + + '
Figure 1
' + + '
' + const expanded = expandEmbeds(sanitizeJournalHtml(raw)) + assert.match(expanded, /class="journal-embed journal-embed--viz"/) + assert.match(expanded, /data-embed="viz"/) + assert.match(expanded, /data-id="chart-1"/) + assert.match(expanded, /style="aspect-ratio:4\/3;min-height:360px"/) + assert.match(expanded, /Chart/) + assert.match(expanded, /
Figure 1<\/figcaption>/) + + // Built-in YouTube remains byte-identical. + assert.equal(expandEmbeds(PLACEHOLDER), IFRAME) + + // Overriding youtube replaces SSR rendering. + registerJournalEmbedProvider('youtube', { + idPattern: /^[A-Za-z0-9_-]{11}$/, + renderSsr: (id) => `
`, + }) + assert.equal(expandEmbeds(PLACEHOLDER), `
`) +})