From adc9d1bf643ee1628f27b400e0d51b7c6aba978b Mon Sep 17 00:00:00 2001 From: Turtle Date: Sat, 7 Nov 2026 16:45:28 +0800 Subject: [PATCH 1/2] fix(web): fall back to execCommand when the clipboard write is refused A refused navigator.clipboard.writeText (blocked permission, embedding policy) returned false without trying the execCommand fallback, so copy buttons did nothing. Fall through to the fallback and return focus to the control afterwards. Fixes #8123 --- .../tests/chat-branch-tails.client.spec.tsx | 21 ++++++++++ .../client/ui-primitives/src/clipboard.ts | 17 ++++---- .../tests/terminal-block.client.spec.tsx | 39 ++++++++++++++++++- .../ui-workspace/tests/apply.client.spec.ts | 2 + 4 files changed, 71 insertions(+), 8 deletions(-) diff --git a/packages/client/ui-chat/tests/chat-branch-tails.client.spec.tsx b/packages/client/ui-chat/tests/chat-branch-tails.client.spec.tsx index 88c42d6dc2..dea8285608 100644 --- a/packages/client/ui-chat/tests/chat-branch-tails.client.spec.tsx +++ b/packages/client/ui-chat/tests/chat-branch-tails.client.spec.tsx @@ -227,11 +227,32 @@ describe('MessageItem arms', () => { expect(exec).toHaveBeenCalledWith('copy') }) + it('user copy succeeds through execCommand when the host refuses the async write', async () => { + Object.defineProperty(navigator, 'clipboard', { + configurable: true, + value: { writeText: vi.fn().mockRejectedValue(new Error('denied')) }, + }) + const exec = vi.fn().mockReturnValue(true) + Object.defineProperty(document, 'execCommand', { configurable: true, value: exec }) + render( + , + ) + fireEvent.click(screen.getByRole('button', { name: '复制' })) + expect(await screen.findByRole('button', { name: '复制成功' })).toBeTruthy() + expect(exec).toHaveBeenCalledWith('copy') + }) + it('user copy never claims success when the host rejects the write', async () => { Object.defineProperty(navigator, 'clipboard', { configurable: true, value: { writeText: vi.fn().mockRejectedValue(new Error('denied')) }, }) + Object.defineProperty(document, 'execCommand', { configurable: true, value: vi.fn(() => false) }) render( { try { await navigator.clipboard.writeText(text) return true - } catch { - // Denied permissions / iframe policy — do not claim success. - return false + } catch (_error) { + // A denied permission or embedding policy blocks only the async API; try the fallback below. } } - // jsdom and older hosts: best-effort execCommand path when present. // execCommand('copy') is the only clipboard fallback where the async API - // is missing; deprecated but deliberately retained. + // is missing or refused; deprecated but deliberately retained. /* oxlint-disable typescript/no-deprecated */ const exec = typeof document.execCommand === 'function' ? document.execCommand.bind(document) : undefined if (exec === undefined) return false + // Selecting the textarea moves focus; return it so a keyboard user stays on the copy control. + const focused = document.activeElement const el = document.createElement('textarea') el.value = text el.setAttribute('readonly', '') @@ -42,6 +44,7 @@ export async function writeClipboard(text: string): Promise { return false } finally { el.remove() + if (focused instanceof HTMLElement) focused.focus() } /* oxlint-enable typescript/no-deprecated */ } diff --git a/packages/client/ui-primitives/tests/terminal-block.client.spec.tsx b/packages/client/ui-primitives/tests/terminal-block.client.spec.tsx index b801dd6e91..5a6dbd9567 100644 --- a/packages/client/ui-primitives/tests/terminal-block.client.spec.tsx +++ b/packages/client/ui-primitives/tests/terminal-block.client.spec.tsx @@ -448,14 +448,32 @@ describe('writeClipboard', () => { expect(writeText).toHaveBeenCalledWith('payload') }) - it('reports false when the Clipboard API rejects', async () => { + it('reports false when the Clipboard API rejects and no fallback exists', async () => { Object.defineProperty(navigator, 'clipboard', { configurable: true, value: { writeText: vi.fn().mockRejectedValue(new Error('denied')) }, }) + Object.defineProperty(document, 'execCommand', { configurable: true, value: undefined }) await expect(writeClipboard('payload')).resolves.toBe(false) }) + it('falls back to execCommand when the Clipboard API rejects', async () => { + Object.defineProperty(navigator, 'clipboard', { + configurable: true, + value: { writeText: vi.fn().mockRejectedValue(new Error('denied')) }, + }) + let selected: string | undefined + const exec = vi.fn(() => { + selected = document.querySelector('textarea[readonly]')?.value + return true + }) + Object.defineProperty(document, 'execCommand', { configurable: true, value: exec }) + await expect(writeClipboard('payload')).resolves.toBe(true) + expect(exec).toHaveBeenCalledWith('copy') + expect(selected).toBe('payload') + expect(document.querySelector('textarea')).toBeNull() + }) + it('selects a detached textarea for the execCommand fallback and removes it after', async () => { Object.defineProperty(navigator, 'clipboard', { configurable: true, value: undefined }) let selected: string | undefined @@ -470,6 +488,25 @@ describe('writeClipboard', () => { expect(document.querySelector('textarea')).toBeNull() }) + it('returns focus to the copy control after the execCommand fallback', async () => { + Object.defineProperty(navigator, 'clipboard', { configurable: true, value: undefined }) + Object.defineProperty(document, 'execCommand', { configurable: true, value: vi.fn(() => true) }) + const button = document.createElement('button') + document.body.appendChild(button) + button.focus() + await expect(writeClipboard('payload')).resolves.toBe(true) + expect(document.activeElement).toBe(button) + button.remove() + }) + + it('copies through the fallback when nothing had focus', async () => { + Object.defineProperty(navigator, 'clipboard', { configurable: true, value: undefined }) + Object.defineProperty(document, 'execCommand', { configurable: true, value: vi.fn(() => true) }) + const activeElement = vi.spyOn(document, 'activeElement', 'get').mockReturnValue(null) + await expect(writeClipboard('payload')).resolves.toBe(true) + activeElement.mockRestore() + }) + it('reports execCommand\'s own refusal verbatim', async () => { Object.defineProperty(navigator, 'clipboard', { configurable: true, value: undefined }) Object.defineProperty(document, 'execCommand', { configurable: true, value: vi.fn(() => false) }) diff --git a/packages/client/ui-workspace/tests/apply.client.spec.ts b/packages/client/ui-workspace/tests/apply.client.spec.ts index d6c18cd763..3007cc8b56 100644 --- a/packages/client/ui-workspace/tests/apply.client.spec.ts +++ b/packages/client/ui-workspace/tests/apply.client.spec.ts @@ -371,6 +371,8 @@ describe('ui-workspace apply', () => { if (!accepted) throw new Error('clipboard denied') }) vi.stubGlobal('navigator', { clipboard: { writeText } }) + // This spec has no DOM; give the fallback a document without execCommand so a refused write stays refused. + vi.stubGlobal('document', {}) onTestFinished(() => { vi.unstubAllGlobals() }) await action.copySessionId(sid('cold-session')) expect(writeText).toHaveBeenCalledExactlyOnceWith('cold-session') From 81ee68dfbaa59ab18437d24b2622048edc8a2b96 Mon Sep 17 00:00:00 2001 From: Turtle Date: Sat, 7 Nov 2026 17:21:21 +0800 Subject: [PATCH 2/2] fix(web): keep the clipboard fallback total and document its limits --- packages/client/ui-primitives/src/clipboard.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/client/ui-primitives/src/clipboard.ts b/packages/client/ui-primitives/src/clipboard.ts index ae90982fcf..f8331f12e2 100644 --- a/packages/client/ui-primitives/src/clipboard.ts +++ b/packages/client/ui-primitives/src/clipboard.ts @@ -1,12 +1,13 @@ // Host clipboard write shared by Web UI copy controls. Success feedback stays -// with each control; this helper only reports whether the host accepted a write. +// with each control; this helper reports whether the host accepted a write and +// leaves focus where it found it. /** * Write text to the host clipboard, preferring the async Clipboard API and * falling back to `execCommand('copy')` when that API is missing (jsdom, - * insecure contexts) or refuses the write (blocked clipboard permission, - * embedding policy). Call it from a user gesture: the fallback relies on the - * gesture's transient activation. + * insecure contexts) or refuses the write (for example, a denied clipboard + * permission). The fallback replaces the page selection and succeeds only while + * the triggering user gesture's transient activation is still valid. * @param text - the exact text to place on the clipboard. * @returns true only when the host accepted the write. */ @@ -19,7 +20,7 @@ export async function writeClipboard(text: string): Promise { await navigator.clipboard.writeText(text) return true } catch (_error) { - // A denied permission or embedding policy blocks only the async API; try the fallback below. + // A denied permission can block the async API while execCommand still works; try it below. } } // execCommand('copy') is the only clipboard fallback where the async API @@ -36,15 +37,15 @@ export async function writeClipboard(text: string): Promise { el.setAttribute('readonly', '') el.style.position = 'fixed' el.style.left = '-9999px' - document.body.appendChild(el) - el.select() try { + document.body.appendChild(el) + el.select() return exec('copy') } catch { return false } finally { el.remove() - if (focused instanceof HTMLElement) focused.focus() + if (focused instanceof HTMLElement) focused.focus({ preventScroll: true }) } /* oxlint-enable typescript/no-deprecated */ }