Repository navigation
[lexical] Bug Fix: TabNode.setTextContent for Safari IME composition - #8598
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
…or Safari IME compat (meta#8596) In Safari, the MutationObserver delivers mid-IME-composition text writes straight onto the `TabNode`'s `\t` text node — Korean / Japanese / similar IMEs compose onto the existing text node, and Safari rewrites it to `\tㅁ`. `flushMutations` then calls `TabNode.setTextContent('\tㅁ')`, the `devInvariant` throws, and the throw cascades through `onError`, freezing the editor (composition, Backspace, Enter, English input all stop working until a hard refresh). The throw existed since `TabNode` was introduced, but the only path that surfaced it before was selection-based typing across the node (fixed by treating the node as token-mode in meta#7602). The MutationObserver / IME path was uncovered. Drop the `devInvariant` check entirely. The body already normalizes to `\t` via `super.setTextContent('\t')`, so the throw never carried information the reconciler couldn't recover from — the next update re-renders the canonical `\t`. A unit test exercises the exact `setTextContent('\tㅁ')` shape `flushMutations` produces so the regression doesn't return.
bd64ca8 to
173a914
Compare
Picks up meta#8593 (linear-time regexes / prototype-pollution guards / faster CSS parsing) and meta#8598 (TabNode.setTextContent Safari IME fix) — both touch lexical/extension/markdown/playground/yjs and don't intersect with the release tooling on this branch, so the merge is clean. https://claude.ai/code/session_01UEAeW4ZkfApZnrJTGc6Xm9
potatowagon
left a comment
There was a problem hiding this comment.
Reviewed by Navi (Tater Thoughts Bobblehead) on behalf of @potatowagon.
LGTM ✅ — Important Safari IME crash fix in TabNode.
What this does: Fixes #8596 where Safari's MutationObserver delivers mid-IME-composition text (e.g. Korean characters) onto the TabNode's \t text node. Previously, setTextContent would throw a devInvariant error if the text wasn't '\t' or '', which cascaded through onError and froze the editor. The fix changes the method to always normalize content back to '\t' regardless of input, ignoring any IME composition payload.
What I checked:
- ✅ Logic correctness: The approach is sound — TabNode's content is canonical (
\t), so any input should be normalized rather than rejected. The reconciler will render the canonical content on the next update, overwriting the DOM mutation that triggered this. ThedevInvariantwas overly strict for a scenario the original author didn't anticipate (browser-initiated mutations via MutationObserver during IME). - ✅ Behavioral change is safe: The old behavior was THROWING on unexpected input. The new behavior is NORMALIZING. Callers that previously got an error now get silent normalization — strictly better UX.
- ✅
devInvariant→super.setTextContent('\t'): Always writes\tregardless of input. The_textparameter is intentionally ignored (underscore prefix convention). - ✅ Test coverage: New test verifies
setTextContentwith various inputs ('\tㅁ','arbitrary','') never throws and always normalizes to\t. - ✅ www compat: This is an internal behavioral fix in a core Lexical node. The change makes TabNode MORE robust — any www code that was previously hitting this invariant (Safari + Korean/CJK IME users) will now work correctly.
- ✅ CI: All checks green.
Safe to land. Critical UX fix for Safari IME users — editor freeze is unacceptable for a text editing library.
Description
Closes #8596.
In Safari, the MutationObserver delivers mid-IME-composition text writes straight onto the
TabNode's\ttext node — IMEs that compose onto the existing text node (verified with Korean) cause Safari to rewrite it to e.g.\tㅁ.flushMutationsthen callsTabNode.setTextContent('\tㅁ'), the currentdevInvariantthrows, and the throw cascades throughonError: composition stays stuck, Backspace / Enter / English input all stop responding until a hard refresh.The throw has existed since
TabNodewas introduced. The only earlier hit on it was selection-based typing across the node, which #7602 already covered by treatingTabNodeas token-mode. The MutationObserver / IME path is a separate route into the same method and was uncovered.Drop the
devInvariantcheck entirely. The body already normalizes to\tviasuper.setTextContent('\t'), so removing the check changes nothing about the stored content — the reconciler still renders the canonical\ton the next update. The dropped check was guarding caller assumptions, not stored state.A few notes for reviewers:
setTextContentsits on the Safari MutationObserver path.spliceTextis gated by the$isTabNode(firstNode)early-return inLexicalSelection.ts;setDetail/setModearen't on any IME-triggered path. Those invariants stay.packages/lexical-yjs/src/CollabTextNode.tscallssetTextContent(collabText)on synced text nodes, including aTabNodewhose remote text might be non-\t. The dev throw previously surfaced that drift in dev; in prod it was already a warn-only path. After this change dev matches prod's behavior — silently normalize to\t. Not a new prod regression.Test plan
pnpm tsc --noEmit -p tsconfig.json— cleanpnpm flow— cleanpnpm vitest run --project unit -t "LexicalTabNode tests"— 18 passed (new test added:setTextContent normalizes back to \t without throwing (#8596)covering thesetTextContent('\tㅁ')shapeflushMutationsproduces, plus an arbitrary string and'')vite build+vite previewoflexical-playground—foo+Tab+ Korean composition now completes; Backspace removes theTabNode, Enter inserts a paragraph, English typing resumes, copy/paste preserves\t, Markdown shortcuts still fire.foo+Tab+ Korean composition, Safari writes the in-flight jamo onto the TabNode's\ttext node and the normalize fires; the Korean character then lands on a sibling TextNode (input /CONTROLLED_TEXT_INSERTIONpath) and composition continues there. Final editor state:paragraph[text "foo", tab "\t", text "안녕하세요"]— Korean preserved.