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

data transfer ignore format case #1180

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 11 additions & 4 deletions src/utils/dataTransfer/DataTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,22 @@ class DataTransferItemListStub
}
}

function normalizeFormat(format: string) {
return format.toLowerCase()
}

function getTypeMatcher(type: string, exact: boolean) {
const [group, sub] = type.split('/')
const normalizedType = normalizeFormat(type)
const [group, sub] = normalizedType.split('/')
const isGroup = !sub || sub === '*'
return (item: DataTransferItem) => {
const normalizedItemType = normalizeFormat(item.type)

return exact
? item.type === (isGroup ? group : type)
? normalizedItemType === (isGroup ? group : normalizedType)
: isGroup
? item.type.startsWith(`${group}/`)
: item.type === group
? normalizedItemType.startsWith(`${group}/`)
: normalizedItemType === group
}
}

Expand Down
15 changes: 15 additions & 0 deletions tests/clipboard/paste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,21 @@ test('should replace selected text all at once', async () => {
expect(element).toHaveValue('hello friend')
})

test('should ignore format case', async () => {
const {element, user} = setup<HTMLInputElement>('<input />')
element.addEventListener('paste', event => {
event.preventDefault()

const data = event.clipboardData?.getData('Text')

element.value = data ?? ''
})

await user.paste('friend')

expect(element).toHaveValue('friend')
})

describe('without Clipboard API', () => {
beforeEach(() => {
Object.defineProperty(window.navigator, 'clipboard', {
Expand Down
1 change: 1 addition & 0 deletions tests/utils/dataTransfer/DataTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('create DataTransfer', () => {
expect(dt.getData('text/html')).toBe('bar')
expect(dt.getData('text/*')).toBe('foo')
expect(dt.getData('text')).toBe('foo')
expect(dt.getData('Text')).toBe('foo')

dt.clearData()
dt.setData('text', 'baz')
Expand Down