SPB Git

Repository Public

renderer.inline.test.tsx 6.1 KB 115 lines tsx

1import { describe, expect, it } from "vitest";2import { renderToStaticMarkup } from "react-dom/server";3import { Inline, inlineToPlain, parseInline, safeHref } from "@/renderer/inline";4import { checkLatex, latexToHtml } from "@/renderer/latex";5import type { Source } from "@/schema";67const sources = [{ id: "a", type: "web", title: "A", authors: [], status: "unverified" }, { id: "b-2", type: "web", title: "B", authors: [], status: "unverified" }] as Source[];8const html = (text: string) => renderToStaticMarkup(<Inline text={text} sources={sources} />);910describe("inline parser", () => {11  it("parses the documented subset", () => {12    expect(parseInline("a **b** *c* `d` $e^2$ [^a]")).toEqual([13      { kind: "text", text: "a " }, { kind: "bold", children: [{ kind: "text", text: "b" }] }, { kind: "text", text: " " },14      { kind: "italic", children: [{ kind: "text", text: "c" }] }, { kind: "text", text: " " }, { kind: "code", text: "d" }, { kind: "text", text: " " },15      { kind: "math", latex: "e^2" }, { kind: "text", text: " " }, { kind: "cite", id: "a" },16    ]);17  });18  it("nests emphasis and keeps code verbatim", () => {19    expect(html("**gras et *italique***")).toBe("<strong>gras et <em>italique</em></strong>");20    expect(html("`**pas gras**`")).toBe('<code class="sh-icode">**pas gras**</code>');21  });22  it("leaves unmatched markers and prices alone", () => {23    expect(html("2 * 3 * 4")).toBe("2 * 3 * 4");24    expect(html("5 $ et 10 $")).toBe("5 $ et 10 $");25    expect(html("coût: $5 et $10")).toBe("coût: $5 et $10");26    expect(html("a ** b")).toBe("a ** b");27  });28  it("renders https and mailto links as external links", () => {29    const out = html("[doc](https://example.org/a?b=1)");30    expect(out).toContain('href="https://example.org/a?b=1"');31    expect(out).toContain('rel="noopener noreferrer"');32    expect(out).toContain('target="_blank"');33    expect(html("[moi](mailto:a@example.org)")).toContain('href="mailto:a@example.org"');34  });35  it("refuses every other scheme", () => {36    for (const url of ["javascript:alert(1)", "JaVaScRiPt:alert(1)", "http://example.org", "data:text/html,x", "//example.org", "/relative", "vbscript:x", "https://exa mple.org", " javascript:alert(1)"]) {37      expect(safeHref(url)).toBeNull();38      const out = html(`[clic](${url})`);39      expect(out).not.toContain("<a");40      expect(out).not.toContain("href");41    }42  });43  it("shows HTML as text (XSS attempts stay text)", () => {44    for (const attack of ['<script>alert(1)</script>', '<img src=x onerror=alert(1)>', '"><svg onload=alert(1)>', "<a href=\"javascript:alert(1)\">x</a>", "**<b onclick=x>**"]) {45      const out = html(attack);46      expect(out).not.toMatch(/<(script|img|svg|a|b)\b/);47      expect(out).toContain("&lt;");48    }49  });50  it("numbers citations from the sources and drops unknown ids", () => {51    expect(html("x[^b-2]")).toBe('x<sup class="sh-cite"><a href="#src-b-2" aria-label="Source 2">2</a></sup>');52    expect(html("x[^nope]")).toBe("x");53    expect(renderToStaticMarkup(<Inline text="x[^a]" />)).toBe("x");54  });55  it("renders inline math with KaTeX and MathML", () => {56    const out = html("soit $x^2$");57    expect(out).toContain('class="katex"');58    expect(out).toContain("<math");59  });60  it("gives plain text for SVG labels", () => {61    expect(inlineToPlain("**a** [b](https://e.org) `c`[^a] $d$")).toBe("a b c d");62  });63});6465describe("latex", () => {66  it("accepts valid formulas", () => {67    expect(checkLatex("\\frac{a}{b} + \\sum_{i=1}^n x_i")).toEqual({ ok: true });68    expect(checkLatex("\\begin{aligned} a &= b \\\\ c &= d \\end{aligned}")).toEqual({ ok: true });69  });70  it("reports syntax errors", () => {71    const r = checkLatex("\\frac{a}{");72    expect(r.ok).toBe(false);73    if (!r.ok) expect(r.error.length).toBeGreaterThan(3);74    expect(checkLatex("  ").ok).toBe(false);75    expect(checkLatex("\\notacommand{x}").ok).toBe(false);76  });77  it("never trusts \\href or \\url and never throws when rendering", () => {78    const out = latexToHtml("\\href{javascript:alert(1)}{x}", false);79    expect(out).not.toContain("href=");80    // A formula KaTeX rejects yields "": MathView then shows a readable text form, never the red source.81    expect(latexToHtml("\\frac{", true)).toBe("");82    expect(latexToHtml("$\\sigma_t$", false)).toContain("katex");83    expect(latexToHtml("\\def\\a{\\a\\a}\\a", false)).not.toBe(null);84  });85});8687import { latexToUnicode, plainMath, prettyMath, stripMathDelims } from "@/renderer/mathtext";88import { parseInline as parseInline2, inlineToPlain as toPlain2 } from "@/renderer/inline";8990describe("maths as text", () => {91  it("removes the delimiters authors leave around LaTeX fields", () => {92    expect(stripMathDelims("$E$")).toBe("E");93    expect(stripMathDelims("$$a+b$$")).toBe("a+b");94    expect(stripMathDelims("\\(x\\)")).toBe("x");95    expect(stripMathDelims("$a$ and $b$")).toBe("$a$ and $b$");96  });97  it("writes a formula in readable Unicode", () => {98    expect(latexToUnicode("\\bar{\\sigma}^2 = \\frac{\\omega}{1-\\alpha-\\beta}")).toBe("σ̄² = ω/(1-α-β)");99    expect(latexToUnicode("\\lVert\\mathbf{u}-\\mathbf{v}\\rVert_2")).toBe("‖u-v‖₂");100    expect(latexToUnicode("q_i^{\\top}k_j/\\sqrt{d}")).toBe("qᵢᵀkⱼ/√d");101  });102  it("gives ASCII maths real sub- and superscripts and leaves identifiers alone", () => {103    expect(prettyMath("σ²_t = ω + α·r²_{t−1}")).toBe("σ²ₜ = ω + α·r²ₜ₋₁");104    expect(prettyMath("top_k, a_previous, snake_case")).toBe("top_k, a_previous, snake_case");105    expect(plainMath("depends only on $k$; costs 5 $ and 10 $")).toBe("depends only on k; costs 5 $ and 10 $");106  });107  it("parses $$…$$, \\(…\\) and \\[…\\] as formulas", () => {108    expect(parseInline2("a $$x^2$$ b")).toEqual([{ kind: "text", text: "a " }, { kind: "math", latex: "x^2", display: true }, { kind: "text", text: " b" }]);109    expect(parseInline2("\\(x_t\\)")).toEqual([{ kind: "math", latex: "x_t", display: false }]);110    expect(parseInline2("\\[a = b\\]")).toEqual([{ kind: "math", latex: "a = b", display: true }]);111    expect(parseInline2("\\[not a link\\]")).toEqual([{ kind: "text", text: "[not a link]" }]);112    expect(toPlain2("with $\\sigma_t^2$")).toBe("with σₜ²");113  });114});115
Files main
Menu

Theme