Scenario

Composition event lifecycle inconsistencies across browsers

Analysis of how out-of-order or missing composition events disrupt editor state synchronization.

ime
Scenario ID
scenario-composition-events

Details

Problem Overview

For CJK (Chinese, Japanese, Korean) languages, the Composition event lifecycle is as important as the Input event loop. Frameworks often “lock” their state during composition to prevent the browser’s intermediate DOM states from corrupting the internal model. If events fire out of order, or if isComposing flags are incorrectly set, the framework may prematurely “unlock” and perform a destructive sync.

Observed Behavior

Scenario 1: Event Order Inversion (Safari)

In Safari, when committing with Enter, compositionend fires before the matching keydown. This clears the isComposing flag too early.

/* Expected Sequence */
// 1. keydown (key: Enter, isComposing: true)
// 2. compositionend

/* Safari Sequence */
// 1. compositionend (State unlocked!)
// 2. keydown (key: Enter, isComposing: false) -> Triggers newline/send!

Scenario 2: Missing Events (Dictation/Auto-correct)

OS-level features like iOS Dictation or macOS auto-correct (double-space period) often modify text by injecting characters directly, sometimes bypassing compositionupdate entirely.

impact

  • Premature Command Execution: Enter keys meant for composition are treated as editor commands (e.g., new paragraph).
  • History Corruption: The undo stack treats the final character as a separate operation from the composition.
  • Visual Stutter: The editor model and DOM fight over the uncommitted text.

Browser Comparison

  • WebKit (Safari): Notable for the “order inversion” bug during Enter commits.
  • Blink (Chrome): Most compliant, but known for “ghost” compositionupdate events during rapid typing.
  • Gecko (Firefox): Fires keydown with keyCode 229 consistently, making it the most reliable to detect IME activity.

Solutions

1. The “Lock Delay” (Safari fix)

Use a short timeout after compositionend before allowing keydown events to proceed normally.

let imeLock = false;
element.addEventListener('compositionstart', () => { imeLock = true; });
element.addEventListener('compositionend', () => {
  setTimeout(() => { imeLock = false; }, 40); // Catch the Safari keydown
});

2. keyCode 229 Check

Reliably detect IME activity across engines by checking the deprecated but universally supported keyCode 229.

References

Scenario flow

Visual view of how this scenario connects to its concrete cases and environments. Nodes can be dragged and clicked.

React Flow mini map

Variants

Each row is a concrete case for this scenario, with a dedicated document and playground.

Case OS Device Browser Keyboard Status
ce-0034-composition-events-missing macOS Ubuntu 22.04 Desktop or Laptop Any Safari 120.0 Chinese IME draft
ce-0567-safari-composition-event-order macOS 15.0 Desktop Any Safari 18.0 Japanese IME confirmed

Browser compatibility

This matrix shows which browser and OS combinations have documented cases for this scenario. Click on a cell to view the specific case.

Confirmed
Draft
No case documented

Cases

This scenario affects multiple languages. Cases are grouped by language/input method below.

Chinese

1 case

Japanese

1 case

Related Scenarios

Other scenarios that share similar tags or category.

Tags: ime, composition

beforeinput and input events have different inputType values

During IME composition or in certain browser/IME combinations, the beforeinput event may have a different inputType than the corresponding input event. For example, beforeinput may fire with insertCompositionText while input fires with deleteContentBackward. This mismatch can cause handlers to misinterpret the actual DOM change and requires storing beforeinput's targetRanges for use in input event handling.

1 case
Tags: ime, composition

Selection mismatch between beforeinput and input events

The selection (window.getSelection()) in beforeinput events can differ from the selection in corresponding input events. This mismatch can occur during IME composition, text prediction, or when typing adjacent to formatted elements like links. The selection in beforeinput may include adjacent formatted text, while input selection reflects the final cursor position.

1 case
Tags: ime, composition

getTargetRanges() returns empty array in beforeinput events

The getTargetRanges() method in beforeinput events may return an empty array or undefined in various scenarios, including text prediction, certain IME compositions, or specific browser/device combinations. When getTargetRanges() is unavailable, developers must rely on window.getSelection() as a fallback, but this may be less accurate.

1 case
Tags: ime, composition

Korean IME composition causes editor crash (Firefox)

On Firefox with Windows 10 and Korean IME, specific key combination during IME composition causes the editor to crash. The crash occurs when typing certain sequences with the Korean IME.

1 case

Comments & Discussion

Have questions, suggestions, or want to share your experience? Join the discussion below.