Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(paste): skip input according to defaultPrevented #862

Merged
merged 5 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 1 addition & 10 deletions src/clipboard/paste.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import {Config, Instance} from '../setup'
import {
createDataTransfer,
isEditable,
readDataTransferFromClipboard,
input,
} from '../utils'
import {createDataTransfer, readDataTransferFromClipboard} from '../utils'

export async function paste(
this: Instance,
Expand All @@ -26,10 +21,6 @@ export async function paste(
this.dispatchUIEvent(target, 'paste', {
clipboardData: dataTransfer,
})

if (isEditable(target)) {
input(this[Config], target, dataTransfer.getData('text'), 'insertFromPaste')
}
}

function getClipboardDataFromString(text: string) {
Expand Down
1 change: 1 addition & 0 deletions src/event/behavior/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import './click'
import './keydown'
import './keypress'
import './keyup'
import './paste'

export {behavior} from './registry'
export type {BehaviorPlugin} from './registry'
13 changes: 13 additions & 0 deletions src/event/behavior/paste.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {input, isEditable} from '../../utils'
import {behavior} from './registry'

behavior.paste = (event, target, config) => {
if (isEditable(target)) {
return () => {
const insertData = event.clipboardData?.getData('text')
if (insertData) {
input(config, target, insertData, 'insertFromPaste')
}
}
}
}
21 changes: 21 additions & 0 deletions tests/clipboard/paste.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import userEvent from '#src'
import {render, setup} from '#testHelpers'
import {createDataTransfer} from '#src/utils'

test('paste with empty clipboard', async () => {
const {element, getEvents, user} = setup(`<input/>`)
Expand All @@ -11,6 +12,17 @@ test('paste with empty clipboard', async () => {
expect(getEvents('input')).toHaveLength(0)
})

test('do not trigger input for paste with file data', async () => {
const {getEvents, user} = setup(`<input/>`)

const f0 = new File(['bar'], 'bar0.txt', {type: 'text/plain'})
const dt = createDataTransfer([f0])
await user.paste(dt)

expect(getEvents('paste')).toHaveLength(1)
expect(getEvents('input')).toHaveLength(0)
})

test.each([
[
`<input/>`,
Expand Down Expand Up @@ -65,6 +77,15 @@ test('does not paste when disabled', async () => {
)
})

test('prevent input per paste event handler', async () => {
const {element, eventWasFired, user} = setup(`<input />`)
element.addEventListener('paste', e => e.preventDefault())

await user.paste('hi')
expect(eventWasFired('paste')).toBe(true)
expect(eventWasFired('input')).toBe(false)
})

test.each(['input', 'textarea'])(
'should paste text in <%s> up to maxLength if provided',
async type => {
Expand Down