Skip to content

[lexical] Bug Fix: don't create a sibling cycle when splice re-inserts the node after the range - #8916

Merged
etrepum merged 1 commit into
meta:mainfrom
LeSingh1:fix/splice-existing-child
Aug 5, 2026
Merged

etrepum merged 1 commit into
meta:mainfrom
LeSingh1:fix/splice-existing-child

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Description

ElementNode.splice() captures nodeAfterRange before mutating and uses it at the end to re-link the tail of the range. The insert loop already handles the case where a node being inserted is prevNode, but not the case where it is nodeAfterRange. When that happens the node ends up as prevNode too, and the tail fix-up sets node.__prev = node.__key and node.__next = node.__key — a self-referencing sibling link.

Any children walk then never terminates: getChildren(), getChildrenKeys(), getNextSibling() loops, and the reconciler all spin until the process dies with RangeError: Invalid array length.

Two ordinary calls reproduce it:

  • parent.splice(1, 0, [parent.getChildAtIndex(1)]) — a no-op re-insert
  • parent.splice(1, 1, [laterSibling]) — delete one child and move a later sibling into its place

splice is public API and is also reached via the caret API — ChildCaretFirst.insert() calls origin.splice(0, 0, [node]).

The fix mirrors the existing prevNode guard: advance nodeAfterRange past a node that is being re-inserted. The tail branch is keyed off nodeAfterRange === null instead of start + deleteCount === oldSize — equivalent when no re-insert happens, and correct when the advance runs off the end so __last is still fixed up.

Test plan

Before

Two new tests in the existing splice describe fail, showing a node whose __next and __prev both equal its own __key:

+ Received:
TextNode { "__key": "8", "__next": "8", "__prev": "8", "__parent": "5", "__text": "Baz", ... }

Tests  2 failed | 40 skipped (42)

Both assertions live inside the same editor.update() as the splice, with the sibling-pointer check first — so on main the test fails fast with a readable message instead of hanging, and the aborted update discards the corrupt pending state rather than handing it to the reconciler. With that fast assertion removed, the same tests instead spin for ~21s and die with RangeError: Invalid array length inside getChildrenKeys, which is the real-world symptom.

After

Test Files  1 passed (1)     Tests  42 passed (42)

Sibling links, getChildrenKeys(), getChildrenSize(), getLastChild() and getTextContent() all correct. Full unit suite green (227 files, 4940 passed).

@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:56am
lexical-playground Ready Ready Preview Aug 4, 2026 6:56am

Request Review

@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
@etrepum
etrepum added this pull request to the merge queue Aug 5, 2026
Merged via the queue into meta:main with commit db7ea92 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 — dc903450 Deployed Aug 4, 2026 by vercel[bot]
Preview – lexical-playground — dc903450 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