Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(h5): preserve clipboard cleanup compatibility
  • Loading branch information
Jarvis636431 authored and Single-Dancer committed Oct 22, 2026
commit b2798960e37b28e9ea5e42fe8c4d440770eb6aae
66 changes: 66 additions & 0 deletions packages/taro-h5/__tests__/device/clipboard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,72 @@ describe('setClipboardData', () => {
else execCommand.mockImplementation(() => { throw new Error('copy denied') })
}

test.each(['succeeds', 'returns false', 'throws'])('cleans up without textarea.remove when copying %s', async mode => {
jest.spyOn(HTMLTextAreaElement.prototype, 'select').mockImplementation(function () {
Object.defineProperty(this, 'remove', { value: undefined })
})
if (mode !== 'succeeds') failCopy(mode)
const success = jest.fn()
const fail = jest.fn()
const complete = jest.fn()

const result = await setClipboardData({ data: 'new content', success, fail, complete })

expect(result).toEqual({
errMsg: mode === 'succeeds'
? 'setClipboardData:ok'
: `setClipboardData:fail ${mode === 'returns false' ? 'Failed to copy to clipboard' : 'copy denied'}`
})
expect(execCommand).toHaveBeenCalledWith('copy')
expect(document.querySelector('textarea')).toBeNull()
expect(success).toHaveBeenCalledTimes(mode === 'succeeds' ? 1 : 0)
expect(fail).toHaveBeenCalledTimes(mode === 'succeeds' ? 0 : 1)
expect(complete).toHaveBeenCalledTimes(1)
expect(complete).toHaveBeenCalledWith(result)
expect(showToast).toHaveBeenCalledTimes(mode === 'succeeds' ? 1 : 0)
expect((await getClipboardData()).data).toBe(mode === 'succeeds' ? 'new content' : 'previous content')
})

describe.each(['QuotaExceededError', 'SecurityError'])('when caching throws %s after a successful copy', errorName => {
beforeEach(() => {
jest.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
throw new DOMException('Clipboard cache write failed', errorName)
})
})

test('rejects and cleans up while leaving the readable cache unchanged', async () => {
await expect(setClipboardData({ data: 'new content' })).rejects.toEqual({
errMsg: 'setClipboardData:fail Clipboard cache write failed'
})

expect(execCommand).toHaveBeenCalledTimes(1)
expect(execCommand).toHaveBeenCalledWith('copy')
expect(document.querySelector('textarea')).toBeNull()
expect(showToast).not.toHaveBeenCalled()
expect((await getClipboardData()).data).toBe('previous content')
})

test('calls fail and complete once without reporting success', async () => {
const success = jest.fn()
const fail = jest.fn()
const complete = jest.fn()

const result = await setClipboardData({ data: 'new content', success, fail, complete })

expect(result).toEqual({ errMsg: 'setClipboardData:fail Clipboard cache write failed' })
expect(execCommand).toHaveBeenCalledTimes(1)
expect(execCommand).toHaveBeenCalledWith('copy')
expect(success).not.toHaveBeenCalled()
expect(fail).toHaveBeenCalledTimes(1)
expect(fail).toHaveBeenCalledWith(result)
expect(complete).toHaveBeenCalledTimes(1)
expect(complete).toHaveBeenCalledWith(result)
expect(document.querySelector('textarea')).toBeNull()
expect(showToast).not.toHaveBeenCalled()
expect((await getClipboardData()).data).toBe('previous content')
})
})

test.each(['returns false', 'throws'])('rejects when copying %s and removes the temporary textarea', async mode => {
failCopy(mode)

Expand Down
5 changes: 4 additions & 1 deletion packages/taro-h5/src/api/device/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,14 @@ export const setClipboardData: typeof Taro.setClipboardData = async ({ data, opt
throw new Error('Failed to copy to clipboard')
}
} finally {
textarea.remove()
if (textarea.parentNode) {
textarea.parentNode.removeChild(textarea)
}
}
} else {
throw new Error('Unsupported Function: \'document.execCommand\'.')
}
// Cache failures are reported even if the system clipboard was already updated.
setStorageSync(CLIPBOARD_STORAGE_NAME, data)
if (!hideDefaultMessage) {
showToast({
Expand Down
Loading