Skip to content

[lexical] Bug Fix: Handle select-all deletion with root slots - #9240

Merged
etrepum merged 3 commits into
meta:mainfrom
etrepum:fix/mdast-editor-delete-all
Sep 26, 2026
Merged

etrepum merged 3 commits into
meta:mainfrom
etrepum:fix/mdast-editor-delete-all

Conversation

@etrepum

@etrepum etrepum commented Sep 26, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Selecting all rich text in the mdast-editor example and pressing Backspace throws getTopLevelElementOrThrow: root nodes are not top level elements when footnotes remain in a root slot. The root has no document children, but isEmpty() returns false because it still holds the slot, preventing restoration of an editable paragraph.

  • Check the root’s child count in $restoreEmptyContainerParagraph, covering deleteCharacter, deleteWord, deleteLine, and removeText.
  • Restore an editable paragraph after node deletion while preserving selection isolation inside slots.
  • Restrict the range-deletion fallback to containers that were removed, so editing a footnote does not populate a previously empty document body.
  • Use the nullable top-level lookup in the example’s toolbar, preserving pending inline formats for root selections.

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. The isEmpty() 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:

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

pnpm run test-unit packages/lexical/src/__tests__/unit/LexicalSlot.test.ts -t 'deleting' also exposed the missing word and line deletion paths:

× 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

After

pnpm run test-unit packages/lexical/src/__tests__/unit/LexicalSlot.test.ts -t 'deleting':

 Test Files  1 passed (1)
      Tests  10 passed | 108 skipped (118)

The related selection, caret, slot, utility, and mdast-editor unit suites passed:

 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:

 Test Files  3 passed (3)
      Tests  26 passed (26)

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, and git diff --check passed.

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

vercel Bot commented Sep 26, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
lexical Ready Ready Preview Sep 26, 2026 4:08pm UTC
lexical-playground Ready Ready Preview Sep 26, 2026 4:08pm UTC

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 Sep 26, 2026
@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Sep 26, 2026
@etrepum
etrepum marked this pull request as ready for review 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.
@etrepum
etrepum added this pull request to the merge queue Sep 26, 2026
Merged via the queue into meta:main with commit 5601522 Sep 26, 2026
78 of 79 checks passed
@etrepum etrepum mentioned this pull request Sep 28, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — f8ddf85d Deployed Sep 26, 2026 by vercel[bot]
Preview – lexical-playground — f8ddf85d Deployed Sep 26, 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.

2 participants