/** * Renderer unit tests — pure-function assertions for render.ts, smartypants.ts, * and print-css.ts. No browser, no PDF generation. */ import { describe, expect, test } from "bun:test"; import { render, sanitizeUntrustedHtml } from "../src/render"; import { smartypants } from "../src/smartypants"; import { printCss, screenCss } from "../src/print-css"; // ─── smartypants ────────────────────────────────────────────── describe("smartypants", () => { test("converts straight double quotes to curly", () => { const out = smartypants(`

She said "hello" to him.

`); expect(out).toContain("\u201chello\u201d"); }); test("converts em dash (--)", () => { const out = smartypants(`

This is it -- the answer.

`); expect(out).toContain("\u2014"); }); test("converts ellipsis (...)", () => { const out = smartypants(`

Wait...

`); expect(out).toContain("\u2026"); }); test("converts apostrophes in contractions", () => { const out = smartypants(`

don't you know?

`); expect(out).toContain("don\u2019t"); }); test("does NOT touch content inside blocks", () => { const input = `
const x = "hello"; // it's fine
`; const out = smartypants(input); expect(out).toBe(input); // unchanged }); test("does NOT touch content inside
 blocks", () => {
    const input = `
"quoted" -- don't
`; const out = smartypants(input); expect(out).toBe(input); }); test("does NOT touch inline code", () => { const out = smartypants(`

Use it's like this: "hello".

`); expect(out).toContain("it's"); expect(out).toContain("\u201chello\u201d"); }); test("does NOT touch URLs", () => { const out = smartypants(`

Visit https://example.com/it's-page for "details".

`); expect(out).toContain("https://example.com/it's-page"); expect(out).toContain("\u201cdetails\u201d"); }); // A URL flush against a following tag (no whitespace between) used to let // URL_RE's \S+ swallow the placeholder standing in for that tag. The // placeholder never restored, so the tag vanished and its styling bled // into the rest of the document. The test above misses this because its // URL is followed by a space. test("does not leak placeholders for a bare autolinked URL", () => { const input = `

see https://ex.com ok

`; const out = smartypants(input); expect(out).not.toContain("SMARTPANTS_PRESERVED"); expect(out).toContain(""); expect(out).toBe(input); }); test("does not leak placeholders for a bold URL", () => { const input = `

see https://ex.com ok

`; const out = smartypants(input); expect(out).not.toContain("SMARTPANTS_PRESERVED"); expect(out).toContain(""); expect(out).toBe(input); }); test("does not leak placeholders when a URL is followed by punctuation", () => { const input = `

see https://ex.com, ok

`; const out = smartypants(input); expect(out).not.toContain("SMARTPANTS_PRESERVED"); expect(out).toContain(""); }); // A quote that opens a paragraph, heading or list item sits right after a // tag, and was read as a closing quote: "Hello," rendered as ”Hello,”. test("opens a quote at the start of a paragraph, heading or list item", () => { expect(smartypants(`

"Hello," she said.

`)).toBe(`

\u201cHello,\u201d she said.

`); expect(smartypants(`

"Quoted" title

`)).toBe(`

\u201cQuoted\u201d title

`); expect(smartypants(``)).toBe(``); expect(smartypants(`

x

"y"

`)).toBe(`

x

\u201cy\u201d

`); expect(smartypants(`

"quoted" word

`)).toBe(`

\u201cquoted\u201d word

`); }); test("reads a quote after an inline tag by the text before the tag", () => { expect(smartypants(`

He said "stop" twice.

`)).toBe(`

He said \u201cstop\u201d twice.

`); expect(smartypants(`

Garry's book

`)).toBe(`

Garry\u2019s book

`); }); test("a flag at the start of a cell, list item or bold run keeps its dashes", () => { expect(smartypants(`--og
  • --check
  • --resume

    `)) .toBe(`--og
  • --check
  • --resume

    `); }); test("dashes at the end of a run before a tag keep their dashes", () => { expect(smartypants(`

    wait --

    run --x

    `)) .toBe(`

    wait --

    run --x

    `); }); test("a quote after an image or an unknown tag still closes", () => { expect(smartypants(`

    "check out " today

    `)).toBe(`

    \u201ccheck out \u201d today

    `); expect(smartypants(`

    "see " here

    `)).toBe(`

    \u201csee \u201d here

    `); }); test("renders a document whose paragraph and heading open with a quote", () => { const html = render({ markdown: `# "Quoted" title\n\n"Hello," she said.\n` }).html; expect(html).toContain(`\u201cQuoted\u201d title`); expect(html).toContain(`

    \u201cHello,\u201d she said.

    `); expect(html).not.toContain(`\u201dQuoted`); expect(html).not.toContain(`\u201dHello`); }); test("does NOT touch HTML attribute values", () => { const out = smartypants(`link`); expect(out).toContain(`href="it's-a-test.html"`); }); test("curls quote entities in text and leaves them encoded inside tags", () => { const out = smartypants(`

    She said "hi" and it's 'ok'.

    a "b" c's`); expect(out).toBe(`

    She said \u201chi\u201d and it\u2019s \u2018ok\u2019.

    a "b" c's`); }); // A tag split differently than the browser splits it (a ">" inside a quoted // value) must not turn the entities after it into straight quotes: the // browser reads them as part of the attribute, where a straight quote would // end the value and start a live onerror attribute. test("never turns a quote entity into a straight quote", () => { const input = ` { // Prose like "try --verbose mode" should not turn -- into em dash const out = smartypants(`

    Try --verbose mode.

    `); // Since "--" is followed by a word char but not preceded by word/space, // it should remain intact. We're lenient here — acceptable either way. expect(out).toMatch(/--verbose|—verbose/); }); }); // ─── sanitizer ────────────────────────────────────────────── describe("sanitizeUntrustedHtml", () => { test("strips

    world

    `; const out = sanitizeUntrustedHtml(input); expect(out).not.toContain("hello

    "); expect(out).toContain("

    world

    "); }); test("strips `; expect(sanitizeUntrustedHtml(input)).not.toContain(" { const input = `click`; const out = sanitizeUntrustedHtml(input); expect(out).not.toContain("onclick"); expect(out).toContain("href=\"#\""); }); test("strips event handlers with mixed case (onClick, ONCLICK)", () => { const input1 = `a`; const input2 = `b`; expect(sanitizeUntrustedHtml(input1)).not.toContain("onClick"); expect(sanitizeUntrustedHtml(input2)).not.toContain("ONCLICK"); }); test("rewrites javascript: URLs in href to #", () => { const input = `bad`; const out = sanitizeUntrustedHtml(input); expect(out).not.toContain("javascript:"); expect(out).toContain('href="#"'); }); test("strips inline SVG `; const out = sanitizeUntrustedHtml(input); expect(out).not.toContain(", , , , ,
    ", () => { const input = `
    `; const out = sanitizeUntrustedHtml(input); expect(out).not.toContain(" { const input = `
    hi
    `; expect(sanitizeUntrustedHtml(input)).not.toContain("srcdoc"); }); }); // ─── end-to-end render ────────────────────────────────────────────── describe("render (end-to-end)", () => { test("produces a full HTML document with title, body, and CSS", () => { const result = render({ markdown: `# Hello\n\nA paragraph with "quotes" and -- dashes.\n`, }); expect(result.html).toContain(""); expect(result.html).toContain("Hello"); expect(result.html).toContain("... expect(result.html).toMatch(/\n\n# Hello\n\nBody.\n` }); const chapters = result.html.match(/class="chapter"/g) ?? []; // One chapter, not two — the invisible "); expect(result.html).toMatch(/