Scenario

insertParagraph preventDefault breaks IME composition state in Safari

In Safari desktop, when preventDefault() is called on keydown or beforeinput events for insertParagraph (Enter key), the IME composition state becomes corrupted. Subsequent text input fails to trigger proper input events, causing characters to not be inserted or composition to malfunction.

ime
Scenario ID
scenario-insertparagraph-preventdefault-composition-broken

Details

Overview

In Safari desktop, when a developer intercepts the insertParagraph action (typically triggered by pressing Enter) by calling preventDefault() in either the keydown or beforeinput event handler, the browser’s internal IME (Input Method Editor) composition state becomes corrupted. After this occurs, subsequent text input using IME (Korean, Japanese, Chinese, etc.) fails to work correctly. The input event may not fire properly, characters may not be inserted, or the composition state may remain in an inconsistent state.

Impact

  • IME Input Failure: After preventing insertParagraph, IME input stops working correctly
  • Missing Input Events: The input event may not fire for subsequent IME input
  • Composition State Corruption: IME composition state becomes inconsistent
  • Character Loss: Typed characters may not appear in the contenteditable element
  • User Experience Degradation: Users cannot continue typing after Enter is prevented

Technical Details

The issue occurs when:

  1. User is typing with IME (Korean, Japanese, Chinese, etc.) in a contenteditable element
  2. User presses Enter key, which triggers insertParagraph
  3. Developer calls preventDefault() in keydown or beforeinput event handler to prevent the paragraph insertion
  4. Safari suppresses the default paragraph insertion behavior
  5. However, Safari’s internal IME composition state management becomes corrupted
  6. Subsequent IME input fails to trigger proper input events or insert characters correctly

Event Sequence

The problematic sequence:

  1. keydown (Enter key) - if preventDefault() is called here
  2. beforeinput (inputType: “insertParagraph”) - if preventDefault() is called here
  3. IME composition state becomes corrupted
  4. Next IME input attempt:
    • compositionstart may not fire
    • compositionupdate may not fire
    • input event may not fire
    • Characters may not be inserted

Browser Comparison

  • Safari (Desktop): This issue occurs. Preventing insertParagraph breaks subsequent IME input
  • Chrome: May have different behavior, needs verification
  • Firefox: May have different behavior, needs verification
  • Edge: May have different behavior, needs verification

Root Cause Analysis

According to research and WebKit bug reports:

  1. Composition Event Overlap: When Enter is pressed during or after IME composition, Safari needs to handle both the paragraph insertion and composition state transitions. Preventing the paragraph insertion disrupts this coordination.

  2. Internal State Mismatch: Safari’s internal IME state tracking becomes out of sync with the actual DOM state when insertParagraph is prevented, especially if there are formatting wrappers or empty nodes.

  3. Event Ordering Issues: WebKit has known issues with event ordering around composition, where keydown that ends a composition session may fire after compositionend, or have incorrect isComposing values.

  4. Non-cancelable Composition Events: Composition-related input events (insertCompositionText) are not cancelable per spec, but canceling insertParagraph may interfere with the composition lifecycle.

Workarounds

1. Check Composition State Before Preventing

Only prevent insertParagraph when not composing:

editor.addEventListener('beforeinput', (e) => {
  if (e.inputType === 'insertParagraph') {
    // Only prevent if not currently composing
    if (!e.isComposing) {
      e.preventDefault();
      // Custom paragraph insertion logic
    }
    // If composing, allow default behavior to maintain IME state
  }
});

2. Handle Enter in keydown with Composition Check

editor.addEventListener('keydown', (e) => {
  if (e.key === 'Enter' && !e.isComposing) {
    e.preventDefault();
    // Custom paragraph insertion
  }
  // If isComposing is true, let the default behavior proceed
});

3. Defer Custom Behavior Until After Composition

let isComposing = false;

editor.addEventListener('compositionstart', () => {
  isComposing = true;
});

editor.addEventListener('compositionend', () => {
  isComposing = false;
});

editor.addEventListener('beforeinput', (e) => {
  if (e.inputType === 'insertParagraph' && isComposing) {
    // Don't prevent during composition
    return;
  }
  if (e.inputType === 'insertParagraph') {
    e.preventDefault();
    // Custom logic
  }
});

4. Manual IME State Reset (Advanced)

If composition state is already corrupted, attempt to reset it:

function resetIMEState(editor) {
  // Blur and refocus to reset IME state
  editor.blur();
  setTimeout(() => {
    editor.focus();
    // Clear any lingering composition state
    const selection = window.getSelection();
    if (selection.rangeCount > 0) {
      selection.removeAllRanges();
      selection.addRange(selection.rangeCount === 0 ? 
        document.createRange() : selection.getRangeAt(0));
    }
  }, 0);
}
  • Case IDs will be added as cases are created for specific environment combinations

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-0557-insertparagraph-preventdefault-composition-broken-safari-korean macOS 14.0+ Desktop or Laptop Any Safari 17.0+ Korean (IME) draft
ce-0558-insertparagraph-preventdefault-composition-broken-safari-japanese macOS 14.0+ Desktop or Laptop Any Safari 17.0+ Japanese (IME) draft
ce-0559-insertparagraph-preventdefault-composition-broken-safari-chinese macOS 14.0+ Desktop or Laptop Any Safari 17.0+ Chinese (IME - Pinyin) draft

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

Korean

1 case

Related Scenarios

Other scenarios that share similar tags or category.

Tags: ime, composition, beforeinput

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, beforeinput

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, beforeinput

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: safari, ime, beforeinput

iOS dictation triggers duplicate input events after completion

On iOS, when using voice dictation to input text into contenteditable elements, the system may fire duplicate beforeinput and input events after the initial dictation completes. The text is split into words and events are re-fired, causing synchronization issues. Composition events do not fire during dictation, making it difficult to distinguish dictation from keyboard input.

1 case

Comments & Discussion

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