fix(ime): stop a commit key with no keyup from eating the next Enter

compositionend only scheduled the carry's expiry when it armed a new carry. A marked commit key (Process/229 with an unknown code) whose keyup never arrives left its carry armed forever, so the next bare Enter in the chat composer and every IME text field was swallowed while Ctrl/Cmd+Enter still submitted. compositionend now always schedules the expiry.

Refs #41072, STA-14827
This commit is contained in:
Kelvin Amoaba
2026-11-12 06:34:24 +00:00
parent affd908567
commit 9fff952602
2 changed files with 64 additions and 3 deletions
@@ -7,6 +7,7 @@ import { useImeEnterGestureOwnership } from './ime-composition-keyboard-event'
type ImeEnterGestureEventForTest = Pick<
ReactKeyboardEvent,
| 'altKey'
| 'code'
| 'ctrlKey'
| 'key'
| 'keyCode'
@@ -20,6 +21,7 @@ describe('useImeEnterGestureOwnership', () => {
function gestureEvent(init: {
key: string
keyCode: number
code?: string
altKey?: boolean
ctrlKey?: boolean
isComposing?: boolean
@@ -30,6 +32,7 @@ describe('useImeEnterGestureOwnership', () => {
return {
key: init.key,
keyCode: init.keyCode,
code: init.code ?? '',
altKey: init.altKey ?? false,
ctrlKey: init.ctrlKey ?? false,
metaKey: init.metaKey ?? false,
@@ -125,4 +128,61 @@ describe('useImeEnterGestureOwnership', () => {
expect(result.current.ownsKeyDown(redispatch)).toBe(true)
expect(redispatch.prevented).toBe(true)
})
// Each commit ends with compositionend and no keyup; a confirm Enter's echo comes before the next frame.
it.each([
{
name: 'expires a Pinyin commit key that reports no code and no keyup',
commitKey: { key: 'Process', keyCode: 229, code: '' },
committed: '你好',
frameRuns: true,
swallowed: false
},
{
name: 'expires a Hankaku/Zenkaku commit that never sends its keyup (#39289)',
commitKey: { key: 'Process', keyCode: 229, code: 'Backquote' },
committed: 'にほんご',
frameRuns: true,
swallowed: false
},
{
name: 'swallows the bare Enter that echoes a Japanese confirm Enter',
commitKey: { key: 'Enter', keyCode: 229, code: 'Enter' },
committed: '日本語',
frameRuns: false,
swallowed: true
},
{
name: 'swallows the IBus confirm Enter that follows compositionend',
commitKey: null,
committed: '日本語',
frameRuns: false,
swallowed: true
}
])('$name', ({ commitKey, committed, frameRuns, swallowed }) => {
const frames: FrameRequestCallback[] = []
const raf = vi.spyOn(window, 'requestAnimationFrame').mockImplementation((callback) => {
frames.push(callback)
return frames.length
})
try {
const { result } = renderHook(() => useImeEnterGestureOwnership())
result.current.setComposing(true)
if (commitKey) {
result.current.ownsKeyDown(gestureEvent({ ...commitKey, isComposing: true }))
}
result.current.onCompositionEnd(committed)
if (frameRuns) {
for (const frame of frames.splice(0)) {
frame(0)
}
}
const enter = gestureEvent({ key: 'Enter', keyCode: 13, code: 'Enter' })
expect(result.current.ownsKeyDown(enter)).toBe(swallowed)
expect(enter.prevented).toBe(swallowed)
} finally {
raf.mockRestore()
}
})
})
@@ -113,9 +113,10 @@ export function useImeEnterGestureOwnership(): {
stateRef.current.composing = false
if (HANGUL_ONLY.test(committedText)) {
stateRef.current.pendingEnter = null
} else if (compositionWasActive && !stateRef.current.pendingEnter) {
// IBus can finish composition before its only unmarked confirming Enter.
stateRef.current.pendingEnter = {}
} else if (compositionWasActive) {
// IBus can finish composition before its only unmarked confirming Enter, and a marked
// commit key may never send the keyup that would otherwise expire its carry.
stateRef.current.pendingEnter ??= {}
expirePendingEnter()
}
},