Skip to content

[lexical-table] Bug Fix: Leave a cut with neither end in the table to the editor - #9285

Open
rinarakaki wants to merge 3 commits into
meta:mainfrom
rinarakaki:fix/table-cut-outside-table
Open

rinarakaki wants to merge 3 commits into
meta:mainfrom
rinarakaki:fix/table-cut-outside-table

Conversation

@rinarakaki

@rinarakaki rinarakaki commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Description

Each table registers a CUT_COMMAND listener at COMMAND_PRIORITY_HIGH in applyTableHandlers. For any RangeSelection in the document it copied the range, called selection.removeText() and returned true, without checking that the range was in its table. So once a document held a table, no cut listener below HIGH ran for any range:

The listener now takes only the cuts that are wholly its table's: a table selection, and a range with both ends in the table. Any other range goes on to the editor's own cut, as it would with no table in the document. A range with one end in the table is first widened over the whole table, as the table's delete listener already does, so the editor copies the whole table it removes and tags the cut. That widening is one function, $selectAroundTable, which both listeners call. A range from a cell of one table into a cell of another takes both tables, since each table widens its own end.

#6590 stays fixed: the table no longer copies such a range before the editor does, so it is copied once, and the clipboard gets its text.

"Cut table in the middle of a range selection", "Cut table as last node in range selection" and "Cut table as first node in range selection" relied on the table cutting ranges with an end outside it, in an editor with only TablePlugin. Those ranges are now the editor's to cut, so the tests register rich text and wait for rich text's cut, which removes the range once its copy resolves. Their expected HTML is unchanged.

Closes #9281

Changeset

.changeset/table-cut-outside-table.md

Test plan

TableCutScope.test.ts cuts with a ClipboardEvent and reports the text/plain copied, how many times it was written, whether a LOW-priority cut listener ran, and whether an update carried CUT_TAG.

Before

pnpm exec vitest --project unit --no-watch --reporter=verbose packages/lexical-table/src/__tests__/unit/TableCutScope.test.ts (describe prefix trimmed), with main's LexicalTableSelectionHelpers.ts:

 × select all over a heading, a table and a paragraph leaves an empty paragraph (withTable: true)
 × select all over a heading and a table leaves an empty paragraph
 × select all over a table and a paragraph leaves an empty paragraph
 × a range outside the table is copied once and cut by the editor (withTable: true)
 × a range from before the table into a cell takes the whole table, and the editor cuts it
 × a range from a cell to after the table takes the whole table, and the editor cuts it
 × a range from a cell of one table into a cell of another takes both tables
 Test Files  1 failed (1)
      Tests  7 failed | 4 passed (11)

With the listener as this PR first had it, which left a range with one end in the table to the table:

 × select all over a heading and a table leaves an empty paragraph
 × select all over a table and a paragraph leaves an empty paragraph
 × a range from before the table into a cell takes the whole table, and the editor cuts it
 × a range from a cell to after the table takes the whole table, and the editor cuts it
 × a range from a cell of one table into a cell of another takes both tables
AssertionError: expected [ [ 'heading', [] ] ] to deeply equal [ [ 'paragraph', [] ] ]
AssertionError: expected false to be true // Object.is equality
AssertionError: expected false to be true // Object.is equality
AssertionError: expected [ …(3) ] to deeply equal [ Array(1) ]
 Test Files  1 failed (1)
      Tests  5 failed | 6 passed (11)

After

 ✓ select all over a heading, a table and a paragraph leaves an empty paragraph (withTable: false)
 ✓ select all over a heading, a table and a paragraph leaves an empty paragraph (withTable: true)
 ✓ select all over a heading and a table leaves an empty paragraph
 ✓ select all over a table and a paragraph leaves an empty paragraph
 ✓ a range outside the table is copied once and cut by the editor (withTable: false)
 ✓ a range outside the table is copied once and cut by the editor (withTable: true)
 ✓ a range in a cell is the table’s
 ✓ a table selection is the table’s
 ✓ a range from before the table into a cell takes the whole table, and the editor cuts it
 ✓ a range from a cell to after the table takes the whole table, and the editor cuts it
 ✓ a range from a cell of one table into a cell of another takes both tables
 Test Files  1 passed (1)
      Tests  11 passed (11)

pnpm exec vitest --project unit --no-watch packages/lexical-table/ packages/lexical-rich-text/ packages/lexical-clipboard/ packages/lexical-playground/ packages/lexical-react/:

 Test Files  151 passed (151)
      Tests  1473 passed | 9 expected fail (1482)

VITEST_BROWSER=chromium,firefox,webkit pnpm exec vitest --project browser --no-watch packages/lexical-table/ packages/lexical-playground/src/__tests__/browser/TablesHTMLCopyAndPaste.test.ts packages/lexical-playground/src/__tests__/browser/HTMLCopyAndPaste.test.ts:

 Test Files  26 passed | 1 skipped (27)
      Tests  204 passed | 3 skipped (207)

pnpm run tsc and pnpm run check-changesets pass, and Prettier and ESLint are clean on the changed files. The E2E suite wasn't run; its one table cut test (Tables.spec.mjs, "cut them with the context menu") cuts a table selection, which stays the table's. macOS and Windows weren't run.

… the editor

## Description

Each table registers a `CUT_COMMAND` listener at `COMMAND_PRIORITY_HIGH` in `applyTableHandlers`. For any `RangeSelection` in the document it copied the range, called `selection.removeText()` and returned `true`, without checking that the range was in its table. So once a document held a table, no cut listener below HIGH ran for any range:

- Rich text's own cut, which widens a whole-document range before removing it (meta#5835) and tags its updates `CUT_TAG` (meta#8649). A select-all cut over `h1 / table / p` left an empty heading, where the same document without the table leaves a paragraph.
- Every NORMAL or LOW cut listener, such as a plugin that guards cuts (meta#7933).

The listener now returns `false` for a range with neither end in its table, so the range goes on to the editor's own cut, as it would with no table in the document. The table still answers a table selection, and a range with an end in the table, which `$deleteCellHandler` widens around the table first.

meta#6590 stays fixed: the table no longer copies such a range before the editor does, so it is copied once, and the clipboard gets its text.

"Cut table in the middle of a range selection" relied on the table cutting a range with both ends outside it, in an editor with only `TablePlugin`. That range is now the editor's to cut, so the test registers rich text and waits for rich text's cut, which removes the range once its copy resolves. Its expected HTML is unchanged.

Closes meta#9281

## Test plan

### Before

The new `TableCutScope.test.ts`, with the fix reverted:

```
$ pnpm exec vitest --project unit --no-watch packages/lexical-table/src/__tests__/unit/TableCutScope.test.ts
     × select all over a heading, a table and a paragraph leaves an empty paragraph (withTable: true)
     × a range outside the table is copied once and cut by the editor (withTable: true)
AssertionError: expected [ [ 'heading', [] ] ] to deeply equal [ [ 'paragraph', [] ] ]
AssertionError: expected false to be true // Object.is equality
 Test Files  1 failed (1)
      Tests  2 failed | 5 passed (7)
```

### After

```
$ pnpm exec vitest --project unit --no-watch --reporter=verbose packages/lexical-table/src/__tests__/unit/TableCutScope.test.ts
 ✓ |unit| packages/lexical-table/src/__tests__/unit/TableCutScope.test.ts > the cuts a table answers (meta#9281) > select all over a heading, a table and a paragraph leaves an empty paragraph (withTable: false)
 ✓ |unit| packages/lexical-table/src/__tests__/unit/TableCutScope.test.ts > the cuts a table answers (meta#9281) > select all over a heading, a table and a paragraph leaves an empty paragraph (withTable: true)
 ✓ |unit| packages/lexical-table/src/__tests__/unit/TableCutScope.test.ts > the cuts a table answers (meta#9281) > a range outside the table is copied once and cut by the editor (withTable: false)
 ✓ |unit| packages/lexical-table/src/__tests__/unit/TableCutScope.test.ts > the cuts a table answers (meta#9281) > a range outside the table is copied once and cut by the editor (withTable: true)
 ✓ |unit| packages/lexical-table/src/__tests__/unit/TableCutScope.test.ts > the cuts a table answers (meta#9281) > a range in a cell is the table’s
 ✓ |unit| packages/lexical-table/src/__tests__/unit/TableCutScope.test.ts > the cuts a table answers (meta#9281) > a table selection is the table’s
 ✓ |unit| packages/lexical-table/src/__tests__/unit/TableCutScope.test.ts > the cuts a table answers (meta#9281) > a range from outside the table into a cell is the table’s
 Test Files  1 passed (1)
      Tests  7 passed (7)
```

```
$ pnpm exec vitest --project unit --no-watch packages/lexical-table/src/__tests__/unit/TableCutScope.test.ts packages/lexical-table/src/__tests__/unit/LexicalTableNode.test.tsx
 Test Files  2 passed (2)
      Tests  49 passed (49)
```

```
$ pnpm run test-unit
 Test Files  370 passed (370)
      Tests  9018 passed | 1 skipped (9019)
```

```
$ pnpm exec vitest --project browser --no-watch packages/lexical-table
 Test Files  4 passed (4)
      Tests  18 passed (18)
```

`pnpm run tsc` and `pnpm run tsc-test` pass, and prettier and eslint pass on the changed files. The browser tests ran in Chromium only. The e2e suite wasn't run; its one table cut test (`Tables.spec.mjs`, "cut them with the context menu") cuts a table selection, which stays the table's.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@vercel

vercel Bot commented Sep 30, 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 Nov 20, 2026 12:30am UTC
lexical-playground Ready Ready Preview Nov 20, 2026 12:30am 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 30, 2026
@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Sep 30, 2026

@rootnode-bot rootnode-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for the clear issue and reproducer. This looks good to me.

The table's CUT_COMMAND listener now handles only ranges that touch its own table (the new guard in packages/lexical-table/src/LexicalTableSelectionHelpers.ts, lines 904–910). A select-all cut over a heading, a table and a paragraph now goes through rich text's own cut: it leaves an empty paragraph, the update is tagged cut, and lower-priority cut listeners run. Table selections, ranges inside a cell, and ranges from outside the table into a cell behave as before. The new withTable: true cases fail before the fix and pass after it.

Optional, non-blocking suggestions:

  1. Ranges with one end in the table (the open review comment). Such a range still takes the table's own path (LexicalTableSelectionHelpers.ts, lines 913–922): it is widened around the table, its text is removed, and the listener returns true. So select all and cut on a document that starts or ends with a table, such as [heading, table], still leaves an empty heading, with no cut tag and no lower-priority listener call. Widening the range and then returning false, as suggested, would send both cases through rich text. It would also mean these changes:

    • flipping the lowListenerRan expectation at TableCutScope.test.ts:247;
    • giving the TablePlugin-only tests "Cut table as last node in range selection" and "Cut table as first node in range selection" in LexicalTableNode.test.tsx the same rich-text setup you gave "Cut table in the middle of a range selection";
    • a range from a cell in one table into a cell in another would then remove both tables.

    Would you like to handle this here, or in a follow-up issue? Since this PR closes #9281, a follow-up issue would keep it tracked.

  2. Test title. a range outside the table is copied once and cut by the editor (TableCutScope.test.ts:153) checks what was copied, not how many times; it would also pass with two identical clipboard writes. Renaming it, or counting the writes, would make the title match what it checks.

  3. History boundary. The issue cites the CUT_TAG history boundary (#8609). An update listener asserting tags.has(CUT_TAG) would pin that boundary directly.

…he whole table, and leave it to the editor

## Description

Follow-up to the review. A range with one end in a table and the other outside it still took the table's own cut path: the table copied the range as selected, widened it around the table, removed its text and returned `true`. So the clipboard didn't hold the whole table that went, the update had no `cut` tag, and lower-priority cut listeners didn't run. Select all on a document that starts or ends with a table, then cut, left an empty heading that way.

The table's `CUT_COMMAND` listener now widens such a range over the whole table, as its delete listener already does, and returns `false`, so rich text's cut copies and removes the widened range with its `cut` tag. The widening is one function, `$selectAroundTable`, which both listeners call. The table keeps a table selection and a range inside it. A range from a cell of one table into a cell of another takes both tables, since each table widens its own end.

Tests:
- `TableCutScope.test.ts`: select all over a document that starts or ends with a table leaves an empty paragraph; a range from before the table into a cell, and from a cell to after the table, is cut by the editor with the whole table on the clipboard; a range across two tables takes both. `cut()` now counts text/plain writes, so "copied once" checks once, and reports whether an update carried `CUT_TAG`, the history boundary from meta#8609.
- `LexicalTableNode.test.tsx`: "Cut table as last node in range selection" and "Cut table as first node in range selection" register rich text, like "Cut table in the middle of a range selection", since the editor now does that cut.

## Changeset

`.changeset/table-cut-outside-table.md`

## Test plan

### Before

`pnpm exec vitest --project unit --no-watch packages/lexical-table/src/__tests__/unit/TableCutScope.test.ts`, with this PR's previous `LexicalTableSelectionHelpers.ts`:

```
 × select all over a heading and a table leaves an empty paragraph
 × select all over a table and a paragraph leaves an empty paragraph
 × a range from before the table into a cell takes the whole table, and the editor cuts it
 × a range from a cell to after the table takes the whole table, and the editor cuts it
 × a range from a cell of one table into a cell of another takes both tables
AssertionError: expected [ [ 'heading', [] ] ] to deeply equal [ [ 'paragraph', [] ] ]
AssertionError: expected false to be true // Object.is equality
AssertionError: expected false to be true // Object.is equality
AssertionError: expected [ …(3) ] to deeply equal [ Array(1) ]
 Test Files  1 failed (1)
      Tests  5 failed | 6 passed (11)
```

With `main`'s:

```
 × select all over a heading, a table and a paragraph leaves an empty paragraph (withTable: true)
 × select all over a heading and a table leaves an empty paragraph
 × select all over a table and a paragraph leaves an empty paragraph
 × a range outside the table is copied once and cut by the editor (withTable: true)
 × a range from before the table into a cell takes the whole table, and the editor cuts it
 × a range from a cell to after the table takes the whole table, and the editor cuts it
 × a range from a cell of one table into a cell of another takes both tables
 Test Files  1 failed (1)
      Tests  7 failed | 4 passed (11)
```

### After

```
 ✓ select all over a heading, a table and a paragraph leaves an empty paragraph (withTable: false)
 ✓ select all over a heading, a table and a paragraph leaves an empty paragraph (withTable: true)
 ✓ select all over a heading and a table leaves an empty paragraph
 ✓ select all over a table and a paragraph leaves an empty paragraph
 ✓ a range outside the table is copied once and cut by the editor (withTable: false)
 ✓ a range outside the table is copied once and cut by the editor (withTable: true)
 ✓ a range in a cell is the table’s
 ✓ a table selection is the table’s
 ✓ a range from before the table into a cell takes the whole table, and the editor cuts it
 ✓ a range from a cell to after the table takes the whole table, and the editor cuts it
 ✓ a range from a cell of one table into a cell of another takes both tables
 Test Files  1 passed (1)
      Tests  11 passed (11)
```

`pnpm exec vitest --project unit --no-watch packages/lexical-table/ packages/lexical-rich-text/ packages/lexical-clipboard/ packages/lexical-playground/ packages/lexical-react/`:

```
 Test Files  151 passed (151)
      Tests  1473 passed | 9 expected fail (1482)
```

`VITEST_BROWSER=chromium,firefox,webkit pnpm exec vitest --project browser --no-watch packages/lexical-table/ packages/lexical-playground/src/__tests__/browser/TablesHTMLCopyAndPaste.test.ts packages/lexical-playground/src/__tests__/browser/HTMLCopyAndPaste.test.ts`:

```
 Test Files  26 passed | 1 skipped (27)
      Tests  204 passed | 3 skipped (207)
```

`pnpm run tsc` and `pnpm run check-changesets` pass, and ESLint and Prettier are clean on the changed files. The E2E suite wasn't run; its table cut (`Tables.spec.mjs`, a context-menu cut of a table selection) takes the path this leaves unchanged.
@pkg-pr-new

pkg-pr-new Bot commented Nov 20, 2026

Copy link
Copy Markdown
lexical

npm i https://pkg.pr.new/lexical@9285

@lexical/a11y

npm i https://pkg.pr.new/@lexical/a11y@9285

@lexical/clipboard

npm i https://pkg.pr.new/@lexical/clipboard@9285

@lexical/code

npm i https://pkg.pr.new/@lexical/code@9285

@lexical/code-core

npm i https://pkg.pr.new/@lexical/code-core@9285

@lexical/code-prism

npm i https://pkg.pr.new/@lexical/code-prism@9285

@lexical/code-shiki

npm i https://pkg.pr.new/@lexical/code-shiki@9285

@lexical/compiler

npm i https://pkg.pr.new/@lexical/compiler@9285

@lexical/devtools-core

npm i https://pkg.pr.new/@lexical/devtools-core@9285

@lexical/dragon

npm i https://pkg.pr.new/@lexical/dragon@9285

@lexical/eslint-plugin

npm i https://pkg.pr.new/@lexical/eslint-plugin@9285

@lexical/extension

npm i https://pkg.pr.new/@lexical/extension@9285

@lexical/fast-check

npm i https://pkg.pr.new/@lexical/fast-check@9285

@lexical/file

npm i https://pkg.pr.new/@lexical/file@9285

@lexical/hashtag

npm i https://pkg.pr.new/@lexical/hashtag@9285

@lexical/headless

npm i https://pkg.pr.new/@lexical/headless@9285

@lexical/history

npm i https://pkg.pr.new/@lexical/history@9285

@lexical/html

npm i https://pkg.pr.new/@lexical/html@9285

@lexical/internal

npm i https://pkg.pr.new/@lexical/internal@9285

@lexical/link

npm i https://pkg.pr.new/@lexical/link@9285

@lexical/list

npm i https://pkg.pr.new/@lexical/list@9285

@lexical/mark

npm i https://pkg.pr.new/@lexical/mark@9285

@lexical/markdown

npm i https://pkg.pr.new/@lexical/markdown@9285

@lexical/mdast

npm i https://pkg.pr.new/@lexical/mdast@9285

@lexical/offset

npm i https://pkg.pr.new/@lexical/offset@9285

@lexical/overflow

npm i https://pkg.pr.new/@lexical/overflow@9285

@lexical/plain-text

npm i https://pkg.pr.new/@lexical/plain-text@9285

@lexical/react

npm i https://pkg.pr.new/@lexical/react@9285

@lexical/rich-text

npm i https://pkg.pr.new/@lexical/rich-text@9285

@lexical/selection

npm i https://pkg.pr.new/@lexical/selection@9285

@lexical/table

npm i https://pkg.pr.new/@lexical/table@9285

@lexical/tailwind

npm i https://pkg.pr.new/@lexical/tailwind@9285

@lexical/text

npm i https://pkg.pr.new/@lexical/text@9285

@lexical/utils

npm i https://pkg.pr.new/@lexical/utils@9285

@lexical/y

npm i https://pkg.pr.new/@lexical/y@9285

@lexical/yjs

npm i https://pkg.pr.new/@lexical/yjs@9285

commit: 5b2163c

@rinarakaki

Copy link
Copy Markdown
Contributor Author

Thanks, both. I merged main and pushed 5b2163c:

  • a range with one end in the table is widened over the whole table and left to the editor, as suggested in the thread above;
  • cut() counts the text/plain writes and reports whether an update carried CUT_TAG, so "copied once" checks once and the editor's cuts check the history boundary (suggestions 2 and 3);
  • the changeset, .changeset/table-cut-outside-table.md.

The description now describes the change as it stands, with the test runs.

@rootnode-bot rootnode-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for the fix, and for widening a range with one end in the table as suggested in review. This looks good to me.

The table's CUT_COMMAND listener now handles only what belongs to its table: a table selection, or a range inside one table. Every other range goes to the editor's own cut. A select-all cut over heading / table / paragraph therefore leaves an empty paragraph, as the same document without a table does, lower-priority cut listeners run, and the cut gets its own CUT_TAG history entry. Reusing the Backspace/Delete widening through $selectAroundTable keeps the two paths consistent. The two-table case (TableCutScope.test.ts:327) is a good addition too: on main the second table is left partly cut. The earlier note about counting clipboard writes is addressed by the plainTextWrites assertions.

Optional, non-blocking:

  • Refresh the PR description and test plan. The Description still says the table "still answers ... a range with an end in the table", and the After output lists 7 tests, including "a range from outside the table into a cell is the table's". Since 5b2163c, such a range is widened and handed to the editor (LexicalTableSelectionHelpers.ts:985-991), and TableCutScope.test.ts has 11 cases. AGENTS.md ("Commits and Pull Requests") asks the Test plan to carry the real Before and After output of the command that was run, so re-pasting it from this head would keep the record accurate.

The changeset follows CONTRIBUTING.md, including the Verify: paragraph a behavior change needs.

Automated review (maintainer link)

@rinarakaki

Copy link
Copy Markdown
Contributor Author

The description was refreshed after this review started: it describes the change at 5b2163c and pastes the Before and After of all 11 cases in TableCutScope.test.ts.

This branch was successfully deployed

2 active deployments
Preview – lexical — 5b2163c1 Deployed Nov 20, 2026 by vercel[bot]
Preview – lexical-playground — 5b2163c1 Deployed Nov 20, 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: [lexical-table] Once a document has a table, every cut goes through the table's handler, even one nowhere near the table

2 participants