Skip to content

Commit

Permalink
data transfer ignore form case
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Fuks committed Nov 24, 2023
1 parent d7483f0 commit a74009a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
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

0 comments on commit a74009a

Please sign in to comment.