Repository navigation
[lexical] Bug Fix: Handle select-all deletion with root slots - #9240
Merged
Merged
Conversation
… slots
## Description
Selecting all rich text in the mdast-editor example and pressing Backspace
leaves a root selection when footnotes remain in a root slot. The toolbar
throws from getTopLevelElementOrThrow, and typing cannot resume because the
deletion helper treats the slot as a nonempty document body.
Use the nullable top-level lookup in ToolbarStateExtension, preserving the
paragraph default and pending inline formats for root selections. Restore an
editable paragraph when deletion empties the root's children, even if root
slots remain. Keep deletion within a slot isolated so it does not move the
caret into the main document.
Add browser coverage for root selections and select-all/Backspace followed by
typing, with and without footnotes. Add range and node selection regressions
for restoring the body while preserving slot contents and selection scope.
## Test plan
### Before
`pnpm exec vitest run --config vitest.local.mts --project browser dev-examples/mdast-editor/src/__tests__/browser/ToolbarState.test.ts`
The local config uses the repository configuration with only the Playwright
Chromium executable and launch arguments overridden for this container.
```text
Error: getTopLevelElementOrThrow: root nodes are not top level elements
❯ RootNode.getTopLevelElementOrThrow packages/lexical/src/nodes/LexicalRootNode.ts:40:5
❯ $readSelectionState dev-examples/mdast-editor/src/extensions/ToolbarStateExtension.ts:56:22
Test Files 1 failed (1)
Tests 3 failed (3)
Errors 4 errors
```
`pnpm run test-unit packages/lexical/src/__tests__/unit/LexicalSlot.test.ts -t 'deleting all children'`
```text
AssertionError: expected +0 to be 1 // Object.is equality
Test Files 1 failed (1)
Tests 2 failed | 108 skipped (110)
```
### After
`pnpm exec vitest run --config vitest.local.mts --project browser dev-examples/mdast-editor/src/__tests__/browser`
```text
Test Files 3 passed (3)
Tests 26 passed (26)
```
With a standard Playwright installation, run the same suite with
`pnpm run test-browser dev-examples/mdast-editor/src/__tests__/browser`.
Firefox and WebKit were not exercised: the container could not install the
requested Chromium archive, so the existing Chromium executable was used;
WebKit could not launch because host libraries are missing.
`pnpm run test-unit dev-examples/mdast-editor packages/lexical/src/__tests__/unit/LexicalSlot.test.ts packages/lexical/src/__tests__/unit/LexicalSelection.test.ts`
```text
Test Files 3 passed (3)
Tests 255 passed (255)
```
`pnpm run tsc-test` and
`pnpm exec tsc --noEmit -p dev-examples/mdast-editor/tsconfig.json` exited 0.
ESLint, Prettier, and `git diff --check` passed for the changed files.
## Description
Select-all followed by deleteWord or deleteLine still leaves the document body
empty when footnotes occupy a root slot. Those range deletion paths use
$restoreEmptyContainerParagraph, whose isEmpty check counts slots as content.
Check the root's child count in the shared refill helper while preserving the
existing shadow-root emptiness check. Limit the caret deletion fallback to a
container that was actually removed, so editing inside a slot does not seed a
paragraph in an unrelated, previously empty document body.
Extend the regression tests to deleteCharacter, deleteWord, deleteLine,
removeText, and node deletion. Check both body restoration with retained
footnotes and deletion within a root slot. The existing node-selection repair
and toolbar root-selection handling remain covered.
## Test plan
### Before
`pnpm run test-unit packages/lexical/src/__tests__/unit/LexicalSlot.test.ts -t 'deleting'`
```text
× deleting all children with a deleteWord selection restores a paragraph before root slots
× deleting all children with a deleteLine selection restores a paragraph before root slots
AssertionError: expected +0 to be 1 // Object.is equality
Test Files 1 failed (1)
Tests 2 failed | 8 passed | 108 skipped (118)
```
### After
`pnpm run test-unit packages/lexical/src/__tests__/unit/LexicalSlot.test.ts -t 'deleting'`
```text
Test Files 1 passed (1)
Tests 10 passed | 108 skipped (118)
```
`pnpm run test-unit dev-examples/mdast-editor packages/lexical/src/__tests__/unit/LexicalSlot.test.ts packages/lexical/src/__tests__/unit/LexicalSlotSelection.test.ts packages/lexical/src/__tests__/unit/LexicalSelection.test.ts packages/lexical/src/__tests__/unit/LexicalUtils.test.ts packages/lexical/src/__tests__/unit/LexicalRootSelectionPhantomParagraph.test.ts packages/lexical/src/caret/__tests__/unit`
```text
Test Files 8 passed (8)
Tests 543 passed (543)
```
`pnpm exec vitest run --config vitest.local.mts --project browser dev-examples/mdast-editor/src/__tests__/browser`
```text
Test Files 3 passed (3)
Tests 26 passed (26)
```
The temporary browser config changes only the Playwright executable and
launch arguments to use the available Chromium installation. The standard
command is `pnpm run test-browser dev-examples/mdast-editor/src/__tests__/browser`.
Firefox and WebKit were not exercised in this container.
`pnpm run tsc-test`, ESLint, Prettier, and `git diff --check` passed.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
etrepum
marked this pull request as ready for review
September 26, 2026 05:45
etrepum
requested review from
acywatson,
fantactuka,
ivailop7,
potatowagon and
zurfyx
as code owners
September 26, 2026 05:45
## Description
The toolbar manually walks to a root or shadow-root child before falling back
to getTopLevelElement. Use getTopLevelElement directly: it already performs
that walk, handles named-slot boundaries, and returns null for a root anchor.
This removes the duplicate lookup and its unused imports while retaining
pending inline formats and the paragraph default for root selections.
## Test plan
### Before
`pnpm exec vitest run --config vitest.local.mts --project browser dev-examples/mdast-editor/src/__tests__/browser/ToolbarState.test.ts`
```text
Test Files 1 passed (1)
Tests 4 passed (4)
```
### After
The same command:
```text
Test Files 1 passed (1)
Tests 4 passed (4)
```
The temporary config uses the repository test configuration with only the
Chromium executable and launch arguments overridden. With a standard
Playwright installation, use `pnpm run test-browser dev-examples/mdast-editor/src/__tests__/browser/ToolbarState.test.ts`.
Firefox and WebKit were not exercised.
`pnpm exec tsc --noEmit -p dev-examples/mdast-editor/tsconfig.json`, ESLint,
Prettier, and `git diff --check` passed.
zurfyx
approved these changes
Sep 26, 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
Selecting all rich text in the mdast-editor example and pressing Backspace throws
getTopLevelElementOrThrow: root nodes are not top level elementswhen footnotes remain in a root slot. The root has no document children, butisEmpty()returns false because it still holds the slot, preventing restoration of an editable paragraph.$restoreEmptyContainerParagraph, coveringdeleteCharacter,deleteWord,deleteLine, andremoveText.The caret-range fallback now refills the main root only when the original root or shadow-root container was removed. When the parent walk ends at a slot that is not a shadow root, deletion no longer falls back to refilling the main root.
Direct
node.remove()calls retain their existing behavior: removing the last block can leave the root without children, with or without slots. TheisEmpty()check in$removeNode’s selection repair and the handling of slots on non-root shadow roots are unchanged.Regression coverage includes all four range-delete methods, node deletion, slot isolation, and select-all → Backspace → typing with and without footnotes.
Test plan
Before
Browser reproduction:
pnpm run test-unit packages/lexical/src/__tests__/unit/LexicalSlot.test.ts -t 'deleting'also exposed the missing word and line deletion paths:After
pnpm run test-unit packages/lexical/src/__tests__/unit/LexicalSlot.test.ts -t 'deleting':The related selection, caret, slot, utility, and mdast-editor unit suites passed:
pnpm exec vitest run --config vitest.local.mts --project browser dev-examples/mdast-editor/src/__tests__/browser:The temporary browser config only overrides Chromium’s executable and launch arguments. With a standard Playwright installation, run
pnpm run test-browser dev-examples/mdast-editor/src/__tests__/browser. Firefox and WebKit were not exercised.pnpm run tsc-test, ESLint, Prettier, andgit diff --checkpassed.