Skip to content

[lexical] Bug Fix: Line breaks in a slot can't be inserted or deleted correctly - #8899

Merged
etrepum merged 2 commits into
meta:mainfrom
sahiee-dev:fix/linebreak-slot-stack-overflow
Aug 1, 2026
Merged

etrepum merged 2 commits into
meta:mainfrom
sahiee-dev:fix/linebreak-slot-stack-overflow

Conversation

@sahiee-dev

@sahiee-dev sahiee-dev commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

Description

Two related bugs in how a bare block slot value (for example the attribution line on a Pull Quote) handles LineBreakNodes, both stemming from the same underlying shape: once a line break ends up with no sibling in a given direction, LineBreakNode.selectStart()/selectEnd() fall back to an element-mode point directly on the slot value's root, and two different pieces of code mishandle that shape.

Insertion (#8897)

RangeSelection.insertNodes has a redirect branch for when the selection anchor is an element-mode point sitting directly on a slot value's root (for example after a slot-scoped Cmd+A). That branch was scoped to any slot value, not just container (shadow-root) values, so a bare block slot value went through it too.

The redirect always jumps to anchorNode.getFirstChild() and ignores the anchor's actual offset. For a bare block value this meant every insert collapsed to the start of the block instead of landing at the caret. Once a LineBreakNode ended up as the first child, LineBreakNode.selectStart() resolved back to the exact same element-mode anchor the branch had just entered on, so the recursive call re-entered itself with unchanged input and the stack overflowed.

Repro: type text in a bare block slot value, place the caret at the end, press Shift+Enter twice. The text shifts after the second line break, and a third line break (or arrow-key navigation through the block) throws RangeError: Maximum call stack size exceeded.

Fix: scope the redirect to anchorNode.isShadowRoot() containers only. A bare block value doesn't need it: $removeTextAndSplitBlock already stops the block-finding walk at a slot host, so the existing fallthrough logic lands on the block directly, at the correct offset, without recursing through a child at all.

Deletion (#8898)

RangeSelection.deleteCharacter has a loop that explores what sits next to the caret when there is no text left in the deletion direction. That loop only has cases for ElementNode and DecoratorNode origins, so a LineBreakNode falls through unhandled.

Past that loop sits a boundary guard: if the caret's node is a slot value, it stops immediately, since slot content behaves like a virtual shadow root and deletion shouldn't cross into the host's real DOM. That guard only checks whether the caret is on a slot value, not whether there was still something left to delete. So when a bare block slot value ends in a line break, the caret lands as an element-mode point directly on the slot value, the sibling-caret loop passes over the line break, and the boundary guard bails out with nothing deleted.

Repro: with the caret at the end of a bare block slot value that ends in one or more line breaks, press Backspace. Nothing happens, repeatably.

Fix: add the missing LineBreakNode case to the sibling-caret loop, mirroring the existing decorator handling, removing the node directly instead of letting it fall through to the boundary guard.

These were originally two separate PRs (this one for insertion, #8900 for deletion) since they were found at different points during testing, but they touch the same function family and the same underlying selection shape, so combining them here per review feedback. #8900 is closed in favor of this one.

Closes #8897
Closes #8898

Test plan

Added three unit tests to packages/lexical/src/__tests__/unit/LexicalSlot.test.ts:

  • repeated linebreaks into a bare block value stay in insertion order (#8897): drives the exact recursive stack-overflow path with repeated Shift+Enter equivalents and asserts the text stays first.
  • insertNodes at the end of a bare block value inserts at the end, not the start: pins the offset-respecting behavior directly.
  • backspace deletes trailing linebreaks in a bare block value one at a time (#8898): builds a bare block slot value ending in two line breaks, backspaces twice, and asserts the count drops 2 → 1 → 0 with the text left intact.

All three fail against the pre-fix code with the symptoms described above, and pass with the fixes. Full lexical core unit suite passes with no regressions.

Before

Line break insertion:

629664921-f6f4d151-d997-4419-84dd-7e2490dd98ac.mov

Backspace:

backspace_before.mov

After

Line break insertion:

lb_after.mov

Backspace:

backspace_after.mov

insertNodes' slot-value redirect branch was scoped to any slot value,
not just shadow-root containers, so a bare block value redirected
through anchorNode.getFirstChild() and ignored the anchor's offset.
That collapsed every insert to offset 0, and once a LineBreakNode
ended up first, LineBreakNode.selectStart() resolved back to the same
element-mode anchor the branch entered on, recursing until the stack
overflowed. Scope the redirect to shadow-root containers only; bare
block values already fall through correctly via $removeTextAndSplitBlock.
@vercel

vercel Bot commented Jul 31, 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 Jul 31, 2026 12:07pm
lexical-playground Ready Ready Preview Jul 31, 2026 12:07pm

Request Review

deleteCharacter's sibling-caret exploration loop only handles
ElementNode and DecoratorNode origins; a LineBreakNode falls through
unhandled and reaches the trailing slot-edge boundary guard, which
bails out unconditionally whenever the anchor's node is a slot value,
without checking whether there is still content in the deletion
direction. The LineBreakNode is left in place and backspace becomes a
silent no-op. Add the missing case, mirroring the existing decorator
handling: remove the LineBreakNode directly instead of falling through.
@etrepum

etrepum commented Jul 31, 2026

Copy link
Copy Markdown
Collaborator

#8900 and #8899 touch the same code and solve similar problems, they should be a single PR. Having this as two PRs basically doubles the review overhead.

@sahiee-dev sahiee-dev changed the title [lexical] Bug Fix: Errors when inserting a line break into a slot [lexical] Bug Fix: Line breaks in a slot can't be inserted or deleted correctly Aug 1, 2026
@sahiee-dev

Copy link
Copy Markdown
Contributor Author

Good call, thanks. Combined the backspace fix (#8900) into this PR since the branch already had both commits on it, I just hadn't merged the PRs together. Updated the description above to cover both bugs and both test plans. Closing #8900 in favor of this one.

@etrepum
etrepum added this pull request to the merge queue Aug 1, 2026
Merged via the queue into meta:main with commit fae68c0 Aug 1, 2026
48 checks passed
@etrepum etrepum mentioned this pull request Aug 31, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — 0e1f5969 Deployed Jul 31, 2026 by vercel[bot]
Preview – lexical-playground — 0e1f5969 Deployed Jul 31, 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. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Backspace does not delete line breaks inserted into a slot Bug: Errors when inserting a line break into a slot

2 participants