Skip to content

fix(muya): keep IME input intact after a soft line break - #5281

Merged
Jocs merged 6 commits into
developfrom
fix/5279-ime-soft-line-break
Sep 10, 2026
Merged

Jocs merged 6 commits into
developfrom
fix/5279-ime-soft-line-break

Conversation

@Jocs

@Jocs Jocs commented Sep 10, 2026 •

Copy link
Copy Markdown
Member

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 \n the moment anything is typed after it. lineBreakAutoPair puts it back, and it already carried a branch for the IME case:

&& (event.inputType === 'insertText' || event.type === 'compositionend')

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. Every IME commit returned before reaching the repair.

The old JS engine has no such gate (packages/muyajs/lib/contentState/inputCtrl.js, same repair, same compositionend condition), so this is a regression from the TypeScript rewrite. Letting compositionend past 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 real CompositionEvent, 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'hang for 第二行 produced i 二行.

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 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 CDP Input.imeSetComposition:

compositionstart   blockText="第一行\n"   domText="第一行\n"
input  d           domText="第一行d"        ← the newline is gone
compositionstart   ← again: the composition restarted, `d` is now plain text
input  i / ie / i'er'hang                   ← the IME's buffer no longer has the `d`
compositionend data="i 二行"

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 on compositionend. It exists only while inputHandler is short-circuited by isComposed, so it is never visible to the DOM ↔ text offset mapping. Two details the browser forces:

  • An empty text node does not work — Chromium ignores it and goes back to the newline.
  • 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 composition renders one line below the caret.

Measured in real Chromium, mid-composition:

DOM during composition at commit
before 第一行dierhang — newline gone 第一行d第二行
after 第一行\ndierhang​ — newline intact 第一行\n第二行

3. Offset compensation, unified

lineBreakAutoPair nudged 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.data is now also required to be a string — a cancelled composition ends with null.

Verified

Driven through CDP Input.imeSetComposition against the E2E host, and confirmed by hand with the WeChat IME on macOS:

plain typing after a soft break 第一行\nabc, caret at 7
IME commit 第一行\n第二行, caret at 7, one compositionstart
composition cancelled / backspaced away caret returns to where it started
IME text then Backspace steps down character by character, newline intact — matches the non-IME control
code block: Enter then IME 第一行\n第二行
lines shown while composing 2, was 3

Unit suite 215 files / 1468 tests, tsc --noEmit, eslint src test (0 errors), madge --circular, stylelint all clean. The handful of E2E specs that fail locally (cross-block-enter, search-replace, ime.spec table cell) fail identically on unmodified develop in 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 compositionstart fired. The commit is rebuilt from the model plus event.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

Jocs and others added 6 commits September 10, 2026 12:27
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
@Jocs Jocs changed the title fix(muya): keep the soft line break when an IME commits the next text fix(muya): keep IME input intact after a soft line break Sep 10, 2026
@Jocs
Jocs merged commit 6402160 into develop Sep 10, 2026
7 checks passed
@Jocs
Jocs deleted the fix/5279-ime-soft-line-break branch September 10, 2026 06:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant