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 can block the async API while execCommand still works; try it 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', '')
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({ preventScroll: true })
}
/* 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 c5828af449..09c3e0510e 100644
--- a/packages/client/ui-workspace/tests/apply.client.spec.ts
+++ b/packages/client/ui-workspace/tests/apply.client.spec.ts
@@ -407,6 +407,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')