lexical icon indicating copy to clipboard operation
lexical copied to clipboard

Bug: Tab + Korean IME composition freezes the editor in Safari (dev + prod)

Open mayrang opened this issue 6 months ago • 2 comments

Lexical version: 0.45 (latest main fd2e1856c)

Found while manual-testing #8594. The Korean IME path looks like it has been broken since the TabNode.setTextContent invariant was added, but it's not in any of the existing reports I could find — probably a long-standing edge case.

Steps To Reproduce

  1. Open https://playground.lexical.dev in Safari on macOS.
  2. Type foo.
  3. Press Tab.
  4. Start typing Korean (e.g. ㅁ).

https://github.com/user-attachments/assets/e360e04c-4598-42bf-ac2e-7d5d05d3e400

Reproduces on:

  • macOS Safari (verified 26.3.1).
  • Deployed playground.lexical.dev and a local vite build + vite preview of lexical-playground (production build).
  • Local vite dev (development build).
  • All Lexical revisions tested via bisect, including a 2025-02 baseline.

Does not reproduce in Chromium with the same Korean IME.

Link to code example: playground.lexical.dev (default configuration).

The current behavior

The editor freezes: composition stays stuck, Backspace and Enter stop responding, English input doesn't recover the editor. Esc partially restores it (English works again), clicking outside and back partially restores Enter; only a hard refresh is a full recovery.

What I observed in DevTools:

After Tab + the first Korean key, Safari's MutationObserver delivers a text change on the TabNode's \t text node — composition targets the <span>\t</span> and Safari writes the new character into the existing text node (\tㅁ).

flushMutations translates that into TabNode.setTextContent('\tㅁ'). The current setTextContent throws (devInvariant against '\t' / ''). The throw goes through onError and breaks the active update; subsequent input / selection / composition events keep re-entering the same broken path:

Error: TabNode does not support setTextContent
  flushMutations
Error: $getTextNodeOffset: invalid offset 2 for size 1 at key 5
  onCompositionStart
  onInput
  onSelectionChange

In the dev bundle the readable message shows. In a local vite build of the playground the minified prod bundle still throws — the call site bundles to a function(t){throw new Error(t)} rather than a warn-only formatter. Whatever transformErrorMessages rewrite the Lexical npm release uses for devInvariant doesn't reach this build, so the throw still cascades end-to-end.

Existing context:

  • #7601 (closed by #7602) covered the "select TabNode + adjacent text, type → text inserted into TabNode" path. That selection path is fixed (token-mode TabNode); this IME path is a different hit on the same setTextContent.
  • #7855 already noted that the production throw on TabNode.setTextContent had cost users sync state ("documents weren't syncing correctly"). The intent of moving invariant to devInvariant was to silence the throw in prod, but as the bundle above shows, in at least our local prod build the call still throws.

The expected behavior

Tab followed by Korean IME composition should behave the same as in Chromium: the Tab character stays in the document as a TabNode, IME composition completes on a new TextNode next to it, and the editor remains responsive.

Making TabNode.setTextContent accept any input and silently normalize to '\t' clears the cascade in both dev and prod for me:

setTextContent(_text: string): this {
  return super.setTextContent('\t');
}

After the change: Korean composition completes, Backspace removes the TabNode, Enter inserts a paragraph, English typing resumes, copy/paste preserves \t, Markdown shortcuts still fire.

A follow-up PR with a unit test that simulates the MutationObserver-driven setTextContent('\tX') call would keep the regression out. If the throw should stay (e.g. a different flushMutations hook strips the foreign text before reaching setTextContent), point at the better shape and I'll move that direction.

Impact of fix

  • Severity: editor unrecoverably frozen after a common keystroke sequence; requires a hard refresh.
  • Affects any Lexical-based editor that enables TabIndentationExtension (or otherwise inserts TabNode) and is used by Korean / Japanese / similar IME users on Safari. CJK-language adoption surface area.
  • Fix is one line plus a unit test.

mayrang avatar May 30 '26 15:05 mayrang

Are you sure that this doesn't create a different class of bug where the additional text is just lost?

etrepum avatar May 30 '26 16:05 etrepum

When filing the issue I tried the normalize-to-\t approach locally and exercised the same Tab + Korean composition path, plus a few neighboring cases (Backspace removing the TabNode, Enter, English typing, copy/paste). Nothing visibly went missing in the editor state. To check that more directly I attached a MutationObserver on the contenteditable and ran foo + Tab + 안녕하세요:

[CHILD] P + SPAN: "\t"          // TabNode added
[CHAR] "\t" → "\t "             // Safari injects a space onto the TabNode text
[CHAR] "\t " → "\tㅇ "           // Safari writes the first Korean jamo onto the TabNode text
[CHAR] "\tㅇ " → "\t"            // setTextContent('\tㅇ ') hits, normalize-to-\t runs
[CHILD] P + SPAN: "ㅇ "          // a sibling SPAN is created next to the TabNode
[CHAR] "ㅇ " → "아 " → "안 " → ...  // composition continues on the sibling span until 안녕하세요

Tree view after the repro:

root
  └ paragraph
    ├ text "foo"
    ├ tab "\t"
    └ text "안녕하세요"

The Korean character lands on a new sibling text node — the input / CONTROLLED_TEXT_INSERTION path picks it up after the normalize. I haven't traced down to the exact handler that creates the sibling, but the sibling-text-node result is reproducible across a few different first jamos and longer compositions.

mayrang avatar May 30 '26 16:05 mayrang