#9277·lexical

Bug: [lexical] After a second edit in a decorator's named slot, the root's cached text repeats the old slot text

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

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

Steps To Reproduce

  1. Make an inline DecoratorNode with a named slot, and put it last in a paragraph that has four or more children: ab, a line break, cd , then the host. The slot holds a paragraph with the text C.
  2. Change the slot's text in two separate updates: to CD, then to CDE. Typing the two letters into the slot does the same.
  3. Read $getRoot().getTextContent() after each update.

Link to code example:

typescript
import {
  $createLineBreakNode,
  $createParagraphNode,
  $createTextNode,
  $getNodeByKey,
  $getRoot,
  $setSlot,
  createEditor,
  DecoratorNode,
  ElementNode,
  type TextNode,
} from 'lexical';

// An inline decorator with one named slot.
class HostNode extends DecoratorNode<null> {
  $config() {
    return this.config('host', {extends: DecoratorNode});
  }
  createDOM() {
    return document.createElement('span');
  }
  updateDOM() {
    return false;
  }
  decorate() {
    return null;
  }
  isInline() {
    return true;
  }
}

const editor = createEditor({
  nodes: [HostNode],
  onError: (error) => {
    throw error;
  },
});
const root = document.createElement('div');
root.contentEditable = 'true';
document.body.append(root);
editor.setRootElement(root);

let key = '';
editor.update(
  () => {
    const host = new HostNode();
    const text = $createTextNode('C');
    key = text.getKey();
    $getRoot().append(
      $createParagraphNode().append(
        $createTextNode('ab'),
        $createLineBreakNode(),
        $createTextNode('cd '),
        host,
      ),
    );
    $setSlot(host, 'slot', $createParagraphNode().append(text));
  },
  {discrete: true},
);
for (const next of ['CD', 'CDE']) {
  editor.update(() => $getNodeByKey<TextNode>(key)!.setTextContent(next), {
    discrete: true,
  });
  editor.read(() => console.log(JSON.stringify($getRoot().getTextContent())));
}
// "ab\ncd CD"
// "ab\ncd CCDE"
editor.read(() =>
  console.log(
    JSON.stringify(ElementNode.prototype.getTextContent.call($getRoot())),
  ),
);
// "ab\ncd CDE"

The current behavior

The first edit is right. After the second, $getRoot().getTextContent() returns ab\ncd CCDE, and after a third ab\ncd CCDCDEF: each edit leaves the start of the previous slot text in the root's cached text. Text-content listeners get the same wrong text. A walk of the same state, ElementNode.prototype.getTextContent.call($getRoot()), gives ab\ncd CDE. The cache stays wrong until the paragraph is next reconciled in full, for example by an edit before the host.

It needs all three of these:

  • a DecoratorNode slot host (an ElementNode host stays right);
  • the host at the end of a parent with four or more children: last in a paragraph as above, or the last of four top-level blocks, with no paragraph or line break at all;
  • a second edit inside the slot, with no change to the host itself in between.

With three children, or with text after the host, the cache stays right.

Why:

  1. An edit inside a slot marks its host dirty by walking up through __slotHost (LexicalUtils.ts#L500-L510, #L562-L577), but it doesn't clone the host. The previous and the next editor state share the same host instance.
  2. The reconciler caches a leaf's text size on the node instance and never rewrites it (LexicalReconciler.ts#L179-L194). It assumes a leaf whose text changed was cloned (#L85-L91). A decorator host's text includes its slots' text (LexicalNode.ts#L1649-L1662), so after the first slot edit its cached size no longer matches its text.
  3. For a parent with four or more children whose dirty children form a suffix, $reconcileChildren splices the parent's cached text: it cuts the suffix's previous size off the end and appends the new text (#L1383, #L1419, #L1485-L1488). $prevSuffixTextSize reads a leaf's previous size from that cache (#L158-L170), so for the host it cuts too little. $tryReconcileSuffixWithSizeDelta uses the same helper (#L1207).
  4. The root's __cachedText is built from these caches (#L1908-L1919). RootNode.getTextContent() returns it in any read (LexicalRootNode.ts#L46-L53), and text-content listeners read it that way (LexicalUpdates.ts#L850-L856).

The expected behavior

$getRoot().getTextContent() matches a walk after every edit: ab\ncd CDE after the second.

This change does that. I checked it on a patched copy of 0.52.0's Lexical.dev.js: every case above keeps the root's text right, typing into the slot included, and the cases that were right stay right. It measures a slot host's previous size in the previous state, which this walk already reads, instead of trusting the cache:

diff
--- a/packages/lexical/src/LexicalReconciler.ts
+++ b/packages/lexical/src/LexicalReconciler.ts
@@ -156,6 +156,11 @@
           if (i < count - 1 && !prevNode.isInline()) {
             size += DOUBLE_LINE_BREAK.length;
           }
+        } else if ($readSlots(prevNode).size > 0) {
+          // A slot host's text includes its slots' text, which an edit inside
+          // a slot changes without cloning the host, so the size cached on the
+          // instance can be stale. Measure it in the previous state instead.
+          size += prevNode.getTextContentSize();
         } else {
           // $reconcileNode / $createNode set the size on every leaf they touch,
           // so a missing entry means the invariant was broken upstream.

It costs the length of the host's slot text, and only for a slot host in a suffix being spliced.

Impact of fix

It affects anyone using named slots with a decorator host, such as a math or form widget with an editable field. Once the host ends a parent with four or more children, every edit in its slot after the first leaves part of the old slot text in the root's text content. That is what $getRoot().getTextContent() returns in a read and what text-content listeners receive, so character counts, empty checks and anything else built on the document's text see it too.