Skip to content

Commit 03c6cd8

Browse files
fix(web): copy text over plain HTTP (pingdotgg#8023)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 4ade365 commit 03c6cd8

2 files changed

Lines changed: 120 additions & 12 deletions

File tree

‎apps/web/src/hooks/useCopyToClipboard.test.ts‎

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe("writeTextToClipboard", () => {
1414
it("reports unavailable clipboard support with structural context", async () => {
1515
vi.stubGlobal("window", {});
1616
vi.stubGlobal("navigator", {});
17+
vi.stubGlobal("document", undefined);
1718

1819
const error = await writeTextToClipboard("plan contents", "plan").then(
1920
() => undefined,
@@ -27,6 +28,72 @@ describe("writeTextToClipboard", () => {
2728
expect((error as Error).message).not.toContain("plan contents");
2829
});
2930

31+
it.each(["success", "denied", "throws"] as const)(
32+
"cleans up the Clipboard API fallback when copying %s",
33+
async (result) => {
34+
const focus = vi.fn();
35+
const restoreFocus = vi.fn();
36+
const appendChild = vi.fn();
37+
const execCommand = vi.fn(() => {
38+
if (result === "throws") throw new Error("copy command failed");
39+
return result === "success";
40+
});
41+
const remove = vi.fn();
42+
const select = vi.fn();
43+
const setAttribute = vi.fn();
44+
const setSelectionRange = vi.fn();
45+
const textarea = {
46+
focus,
47+
remove,
48+
select,
49+
setAttribute,
50+
setSelectionRange,
51+
style: {},
52+
value: "",
53+
};
54+
55+
vi.stubGlobal("window", {});
56+
vi.stubGlobal("navigator", {});
57+
vi.stubGlobal("document", {
58+
activeElement: { focus: restoreFocus },
59+
body: { appendChild },
60+
createElement: vi.fn(() => textarea),
61+
execCommand,
62+
});
63+
64+
const pendingCopy = writeTextToClipboard("remote command", "command");
65+
// The fallback must run during the original user gesture, before any await.
66+
expect(execCommand).toHaveBeenCalledWith("copy");
67+
if (result === "success") {
68+
await expect(pendingCopy).resolves.toBe(true);
69+
} else {
70+
await expect(pendingCopy).rejects.toBeInstanceOf(ClipboardApiUnavailableError);
71+
}
72+
73+
expect(textarea.value).toBe("remote command");
74+
expect(textarea.style).toMatchObject({ fontSize: "16px" });
75+
expect(appendChild).toHaveBeenCalledWith(textarea);
76+
expect(focus).toHaveBeenCalledWith({ preventScroll: true });
77+
expect(select).toHaveBeenCalledOnce();
78+
expect(setSelectionRange).toHaveBeenCalledWith(0, "remote command".length);
79+
expect(remove).toHaveBeenCalledOnce();
80+
expect(restoreFocus).toHaveBeenCalledOnce();
81+
},
82+
);
83+
84+
it("uses the Clipboard API without touching the fallback when it is available", async () => {
85+
const writeText = vi.fn().mockResolvedValue(undefined);
86+
const execCommand = vi.fn();
87+
vi.stubGlobal("window", {});
88+
vi.stubGlobal("navigator", { clipboard: { writeText } });
89+
vi.stubGlobal("document", { execCommand });
90+
91+
await expect(writeTextToClipboard("remote command", "command")).resolves.toBe(true);
92+
93+
expect(writeText).toHaveBeenCalledWith("remote command");
94+
expect(execCommand).not.toHaveBeenCalled();
95+
});
96+
3097
it("preserves the exact clipboard failure without exposing copied contents", async () => {
3198
const cause = new Error("browser clipboard failure");
3299
const writeText = vi.fn().mockRejectedValue(cause);
@@ -47,12 +114,18 @@ describe("writeTextToClipboard", () => {
47114
expect((error as Error).message).not.toContain("secret clipboard contents");
48115
});
49116

50-
it("keeps empty values as a no-op when clipboard support is available", async () => {
51-
const writeText = vi.fn();
52-
vi.stubGlobal("window", {});
53-
vi.stubGlobal("navigator", { clipboard: { writeText } });
117+
it.each([true, false])(
118+
"keeps empty values as a no-op with Clipboard API support: %s",
119+
async (available) => {
120+
const writeText = vi.fn();
121+
vi.stubGlobal("window", {});
122+
const execCommand = vi.fn();
123+
vi.stubGlobal("navigator", available ? { clipboard: { writeText } } : {});
124+
vi.stubGlobal("document", { execCommand });
54125

55-
await expect(writeTextToClipboard("", "plan")).resolves.toBe(false);
56-
expect(writeText).not.toHaveBeenCalled();
57-
});
126+
await expect(writeTextToClipboard("", "plan")).resolves.toBe(false);
127+
expect(writeText).not.toHaveBeenCalled();
128+
expect(execCommand).not.toHaveBeenCalled();
129+
},
130+
);
58131
});

‎apps/web/src/hooks/useCopyToClipboard.ts‎

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,54 @@ export class ClipboardReadError extends Schema.TaggedErrorClass<ClipboardReadErr
4747
}
4848
}
4949

50+
/** Copy fallback for remote web pages served over plain HTTP. */
51+
function writeTextWithExecCommand(value: string): boolean {
52+
if (typeof document === "undefined" || typeof document.execCommand !== "function") return false;
53+
54+
const textarea = document.createElement("textarea");
55+
textarea.value = value;
56+
textarea.setAttribute("readonly", "");
57+
textarea.setAttribute("aria-hidden", "true");
58+
textarea.style.position = "fixed";
59+
textarea.style.top = "0";
60+
textarea.style.left = "0";
61+
textarea.style.opacity = "0";
62+
textarea.style.fontSize = "16px";
63+
64+
const previouslyFocused = document.activeElement;
65+
document.body.appendChild(textarea);
66+
try {
67+
textarea.focus({ preventScroll: true });
68+
textarea.select();
69+
textarea.setSelectionRange(0, value.length);
70+
return document.execCommand("copy");
71+
} catch {
72+
return false;
73+
} finally {
74+
textarea.remove();
75+
const restoreFocus = (previouslyFocused as { focus?: unknown } | null)?.focus;
76+
if (typeof restoreFocus === "function") {
77+
restoreFocus.call(previouslyFocused);
78+
}
79+
}
80+
}
81+
5082
export async function writeTextToClipboard(value: string, target = "text") {
51-
if (
52-
typeof window === "undefined" ||
53-
typeof navigator === "undefined" ||
54-
!navigator.clipboard?.writeText
55-
) {
83+
if (typeof window === "undefined") {
5684
throw new ClipboardApiUnavailableError({
5785
target,
5886
});
5987
}
6088

6189
if (!value) return false;
6290

91+
if (typeof navigator === "undefined" || !navigator.clipboard?.writeText) {
92+
if (writeTextWithExecCommand(value)) return true;
93+
throw new ClipboardApiUnavailableError({
94+
target,
95+
});
96+
}
97+
6398
try {
6499
await navigator.clipboard.writeText(value);
65100
return true;

0 commit comments

Comments
 (0)