Skip to content

[lexical] Bug Fix: Clean up trailing shadow root after select-all delete - #8751

Merged
etrepum merged 9 commits into
meta:mainfrom
mayrang:fix/8745-select-all-shadow-root
Jun 28, 2026
Merged

etrepum merged 9 commits into
meta:mainfrom
mayrang:fix/8745-select-all-shadow-root

Conversation

@mayrang

@mayrang mayrang commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator

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.

$removeTextFromCaretRange handles 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 (seenStart set), 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: Two root.isEmpty() guards cover edge cases where $collapseAtStart (in deleteCharacter) or NodeSelection.deleteNodes empties 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 $getBlockMergeTargets returns null. It uses isEmpty() (getChildrenSize() === 0) as the removal criterion — this is sufficient because the DFS pass in $removeTextFromCaretRange already 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 $getBlockMergeTargets to 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:

    • Trailing shadow root (basic, nested, multi-child columns-like)
    • Shadow root in the middle of content
    • Forward/backward deleteCharacter, removeText, deleteLine
    • NodeSelection.deleteNodes with block decorator + shadow root
    • HR + collapsible only (no paragraph — the D2 regression scenario)
    • Partial deletion within shadow root preserves structure
    • Shadow root as the only root child
  • Manual 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)

    • A1. paragraph + Collapsible → Cmd+A → Backspace → one empty paragraph, typing works
    • A2. heading + paragraph + Collapsible → Cmd+A → Backspace → one empty paragraph (paragraph, not heading)
    • A3. paragraph + nested Collapsible → Cmd+A → Backspace → one empty paragraph

    B. Different delete methods

    • B1. paragraph + Collapsible → Cmd+A → Delete (forward) → one empty paragraph
    • B2. paragraph + Collapsible → Cmd+A → Cmd+Backspace (line delete) → one empty paragraph
    • B3. paragraphs only → Cmd+A → Backspace → one empty paragraph (no regression)

    C. Partial selection (shadow root boundary)

    • C1. Drag select within Collapsible (title partial ~ content partial) → Backspace → structure preserved, only selected text removed
    • C2. paragraph partial ~ Collapsible title partial → Backspace → selected text removed
    • C3. Collapsible content partial ~ paragraph after Collapsible partial → Backspace → selected text removed

    D. Block decorator + shadow root

    • D1. paragraph + HR → Cmd+A → Backspace → one empty paragraph
    • D2. HR + Collapsible (no paragraph) → Cmd+A → Backspace → one empty paragraph, typing works
    • D3. HR + Collapsible + HR → Cmd+A → Backspace → one empty paragraph

    E. NodeSelection path

    • E1. Click HR (selected) → Cmd+A → Backspace → one empty paragraph
    • E2. Click image (block decorator) → Cmd+A → Backspace → one empty paragraph

    F. Non-Collapsible shadow root

    • F1. Columns layout → Cmd+A → Backspace → one empty paragraph (requires a paragraph above the columns; columns-only has a pre-existing issue on main)
    • F2. paragraph + Columns → partial selection → Backspace → structure preserved or merged normally
  • tsc / prettier clean

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
@vercel

vercel Bot commented Jun 26, 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 Jun 28, 2026 5:34pm
lexical-playground Ready Ready Preview Jun 28, 2026 5:34pm

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 Jun 26, 2026

@potatowagon potatowagon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. Logic correctness of the cascade removal (LexicalCaretUtils.ts):

    • The new else-if branch only fires when $getBlockMergeTargets returns 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() <= 1 prevents removing the last root child — this is the right invariant.
    • The seenStart.has(focusBlock.getKey()) and seenStart.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 === 0 check avoids removing named-slot containers that may have structural significance.
  2. The root.isEmpty() guards in LexicalSelection.ts:

    • Both in NodeSelection.deleteNodes and after $collapseAtStart in RangeSelection.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.
  3. 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.
  4. Test coverage: 15 comprehensive unit tests covering all the scenarios described above plus both forward/backward deletion and NodeSelection paths. Tests use TestShadowRootNode and TestDecoratorNode from existing test utils.

  5. 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.

  6. 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.

@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Jun 26, 2026
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
@etrepum
etrepum added this pull request to the merge queue Jun 27, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jun 27, 2026
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.).
@mayrang

mayrang commented Jun 27, 2026

Copy link
Copy Markdown
Collaborator Author

The merge queue failed on select-all + deleteLine — deleteLine takes the slot-frame early path, collapses the selection, then calls deleteCharacter → Selection.modify() → getWindow() invariant in jsdom.

I removed that test since the shadow root cleanup is already covered by the other 14 tests (deleteCharacter forward/backward, removeText, NodeSelection.deleteNodes, etc.) that don't need native Selection.modify. Is there a better way to handle this?

@etrepum

etrepum commented Jun 27, 2026

Copy link
Copy Markdown
Collaborator

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.
@mayrang

mayrang commented Jun 27, 2026

Copy link
Copy Markdown
Collaborator Author

Ported it to a browser unit test — __tests__/browser/Issue8745DeleteLine.test.ts.

etrepum

This comment was marked as outdated.

@etrepum
etrepum dismissed their stale review June 28, 2026 05:34

Had the wrong PR open when writing the approval

@etrepum
etrepum added this pull request to the merge queue Jun 28, 2026
Merged via the queue into meta:main with commit 51d47b7 Jun 28, 2026
77 checks passed
@etrepum etrepum mentioned this pull request Jul 9, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — 4e0229da Deployed Jun 28, 2026 by vercel[bot]
Preview – lexical-playground — 4e0229da Deployed Jun 28, 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: Not all selected nodes are removed if a shadow root element is set at the end

3 participants