Repository navigation
[lexical] Bug Fix: keep parent __size correct when replace() is passed a sibling - #8917
Merged
Merged
Conversation
LeSingh1
requested review from
acywatson,
etrepum,
fantactuka,
ivailop7,
potatowagon and
zurfyx
as code owners
August 4, 2026 06:55
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
etrepum
approved these changes
Aug 5, 2026
Merged
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
LexicalNode.replace()reads the parent's__sizebefore detachingreplaceWith, then restores that value at the end. WhenreplaceWithis already a child of the same parent, the parent loses two children (replaceWithvia$removeFromParent,thisvia$removeNode) but only gains one back, so the restored size is one too large:The sibling links are correct — only the counter is wrong — but the desync trips the
$reconcileChildren: nextChildren.length !== nextChildrenSizeinvariant on the next commit, which crashes the editor.getChildrenSize(),ElementNode.select()'s default offsets, andsplice's bounds check all read the inflated value first.NodeCaret.splicealready works around this by detaching same-parent nodes withnode.remove()before callingtarget.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 parentfails:Worth noting the existing
Element-anchored selection on old parent (#6031)suite does cover within-parent moves, but only forinsertBefore/insertAfter—replaceis filtered out of that table viaactNoRestore: null, so this path was uncovered.After
Size, children and rendered HTML all agree. Full unit suite green (227 files, 4940 passed).