Repository navigation
fix(muya): keep IME input intact after a soft line break - #5281
Merged
Merged
Conversation
Chromium drops the trailing `\n` from the DOM as soon as text is typed at the end of a block that ends in one; `lineBreakAutoPair` re-attaches it and already carried a `compositionend` branch for the IME case. That branch was unreachable. `composeHandler` forwards the raw `CompositionEvent` into `inputHandler`, but `autoPair` gates on `isInputEvent` (`'inputType' in event`) and a CompositionEvent has no `inputType`, so the IME commit returned before reaching the repair. A Shift+Enter soft line break was therefore lost whenever the next text came from a CJK IME — both lines collapsed into one. Let `compositionend` past the guard. The auto-pair branch beside it stays restricted to real input events, so only the line-break repair gains the new path. Fixes #5279 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01QzDANHQnQyWY3pMWQVR4yB
Two layers, both driven by what Chromium actually does rather than by a hand-staged DOM. The E2E spec drives a real composition through CDP `Input.imeSetComposition` and lets the browser mutate the DOM itself. The existing `ime.spec.ts` dispatches a hand-built event sequence, which cannot catch this class of bug: it stages the DOM state the test author expects, not the one Chromium produces. The unit spec replays that captured mutation — the composed text overwrites the `\n` held by the soft-line-break span — and builds the commit event with the real `CompositionEvent` constructor, under jsdom because only jsdom carries `data` through it. Each spec pairs the IME case with the same edit typed without an IME, so a harness that stopped exercising the editor fails loudly instead of passing by construction. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01QzDANHQnQyWY3pMWQVR4yB
The regression test pinning the `compositionend` branch built its event as an object literal carrying `inputType: ''`. That key is what `isInputEvent` looks for, so the mock sailed past the entry guard that rejects every real CompositionEvent — the test stayed green for years while the production path it claimed to pin was dead code (#5279). Drop the key so the mock has the shape of the event it stands in for. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01QzDANHQnQyWY3pMWQVR4yB
`lineBreakAutoPair` nudged the DOM offset by one after rebuilding the text, skipping the nudge on Firefox under a comment admitting nobody knew why. The reason is that the browsers disagree about the text being rebuilt: Chromium overwrites the block's trailing newline the moment anything is typed after it, Firefox leaves it in place. So Chromium's DOM offset is one short of the rebuilt text and Firefox's already matches. Setting the offset to the end of the rebuilt text says that directly and holds for both, so the browser test goes away. Also require `event.data` to be a string before appending it — a cancelled composition ends with `null` and would otherwise be pasted in verbatim. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01QzDANHQnQyWY3pMWQVR4yB
A block whose text ends with `\n` renders that newline as the last thing in its DOM, so a caret at the end of the block sits inside the newline's own text node. Chromium will not sustain an IME composition there — it overwrites the newline and abandons the composition, and the input method loses the keystroke that started the word. Typing `di'er'hang` for 第二行 left the IME matching `i'er'hang` and committing `i 二行` (#3468, open since 2022 and present in the old engine too). Seed a zero-width space for the composition to live in instead, on `compositionstart`, and strip it again on `compositionend`. It exists only while `inputHandler` is short-circuited by `isComposed`, so it is never visible to the DOM ↔ text offset mapping. An empty text node does not work; Chromium ignores it and goes back to the newline. Two details the browser forces: - `mu-line-end` gives the trailing newline a block box of its own, because a newline with nothing after it renders no line at all. The anchor is now that something, so the class is suspended while the anchor is parked — otherwise the block keeps a line to itself and the composition renders one line below the caret. - The caret goes in front of the zero-width space. Chromium composes at the caret and leaves the caret where it was, so a trailing anchor would draw the caret to the left of the glyph being typed. When a composition is cancelled or backspaced away the anchor goes with it, taking the caret along, so the caret is put back at the end of the block's own text. Fixes #3468 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01QzDANHQnQyWY3pMWQVR4yB
…ommit The existing assertions could not fail for the reason that matters. The commit is rebuilt from the model plus `event.data`, so the markdown comes out right even when the browser overwrote the block's trailing newline mid-composition — the case that costs the input method its first keystroke. Assert the newline is still in the live DOM while the composition is open, and that only one `compositionstart` fired: a newline the browser overwrites takes the composition down with it and the IME restarts. Both were verified to fail with the anchor removed while the non-IME control kept passing. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01QzDANHQnQyWY3pMWQVR4yB
Closed
1 task done
This was referenced Sep 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #5279
Fixes #3468
Shift+Enter, then type the next line with a CJK IME. Two separate defects met there; both are fixed here, and the second turned out to be the older and the worse of the two.
1. The soft line break was lost (#5279 — a regression)
Chromium overwrites a block's trailing
\nthe moment anything is typed after it.lineBreakAutoPairputs it back, and it already carried a branch for the IME case:That branch was unreachable.
composeHandlerforwards the rawCompositionEventintoinputHandler, butautoPairgates onisInputEvent—'inputType' in event— and aCompositionEventhas noinputType. Every IME commit returned before reaching the repair.The old JS engine has no such gate (
packages/muyajs/lib/contentState/inputCtrl.js, same repair, samecompositionendcondition), so this is a regression from the TypeScript rewrite. Lettingcompositionendpast the guard restores the old engine's behaviour.Why it shipped with a green test: the regression test pinning that branch built its event as an object literal carrying
inputType: ''— the very key the guard looks for. The mock sailed past a guard that rejects every realCompositionEvent, so the test passed while the path it claimed to pin was dead code.2. The IME lost the first keystroke of the word (#3468 — open since 2022)
With the newline preserved, a second defect became visible: typing
di'er'hangfor 第二行 producedi 二行.A block whose text ends with
\nrenders that newline as the last thing in its DOM, so a caret at the end of the block sits inside the newline's own text node. Chromium will not sustain a composition there — it overwrites the newline and abandons the composition, and the input method never receives the keystroke that started the word. Captured from a real composition driven through CDPInput.imeSetComposition:This is #3468 verbatim ("输入拼音,第一个字母会被吞掉,输入法中识别不到"), reported in 2022 against the old engine and reproducible there today — it was closed this June on the strength of the code the investigation above shows to be dead.
Fix: seed a zero-width space for the composition to live in on
compositionstart, and strip it oncompositionend. It exists only whileinputHandleris short-circuited byisComposed, so it is never visible to the DOM ↔ text offset mapping. Two details the browser forces:mu-line-endgives the trailing newline a block box of its own, because a newline with nothing after it renders no line at all. The anchor is now that something, so the class is suspended while the anchor is parked; otherwise the composition renders one line below the caret.Measured in real Chromium, mid-composition:
第一行dierhang— newline gone第一行d第二行第一行\ndierhang— newline intact第一行\n第二行3. Offset compensation, unified
lineBreakAutoPairnudged the DOM offset by one and skipped the nudge on Firefox, under a comment admitting nobody knew why. The reason is that the browsers disagree about the text being rebuilt: Chromium overwrites the trailing newline, Firefox leaves it. Setting the offset to the end of the rebuilt text says that directly and holds for both, so the browser test goes away.event.datais now also required to be a string — a cancelled composition ends withnull.Verified
Driven through CDP
Input.imeSetCompositionagainst the E2E host, and confirmed by hand with the WeChat IME on macOS:第一行\nabc, caret at 7第一行\n第二行, caret at 7, onecompositionstart第一行\n第二行Unit suite 215 files / 1468 tests,
tsc --noEmit,eslint src test(0 errors),madge --circular,stylelintall clean. The handful of E2E specs that fail locally (cross-block-enter,search-replace,ime.spectable cell) fail identically on unmodifieddevelopin the same setup — the baseline failed 7, this branch 4.On the tests
No unit test for the anchor: jsdom does not reproduce Chromium's composition behaviour, so a unit test there would assert the implementation back at itself.
The E2E spec instead asserts the newline is still in the live DOM while the composition is open, and that only one
compositionstartfired. The commit is rebuilt from the model plusevent.data, so a markdown assertion alone comes out right even with the anchor removed — these two do not. Both were verified to fail with the anchor disabled while the non-IME control kept passing.🤖 Generated with Claude Code
https://claude.ai/code/session_01QzDANHQnQyWY3pMWQVR4yB