Repository navigation
[lexical] Bug Fix: Line breaks in a slot can't be inserted or deleted correctly - #8899
Merged
Merged
Conversation
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.
sahiee-dev
requested review from
acywatson,
etrepum,
fantactuka,
ivailop7,
potatowagon and
zurfyx
as code owners
July 31, 2026 11:26
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
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.
Collaborator
Contributor
Author
etrepum
approved these changes
Aug 1, 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
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.insertNodeshas 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-scopedCmd+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 aLineBreakNodeended 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+Entertwice. The text shifts after the second line break, and a third line break (or arrow-key navigation through the block) throwsRangeError: Maximum call stack size exceeded.Fix: scope the redirect to
anchorNode.isShadowRoot()containers only. A bare block value doesn't need it:$removeTextAndSplitBlockalready 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.deleteCharacterhas 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 forElementNodeandDecoratorNodeorigins, so aLineBreakNodefalls 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
LineBreakNodecase 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 repeatedShift+Enterequivalents 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