Repository navigation
[lexical] Bug Fix: Clean up trailing shadow root after select-all delete - #8751
Conversation
When a document ends with a shadow root element, select-all + Backspace leaves two empty paragraphs instead of one because $getBlockMergeTargets returns null at shadow root boundaries, leaving the focus-side empty block and its ancestors behind. Add a cascade-removal else-if branch in $removeTextFromCaretRange for the merge-failure path, plus root.isEmpty() guards in deleteCharacter and NodeSelection.deleteNodes for edge cases where $collapseAtStart empties the root entirely. Closes meta#8745
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
potatowagon
left a comment
There was a problem hiding this comment.
Reviewed by Navi (Tater Thoughts Bobblehead) on behalf of @potatowagon.
Assessment: LGTM ✅
This is a well-structured bug fix for #8745 — select-all + delete leaving a trailing shadow root (e.g. CollapsibleContainerNode) behind as an extra empty paragraph.
What I checked:
-
Logic correctness of the cascade removal (
LexicalCaretUtils.ts):- The new else-if branch only fires when
$getBlockMergeTargetsreturns null (merge across shadow root boundaries impossible) — correct scope. - It walks up from the focus-side empty block, removing empty ancestors until hitting root. The guard
grandparent.getChildrenSize() <= 1prevents removing the last root child — this is the right invariant. - The
seenStart.has(focusBlock.getKey())andseenStart.has(topmostShadowRoot.getKey())checks ensure we only clean up nodes that were actually part of the deleted range, not unrelated shadow roots. - The
$getSlotNames(focusBlock).length === 0check avoids removing named-slot containers that may have structural significance.
- The new else-if branch only fires when
-
The
root.isEmpty()guards inLexicalSelection.ts:- Both in
NodeSelection.deleteNodesand after$collapseAtStartinRangeSelection.deleteCharacter— these are safety nets for edge cases where the root ends up completely empty (e.g. HR + Collapsible with no paragraph). Creating a fresh paragraph and selecting it is the standard recovery pattern used elsewhere in Lexical.
- Both in
-
Edge cases:
- Shadow root as only root child — handled by the isEmpty guard.
- Nested shadow roots — the topmost shadow root walk ensures the whole ancestry is in scope.
- Partial selection within shadow root — test verifies structure is preserved (cascade only fires for empty blocks in the deleted range).
- Middle-of-document shadow root — merge works normally via standard path.
-
Test coverage: 15 comprehensive unit tests covering all the scenarios described above plus both forward/backward deletion and NodeSelection paths. Tests use
TestShadowRootNodeandTestDecoratorNodefrom existing test utils. -
www compat: No exported API changes, no renamed exports, no signature changes. The fix is purely internal to the deletion algorithm. Internal consumers (MLCComposer, EPS) are unaffected.
-
Risk assessment: Low. The new else-if branch is narrowly scoped (only fires when merge targets are null AND focus block is empty AND in the deleted range). The root.isEmpty() guards are redundant safety nets for an already-handled case. No behavioral change for normal (non-shadow-root) deletion.
CI status: Core tests still running at time of review. CLA signed, Vercel deploy succeeded. Recommend waiting for unit/integration results before merging.
Ready to approve once CI is green.
When a document ends with a shadow root element, select-all + Backspace leaves two empty paragraphs instead of one because $getBlockMergeTargets returns null at shadow root boundaries, leaving the focus-side empty block and its ancestors behind. Add a cascade-removal else-if branch in $removeTextFromCaretRange for the merge-failure path, plus root.isEmpty() guards in deleteCharacter and NodeSelection.deleteNodes for edge cases where $collapseAtStart empties the root entirely. Closes meta#8745
deleteLine internally calls Selection.modify() which is not available in jsdom. The shadow root cleanup behavior is already covered by the remaining 14 tests (deleteCharacter, removeText, etc.).
|
The merge queue failed on I removed that test since the shadow root cleanup is already covered by the other 14 tests ( |
|
You can port it to a browser unit test instead, which runs in real browsers instead of jsdom but doesn't require the full playground e2e harness |
Selection.modify() is not available in jsdom, so the deleteLine test was removed in the previous commit. Re-add it as a browser unit test that runs in a real browser via playwright.
|
Ported it to a browser unit test — |
Had the wrong PR open when writing the approval
Description
When a document ends with a shadow root element (e.g. a
CollapsibleContainerNode), select-all + Backspace leaves two empty paragraphs instead of one. The extra paragraph comes from the shadow root's inner content surviving the delete.$removeTextFromCaretRangehandles post-delete block merging via$getBlockMergeTargets, but that returns null when the anchor and focus sit in different shadow root boundaries — merging across those boundaries doesn't make sense. In that case the focus-side empty block and its now-empty shadow root ancestors were left behind.Fix
LexicalCaretUtils.ts: Added an else-if branch for the merge-failure path. When the focus-side block is empty and was part of the deleted range (seenStartset), cascade-remove it and any empty ancestors up to (but not including) the root. A guard prevents removing the last child of root.LexicalSelection.ts: Tworoot.isEmpty()guards cover edge cases where$collapseAtStart(indeleteCharacter) orNodeSelection.deleteNodesempties the root entirely — e.g. an HR + Collapsible with no paragraph in the document.Design notes
The cascade approach works on the focus side only, after
$getBlockMergeTargetsreturns null. It usesisEmpty()(getChildrenSize() === 0) as the removal criterion — this is sufficient because the DFS pass in$removeTextFromCaretRangealready deleted all children between anchor and focus. If a shadow root ancestor is still non-empty after the DFS, it has content outside the selection range that should be preserved.The alternative — modifying
$getBlockMergeTargetsto merge across shadow root boundaries — would change the merge semantics for all deletion paths, not just the select-all case.Closes #8745
Test plan
New unit test file
Issue8745Repro.test.ts— 15 tests:deleteCharacter,removeText,deleteLineNodeSelection.deleteNodeswith block decorator + shadow rootManual playground (Safari, Chrome, Firefox):
Full manual test scenarios
A. Basic (original issue Bug: Not all selected nodes are removed if a shadow root element is set at the end #8745)
B. Different delete methods
C. Partial selection (shadow root boundary)
D. Block decorator + shadow root
E. NodeSelection path
F. Non-Collapsible shadow root
tsc / prettier clean