Bug: Cannot enter a character after a TextNode in token mode. (playground)
When certain characters are entered in Japanese, the content of the TextNode in token mode is changed or the entire paragraph disappears.
Lexical version: 0.12.0
Steps To Reproduce
- Open https://playground.lexical.dev/
- Move the caret to the immediate right of the 🙂
- Set the keyboard to Japanese input mode.
- At this time, entering "t" consecutively changes the contents of the TextNode in token mode. If you type "tte" to enter "って", the entire paragraph will disappear.
This occurs when the same key is pressed in succession to input "っ".
I was able to reproduce it on my Mac with Chrome and Edge and not with Firefox (Safari has a different bug situation, but I can't type correctly.). I could not reproduce it on either browser on my Windows computer.
The current behavior
https://github.com/facebook/lexical/assets/32331100/304b8156-8709-49ff-88bd-dc554bd848e8
The expected behavior
Characters can be entered after the textNode in token mode.
Investigation Report: IME composition corruption with token mode TextNodes
We've been investigating this issue extensively in our Japanese novel editor (NoveLand). Here's a detailed report of our findings.
Environment
- Lexical: v0.41.0
- Browser: Chrome (Mac)
- OS: macOS
- IME: macOS built-in Japanese input
Root Cause (confirmed via logging)
The corruption happens when typing characters that generate "っ" (small tsu) via repeated key presses (e.g., typing "tte" → "って"). The IME engine moves the selection anchor into the token node's span element during the "っt" → "って" conversion.
Here's the exact event timeline captured from console logs:
compositionstart
anchorNode: normal text span (after token node)
anchorText: "──大魔導士の称号を持つ..." (starts with ZWSP = COMPOSITION_START_CHAR)
compositionupdate "t" → normal span, no issue
beforeinput "t" → targetRanges: normal span only
compositionupdate "っt" → normal span, no issue
beforeinput "っt" → targetRanges: normal span only
★ compositionupdate "って" ← CORRUPTION POINT
anchorNode: TOKEN NODE span ("セレナ") ← browser moved selection INTO token node
anchorOffset: 0
parentDataObjectId: "c8916019-..." ← token node's data attribute
beforeinput "って"
targetRanges: [{
start: TOKEN span (data-object-id="c8916019-..."), offset=0
end: normal span, offset=1
}]
→ Range CROSSES the token node boundary
(compositionend never fires — paragraph is destroyed)
Key insight: The COMPOSITION_START_CHAR (ZWSP) inserted by Lexical's onCompositionStart is placed at position 0 of the text node after the token node. When the IME generates "っ" (by converting "っt" → "って"), the browser's IME engine crosses backwards past the ZWSP into the previous span (the token node). This is a browser-level behavior below JavaScript event handler control.
Approaches we tried (all failed)
| # | Approach | Result |
|---|---|---|
| 1 | Insert empty TextNode on compositionstart (editor.update({ discrete: true })) |
IME locks to DOM position before update executes |
| 2 | Set contentEditable="false" on token node DOM |
Lexical reconciler overrides to "inherit" |
| 3 | keydown(229) handler to move cursor before compositionstart | Cursor is already outside token node; problem is IME boundary crossing |
| 4 | MutationObserver to revert token node text changes | Revert succeeds but IME continues composing → paragraph collapses on next compositionupdate |
| 5 | beforeinput preventDefault on insertCompositionText |
getTargetRanges() reports start=normal during "っt" phase (inaccurate); by "って" phase it's too late |
| 6 | patch-package to add DOM text revert in $updateTextNodeFromDOMContent |
Correct approach but Turbopack (Next.js dev bundler) doesn't pick up node_modules patches |
| 7 | MutationObserver revert + blur()/focus() to force-end composition |
blur()/focus() does NOT trigger compositionend — IME composition continues after blur |
What we believe would fix this
In $updateTextNodeFromDOMContent, when node.isToken() is true, the current code only calls node.markDirty(). It should also immediately revert the DOM text content to prevent the IME from continuing to corrupt the node:
if (node.isToken()) {
node.markDirty();
// Add: revert DOM text immediately
const editor = getActiveEditor();
const element = editor.getElementByKey(node.getKey());
if (element) {
const domText = getDOMTextNode(element);
if (domText) {
domText.textContent = prevTextContent;
}
}
return;
}
We confirmed via patching that this code exists at the right location, but couldn't verify the fix because Turbopack doesn't reflect node_modules patches in dev builds.
getTargetRanges() accuracy issue
During the "っt" compositionupdate, beforeinput.getTargetRanges() reports both start and end containers as the normal text span, even though the browser simultaneously writes "っ" into the token node's span. The range only accurately reports the token node during the final "って" phase, which is too late for prevention.
This means getTargetRanges() cannot be relied upon to detect IME span-boundary-crossing during intermediate composition steps.
The simplest reproduction I've found with Chrome on macOS using the "Japanese - Romaji" keyboard on the playground with the default content (or something even more minimal like a TextNode followed by an EmojiNode)
- Position the cursor just after the last emoji (before the period)
- Type "ttte" and notice that the text is inserted in the wrong place
Some similar problems will happen you're entering this sequence into a TextNode that has a format different from the previous sibling TextNode.
Unsurprisingly the root of this broken behavior is below the lexical layer, in this demo you can see that the browser+IME combination ends up editing the wrong nodes even in a minimal pure HTML example:
<div contenteditable=true>
<span>a</span><span>b</span><span id="last-span">c</span>
</div>
https://github.com/user-attachments/assets/3c69718f-d8d3-4cfe-9990-4e15e30bd756
Something like this is close to working, although the result of "ttte" is a bit different than other text fields "tって" is expected but it inserts "っって" so there's another workaround or two necessary.
<div contenteditable=true>
<span>a</span><span style="color: red">b</span><span id="last-span">c</span>
</div>
<script type=module>
const div = document.querySelector('div');
div.addEventListener('compositionupdate', (e) => {
const sel = window.getSelection();
if (!sel || !sel.rangeCount) {
return;
}
const range = sel.getRangeAt(0);
const {endContainer} = range;
const hasStart = endContainer.textContent.startsWith('\u200b');
const isSameContainer = endContainer === range.startContainer;
let startOffset = isSameContainer ? range.startOffset : 0;
let endOffset = range.endOffset;
if (hasStart && isSameContainer || startOffset > 0) {
return;
}
const fixed = document.createRange();
if (!hasStart) {
endContainer.textContent = '\u200b' + endContainer.textContent;
endOffset += 1;
}
fixed.setStart(endContainer, startOffset);
fixed.setEnd(endContainer, endOffset);
sel.removeAllRanges();
sel.addRange(fixed);
});
</script>
Actually I guess that's the expected behavior when there's adjacent text, so I think just a workaround like this is sufficient.
@etrepum Thank you so much for the suggested workaround! We implemented it in our project (a Japanese novel editor built on Lexical) and can confirm it works correctly.
We tested with Japanese IME input (e.g., typing "って" after token-mode TextNodes) on macOS Chrome, and the token nodes are no longer corrupted. The IME input correctly lands in the adjacent text node instead of overwriting the token node.
This issue has been a long-standing pain point for us since we first opened it, so we're really happy to finally have a working solution. Thank you again for taking the time to investigate and share it!
Glad to hear it works! It would be nice to have a workaround built-in to lexical, if you have any interest in making a PR it would be appreciated
@etrepum Thanks for the suggestion! We'd love to contribute this back to Lexical. We'll run it in production for a while first to make sure there are no edge cases, and then open a PR.
Hi @dododoSA — are you still working on the PR for this?
FWIW I couldn't reproduce this on macOS Chrome — tried both current main and a v0.41.0 checkout (the same version dododoSA was on during the original investigation, 2026-03-28). Same setup: :) to produce an EmojiNode 🙂, Japanese-Romaji keyboard, caret right after the emoji, ttt... entered consecutively. EmojiNode stays intact and the t lands as a separate TextNode next to it in both builds.
Something has presumably been fixed somewhere along the way, but I can't tell where — Lexical wasn't the variable (same 0.41 code, different result), so I'd guess Chrome / macOS, but the release notes don't call out anything specific.
I did notice one residual oddity while testing where compositionend.data included the emoji (e.g. "🙂t") even though the final DOM was correct. Filed it as #8507 and closed it shortly after — after multiple attempts I couldn't reliably reproduce that either; looks like the same timing / IME-state flake. Left an architectural note there for future reference ($handleCompositionStart's heuristic doesn't cover the token-mode TextNode boundary case) in case anyone hits a consistent repro.
If anyone can still reproduce the original token corruption / paragraph loss on a recent build, useful to know what env (Chrome version, macOS version, IME variant).
Following up — it's been a couple of months since my last comment and this still doesn't reproduce on macOS Chrome with current main. Since etrepum identified the root cause as being below the Lexical layer (browser + IME behavior), this may have been resolved by browser updates. If no one can still reproduce this, it could probably be closed.