Skip to content

[lexical] Bug Fix: keep parent __size correct when replace() is passed a sibling - #8917

Merged
etrepum merged 1 commit into
meta:mainfrom
LeSingh1:fix/replace-sibling-size
Aug 5, 2026
Merged

etrepum merged 1 commit into
meta:mainfrom
LeSingh1:fix/replace-sibling-size

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Description

LexicalNode.replace() reads the parent's __size before detaching replaceWith, then restores that value at the end. When replaceWith is already a child of the same parent, the parent loses two children (replaceWith via $removeFromParent, this via $removeNode) but only gains one back, so the restored size is one too large:

paragraph.append(a, b);
a.replace(b);
paragraph.getChildrenSize(); // 2
paragraph.getChildren();     // [b]  -> one actual child

The sibling links are correct — only the counter is wrong — but the desync trips the $reconcileChildren: nextChildren.length !== nextChildrenSize invariant on the next commit, which crashes the editor. getChildrenSize(), ElementNode.select()'s default offsets, and splice's bounds check all read the inflated value first.

NodeCaret.splice already works around this by detaching same-parent nodes with node.remove() before calling target.replace(node) (LexicalCaret.ts). No other caller does. This fixes it at the source so they don't have to.

Test plan

Before

New test LexicalNode.replace(): with a sibling in the same parent fails:

AssertionError: expected 2 to be 1 // Object.is equality
- 1
+ 2

Tests  1 failed | 100 skipped (101)

Worth noting the existing Element-anchored selection on old parent (#6031) suite does cover within-parent moves, but only for insertBefore/insertAfter — replace is filtered out of that table via actNoRestore: null, so this path was uncovered.

After

Test Files  1 passed (1)     Tests  101 passed (101)

Size, children and rendered HTML all agree. Full unit suite green (227 files, 4940 passed).

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 4, 2026
@vercel

vercel Bot commented Aug 4, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview Aug 4, 2026 6:57am
lexical-playground Ready Ready Preview Aug 4, 2026 6:57am

Request Review

@etrepum
etrepum added this pull request to the merge queue Aug 5, 2026
Merged via the queue into meta:main with commit 39db188 Aug 5, 2026
76 of 77 checks passed
@etrepum etrepum mentioned this pull request Aug 31, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — fcc2f29a Deployed Aug 4, 2026 by vercel[bot]
Preview – lexical-playground — fcc2f29a Deployed Aug 4, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants