/** * `htmlText` returns plain text, `<` included: `<script>` in markdown is the text * ` ">'; const ORIGIN = "https://haber.example"; /** Every element and comment under `node` (its own tag included when it is one). */ function markup(node: Node, out: string[] = []): string[] { if (node.nodeName === "#comment") out.push("#comment"); else if ("tagName" in node) out.push(node.tagName); for (const child of "childNodes" in node ? node.childNodes : []) markup(child as Node, out); if ("content" in node && node.content) markup(node.content as Node, out); return out; } /** The first `tag` element under `node`, depth first. */ function firstElement(node: Node, tag: string): DefaultTreeAdapterMap["element"] | undefined { if ("tagName" in node && node.tagName === tag) return node; for (const child of "childNodes" in node ? node.childNodes : []) { const found = firstElement(child as Node, tag); if (found) return found; } return undefined; } /** The elements and comments an HTML fragment parses to. */ const fragmentMarkup = (html: string) => markup(parseFragment(html) as unknown as Node); /** The elements and comments inside `` when `html` is all the head holds. */ function headMarkup(html: string): string[] { const doc = parse(`${html}`); const head = doc.childNodes .flatMap((n) => ("childNodes" in n ? n.childNodes : [])) .find((n) => n.nodeName === "head") as Node; return markup(head).slice(1); } const site = { id: "site_aaaaaaaaaaaa", name: "Haber", slug: "haber", defaultLocale: "en", locales: ["en"], domains: [], } as unknown as Site; const archive = { locale: "en", page: 1, path: "/category/news", root: "/category/news", shown: { name: "News", slug: "news", href: "/category/news", taxonomy: { key: "category", labels: { en: { plural: "Categories" } } }, }, trail: [], total: 0, perPage: 10, term: { labels: { en: { name: "News", description: `Intro ${ATTACK}` } } }, taxonomy: { key: "category" }, entries: [], } as never; describe("htmlText's sinks with decoded markup in the text (alert 41)", () => { const head = archiveHead(site, archive, ORIGIN); it("the description is the decoded text, verbatim", () => { expect(markdownText(`Intro ${ATTACK}`)).toBe(`Intro ${DECODED}`); expect(head.description).toBe(`Intro ${DECODED}`); }); it("meta description, og: and twitter: tags: an attribute value, never markup", () => { // What SeoHead.astro writes for `meta.description`, `meta.openGraph` and `meta.twitter`. const tags = [ ``, ...head.openGraph.map( ([p, c]) => ``, ), ...head.twitter.map( ([n, c]) => ``, ), ]; expect(head.openGraph).toContainEqual(["og:description", `Intro ${DECODED}`]); for (const tag of tags) expect(headMarkup(tag), tag).toEqual(["meta"]); const meta = firstElement(parse(`${tags[0]}`) as unknown as Node, "meta"); expect(meta?.attrs.find((a) => a.name === "content")?.value).toBe(`Intro ${DECODED}`); }); it("JSON-LD: no ,