Open History to see the last commit
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 "© 😀 &bogus; � \\& *a **b** c*",
76 '<p dir="ltr">© 😀 &bogus; � & <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 "\n\nText\n",
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="a.ts"">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 const rawHtml =
172 '<p align="center">\n <img src="https://x.dev/a.png" width="400">\n</p>\n\n<ul>\n<li>\none\ntwo\n</li>\n</ul>';
173 expect(wordsHtml(rawHtml, { breaks: true })).toBe(
174 '<p align="center">\n <img src="https://x.dev/a.png" width="400" data-standalone>\n</p>\n<ul dir="ltr">\n<li>\none\ntwo\n</li>\n</ul>',
175 );
176 });
177
178 it("keeps the composer's tildes and list numbers in user messages", () => {
179 const markdown = "Install as ~/a/x, then (~/b.md). ~one~ ~~two~~\n\n3. three\n5. five";
180 expect(wordsHtml(markdown, { extensions: { userMessage: true } })).toBe(
181 '<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>',
182 );
183 // Typed indentation past a wide list marker counts from where the item's text starts.
184 expect(
185 wordsHtml("- wide\n continued", {
186 breaks: true,
187 extensions: { userMessage: true, leadingWhitespace: true },
188 }),
189 ).toBe(
190 '<ul dir="ltr">\n<li>wide<br>\n<span data-leading-whitespace=""> </span>continued</li>\n</ul>',
191 );
192 });
193
194 it("gives elements the source positions components read", () => {
195 // A wide item's text is parsed again on its own, so its link has no place in the message.
196 const markdown = "é [a](u)\n\n- [ ] task\n\n```sh\nls\n```\n\n- [b](./b.ts)";
197 const tree = wordsHast(markdown, ASSISTANT);
198 const slices: string[] = [];
199 const visit = (node: Root | Element) => {
200 for (const child of node.children) {
201 if (child.type !== "element") continue;
202 if (child.position) {
203 slices.push(markdown.slice(child.position.start.offset, child.position.end.offset));
204 }
205 visit(child);
206 }
207 };
208 visit(tree);
209 expect(slices).toEqual(["[a](u)", "- [ ] task", "```sh\nls\n```", "- [b](./b.ts)"]);
210 });
211
212 it("renders insight blocks as remarkAssistantInsights did", () => {
213 const markdown =
214 "Intro\n\n`★ Insight ─────────────────────────────────────`\n- one\n- two\n`─────────────────────────────────────────────────`\n\nAfter\n\n```\n★ Insight ─────\n```";
215 expect(wordsHtml(markdown, { insights: true })).toBe(
216 '<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>',
217 );
218 // Code in a list, an HTML block and a quote's lazy line are not where an insight opens.
219 expect(
220 wordsHtml(
221 "- x\n ```\n ★ Insight ─────\n ```\n\n<div>\n★ Insight ─────\n</div>\n\n> q\n★ Insight ─────",
222 {
223 insights: true,
224 },
225 ),
226 ).toBe(
227 '<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>',
228 );
229 // What follows an opening keeps its offsets in the message, which components slice.
230 const insight = "★ Insight ─────\nSee [a](./a.ts)\n```sh\nls\n```\n─────";
231 const tree = wordsHast(insight, { ...ASSISTANT, insights: true });
232 const quote = tree.children.find((child): child is Element => child.type === "element")!;
233 const sliced = (node: Element) =>
234 insight.slice(node.position!.start.offset, node.position!.end.offset);
235 const [paragraph, pre] = quote.children.filter((child) => child.type === "element");
236 const link = paragraph!.children.find((child): child is Element => child.type === "element")!;
237 expect([sliced(link), sliced(pre!)]).toEqual(["[a](./a.ts)", "```sh\nls\n```"]);
238 });
239
240 it("shows raw HTML as its text when asked", () => {
241 expect(wordsHtml("a <b>x</b> and <https://a.dev>", { escapeHtml: true })).toBe(
242 '<p dir="ltr">a <b>x</b> and <a href="https://a.dev">https://a.dev</a></p>',
243 );
244 });
245
246 it("flattens a preview to one line of text and math, within its length", () => {
247 const preview = { fallback: "Thinking", maxLength: 24 };
248 const options = { math: true, extensions: { preview } };
249 expect(wordsHtml("# Plan\n\n- **one** <b>x</b>\n- `two` $a$", options)).toMatch(
250 /^Plan one x two <span[^>]* data-markdown-math="inline"[^>]*><svg [^>]*aria-label="a"/,
251 );
252 expect(wordsHtml("x".repeat(30), options)).toBe("x".repeat(24));
253 expect(wordsHtml("<!-- only -->", options)).toBe("Thinking");
254 });
255
256 it("makes a saved file's Markdown inert", () => {
257 expect(
258 wordsHtml(
259 "[a](https://x.dev)  `c` <img src=y>\n\n```mermaid\nA\n```",
260 {
261 extensions: { inert: true },
262 },
263 ),
264 ).toBe('<p dir="ltr">a b c </p>\n<pre><code class="language-text">A\n</code></pre>');
265 });
266
267 it("links a change request's references and resolves its root-relative media", () => {
268 const autolinks = (text: string) =>
269 [...text.matchAll(/#(\d+)/g)].map((match) => ({
270 index: match.index,
271 text: match[0],
272 label: match[0],
273 url: `https://forge.dev/o/r/pull/${match[1]}`,
274 kind: "reference" as const,
275 }));
276 const html = wordsHtml("See #12 and `#13`.\n\n", {
277 extensions: { changeRequest: { repositoryUrl: "https://forge.dev/o/r", autolinks } },
278 });
279 expect(html).toContain(
280 '<a href="https://forge.dev/o/r/pull/12" data-pull-request-autolink="reference">#12</a>',
281 );
282 expect(html).toContain('<code data-inline-code="" dir="ltr">#13</code>');
283 expect(html).toContain('src="https://forge.dev/api/attachments/1"');
284 });
285
286 it("renders math as SVG, and keeps money as text", () => {
287 const html = toHtml(
288 wordsHast("Costs $34.58 and $15.00, and $x^2 \\text{ for } n$ is math.\n\n$$\na\n$$", {
289 ...ASSISTANT,
290 math: true,
291 }),
292 );
293 expect(html).toContain("Costs $34.58 and $15.00, and ");
294 expect(html.match(/<svg /g)).toHaveLength(2);
295 expect(html).toContain('aria-label="x^2 \\text{ for } n"');
296 expect(html).toContain('style="white-space:pre"> for </text>');
297 });
298
299 it("keeps control bytes, blank lines, unclosed blocks, shell vars, and table dollars intact in math", () => {
300 const options = { ...ASSISTANT, math: true };
301 expect(toHtml(wordsHast("Pasted: $a\u001bb + c$", options))).toContain("<svg ");
302 expect(toHtml(wordsHast("$$\na = 1 \\\\\n\nb = 2\n$$", options))).toContain("<svg ");
303 expect(toHtml(wordsHast("$$\n\\frac{a}{b}", options))).toBe(
304 '<p dir="ltr">$$\n\\frac{a}{b}</p>',
305 );
306 expect(toHtml(wordsHast("```math\nx^2", options))).toBe(
307 '<pre><code class="language-math">x^2\n</code></pre>',
308 );
309 expect(toHtml(wordsHast("Set $HOME,$PATH and $FOO to `$BAR`.", options))).toBe(
310 '<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>',
311 );
312 expect(
313 toHtml(
314 wordsHast("| [a](https://example.com/$x) | `$x` | y$ |\n| --- | --- | --- |", options),
315 ),
316 ).toContain("<table ");
317 });
318
319 it.each([
320 [
321 "Status \ncontinues",
322 '<p dir="ltr">Status <img src="https://x.dev/b.png" alt="badge">\ncontinues</p>',
323 '<p dir="ltr">Status <img src="https://x.dev/b.png" alt="badge"><br>\ncontinues</p>',
324 ],
325 [
326 "**Label**\n[](https://x.dev)\ncontinues",
327 '<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>',
328 '<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>',
329 ],
330 [
331 'Status\n<img src="https://x.dev/b.png" />\ncontinues',
332 '<p dir="ltr">Status\n<img src="https://x.dev/b.png">\ncontinues</p>',
333 '<p dir="ltr">Status<br>\n<img src="https://x.dev/b.png"><br>\ncontinues</p>',
334 ],
335 [
336 "Hard \nbreak and a\\\nbackslash",
337 '<p dir="ltr">Hard<br>\nbreak and a<br>\nbackslash</p>',
338 '<p dir="ltr">Hard<br>\nbreak and a<br>\nbackslash</p>',
339 ],
340 ])(
341 "breaks lines next to images and hard breaks as remark did: %j",
342 (markdown, html, withBreaks) => {
343 expect(wordsHtml(markdown)).toBe(html);
344 expect(wordsHtml(markdown, { breaks: true })).toBe(withBreaks);
345 },
346 );
347
348 it("reads raw HTML attributes as an HTML parser does", () => {
349 expect(wordsHtml('<a href="https://x.dev/?q=1©=2&b">x</a>')).toContain(
350 'href="https://x.dev/?q=1&copy=2&b"',
351 );
352 });
353
354 it("treats only Words' own directives as directives", () => {
355 const forged =
356 '<span data-directive="codex-followup" data-attributes="null">Ask</span> and <div data-directive="artifact-template" data-attributes="[1]"></div>';
357 expect(wordsHtml(forged)).toBe('<p dir="ltr"><span>Ask</span> and </p><div></div><p></p>');
358 // Raw HTML can write a source position too, even the one Words gave a real directive.
359 const positioned = `<span data-directive="codex-file-citation" data-attributes='{"path":"/etc/passwd"}' data-sourcepos="0-5">x</span>`;
360 expect(wordsHtml(positioned)).toBe('<p dir="ltr"><span>x</span></p>');
361 const real = ':codex-file-citation[a]{path="a.ts"}';
362 expect(
363 wordsHtml(
364 `${real} <span data-directive="codex-file-citation" data-attributes='{"path":"/etc/passwd"}' data-sourcepos="0-${real.length}">x</span>`,
365 ),
366 ).not.toContain("/etc/passwd");
367 });
368
369 it("reads a directive's label as written", () => {
370 expect(
371 codexDirectiveSources(':codex-followup[pick {a} or [b]]{prompt="go"}').map(
372 (directive) => directive.label,
373 ),
374 ).toEqual(["pick {a} or [b]"]);
375 });
376
377 it.each([
378 ["# Title\n[a]: https://x.dev", ["[a]: <https://x.dev>"]],
379 ['> [a]:\n> https://x.dev "The\n> guide"', ['[a]: <https://x.dev> "The guide"']],
380 ["- [a]: https://x.dev\n\n[A]: https://second.dev", ["[a]: <https://x.dev>"]],
381 ["<div>\n[a]: https://x.dev\n</div>", []],
382 ["- ```\n [a]: https://x.dev\n ```", []],
383 ["text\n[a]: https://x.dev", []],
384 ])("finds the reference definitions in %j as Words reads them", (markdown, definitions) => {
385 expect(markdownReferenceDefinitions(markdown)).toEqual(definitions);
386 });
387
388 it("keeps authored SVG and ids out of the math it restores", () => {
389 const html = wordsHtml(
390 '<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$',
391 { math: true },
392 );
393 expect(html).not.toContain("99999");
394 expect(html).toContain("kept");
395 expect(html.match(/<svg /g)).toHaveLength(1);
396 });
397
398 it("keeps only math's own SVG elements and attributes", () => {
399 const html = toHtml(
400 wordsHast(
401 '<svg onload="x()" style="position:fixed;display:inline"><a href="javascript:x()"><text x="1">t</text></a><script>x()</script></svg> $y$',
402 { ...ASSISTANT, math: true },
403 ),
404 );
405 expect(html).not.toMatch(/onload|javascript|position|<a |<script/);
406 expect(html).toContain('aria-label="y"');
407 });
408});