import { afterEach, beforeEach, describe, expect, it } from "vitest"; import { renderSanitizedMarkdown, sanitizeIconSvg, sanitizeMarkedHtml } from "./html-sanitizer"; describe("html sanitizer", () => { it("removes active HTML and unsafe URLs from marked output", () => { const html = sanitizeMarkedHtml(`

Title

bad also bad external `); expect(html).not.toMatch(/external'); }); it("sanitizes markdown links before rendering previews", () => { const html = renderSanitizedMarkdown(`[unsafe](javascript:alert(1)) [safe](https://example.com)`); expect(html).not.toMatch(/javascript:/i); expect(html).toContain('href="https://example.com" rel="noopener noreferrer"'); }); it("escapes author-typed raw HTML in markdown instead of passing it through", () => { const html = renderSanitizedMarkdown(`
`); // The dangerous markup must survive only as inert escaped text, never as a live tag. expect(html).not.toMatch(/bold"); }); describe("server string fallback (no DOMParser)", () => { const originalDomParser = globalThis.DOMParser; beforeEach(() => { // Force the SSR code path that does not have a real HTML parser. delete (globalThis as { DOMParser?: unknown }).DOMParser; }); afterEach(() => { globalThis.DOMParser = originalDomParser; }); it("strips slash-separated event handlers that the previous regex missed", () => { const html = sanitizeMarkedHtml(``); expect(html).not.toMatch(/onerror/i); }); it("strips whitespace-separated event handlers and unsafe protocols", () => { const html = sanitizeMarkedHtml(`x`); expect(html).not.toMatch(/onerror/i); expect(html).not.toMatch(/javascript:/i); }); it("keeps the markdown render path safe even without a DOM parser", () => { const html = renderSanitizedMarkdown(``); expect(html).not.toMatch(/ { const svg = sanitizeIconSvg( ``, "h-5 w-5" ); expect(svg).not.toMatch(/onload/i); expect(svg).not.toMatch(/