fix(ime-guard): trust keyCode 229 only inside a live composition context
Synthetic 229 keydowns emitted outside any IME (layout-switcher utilities, e.g. KeyRay's layout swap: backspaces + one keydown carrying the whole converted word via CGEventKeyboardSetUnicodeString, which Chromium reports as keyCode 229 / isComposing=false) were swallowed by the document-capture guard, so only the backspaces landed and the replacement text silently disappeared. Now the legacy 229 signal is trusted only while a composition is live, plus a 50ms grace window after compositionend (covers legacy engines' closing 229 keydown with isComposing === false, e.g. Safari). Real IME composition keys (isComposing === true) are unaffected.
This commit is contained in:
1 parent
3aa523de1c
commit
7599d4ac02
2 files changed
+85
-9
No files matched your search
+39
-2
@@ -23,14 +23,38 @@
|
||||
* The composition signal follows the DSH core convention (InputBar's IME
|
||||
* guard, issue #535): `isComposing` for modern engines, keyCode 229 as the
|
||||
* legacy signal engines emit without isComposing.
|
||||
*
|
||||
* The legacy 229 signal is only trusted while (or right after) a real
|
||||
* composition is running. Legacy engines (Safari) emit 229 keydowns with
|
||||
* `isComposing === false` *during* a composition, and that context is what
|
||||
* distinguishes them from synthetic 229 keydowns emitted outside any IME —
|
||||
* layout-switcher utilities (e.g. KeyRay's layout swap) send a keydown
|
||||
* carrying the whole converted word as a multi-character unicode string,
|
||||
* Chromium reports it as keyCode 229 / isComposing=false, and swallowing it
|
||||
* made the backspaces land while the replacement text never did: the word
|
||||
* silently disappeared instead of being rewritten.
|
||||
*/
|
||||
|
||||
/** Live composition context, tracked by the guard's composition listeners. */
|
||||
let guardComposing = false
|
||||
/** Grace window (ms) after compositionend that still counts as composition,
|
||||
* covering legacy engines' closing 229 keydown with isComposing === false. */
|
||||
const GUARD_COMPOSITION_END_GRACE_MS = 50
|
||||
let guardComposingUntil = 0
|
||||
|
||||
function imeCompositionLive(): boolean {
|
||||
return guardComposing || Date.now() < guardComposingUntil
|
||||
}
|
||||
|
||||
/** The pure decision: is this keyboard event part of an IME composition?
|
||||
* `isComposing` is optional on the input: React's synthetic KeyboardEvent
|
||||
* type does not declare it (the DOM event always carries it), and the
|
||||
* keyCode 229 fallback covers exactly those callers. */
|
||||
* keyCode 229 fallback covers exactly those callers — but only inside a
|
||||
* live composition context (see the module doc above). */
|
||||
export function isImeComposition(event: { isComposing?: boolean; keyCode: number }): boolean {
|
||||
return event.isComposing === true || event.keyCode === 229
|
||||
if (event.isComposing === true) return true
|
||||
if (event.keyCode !== 229) return false
|
||||
return imeCompositionLive()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -41,10 +65,23 @@ export function registerImeGuard(): () => void {
|
||||
const onKey = (event: KeyboardEvent): void => {
|
||||
if (isImeComposition(event)) event.stopPropagation()
|
||||
}
|
||||
const onCompositionStart = (): void => {
|
||||
guardComposing = true
|
||||
}
|
||||
const onCompositionEnd = (): void => {
|
||||
guardComposing = false
|
||||
guardComposingUntil = Date.now() + GUARD_COMPOSITION_END_GRACE_MS
|
||||
}
|
||||
document.addEventListener('keydown', onKey, true)
|
||||
document.addEventListener('keyup', onKey, true)
|
||||
document.addEventListener('compositionstart', onCompositionStart, true)
|
||||
document.addEventListener('compositionend', onCompositionEnd, true)
|
||||
return () => {
|
||||
document.removeEventListener('keydown', onKey, true)
|
||||
document.removeEventListener('keyup', onKey, true)
|
||||
document.removeEventListener('compositionstart', onCompositionStart, true)
|
||||
document.removeEventListener('compositionend', onCompositionEnd, true)
|
||||
guardComposing = false
|
||||
guardComposingUntil = 0
|
||||
}
|
||||
}
|
||||
+46
-7
@@ -3,8 +3,10 @@
|
||||
* IME-composition guard tests.
|
||||
*
|
||||
* The guard registers document CAPTURE-phase keydown/keyup listeners that
|
||||
* stopPropagation() while an input method is composing (isComposing or the
|
||||
* legacy keyCode 229). These tests pin:
|
||||
* stopPropagation() while an input method is composing (isComposing, or the
|
||||
* legacy keyCode 229 *inside a live composition context* — bare synthetic
|
||||
* 229 keydowns from layout-switcher utilities must pass through). These tests
|
||||
* pin:
|
||||
*
|
||||
* 1. the pure decision (`isImeComposition`);
|
||||
* 2. the native-listener path — a bubble listener on `document` and a
|
||||
@@ -22,7 +24,7 @@
|
||||
* production — dispatching on `document` itself would run all listeners in
|
||||
* the target phase where stopPropagation does not stop same-node listeners.
|
||||
*/
|
||||
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { createElement } from 'react'
|
||||
import { createRoot, type Root } from 'react-dom/client'
|
||||
import { act } from 'react-dom/test-utils'
|
||||
@@ -48,8 +50,11 @@ describe('isImeComposition', () => {
|
||||
expect(isImeComposition({ isComposing: true, keyCode: 0 })).toBe(true)
|
||||
})
|
||||
|
||||
it('treats keyCode 229 as composition (legacy engines without isComposing)', () => {
|
||||
expect(isImeComposition({ isComposing: false, keyCode: 229 })).toBe(true)
|
||||
it('treats keyCode 229 as composition only inside a live composition context', () => {
|
||||
// No composition events were dispatched, so no composition is live: a bare
|
||||
// synthetic 229 (layout-switcher utilities emit these) must NOT count.
|
||||
expect(isImeComposition({ isComposing: false, keyCode: 229 })).toBe(false)
|
||||
expect(isImeComposition({ isComposing: true, keyCode: 229 })).toBe(true)
|
||||
})
|
||||
|
||||
it('treats both signals together as composition', () => {
|
||||
@@ -108,12 +113,46 @@ describe('registerImeGuard — native listeners', () => {
|
||||
expect(seen).toEqual([])
|
||||
})
|
||||
|
||||
it('blocks composition keys signalled by keyCode 229 only', () => {
|
||||
it('blocks a keyCode 229 keydown inside a live composition (legacy engines emit 229 with isComposing=false)', () => {
|
||||
dispose = registerImeGuard()
|
||||
input.dispatchEvent(keyEvent('keydown', { key: 'ArrowDown', keyCode: 229 }))
|
||||
document.dispatchEvent(new Event('compositionstart'))
|
||||
input.dispatchEvent(keyEvent('keydown', { key: 'ArrowDown', isComposing: false, keyCode: 229 }))
|
||||
expect(seen).toEqual([])
|
||||
})
|
||||
|
||||
it('keeps blocking a keyCode 229 keydown right after compositionend (legacy closing keydown)', () => {
|
||||
vi.useFakeTimers()
|
||||
try {
|
||||
dispose = registerImeGuard()
|
||||
document.dispatchEvent(new Event('compositionstart'))
|
||||
document.dispatchEvent(new Event('compositionend'))
|
||||
input.dispatchEvent(keyEvent('keydown', { key: 'a', isComposing: false, keyCode: 229 }))
|
||||
expect(seen).toEqual([])
|
||||
} finally {
|
||||
vi.useRealTimers()
|
||||
}
|
||||
})
|
||||
|
||||
it('lets a bare synthetic keyCode 229 keydown through when no composition ever started (KeyRay regression)', () => {
|
||||
dispose = registerImeGuard()
|
||||
input.dispatchEvent(keyEvent('keydown', { key: 'ArrowDown', isComposing: false, keyCode: 229 }))
|
||||
expect(seen).toEqual(['input:keydown', 'document:keydown'])
|
||||
})
|
||||
|
||||
it('lets a keyCode 229 keydown through after the post-compositionend grace window expires', () => {
|
||||
vi.useFakeTimers()
|
||||
try {
|
||||
dispose = registerImeGuard()
|
||||
document.dispatchEvent(new Event('compositionstart'))
|
||||
document.dispatchEvent(new Event('compositionend'))
|
||||
vi.advanceTimersByTime(51)
|
||||
input.dispatchEvent(keyEvent('keydown', { key: 'ArrowDown', isComposing: false, keyCode: 229 }))
|
||||
expect(seen).toEqual(['input:keydown', 'document:keydown'])
|
||||
} finally {
|
||||
vi.useRealTimers()
|
||||
}
|
||||
})
|
||||
|
||||
it('still blocks after the composition signal is gone (guard is per-event)', () => {
|
||||
dispose = registerImeGuard()
|
||||
input.dispatchEvent(keyEvent('keydown', { key: 'ArrowDown', isComposing: true }))
|
||||
|
||||
Reference in new issue
Block a user