From 6ec259a0e71174651bae95d4628138bf6fd68742 Mon Sep 17 00:00:00 2001 From: kj_sh604 Date: Sun, 15 Mar 2026 16:19:35 -0400 Subject: refactor: packages/ --- packages/excalidraw/clipboard.test.ts | 196 ++++++++++++++++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 packages/excalidraw/clipboard.test.ts (limited to 'packages/excalidraw/clipboard.test.ts') diff --git a/packages/excalidraw/clipboard.test.ts b/packages/excalidraw/clipboard.test.ts new file mode 100644 index 0000000..770bcc9 --- /dev/null +++ b/packages/excalidraw/clipboard.test.ts @@ -0,0 +1,196 @@ +import { + createPasteEvent, + parseClipboard, + serializeAsClipboardJSON, +} from "./clipboard"; +import { API } from "./tests/helpers/api"; + +describe("parseClipboard()", () => { + it("should parse JSON as plaintext if not excalidraw-api/clipboard data", async () => { + let text; + let clipboardData; + // ------------------------------------------------------------------------- + + text = "123"; + clipboardData = await parseClipboard( + createPasteEvent({ types: { "text/plain": text } }), + ); + expect(clipboardData.text).toBe(text); + + // ------------------------------------------------------------------------- + + text = "[123]"; + clipboardData = await parseClipboard( + createPasteEvent({ types: { "text/plain": text } }), + ); + expect(clipboardData.text).toBe(text); + + // ------------------------------------------------------------------------- + + text = JSON.stringify({ val: 42 }); + clipboardData = await parseClipboard( + createPasteEvent({ types: { "text/plain": text } }), + ); + expect(clipboardData.text).toBe(text); + }); + + it("should parse valid excalidraw JSON if inside text/plain", async () => { + const rect = API.createElement({ type: "rectangle" }); + + const json = serializeAsClipboardJSON({ elements: [rect], files: null }); + const clipboardData = await parseClipboard( + createPasteEvent({ + types: { + "text/plain": json, + }, + }), + ); + expect(clipboardData.elements).toEqual([rect]); + }); + + it("should parse valid excalidraw JSON if inside text/html", async () => { + const rect = API.createElement({ type: "rectangle" }); + + let json; + let clipboardData; + // ------------------------------------------------------------------------- + json = serializeAsClipboardJSON({ elements: [rect], files: null }); + clipboardData = await parseClipboard( + createPasteEvent({ + types: { + "text/html": json, + }, + }), + ); + expect(clipboardData.elements).toEqual([rect]); + // ------------------------------------------------------------------------- + json = serializeAsClipboardJSON({ elements: [rect], files: null }); + clipboardData = await parseClipboard( + createPasteEvent({ + types: { + "text/html": `
${json}
`, + }, + }), + ); + expect(clipboardData.elements).toEqual([rect]); + // ------------------------------------------------------------------------- + }); + + it("should parse `src` urls out of text/html", async () => { + let clipboardData; + // ------------------------------------------------------------------------- + clipboardData = await parseClipboard( + createPasteEvent({ + types: { + "text/html": ``, + }, + }), + ); + expect(clipboardData.mixedContent).toEqual([ + { + type: "imageUrl", + value: "https://example.com/image.png", + }, + ]); + // ------------------------------------------------------------------------- + clipboardData = await parseClipboard( + createPasteEvent({ + types: { + "text/html": `
`, + }, + }), + ); + expect(clipboardData.mixedContent).toEqual([ + { + type: "imageUrl", + value: "https://example.com/image.png", + }, + { + type: "imageUrl", + value: "https://example.com/image2.png", + }, + ]); + }); + + it("should parse text content alongside `src` urls out of text/html", async () => { + const clipboardData = await parseClipboard( + createPasteEvent({ + types: { + "text/html": `hello
my friend!`, + }, + }), + ); + expect(clipboardData.mixedContent).toEqual([ + { + type: "text", + // trimmed + value: "hello", + }, + { + type: "imageUrl", + value: "https://example.com/image.png", + }, + { + type: "text", + value: "my friend!", + }, + ]); + }); + + it("should parse spreadsheet from either text/plain and text/html", async () => { + let clipboardData; + // ------------------------------------------------------------------------- + clipboardData = await parseClipboard( + createPasteEvent({ + types: { + "text/plain": `a b + 1 2 + 4 5 + 7 10`, + }, + }), + ); + expect(clipboardData.spreadsheet).toEqual({ + title: "b", + labels: ["1", "4", "7"], + values: [2, 5, 10], + }); + // ------------------------------------------------------------------------- + clipboardData = await parseClipboard( + createPasteEvent({ + types: { + "text/html": `a b + 1 2 + 4 5 + 7 10`, + }, + }), + ); + expect(clipboardData.spreadsheet).toEqual({ + title: "b", + labels: ["1", "4", "7"], + values: [2, 5, 10], + }); + // ------------------------------------------------------------------------- + clipboardData = await parseClipboard( + createPasteEvent({ + types: { + "text/html": ` + +
ab
12
45
710
+ + `, + "text/plain": `a b + 1 2 + 4 5 + 7 10`, + }, + }), + ); + expect(clipboardData.spreadsheet).toEqual({ + title: "b", + labels: ["1", "4", "7"], + values: [2, 5, 10], + }); + }); +}); -- cgit v1.2.3