#9289·lexical

Bug: [lexical] Composing into a TextNode subclass replaces it with a plain TextNode, losing its type, key and node state

Author: rinarakakiCreated Sep 30, 2026Updated Sep 30, 2026

Lexical version: 0.52.0 (the current latest), Chromium 153 on Linux. The code is unchanged on main at f0fefed.

Steps To Reproduce

  1. Run the code below: a rich-text editor holding hello and a TextNode subclass, HighlightNode, holding world, with node state id="h1".
  2. Click in world, between wor and ld.
  3. Type with an input method: in Japanese, か then な, convert to 仮名 and commit. A dead key does the same, such as Option+E then E for é on macOS.
  4. Read the log.

Link to code example:

javascript
import {registerRichText} from '@lexical/rich-text';
import {
  $create,
  $createParagraphNode,
  $createTextNode,
  $getRoot,
  $getState,
  $setState,
  createEditor,
  createState,
  TextNode,
} from 'lexical';

// Any TextNode subclass, here carrying node state.
const idState = createState('id', {
  parse: (value) => (typeof value === 'string' ? value : ''),
});
class HighlightNode extends TextNode {
  $config() {
    return this.config('highlight', {extends: TextNode});
  }
}

const root = document.createElement('div');
root.contentEditable = 'true';
document.body.append(root);
const editor = createEditor({
  nodes: [HighlightNode],
  onError: (error) => {
    throw error;
  },
});
editor.setRootElement(root);
registerRichText(editor);
editor.registerUpdateListener(({editorState}) => {
  editorState.read(() => {
    const paragraph = $getRoot().getFirstChildOrThrow();
    console.log(
      paragraph
        .getChildren()
        .map(
          (node) =>
            `${node.getType()}(${JSON.stringify(node.getTextContent())}, id=${JSON.stringify($getState(node, idState))})`,
        )
        .join(', '),
    );
  });
});
editor.update(() => {
  const highlight = $create(HighlightNode).setTextContent('world');
  $setState(highlight, idState, 'h1');
  $getRoot().append(
    $createParagraphNode().append($createTextNode('hello '), highlight),
  );
});

The current behavior

While composing, the log keeps the subclass: highlight("worかld", id="h1"), then highlight("wor仮名ld", id="h1"). When the composition ends, the last line is:

text("hello wor仮名ld", id="")

The HighlightNode has been replaced by a plain TextNode, which then merged with hello . Its key and its node state are gone too. Typing the same letters without an input method keeps it: highlight("worqld", id="h1").

I checked this in Chromium, driving composition through CDP (Input.imeSetComposition, then Input.insertText). It happens in all 12 cases I tried: plain and rich text, the caret in the middle or at the end of the node, and Japanese, a dead key and Android's composition (keydown 229 before each input).

Why:

  • $cleanupComposedSubclass runs at the end of every composition:
    • in $onCompositionEndImpl, which Chrome reaches through COMPOSITION_END_COMMAND, Firefox through $handleInput, and Safari through $handleKeyDown;
    • and for Safari's insertFromComposition in $handleBeforeInput.
  • It replaces the composed node with $createTextNode(...) whenever the node is a TextNode subclass in normal mode, copying only its format and style. Only token and segmented nodes are left alone.
  • It came with #8784, which fixed composition in the middle of a segmented node (#5065). There, insertText switches the segmented node to normal mode so that the browser's composition keeps its DOM element. $cleanupComposedSubclass then turns it into plain text so the mention's styling doesn't stay.
  • But its condition doesn't check that the node was switched. Every TextNode subclass that was already in normal mode matches, so each one is replaced whenever someone composes into it.

The expected behavior

Composing into a TextNode subclass keeps it, with its type, key and node state, as typing does. Only the node that insertText switched out of segmented mode is replaced with plain text.

This change does that. It records the node insertText switched, on the editor's input state, and $cleanupComposedSubclass acts on that node alone:

diff
--- a/packages/lexical/src/LexicalEditor.ts
+++ b/packages/lexical/src/LexicalEditor.ts
@@ export interface InputState {
   compositionPhase: 'idle' | 'composing' | 'ending-firefox' | 'ending-safari';
   compositionEndData: string;
+  /**
+   * The segmented node that insertText switched to normal mode for a
+   * composition, which is replaced with a plain TextNode when it ends.
+   */
+  composedSegmentedKey: NodeKey | null;
   hadOrphanedCompositionEvents: boolean;
@@ export function createInputState(): InputState {
     compositionEndData: '',
     compositionPhase: 'idle',
+    composedSegmentedKey: null,
     hadOrphanedCompositionEvents: false,
--- a/packages/lexical/src/LexicalSelection.ts
+++ b/packages/lexical/src/LexicalSelection.ts
@@ insertText(text: string): void {
       if (anchorNode.isSegmented() && offset !== 0 && offset !== anchorSize) {
         if ($getCompositionKey() !== null) {
           anchorNode.setMode('normal').setFormat(format).setStyle(style);
+          getActiveEditor()._inputState.composedSegmentedKey = anchorNode.getKey();
         } else {
--- a/packages/lexical/src/LexicalEvents.ts
+++ b/packages/lexical/src/LexicalEvents.ts
@@ function $cleanupComposedSubclass(compositionKey: NodeKey | null): void {
-  if (compositionKey === null) {
+  const inputState = getActiveEditor()._inputState;
+  if (
+    compositionKey === null ||
+    inputState.composedSegmentedKey !== compositionKey
+  ) {
     return;
   }
+  inputState.composedSegmentedKey = null;
   const composedNode = $getNodeByKey(compositionKey);

I checked it against a copy of 0.52.0's build patched the same way, in Chromium:

  • In all 12 cases above, the HighlightNode keeps its type, key and state: highlight("wor仮名ld", id="h1").
  • #8784's own case, a segmented node composed into in its middle, still ends as plain text, the same as today.
  • Every browser gets the fix, because all four composition-end paths go through this one function. I ran Chromium only.

Impact of fix

It affects every custom TextNode subclass in normal mode:

  • hashtags and the playground's keywords, which their text-entity transforms then rebuild from the text, under new keys;
  • code highlight nodes;
  • anything that carries node state, such as ids or metadata.

Every composition into one silently replaces it with plain text. That's every word typed in Japanese, Chinese or Korean, and every dead key in European layouts. Its type, key and state are lost, and it merges into the text beside it. It has happened since #8784, released in 0.47.0. We hit it in an editor with inline math built on Lexical, whose tokens are TextNode subclasses.