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

wordsHast.test.ts

403 lines20.8 KB

1import type { Element, Root } from "hast";
2import { toHtml } from "hast-util-to-html";
3import { describe, expect, it } from "vite-plus/test";
4​
5import {
6 codexDirectiveSources,
7 markdownReferenceDefinitions,
8 wordsHast,
9 type WordsHastOptions,
10} from "./wordsHast";
11​
12const ASSISTANT: WordsHastOptions = {
13 math: false,
14 breaks: false,
15 escapeHtml: false,
16 insights: false,
17 extensions: {},
18};
19​
20const wordsHtml = (markdown: string, options: Partial<WordsHastOptions> = {}) =>
21 toHtml(wordsHast(markdown, { ...ASSISTANT, ...options }));
22​
23// The chat's syntax in the shapes agents and review bots write it, and raw HTML, with the HTML
24// remark and the chat's plugins rendered for it before Words replaced them.
25const CASES: ReadonlyArray<readonly [markdown: string, html: string]> = [
26 [
27 "| a | b |\n|:-|-:|\n| `x \\| y` | **2** |\n| 3 |",
28 '<table dir="ltr">\n<thead>\n<tr>\n<th align="left">a</th>\n<th align="right">b</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td align="left"><code data-inline-code="" dir="ltr">x | y</code></td>\n<td align="right"><strong>2</strong></td>\n</tr>\n<tr>\n<td align="left">3</td>\n<td align="right"></td>\n</tr>\n</tbody>\n</table>',
29 ],
30 [
31 "a | b\n--- | ---\n1 | 2\n\nnot a table",
32 '<table dir="ltr">\n<thead>\n<tr>\n<th>a</th>\n<th>b</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>1</td>\n<td>2</td>\n</tr>\n</tbody>\n</table>\n<p dir="ltr">not a table</p>',
33 ],
34 [
35 "- [ ] open\n- [x] done\n- [X] spaced",
36 '<ul class="contains-task-list" dir="ltr">\n<li class="task-list-item"><input type="checkbox" disabled> open</li>\n<li class="task-list-item"><input type="checkbox" checked disabled> done</li>\n<li class="task-list-item"><input type="checkbox" checked disabled> spaced</li>\n</ul>',
37 ],
38 [
39 "- [ ] open\n\n- [x] done",
40 '<ul class="contains-task-list" dir="ltr">\n<li class="task-list-item">\n<p><input type="checkbox" disabled> open</p>\n</li>\n<li class="task-list-item">\n<p><input type="checkbox" checked disabled> done</p>\n</li>\n</ul>',
41 ],
42 [
43 "See www.example.com/a_(b), https://x.dev/path?q=1. and me@example.com!",
44 '<p dir="ltr">See <span class="has-[[data-slot=context-chip-label]]:whitespace-nowrap"><a href="http://www.example.com/a_(b)">www.example.com/a_(b)</a>,</span> <span class="has-[[data-slot=context-chip-label]]:whitespace-nowrap"><a href="https://x.dev/path?q=1">https://x.dev/path?q=1</a>.</span> and <span class="has-[[data-slot=context-chip-label]]:whitespace-nowrap"><a href="mailto:me@example.com">me@example.com</a>!</span></p>',
45 ],
46 [
47 "See https://x.com/dimasike_ and 可以看了:**http://localhost:18780**(已重启)。",
48 '<p dir="ltr">See <a href="https://x.com/dimasike_">https://x.com/dimasike_</a> and 可以看了:<strong><a href="http://localhost:18780">http://localhost:18780</a></strong>(已重启)。</p>',
49 ],
50 [
51 "Angle <https://angle.dev> and (https://paren.dev/x)",
52 '<p dir="ltr">Angle <a href="https://angle.dev">https://angle.dev</a> and (<span class="has-[[data-slot=context-chip-label]]:whitespace-nowrap"><a href="https://paren.dev/x">https://paren.dev/x</a>)</span></p>',
53 ],
54 [
55 "Footnote[^1] and again[^1], unknown[^2].\n\n[^1]: The note\n continues.",
56 '<p dir="ltr">Footnote<sup><a href="#user-content-fn-1" id="user-content-user-content-fnref-1" data-footnote-ref="" aria-describedby="user-content-footnote-label">1</a></sup> and again<sup><a href="#user-content-fn-1" id="user-content-user-content-fnref-1-2" data-footnote-ref="" aria-describedby="user-content-footnote-label">1</a></sup>, unknown[^2].</p>\n<section data-footnotes="" class="footnotes" dir="ltr"><h2 class="sr-only" id="user-content-footnote-label">Footnotes</h2>\n<ol>\n<li id="user-content-user-content-fn-1">\n<p>The note\ncontinues. <a href="#user-content-fnref-1" data-footnote-backref="" aria-label="Back to reference 1" class="data-footnote-backref">↩</a> <a href="#user-content-fnref-1-2" data-footnote-backref="" aria-label="Back to reference 1-2" class="data-footnote-backref">↩<sup>2</sup></a></p>\n</li>\n</ol>\n</section>',
57 ],
58 [
59 "> [!NOTE]\n> An alert\n\n> [!warning] lower\n> case",
60 '<blockquote data-alert="note" dir="ltr">\n<p>An alert</p>\n</blockquote>\n<blockquote dir="ltr">\n<p>[!warning] lower\ncase</p>\n</blockquote>',
61 ],
62 [
63 "- wide\n- [x](./x.ts) items\n\ntext\n\n- loose\n\n- items",
64 '<ul dir="ltr">\n<li>wide</li>\n<li><a href="./x.ts">x</a> items</li>\n</ul>\n<p dir="ltr">text</p>\n<ul dir="ltr">\n<li>\n<p>loose</p>\n</li>\n<li>\n<p>items</p>\n</li>\n</ul>',
65 ],
66 [
67 "> [!TIP]**joined**",
68 '<blockquote dir="ltr">\n<p>[!TIP]<strong>joined</strong></p>\n</blockquote>',
69 ],
70 [
71 "1. one\r\n2. two\r\n\r\n loose\r\n",
72 '<ol dir="ltr">\n<li>\n<p>one</p>\n</li>\n<li>\n<p>two</p>\n<p>loose</p>\n</li>\n</ol>',
73 ],
74 [
75 "&copy; &#x1F600; &bogus; &#0; \\& *a **b** c*",
76 '<p dir="ltr">© 😀 &#x26;bogus; � &#x26; <em>a <strong>b</strong> c</em></p>',
77 ],
78 [
79 "Hard \nbreak\\\nand `` code ` span ``",
80 '<p dir="ltr">Hard<br>\nbreak<br>\nand <code data-inline-code="" dir="ltr">code ` span</code></p>',
81 ],
82 [
83 '[ref] and [Ref][] and [x][REF]\n\n[ref]: /url "title"',
84 '<p dir="ltr"><a href="/url">ref</a> and <a href="/url">Ref</a> and <a href="/url">x</a></p>',
85 ],
86 [
87 "Open [a.ts](./a.ts) and /Users/me/b.ts:3. Run `src/b.ts`.",
88 '<p dir="ltr">Open <a href="./a.ts">a.ts</a> and <span class="has-[[data-slot=context-chip-label]]:whitespace-nowrap"><a href="/Users/me/b.ts:3">/Users/me/b.ts:3</a>.</span> Run <span class="has-[[data-slot=context-chip-label]]:whitespace-nowrap"><code data-inline-code="" dir="ltr">src/b.ts</code>.</span></p>',
89 ],
90 [
91 "[a](CURSOR://file/Users/me/a.md:3) [b](vscode://file/C:/a.ts) [c](cursor://anysphere.x/p)",
92 '<p dir="ltr"><a href="cursor://file/Users/me/a.md:3">a</a> <a href="vscode://file/C:/a.ts">b</a> <a href="cursor://anysphere.x/p">c</a></p>',
93 ],
94 [
95 "![one](https://x.dev/1.png)\n![two](https://x.dev/2.png)\nText\n![three](https://x.dev/3.png)",
96 '<p dir="ltr"><img src="https://x.dev/1.png" alt="one"><br>\n<img src="https://x.dev/2.png" alt="two"><br>\nText<br>\n<img src="https://x.dev/3.png" alt="three"></p>',
97 ],
98 [
99 'Read :codex-file-citation[a.ts]{path="src/a.ts" line_range_start="3"} and [x :codex-file-citation[b]{path="b.ts"}](u).',
100 '<p dir="ltr">Read <a href="src/a.ts#L3">a.ts</a> and <span class="has-[[data-slot=context-chip-label]]:whitespace-nowrap"><a href="u">x :codex-file-citation[b]{path="b.ts"}</a>.</span></p>',
101 ],
102 [
103 ':codex-followup[Ask]{prompt="go"} and :codex-followup[No prompt]{label="x"} stay apart.',
104 '<p dir="ltr"><span>Ask</span> and :codex-followup[No prompt]{label="x"} stay apart.</p>',
105 ],
106 [
107 '::artifact-template{artifact_kind="document" display_name="Doc" skill_directory="/s" skill_name="artifact-template-doc"}\n\n::artifact-template{bad="x"}',
108 '<div data-codex-artifact-template="true" data-artifact-kind="document" data-display-name="Doc" data-skill-directory="/s" data-skill-name="artifact-template-doc"></div>\n<p dir="ltr">::artifact-template{bad="x"}</p>',
109 ],
110 [
111 "[file](C:\\work\\a.ts) and C:\\Users\\me\\.t3\\shot.png and \\*kept\\*",
112 '<p dir="ltr"><a href="file:///C:/work/a.ts">file</a> and C:\\Users\\me\\.t3\\shot.png and *kept*</p>',
113 ],
114 [
115 '```ts title="a.ts"\nconst a = 1;\n```',
116 '<pre><code class="language-ts" data-code-meta="title=&#x22;a.ts&#x22;">const a = 1;\n</code></pre>',
117 ],
118 [
119 "Cost ~5 and ~~gone~~ in an answer.",
120 '<p dir="ltr">Cost ~5 and <del>gone</del> in an answer.</p>',
121 ],
122 [
123 "<details>\n<summary>More</summary>\n\nHidden **bold**\n\n</details>",
124 '<details dir="ltr">\n<summary>More</summary>\n<p>Hidden <strong>bold</strong></p>\n</details>',
125 ],
126 [
127 'Inline <sup>1</sup> and <img src="a.png" alt="x"> html',
128 '<p dir="ltr">Inline <sup>1</sup> and <img src="a.png" alt="x"> html</p>',
129 ],
130 [
131 '<picture><source media="(prefers-color-scheme: dark)" srcset="d.png"><source media="(prefers-color-scheme: light)" srcset="l.png"><img src="l.png" alt="x"></picture>',
132 '<p><picture><img src="l.png" alt="x" data-dark-src="d.png" data-light-src="l.png"></picture></p>',
133 ],
134 [
135 "<details>\n<summary>⚠️ Outside diff (3)</summary><blockquote>\n\n**a**\n\n</blockquote></details>",
136 '<details dir="ltr">\n<summary>⚠️ Outside diff (3)</summary><blockquote>\n<p><strong>a</strong></p>\n</blockquote></details>',
137 ],
138 [
139 '<div align="center"><img width="837" src="https://x.dev/a.png" title="t" /></div>\n\nafter',
140 '<div align="center"><img width="837" src="https://x.dev/a.png" data-markdown-title="t" data-standalone></div>\n<p dir="ltr">after</p>',
141 ],
142 [
143 'Text <sup>[1](#n)</sup> <sub>x</sub> <br> <a href="https://x.dev" target="_blank">link</a>',
144 '<p dir="ltr">Text <sup><a href="#n">1</a></sup> <sub>x</sub> <br> <a href="https://x.dev">link</a></p>',
145 ],
146 ["<!-- hidden -->\nVisible <!-- inline --> text", '\n<p dir="ltr">Visible text</p>'],
147 [
148 '<custom-tag>kept text</custom-tag> and <a href="javascript:x">bad</a>',
149 '<p dir="ltr">kept text and <a>bad</a></p>',
150 ],
151 [
152 "> <details>\n> <summary>quoted</summary>\n>\n> body\n> </details>",
153 '<blockquote dir="ltr">\n<details>\n<summary>quoted</summary>\n<p>body</p>\n</details>\n</blockquote>',
154 ],
155 [
156 '<h3 id="x">Raw heading</h3>\n\n## Markdown heading',
157 '<h3 id="user-content-x" dir="ltr">Raw heading</h3>\n<h2 id="user-content-markdown-heading" dir="ltr">Markdown heading</h2>',
158 ],
159];
160​
161describe("wordsHast", () => {
162 it.each(CASES)("renders %j as the remark pipeline did", (markdown, html) => {
163 expect(wordsHtml(markdown)).toBe(html);
164 });
165​
166 it("breaks soft lines as remark-breaks did", () => {
167 const markdown = "one\ntwo **three\nfour**\n\n- item\n next\n - sub\n\n```\nkept\nlines\n```";
168 expect(wordsHtml(markdown, { breaks: true })).toBe(
169 '<p dir="ltr">one<br>\ntwo <strong>three<br>\nfour</strong></p>\n<ul dir="ltr">\n<li>item<br>\nnext\n<ul>\n<li>sub</li>\n</ul>\n</li>\n</ul>\n<pre><code>kept\nlines\n</code></pre>',
170 );
171 });
172​
173 it("keeps the composer's tildes and list numbers in user messages", () => {
174 const markdown = "Install as ~/a/x, then (~/b.md). ~one~ ~~two~~\n\n3. three\n5. five";
175 expect(wordsHtml(markdown, { extensions: { userMessage: true } })).toBe(
176 '<p dir="ltr">Install as ~/a/x, then (~/b.md). ~one~ <del>two</del></p>\n<ol start="3" dir="ltr">\n<li>three</li>\n<li value="5">five</li>\n</ol>',
177 );
178 // Typed indentation past a wide list marker counts from where the item's text starts.
179 expect(
180 wordsHtml("- wide\n continued", {
181 breaks: true,
182 extensions: { userMessage: true, leadingWhitespace: true },
183 }),
184 ).toBe(
185 '<ul dir="ltr">\n<li>wide<br>\n<span data-leading-whitespace=""> </span>continued</li>\n</ul>',
186 );
187 });
188​
189 it("gives elements the source positions components read", () => {
190 // A wide item's text is parsed again on its own, so its link has no place in the message.
191 const markdown = "é [a](u)\n\n- [ ] task\n\n```sh\nls\n```\n\n- [b](./b.ts)";
192 const tree = wordsHast(markdown, ASSISTANT);
193 const slices: string[] = [];
194 const visit = (node: Root | Element) => {
195 for (const child of node.children) {
196 if (child.type !== "element") continue;
197 if (child.position) {
198 slices.push(markdown.slice(child.position.start.offset, child.position.end.offset));
199 }
200 visit(child);
201 }
202 };
203 visit(tree);
204 expect(slices).toEqual(["[a](u)", "- [ ] task", "```sh\nls\n```", "- [b](./b.ts)"]);
205 });
206​
207 it("renders insight blocks as remarkAssistantInsights did", () => {
208 const markdown =
209 "Intro\n\n`★ Insight ─────────────────────────────────────`\n- one\n- two\n`─────────────────────────────────────────────────`\n\nAfter\n\n```\n★ Insight ─────\n```";
210 expect(wordsHtml(markdown, { insights: true })).toBe(
211 '<p dir="ltr">Intro</p>\n<blockquote data-alert="insight" dir="ltr">\n<ul>\n<li>one</li>\n<li>two</li>\n</ul>\n</blockquote>\n<p dir="ltr">After</p>\n<pre><code>★ Insight ─────\n</code></pre>',
212 );
213 // Code in a list, an HTML block and a quote's lazy line are not where an insight opens.
214 expect(
215 wordsHtml(
216 "- x\n ```\n ★ Insight ─────\n ```\n\n<div>\n★ Insight ─────\n</div>\n\n> q\n★ Insight ─────",
217 {
218 insights: true,
219 },
220 ),
221 ).toBe(
222 '<ul dir="ltr">\n<li>x\n<pre><code>★ Insight ─────\n</code></pre>\n</li>\n</ul>\n<div dir="ltr">\n★ Insight ─────\n</div>\n<blockquote dir="ltr">\n<p>q\n★ Insight ─────</p>\n</blockquote>',
223 );
224 // What follows an opening keeps its offsets in the message, which components slice.
225 const insight = "★ Insight ─────\nSee [a](./a.ts)\n```sh\nls\n```\n─────";
226 const tree = wordsHast(insight, { ...ASSISTANT, insights: true });
227 const quote = tree.children.find((child): child is Element => child.type === "element")!;
228 const sliced = (node: Element) =>
229 insight.slice(node.position!.start.offset, node.position!.end.offset);
230 const [paragraph, pre] = quote.children.filter((child) => child.type === "element");
231 const link = paragraph!.children.find((child): child is Element => child.type === "element")!;
232 expect([sliced(link), sliced(pre!)]).toEqual(["[a](./a.ts)", "```sh\nls\n```"]);
233 });
234​
235 it("shows raw HTML as its text when asked", () => {
236 expect(wordsHtml("a <b>x</b> and <https://a.dev>", { escapeHtml: true })).toBe(
237 '<p dir="ltr">a &#x3C;b>x&#x3C;/b> and <a href="https://a.dev">https://a.dev</a></p>',
238 );
239 });
240​
241 it("flattens a preview to one line of text and math, within its length", () => {
242 const preview = { fallback: "Thinking", maxLength: 24 };
243 const options = { math: true, extensions: { preview } };
244 expect(wordsHtml("# Plan\n\n- **one** <b>x</b>\n- `two` $a$", options)).toMatch(
245 /^Plan one x two <span[^>]* data-markdown-math="inline"[^>]*><svg [^>]*aria-label="a"/,
246 );
247 expect(wordsHtml("x".repeat(30), options)).toBe("x".repeat(24));
248 expect(wordsHtml("<!-- only -->", options)).toBe("Thinking");
249 });
250​
251 it("makes a saved file's Markdown inert", () => {
252 expect(
253 wordsHtml(
254 "[a](https://x.dev) ![b](https://x.dev/i.png) `c` <img src=y>\n\n```mermaid\nA\n```",
255 {
256 extensions: { inert: true },
257 },
258 ),
259 ).toBe('<p dir="ltr">a b c </p>\n<pre><code class="language-text">A\n</code></pre>');
260 });
261​
262 it("links a change request's references and resolves its root-relative media", () => {
263 const autolinks = (text: string) =>
264 [...text.matchAll(/#(\d+)/g)].map((match) => ({
265 index: match.index,
266 text: match[0],
267 label: match[0],
268 url: `https://forge.dev/o/r/pull/${match[1]}`,
269 kind: "reference" as const,
270 }));
271 const html = wordsHtml("See #12 and `#13`.\n\n![shot](/api/attachments/1)", {
272 extensions: { changeRequest: { repositoryUrl: "https://forge.dev/o/r", autolinks } },
273 });
274 expect(html).toContain(
275 '<a href="https://forge.dev/o/r/pull/12" data-pull-request-autolink="reference">#12</a>',
276 );
277 expect(html).toContain('<code data-inline-code="" dir="ltr">#13</code>');
278 expect(html).toContain('src="https://forge.dev/api/attachments/1"');
279 });
280​
281 it("renders math as SVG, and keeps money as text", () => {
282 const html = toHtml(
283 wordsHast("Costs $34.58 and $15.00, and $x^2 \\text{ for } n$ is math.\n\n$$\na\n$$", {
284 ...ASSISTANT,
285 math: true,
286 }),
287 );
288 expect(html).toContain("Costs $34.58 and $15.00, and ");
289 expect(html.match(/<svg /g)).toHaveLength(2);
290 expect(html).toContain('aria-label="x^2 \\text{ for } n"');
291 expect(html).toContain('style="white-space:pre"> for </text>');
292 });
293​
294 it("keeps control bytes, blank lines, unclosed blocks, shell vars, and table dollars intact in math", () => {
295 const options = { ...ASSISTANT, math: true };
296 expect(toHtml(wordsHast("Pasted: $a\u001bb + c$", options))).toContain("<svg ");
297 expect(toHtml(wordsHast("$$\na = 1 \\\\\n\nb = 2\n$$", options))).toContain("<svg ");
298 expect(toHtml(wordsHast("$$\n\\frac{a}{b}", options))).toBe(
299 '<p dir="ltr">$$\n\\frac{a}{b}</p>',
300 );
301 expect(toHtml(wordsHast("```math\nx^2", options))).toBe(
302 '<pre><code class="language-math">x^2\n</code></pre>',
303 );
304 expect(toHtml(wordsHast("Set $HOME,$PATH and $FOO to `$BAR`.", options))).toBe(
305 '<p dir="ltr">Set $HOME,$PATH and $FOO to <span class="has-[[data-slot=context-chip-label]]:whitespace-nowrap"><code data-inline-code="" dir="ltr">$BAR</code>.</span></p>',
306 );
307 expect(
308 toHtml(
309 wordsHast("| [a](https://example.com/$x) | `$x` | y$ |\n| --- | --- | --- |", options),
310 ),
311 ).toContain("<table ");
312 });
313​
314 it.each([
315 [
316 "Status ![badge](https://x.dev/b.png)\ncontinues",
317 '<p dir="ltr">Status <img src="https://x.dev/b.png" alt="badge">\ncontinues</p>',
318 '<p dir="ltr">Status <img src="https://x.dev/b.png" alt="badge"><br>\ncontinues</p>',
319 ],
320 [
321 "**Label**\n[![shot](https://x.dev/b.png)](https://x.dev)\ncontinues",
322 '<p dir="ltr"><strong>Label</strong><br>\n<a href="https://x.dev"><img src="https://x.dev/b.png" alt="shot"></a><br>\ncontinues</p>',
323 '<p dir="ltr"><strong>Label</strong><br>\n<a href="https://x.dev"><img src="https://x.dev/b.png" alt="shot"></a><br>\ncontinues</p>',
324 ],
325 [
326 'Status\n<img src="https://x.dev/b.png" />\ncontinues',
327 '<p dir="ltr">Status\n<img src="https://x.dev/b.png">\ncontinues</p>',
328 '<p dir="ltr">Status<br>\n<img src="https://x.dev/b.png"><br>\ncontinues</p>',
329 ],
330 [
331 "Hard \nbreak and a\\\nbackslash",
332 '<p dir="ltr">Hard<br>\nbreak and a<br>\nbackslash</p>',
333 '<p dir="ltr">Hard<br>\nbreak and a<br>\nbackslash</p>',
334 ],
335 ])(
336 "breaks lines next to images and hard breaks as remark did: %j",
337 (markdown, html, withBreaks) => {
338 expect(wordsHtml(markdown)).toBe(html);
339 expect(wordsHtml(markdown, { breaks: true })).toBe(withBreaks);
340 },
341 );
342​
343 it("reads raw HTML attributes as an HTML parser does", () => {
344 expect(wordsHtml('<a href="https://x.dev/?q=1&copy=2&amp;b">x</a>')).toContain(
345 'href="https://x.dev/?q=1&#x26;copy=2&#x26;b"',
346 );
347 });
348​
349 it("treats only Words' own directives as directives", () => {
350 const forged =
351 '<span data-directive="codex-followup" data-attributes="null">Ask</span> and <div data-directive="artifact-template" data-attributes="[1]"></div>';
352 expect(wordsHtml(forged)).toBe('<p dir="ltr"><span>Ask</span> and </p><div></div><p></p>');
353 // Raw HTML can write a source position too, even the one Words gave a real directive.
354 const positioned = `<span data-directive="codex-file-citation" data-attributes='{"path":"/etc/passwd"}' data-sourcepos="0-5">x</span>`;
355 expect(wordsHtml(positioned)).toBe('<p dir="ltr"><span>x</span></p>');
356 const real = ':codex-file-citation[a]{path="a.ts"}';
357 expect(
358 wordsHtml(
359 `${real} <span data-directive="codex-file-citation" data-attributes='{"path":"/etc/passwd"}' data-sourcepos="0-${real.length}">x</span>`,
360 ),
361 ).not.toContain("/etc/passwd");
362 });
363​
364 it("reads a directive's label as written", () => {
365 expect(
366 codexDirectiveSources(':codex-followup[pick {a} or [b]]{prompt="go"}').map(
367 (directive) => directive.label,
368 ),
369 ).toEqual(["pick {a} or [b]"]);
370 });
371​
372 it.each([
373 ["# Title\n[a]: https://x.dev", ["[a]: <https://x.dev>"]],
374 ['> [a]:\n> https://x.dev "The\n> guide"', ['[a]: <https://x.dev> "The guide"']],
375 ["- [a]: https://x.dev\n\n[A]: https://second.dev", ["[a]: <https://x.dev>"]],
376 ["<div>\n[a]: https://x.dev\n</div>", []],
377 ["- ```\n [a]: https://x.dev\n ```", []],
378 ["text\n[a]: https://x.dev", []],
379 ])("finds the reference definitions in %j as Words reads them", (markdown, definitions) => {
380 expect(markdownReferenceDefinitions(markdown)).toEqual(definitions);
381 });
382​
383 it("keeps authored SVG and ids out of the math it restores", () => {
384 const html = wordsHtml(
385 '<svg width="99999" viewBox="0 0 1 1"><rect x="0" y="0" width="1" height="1"/></svg> <svg data-sourcepos="0-3" height="99999"></svg> <span id="t3-math-0">kept</span> $x$',
386 { math: true },
387 );
388 expect(html).not.toContain("99999");
389 expect(html).toContain("kept");
390 expect(html.match(/<svg /g)).toHaveLength(1);
391 });
392​
393 it("keeps only math's own SVG elements and attributes", () => {
394 const html = toHtml(
395 wordsHast(
396 '<svg onload="x()" style="position:fixed;display:inline"><a href="javascript:x()"><text x="1">t</text></a><script>x()</script></svg> $y$',
397 { ...ASSISTANT, math: true },
398 ),
399 );
400 expect(html).not.toMatch(/onload|javascript|position|<a |<script/);
401 expect(html).toContain('aria-label="y"');
402 });
403});