|
| 1 | +import { describe, expect, it, vi } from "vite-plus/test"; |
| 2 | + |
| 3 | +import { fakeTimers } from "../../test/render"; |
| 4 | +import { createSelectionCopier, observeCopyOnSelect, readCopyableSelection } from "./copyOnSelect"; |
| 5 | +import { SELECTION_MULTI_CLICK_INTERVAL_MS } from "./selectionActions"; |
| 6 | + |
| 7 | +function mount(html: string) { |
| 8 | + const container = document.createElement("div"); |
| 9 | + container.innerHTML = html; |
| 10 | + document.body.append(container); |
| 11 | + return Object.assign(container, { |
| 12 | + [Symbol.dispose]: () => { |
| 13 | + document.getSelection()?.removeAllRanges(); |
| 14 | + container.remove(); |
| 15 | + }, |
| 16 | + }); |
| 17 | +} |
| 18 | + |
| 19 | +function textOf(root: ParentNode, selector: string): Text { |
| 20 | + return root.querySelector(selector)!.firstChild as Text; |
| 21 | +} |
| 22 | + |
| 23 | +function select(start: Text, startOffset: number, end: Text, endOffset: number) { |
| 24 | + document.getSelection()!.setBaseAndExtent(start, startOffset, end, endOffset); |
| 25 | +} |
| 26 | + |
| 27 | +function mouse(type: "mousedown" | "mouseup", target: EventTarget, init: MouseEventInit = {}) { |
| 28 | + target.dispatchEvent(new MouseEvent(type, { bubbles: true, button: 0, detail: 1, ...init })); |
| 29 | +} |
| 30 | + |
| 31 | +describe("readCopyableSelection", () => { |
| 32 | + it.each([ |
| 33 | + ["plain text", '<p id="a">alpha</p><p id="b">bravo</p>', "alpha\n\nbravo"], |
| 34 | + ["a link", '<p id="a">see <a href="#">docs</a></p><p id="b">bravo</p>', "see docs\n\nbravo"], |
| 35 | + ["a button", '<p id="a">run <button>go</button></p><p id="b">bravo</p>', "run go\n\nbravo"], |
| 36 | + [ |
| 37 | + "a citation chip", |
| 38 | + '<p id="a">cite <span contenteditable="false">[1]</span></p><p id="b">bravo</p>', |
| 39 | + "cite [1]\n\nbravo", |
| 40 | + ], |
| 41 | + ])("copies a range across %s", (_name, html, expected) => { |
| 42 | + using container = mount(html); |
| 43 | + select(textOf(container, "#a"), 0, textOf(container, "#b"), 5); |
| 44 | + expect(readCopyableSelection(document.getSelection(), container)).toBe(expected); |
| 45 | + }); |
| 46 | + |
| 47 | + it.each([ |
| 48 | + ["an input", '<input value="secret" />'], |
| 49 | + ["a textarea", "<textarea>secret</textarea>"], |
| 50 | + ["a select", "<select><option>secret</option></select>"], |
| 51 | + ["editable content", '<div contenteditable="true">secret</div>'], |
| 52 | + ["bare contenteditable", "<div contenteditable>secret</div>"], |
| 53 | + ["plaintext-only content", '<div contenteditable="plaintext-only">secret</div>'], |
| 54 | + ["an opted-out subtree", "<div data-no-copy-on-select>secret</div>"], |
| 55 | + ])("rejects a range whose ordinary endpoints cross %s", (_name, excluded) => { |
| 56 | + using container = mount(`<p id="a">alpha</p>${excluded}<p id="b">bravo</p>`); |
| 57 | + select(textOf(container, "#a"), 0, textOf(container, "#b"), 5); |
| 58 | + expect(readCopyableSelection(document.getSelection(), container)).toBeNull(); |
| 59 | + }); |
| 60 | + |
| 61 | + it("rejects selections that end in editable content, leave the container, or hold no text", () => { |
| 62 | + using container = mount('<p id="a">alpha</p><div id="e" contenteditable>edit</div>'); |
| 63 | + using outside = mount('<p id="o">outside</p>'); |
| 64 | + const alpha = textOf(container, "#a"); |
| 65 | + |
| 66 | + select(alpha, 0, textOf(container, "#e"), 2); |
| 67 | + expect(readCopyableSelection(document.getSelection(), container)).toBeNull(); |
| 68 | + select(alpha, 0, textOf(outside, "#o"), 3); |
| 69 | + expect(readCopyableSelection(document.getSelection(), container)).toBeNull(); |
| 70 | + select(alpha, 2, alpha, 2); |
| 71 | + expect(readCopyableSelection(document.getSelection(), container)).toBeNull(); |
| 72 | + container.querySelector("#a")!.textContent = " "; |
| 73 | + select(textOf(container, "#a"), 0, textOf(container, "#a"), 3); |
| 74 | + expect(readCopyableSelection(document.getSelection(), container)).toBeNull(); |
| 75 | + }); |
| 76 | +}); |
| 77 | + |
| 78 | +describe("observeCopyOnSelect", () => { |
| 79 | + function setup() { |
| 80 | + const container = mount('<p id="a">alpha bravo</p><textarea id="t">draft</textarea>'); |
| 81 | + const copy = vi.fn(); |
| 82 | + const stop = observeCopyOnSelect(container, copy); |
| 83 | + const unmount = container[Symbol.dispose]; |
| 84 | + return Object.assign(container, { |
| 85 | + copy, |
| 86 | + text: textOf(container, "#a"), |
| 87 | + paragraph: container.querySelector("#a")!, |
| 88 | + [Symbol.dispose]: () => { |
| 89 | + stop(); |
| 90 | + unmount(); |
| 91 | + }, |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + it("copies a fresh drag, including one released outside the container", () => { |
| 96 | + using view = setup(); |
| 97 | + mouse("mousedown", view.paragraph); |
| 98 | + select(view.text, 0, view.text, 5); |
| 99 | + mouse("mouseup", document.body); |
| 100 | + expect(view.copy).toHaveBeenCalledExactlyOnceWith("alpha"); |
| 101 | + }); |
| 102 | + |
| 103 | + it("ignores clicks that leave the selection unchanged, modifier clicks, and editable targets", () => { |
| 104 | + using view = setup(); |
| 105 | + select(view.text, 0, view.text, 5); |
| 106 | + mouse("mousedown", view.paragraph); |
| 107 | + mouse("mouseup", view.paragraph); |
| 108 | + |
| 109 | + mouse("mousedown", view.paragraph, { metaKey: true }); |
| 110 | + select(view.text, 6, view.text, 11); |
| 111 | + mouse("mouseup", view.paragraph, { metaKey: true }); |
| 112 | + |
| 113 | + mouse("mousedown", view.querySelector("#t")!); |
| 114 | + select(view.text, 0, view.text, 11); |
| 115 | + mouse("mouseup", view.paragraph); |
| 116 | + |
| 117 | + expect(view.copy).not.toHaveBeenCalled(); |
| 118 | + }); |
| 119 | + |
| 120 | + it("copies a double then triple click once, after the sequence settles", () => { |
| 121 | + using _timers = fakeTimers(); |
| 122 | + using view = setup(); |
| 123 | + mouse("mousedown", view.paragraph); |
| 124 | + mouse("mouseup", view.paragraph); |
| 125 | + mouse("mousedown", view.paragraph, { detail: 2 }); |
| 126 | + select(view.text, 0, view.text, 5); |
| 127 | + mouse("mouseup", view.paragraph, { detail: 2 }); |
| 128 | + mouse("mousedown", view.paragraph, { detail: 3 }); |
| 129 | + select(view.text, 0, view.text, 11); |
| 130 | + mouse("mouseup", view.paragraph, { detail: 3 }); |
| 131 | + expect(view.copy).not.toHaveBeenCalled(); |
| 132 | + |
| 133 | + vi.advanceTimersByTime(SELECTION_MULTI_CLICK_INTERVAL_MS); |
| 134 | + expect(view.copy).toHaveBeenCalledExactlyOnceWith("alpha bravo"); |
| 135 | + }); |
| 136 | +}); |
| 137 | + |
| 138 | +describe("createSelectionCopier", () => { |
| 139 | + it("fails silently without touching focused input, then retries the same text", async () => { |
| 140 | + using input = mount('<textarea id="t">にほんご</textarea>'); |
| 141 | + const terminalInput = input.querySelector("textarea")!; |
| 142 | + terminalInput.focus(); |
| 143 | + terminalInput.setSelectionRange(4, 4); |
| 144 | + using execCommand = vi.spyOn(document, "execCommand"); |
| 145 | + using writeText = vi |
| 146 | + .spyOn(navigator.clipboard, "writeText") |
| 147 | + .mockRejectedValueOnce(new Error("denied")) |
| 148 | + .mockResolvedValueOnce(undefined); |
| 149 | + const onCopied = vi.fn(); |
| 150 | + const copy = createSelectionCopier(onCopied); |
| 151 | + |
| 152 | + await expect(copy("git status")).resolves.toBe(false); |
| 153 | + expect(onCopied).not.toHaveBeenCalled(); |
| 154 | + expect(document.activeElement).toBe(terminalInput); |
| 155 | + expect(terminalInput.value).toBe("にほんご"); |
| 156 | + expect([terminalInput.selectionStart, terminalInput.selectionEnd]).toEqual([4, 4]); |
| 157 | + expect(execCommand).not.toHaveBeenCalled(); |
| 158 | + |
| 159 | + await expect(copy("git status")).resolves.toBe(true); |
| 160 | + expect(onCopied).toHaveBeenCalledOnce(); |
| 161 | + expect(writeText).toHaveBeenCalledTimes(2); |
| 162 | + }); |
| 163 | + |
| 164 | + it("does not repeat a write of the same text while it is in flight", async () => { |
| 165 | + let resolve = () => {}; |
| 166 | + const promise = new Promise<void>((done) => (resolve = done)); |
| 167 | + using writeText = vi.spyOn(navigator.clipboard, "writeText").mockReturnValue(promise); |
| 168 | + const onCopied = vi.fn(); |
| 169 | + const copy = createSelectionCopier(onCopied); |
| 170 | + |
| 171 | + const first = copy("git status"); |
| 172 | + await expect(copy("git status")).resolves.toBe(false); |
| 173 | + resolve(); |
| 174 | + await expect(first).resolves.toBe(true); |
| 175 | + expect(writeText).toHaveBeenCalledOnce(); |
| 176 | + expect(onCopied).toHaveBeenCalledOnce(); |
| 177 | + }); |
| 178 | +}); |
0 commit comments