fix: guard inline input types

This commit is contained in:
lucaronin
2026-04-27 20:36:59 +02:00
parent b6c11b1468
commit ee90b05159
2 changed files with 41 additions and 5 deletions

View File

@@ -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} />)

View File

@@ -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'