fix: guard inline input types
This commit is contained in:
@@ -549,6 +549,7 @@ describe('WikilinkChatInput', () => {
|
||||
it('treats missing inputType as a non-insert beforeinput event', () => {
|
||||
expect(() => isInsertBeforeInput({} as InputEvent)).not.toThrow()
|
||||
expect(isInsertBeforeInput({} as InputEvent)).toBe(false)
|
||||
expect(isInsertBeforeInput({ inputType: 42 })).toBe(false)
|
||||
expect(isInsertBeforeInput({ inputType: 'insertFromPaste' } as InputEvent)).toBe(true)
|
||||
})
|
||||
|
||||
@@ -567,6 +568,26 @@ describe('WikilinkChatInput', () => {
|
||||
expect(editor.textContent).toContain('still works')
|
||||
})
|
||||
|
||||
it('ignores beforeinput events with non-string inputType instead of calling startsWith', () => {
|
||||
render(<Controlled />)
|
||||
|
||||
const editor = screen.getByTestId('agent-input')
|
||||
const startsWith = vi.fn(() => true)
|
||||
const beforeInputEvent = new Event('beforeinput', {
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
})
|
||||
Object.defineProperty(beforeInputEvent, 'inputType', {
|
||||
value: { startsWith },
|
||||
})
|
||||
|
||||
expect(() => fireEvent(editor, beforeInputEvent)).not.toThrow()
|
||||
expect(startsWith).not.toHaveBeenCalled()
|
||||
|
||||
updateEditorText('still works')
|
||||
expect(editor.textContent).toContain('still works')
|
||||
})
|
||||
|
||||
it('recovers if unsupported media lands in the editor DOM', async () => {
|
||||
const onUnsupportedPaste = vi.fn()
|
||||
render(<Controlled onUnsupportedPaste={onUnsupportedPaste} />)
|
||||
|
||||
@@ -1,11 +1,26 @@
|
||||
export function isInsertBeforeInput(nativeEvent: Pick<InputEvent, 'inputType'>) {
|
||||
return typeof nativeEvent.inputType === 'string'
|
||||
&& nativeEvent.inputType.startsWith('insert')
|
||||
type BeforeInputLike = {
|
||||
inputType?: unknown
|
||||
}
|
||||
|
||||
type PlainTextBeforeInputLike = BeforeInputLike & {
|
||||
data?: unknown
|
||||
isComposing?: unknown
|
||||
}
|
||||
|
||||
type PlainTextBeforeInput = PlainTextBeforeInputLike & {
|
||||
data: string
|
||||
inputType: 'insertText'
|
||||
isComposing?: false
|
||||
}
|
||||
|
||||
export function isInsertBeforeInput(nativeEvent: BeforeInputLike) {
|
||||
const { inputType } = nativeEvent
|
||||
return typeof inputType === 'string' && inputType.startsWith('insert')
|
||||
}
|
||||
|
||||
export function isPlainTextBeforeInput(
|
||||
nativeEvent: Pick<InputEvent, 'data' | 'inputType' | 'isComposing'>,
|
||||
): nativeEvent is Pick<InputEvent, 'inputType' | 'isComposing'> & { data: string } {
|
||||
nativeEvent: PlainTextBeforeInputLike,
|
||||
): nativeEvent is PlainTextBeforeInput {
|
||||
return nativeEvent.inputType === 'insertText'
|
||||
&& !nativeEvent.isComposing
|
||||
&& typeof nativeEvent.data === 'string'
|
||||
|
||||
Reference in New Issue
Block a user