Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

MermaidDiagram.browser.test.tsx

66 lines3.2 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { sanitizeMermaidSvg } from "./MermaidDiagram";
4​
5// The label markup Mermaid's sequence diagrams emit for $$...$$ text.
6function noteSvg(label: string) {
7 return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><foreignObject height="34" width="35" x="57" y="85"><div style="width: fit-content;" xmlns="http://www.w3.org/1999/xhtml"><div style="display: flex; align-items: center; justify-content: center; white-space: nowrap;"><span class="katex">${label}</span></div></div></foreignObject></svg>`;
8}
9​
10function sanitize(svg: string) {
11 const host = document.createElement("div");
12 host.innerHTML = sanitizeMermaidSvg(svg);
13 return host;
15​
16describe("sanitizeMermaidSvg", () => {
17 it("keeps the MathML Mermaid renders for math labels", () => {
18 const host = sanitize(
19 noteSvg(
20 '<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow><mfrac><mi>d</mi><mrow><mi>d</mi><mi>x</mi></mrow></mfrac><mrow><mo fence="true">(</mo><mi mathvariant="bold">v</mi><mo fence="true">)</mo></mrow><msqrt><mn>2</mn></msqrt></mrow></math>',
21 ),
22 );
23​
24 const math = host.querySelector(".katex > math");
25 expect(math?.namespaceURI).toBe("http://www.w3.org/1998/Math/MathML");
26 expect(math?.getAttribute("display")).toBe("block");
27 expect(math?.querySelector("mfrac")?.textContent).toBe("ddx");
28 expect(math?.querySelector("msqrt")?.textContent).toBe("2");
29 expect(math?.querySelector("mi[mathvariant='bold']")?.textContent).toBe("v");
30 expect(math?.querySelectorAll("mo[fence='true']")).toHaveLength(2);
31 });
32​
33 it("keeps spaced math labels parseable as an SVG image", () => {
34 // KaTeX renders \text{total cost} with a no-break space.
35 const svg = sanitizeMermaidSvg(noteSvg("<math><mrow><mtext>total cost</mtext></mrow></math>"));
36​
37 const image = new DOMParser().parseFromString(svg, "image/svg+xml");
38 expect(image.documentElement.localName).toBe("svg");
39 expect(image.querySelector("mtext")?.textContent).toBe("total cost");
40 });
41​
42 it("strips links, scripts, and MathML outside what KaTeX emits", () => {
43 const host = sanitize(
44 noteSvg(
45 '<math href="javascript:alert(1)"><mi href="javascript:alert(1)" onclick="alert(1)">x</mi><maction actiontype="toggle"><mi>a</mi></maction><mglyph src="https://example.com/a.png"></mglyph><annotation-xml encoding="text/html"><img src="x" onerror="alert(1)"></annotation-xml><semantics><mi>y</mi><annotation encoding="application/x-tex">y</annotation></semantics></math>',
46 ),
47 );
48​
49 const math = host.querySelector("math");
50 expect(math).not.toBeNull();
51 expect(math?.querySelector("mi")?.textContent).toBe("x");
52 expect(host.querySelector("[href], [onclick], [src], [onerror]")).toBeNull();
53 expect(host.querySelector("maction, mglyph, annotation-xml, annotation, semantics, img")).toBe(
54 null,
55 );
56 });
57​
58 it("only accepts math as an HTML label and keeps its attributes on MathML", () => {
59 const host = sanitize(
60 '<svg xmlns="http://www.w3.org/2000/svg"><math><mi>x</mi></math><text mathvariant="bold" fence="true">label</text></svg>',
61 );
62​
63 expect(host.querySelector("math, mi")).toBeNull();
64 expect(host.querySelector("text")?.getAttributeNames()).toEqual([]);
65 });
66});