Merge pull request #8125 from deepseek-harness/turtle/fix-copy-failure-3ccb99f806dd
fix(web): fall back to execCommand when the clipboard write is refused
This commit is contained in:
commit
fce3880969
4 files changed
+75
-11
No files matched your search
@@ -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(
|
||||
<MessageItem t={t} node={{
|
||||
kind: 'user', seq: 1, time: 1_000,
|
||||
content: [{ type: 'text', text: 'refused body' }] as never,
|
||||
source: null,
|
||||
}}
|
||||
/>,
|
||||
)
|
||||
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(
|
||||
<MessageItem t={t} node={{
|
||||
kind: 'user', seq: 1, time: 1_000,
|
||||
|
||||
@@ -1,10 +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')` on hosts (jsdom, insecure contexts)
|
||||
* that omit it.
|
||||
* falling back to `execCommand('copy')` when that API is missing (jsdom,
|
||||
* 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.
|
||||
*/
|
||||
@@ -16,32 +19,33 @@ export async function writeClipboard(text: string): Promise<boolean> {
|
||||
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 */
|
||||
}
|
||||
@@ -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<HTMLTextAreaElement>('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) })
|
||||
|
||||
@@ -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')
|
||||
|
||||
Reference in new issue
Block a user