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 1 commit
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'
17 changes: 17 additions & 0 deletions src/event/behavior/paste.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {input, isEditable} from '../../utils'
import {behavior} from './registry'

behavior.paste = (event, target, config) => {
if (isEditable(target)) {
return () => {
if (event.clipboardData) {
Gudahtt marked this conversation as resolved.
Show resolved Hide resolved
input(
config,
target,
event.clipboardData.getData('text'),
'insertFromPaste',
)
Gudahtt marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
12 changes: 12 additions & 0 deletions tests/clipboard/paste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ test('does not paste when disabled', async () => {
)
})

test('does not paste when preventDefault is called', async () => {
Gudahtt marked this conversation as resolved.
Show resolved Hide resolved
const {element, getEventSnapshot, user} = setup(`<input />`)
element.addEventListener('paste', e => e.preventDefault())

await user.paste('hi')
expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: input[value=""]

input[value=""] - paste
`)
Gudahtt marked this conversation as resolved.
Show resolved Hide resolved
})

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