Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { chatMarkdownClipboardPayload } from "./markdown-clipboard";
4
5function select(start: Node, startOffset: number, end: Node, endOffset: number) {
6 const range = document.createRange();
7 range.setStart(start, startOffset);
8 range.setEnd(end, endOffset);
9 const selection = window.getSelection()!;
10 selection.removeAllRanges();
11 selection.addRange(range);
12 return selection;
13}
14
15describe("copying rendered math", () => {
16 it("copies a formula's TeX whether the selection spans it or starts inside it", () => {
17 document.body.innerHTML = `<div class="chat-markdown"><p>Area <span data-markdown-math="" data-markdown-copy="$\\pi r^2$"><span class="katex"><span class="katex-mathml">πr2</span><span class="katex-html" aria-hidden="true">πr2</span></span></span> grows.</p></div>`;
18 const paragraph = document.querySelector("p")!;
19 const glyphs = document.querySelector(".katex-mathml")!.firstChild!;
20
21 expect(
22 chatMarkdownClipboardPayload(select(paragraph.firstChild!, 0, paragraph.lastChild!, 7))?.text,
23 ).toBe("Area $\\pi r^2$ grows.");
24 expect(chatMarkdownClipboardPayload(select(glyphs, 0, glyphs, 1))?.text).toBe("$\\pi r^2$");
25 });
26});