insertParagraph preventDefault breaks Chinese IME composition in Safari
OS: macOS 14.0+ · Device: Desktop or Laptop Any · Browser: Safari 17.0+
Open case →Scenario
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.
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.
input event may not fire for subsequent IME inputThe issue occurs when:
insertParagraphpreventDefault() in keydown or beforeinput event handler to prevent the paragraph insertioninput events or insert characters correctlyThe problematic sequence:
keydown (Enter key) - if preventDefault() is called herebeforeinput (inputType: “insertParagraph”) - if preventDefault() is called herecompositionstart may not firecompositionupdate may not fireinput event may not fireAccording to research and WebKit bug reports:
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.
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.
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.
Non-cancelable Composition Events: Composition-related input events (insertCompositionText) are not cancelable per spec, but canceling insertParagraph may interfere with the composition lifecycle.
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
}
});
editor.addEventListener('keydown', (e) => {
if (e.key === 'Enter' && !e.isComposing) {
e.preventDefault();
// Custom paragraph insertion
}
// If isComposing is true, let the default behavior proceed
});
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
}
});
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);
}
Visual view of how this scenario connects to its concrete cases and environments. Nodes can be dragged and clicked.
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 |
This matrix shows which browser and OS combinations have documented cases for this scenario. Click on a cell to view the specific case.
| Browser | macOS |
|---|---|
| Safari |
This scenario affects multiple languages. Cases are grouped by language/input method below.
OS: macOS 14.0+ · Device: Desktop or Laptop Any · Browser: Safari 17.0+
Open case →OS: macOS 14.0+ · Device: Desktop or Laptop Any · Browser: Safari 17.0+
Open case →OS: macOS 14.0+ · Device: Desktop or Laptop Any · Browser: Safari 17.0+
Open case →Other scenarios that share similar tags or category.
Some browsers and keyboards emit duplicate composition-related input or beforeinput events—especially iOS Safari dictation paths and certain Android keyboards—so naive handlers that insert text on every input may double characters or corrupt state.
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.
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.
Some beforeinput events during IME composition cannot be canceled per spec or implementation—calling preventDefault may throw or be ignored, so editors cannot always block native insertion.
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.
Have questions, suggestions, or want to share your experience? Join the discussion below.