Skip to content

Commit d41b3bf

Browse files
tskimmettRonaldJerez
authored andcommitted
fix: assume insertText if event.inputType is unsupported
1 parent 7e20362 commit d41b3bf

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/core.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,11 @@ export function updateCursor(event, originalValue, originalPosition) {
9595
return
9696
}
9797

98+
// if event.inputType is not supported, assume 'insertText'
99+
const inputType = event.inputType || 'insertText'
100+
98101
// get some information about the cursor based on the original value
99-
const isInsertEvent = ['insertText', 'insertFromPaste'].includes(event.inputType)
102+
const isInsertEvent = ['insertText', 'insertFromPaste'].includes(inputType)
100103
const wasCursorAtEnd = isInsertEvent && originalPosition == originalValue.length
101104
let lastInsertedChar = isInsertEvent && originalValue[originalPosition - 1]
102105

tests/directive.test.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ describe('Directive', () => {
135135
})
136136
})
137137

138-
describe('Cursor updates', () => {
138+
describe.each([['insertText'], [undefined]])('Cursor updates (inputType = %s)', (inputType) => {
139139
let element
140140

141141
beforeEach(() => {
@@ -154,7 +154,18 @@ describe('Directive', () => {
154154
const newCursorPos = cursorPos + 1 // one new char inserted before
155155

156156
element.selectionEnd = cursorPos
157-
wrapper.find('input').trigger('input', { inputType: 'insertText' })
157+
wrapper.find('input').trigger('input', { inputType })
158+
159+
expect(wrapper.element.setSelectionRange).toBeCalledWith(newCursorPos, newCursorPos)
160+
})
161+
162+
test('Should stay next to the char just inserted', () => {
163+
element.value = 'ABC1|23'
164+
const cursorPos = element.value.indexOf('|')
165+
const newCursorPos = cursorPos + 1 // one new char inserted before
166+
167+
element.selectionEnd = cursorPos
168+
wrapper.find('input').trigger('input', { inputType })
158169

159170
expect(wrapper.element.setSelectionRange).toBeCalledWith(newCursorPos, newCursorPos)
160171
})
@@ -165,7 +176,7 @@ describe('Directive', () => {
165176
const newCursorPos = cursorPos + 2 // two new characters after masking
166177

167178
element.selectionEnd = cursorPos
168-
wrapper.find('input').trigger('input', { inputType: 'insertText' })
179+
wrapper.find('input').trigger('input', { inputType })
169180

170181
expect(wrapper.element.setSelectionRange).toBeCalledWith(newCursorPos, newCursorPos)
171182
})
@@ -176,7 +187,7 @@ describe('Directive', () => {
176187
const newCursorPos = cursorPos - 1 // needs to move back as 'j' is not an allowed char
177188

178189
element.selectionEnd = cursorPos
179-
wrapper.find('input').trigger('input', { inputType: 'insertText' })
190+
wrapper.find('input').trigger('input', { inputType })
180191

181192
expect(wrapper.element.setSelectionRange).toBeCalledWith(newCursorPos, newCursorPos)
182193
})
@@ -189,7 +200,7 @@ describe('Directive', () => {
189200
element.value = 'ABC-1J|2'
190201
const cursorPos = element.value.indexOf('|')
191202
element.selectionEnd = cursorPos
192-
wrapper.find('input').trigger('input', { inputType: 'insertText' })
203+
wrapper.find('input').trigger('input', { inputType })
193204
expect(wrapper.element.setSelectionRange).not.toBeCalled()
194205
})
195206
})

0 commit comments

Comments
 (0)