Skip to content

[lexical-table] Bug Fix: Remove the whole table when a range from outside it is deleted by character or word - #10048

Open
mayrang wants to merge 1 commit into
meta:mainfrom
mayrang:fix/table-outside-in-delete-text
Open

mayrang wants to merge 1 commit into
meta:mainfrom
mayrang:fix/table-outside-in-delete-text

Conversation

@mayrang

@mayrang mayrang commented Dec 5, 2026 •

Copy link
Copy Markdown
Collaborator

Description

A range with one end outside a table and the other inside it is what Shift+ArrowUp from the paragraph below a table leaves, and so does a mouse drag from there up into the table (the table's selection handling moves the end to the start of the first cell). Backspace and Delete on that range remove the whole table, because $deleteCellHandler moves the end in the table to the table's outer edge first (#6650). Deleting the same range by word (Option+Backspace on macOS, Ctrl+Backspace elsewhere) or with Ctrl+H / Ctrl+D on macOS dispatches DELETE_WORD_COMMAND or DELETE_CHARACTER_COMMAND instead, and the table's handler for those only handled a TableSelection. The editor then removed part of the table: in Chromium, with p, a table of c / d and ab, Option+Backspace left p, a table with one empty cell, and b. With a cell that spans two rows, it left that cell in a one-row table.

DELETE_CHARACTER_COMMAND and DELETE_WORD_COMMAND now go through $deleteCellHandler too. On its own that made Backspace worse. selectPrevious / selectNext can put the moved end inside another table: an adjacent one, or one at the edge of an element that cannot be empty, because ElementNode.select() descends into such an element's first or last child. That table's handler then expanded the range around itself as well, in the same key command or when Backspace or Delete re-dispatches DELETE_CHARACTER_COMMAND. So when the end lands in a table that is not an ancestor of this one, it now goes beside this table, or beside the outermost element this table is at that edge of, stopping at a table cell or at an element that holds the other end. An element the range empties from outside then goes with the table instead of being left empty, and a cell keeps the rest of its content. A shadow root that holds both ends of the range stays even when the range empties it, as the core removal keeps it when the range holds no table. When the climb stops at a cell whose table does not hold the other end, the end stays where it landed, since that table expands around itself too and a cut only runs the first table's handler (#9281).

That also fixes cases Backspace and Delete already got wrong on main: a range from the paragraph above into the first of two adjacent tables, or into a table followed by such an element that starts with a table, removed the other table too, and a range into a table that is the first or last child of a cell, or that starts or ends another element that cannot be empty, left part of the table behind. DELETE_LINE_COMMAND keeps its own handler and does not expand the range.

Two things this does not touch. Cutting such a range with two tables in the document still goes through whichever table registered first (#9281). And #9285 moves the same expansion block into a $selectAroundTable helper, so whichever of the two lands second needs a small rebase.

Changeset

.changeset/table-delete-range-into-table.md: @lexical/table patch, [fix], with a Verify: paragraph for the word delete / Ctrl+H case, the adjacent tables, the table inside a cell or another element that cannot be empty, and line deletion being unchanged.

Test plan

The unit test runs every document with the selection in both directions and with Backspace, Delete and the two text commands, because each direction goes through a different part of the new branch. The documents cover adjacent tables, tables at the edge of cells and of elements that cannot be empty (nested, as a shadow root like the playground's page content, and in a slot value), and a cut. The browser test uses real Shift+ArrowUp and keys, and first checks that the selection really ends inside the table.

Before

pnpm vitest run --project unit packages/lexical-table/src/__tests__/unit/DeleteRangeIntoTable.test.ts with LexicalTableSelectionHelpers.ts from main:

Tests 138 failed | 81 passed (219)
     × DELETE_CHARACTER_COMMAND from below into the first cell
     × DELETE_WORD_COMMAND from below into the first cell
     × DELETE_CHARACTER_COMMAND from below into the first cell, reversed
     × DELETE_WORD_COMMAND from below into the first cell, reversed
     × DELETE_CHARACTER_COMMAND from above into the first cell
     × DELETE_WORD_COMMAND from above into the first cell
     × DELETE_CHARACTER_COMMAND from above into the first cell, reversed
     × DELETE_WORD_COMMAND from above into the first cell, reversed
     × DELETE_CHARACTER_COMMAND into a cell spanning two rows
     × DELETE_WORD_COMMAND into a cell spanning two rows
     × DELETE_CHARACTER_COMMAND into a cell spanning two rows, reversed
     × DELETE_WORD_COMMAND into a cell spanning two rows, reversed
     × DELETE_CHARACTER_COMMAND into the later of two tables
     × DELETE_WORD_COMMAND into the later of two tables
     × DELETE_CHARACTER_COMMAND into the later of two tables, reversed
     × DELETE_WORD_COMMAND into the later of two tables, reversed
     × Backspace into the earlier of two tables
     × Delete into the earlier of two tables
     × Backspace into the earlier of two tables, reversed
     × Delete into the earlier of two tables, reversed
     × Backspace into a table ending a cell
     × Delete into a table ending a cell
     × DELETE_CHARACTER_COMMAND into a table ending a cell
     × DELETE_WORD_COMMAND into a table ending a cell
     × Backspace into a table ending a cell, reversed
     × Delete into a table ending a cell, reversed
     × DELETE_CHARACTER_COMMAND into a table ending a cell, reversed
     × DELETE_WORD_COMMAND into a table ending a cell, reversed
     × Backspace into a table starting a cell
     × Delete into a table starting a cell
     × DELETE_CHARACTER_COMMAND into a table starting a cell
     × DELETE_WORD_COMMAND into a table starting a cell
     × Backspace into a table starting a cell, reversed
     × Delete into a table starting a cell, reversed
     × DELETE_CHARACTER_COMMAND into a table starting a cell, reversed
     × DELETE_WORD_COMMAND into a table starting a cell, reversed
     × DELETE_CHARACTER_COMMAND into a table inside a cell
     × DELETE_WORD_COMMAND into a table inside a cell
     × DELETE_CHARACTER_COMMAND into a table inside a cell, reversed
     × DELETE_WORD_COMMAND into a table inside a cell, reversed
     × DELETE_CHARACTER_COMMAND into a table after a box ending in a ta…
     × DELETE_WORD_COMMAND into a table after a box ending in a ta…
     × DELETE_CHARACTER_COMMAND into a table after a box ending in a ta…
     × DELETE_WORD_COMMAND into a table after a box ending in a ta…
     × Backspace into a table before a box starting with…
     × Delete into a table before a box starting with…
     × Backspace into a table before a box starting with…
     × Delete into a table before a box starting with…
     × Backspace into a table starting a box the range c…
     × Delete into a table starting a box the range c…
     × DELETE_CHARACTER_COMMAND into a table starting a box the range c…
     × DELETE_WORD_COMMAND into a table starting a box the range c…
     × Backspace into a table starting a box the range c…
     × Delete into a table starting a box the range c…
     × DELETE_CHARACTER_COMMAND into a table starting a box the range c…
     × DELETE_WORD_COMMAND into a table starting a box the range c…
     × Backspace into a table starting a box from inside…
     × Delete into a table starting a box from inside…
     × DELETE_CHARACTER_COMMAND into a table starting a box from inside…
     × DELETE_WORD_COMMAND into a table starting a box from inside…
     × Backspace into a table starting a box from inside…
     × Delete into a table starting a box from inside…
     × DELETE_CHARACTER_COMMAND into a table starting a box from inside…
     × DELETE_WORD_COMMAND into a table starting a box from inside…
     × DELETE_CHARACTER_COMMAND into a table ending a cell, from above
     × DELETE_WORD_COMMAND into a table ending a cell, from above
     × DELETE_CHARACTER_COMMAND into a table ending a cell, from above,…
     × DELETE_WORD_COMMAND into a table ending a cell, from above,…
     × Backspace into a table starting a box from its end
     × Delete into a table starting a box from its end
     × DELETE_CHARACTER_COMMAND into a table starting a box from its end
     × DELETE_WORD_COMMAND into a table starting a box from its end
     × Backspace into a table starting a box from its en…
     × Delete into a table starting a box from its en…
     × DELETE_CHARACTER_COMMAND into a table starting a box from its en…
     × DELETE_WORD_COMMAND into a table starting a box from its en…
     × Backspace into a table starting a box after a sib…
     × Delete into a table starting a box after a sib…
     × DELETE_CHARACTER_COMMAND into a table starting a box after a sib…
     × DELETE_WORD_COMMAND into a table starting a box after a sib…
     × Backspace into a table starting a box after a sib…
     × Delete into a table starting a box after a sib…
     × DELETE_CHARACTER_COMMAND into a table starting a box after a sib…
     × DELETE_WORD_COMMAND into a table starting a box after a sib…
     × Backspace into a table starting a shadow root the…
     × Delete into a table starting a shadow root the…
     × DELETE_CHARACTER_COMMAND into a table starting a shadow root the…
     × DELETE_WORD_COMMAND into a table starting a shadow root the…
     × Backspace into a table starting a shadow root the…
     × Delete into a table starting a shadow root the…
     × DELETE_CHARACTER_COMMAND into a table starting a shadow root the…
     × DELETE_WORD_COMMAND into a table starting a shadow root the…
     × Backspace into a table starting a shadow root fro…
     × Delete into a table starting a shadow root fro…
     × DELETE_CHARACTER_COMMAND into a table starting a shadow root fro…
     × DELETE_WORD_COMMAND into a table starting a shadow root fro…
     × Backspace into a table starting a shadow root fro…
     × Delete into a table starting a shadow root fro…
     × DELETE_CHARACTER_COMMAND into a table starting a shadow root fro…
     × DELETE_WORD_COMMAND into a table starting a shadow root fro…
     × Backspace into a table ending a shadow root from …
     × Delete into a table ending a shadow root from …
     × DELETE_CHARACTER_COMMAND into a table ending a shadow root from …
     × DELETE_WORD_COMMAND into a table ending a shadow root from …
     × Backspace into a table ending a shadow root from …
     × Delete into a table ending a shadow root from …
     × DELETE_CHARACTER_COMMAND into a table ending a shadow root from …
     × DELETE_WORD_COMMAND into a table ending a shadow root from …
     × Backspace into a table starting a shadow root fro…
     × Delete into a table starting a shadow root fro…
     × DELETE_CHARACTER_COMMAND into a table starting a shadow root fro…
     × DELETE_WORD_COMMAND into a table starting a shadow root fro…
     × Backspace into a table starting a shadow root fro…
     × Delete into a table starting a shadow root fro…
     × DELETE_CHARACTER_COMMAND into a table starting a shadow root fro…
     × DELETE_WORD_COMMAND into a table starting a shadow root fro…
     × Backspace into a table starting a shadow root in …
     × Delete into a table starting a shadow root in …
     × DELETE_CHARACTER_COMMAND into a table starting a shadow root in …
     × DELETE_WORD_COMMAND into a table starting a shadow root in …
     × Backspace into a table starting a shadow root in …
     × Delete into a table starting a shadow root in …
     × DELETE_CHARACTER_COMMAND into a table starting a shadow root in …
     × DELETE_WORD_COMMAND into a table starting a shadow root in …
     × Backspace into the first of two tables in a cell
     × Delete into the first of two tables in a cell
     × DELETE_CHARACTER_COMMAND into the first of two tables in a cell
     × DELETE_WORD_COMMAND into the first of two tables in a cell
     × Backspace into the first of two tables in a cell,…
     × Delete into the first of two tables in a cell,…
     × DELETE_CHARACTER_COMMAND into the first of two tables in a cell,…
     × DELETE_WORD_COMMAND into the first of two tables in a cell,…
     × a range from the end of a box into a table starting it removes the box
     × Backspace from the end of a box ending with a decorator into a table starting it removes the box
     × DELETE_CHARACTER_COMMAND from the end of a box ending with a decorator into a table starting it removes the box
     × DELETE_WORD_COMMAND from the end of a box ending with a decorator into a table starting it removes the box
     × a range into a table in a slot value removes only that table
     × the caret is left beside the remaining adjacent table
      Tests  138 failed | 81 passed (219)

VITEST_BROWSER=chromium pnpm vitest run --project browser packages/lexical-table/src/__tests__/browser/DeleteRangeIntoTableKeys.test.ts with the same file from main:

     × word Backspace removes the whole table
     × word Delete removes the whole table
- Expected
+ Received
  [
-   "paragraph:pb",
+   "paragraph:p",
+   "table:",
+   "paragraph:b",
  ]
      Tests  2 failed | 1 passed (3)

After

DeleteRangeIntoTable.test.ts           Tests  219 passed (219)
DeleteRangeIntoTableKeys.test.ts       Tests  3 passed (3)
pnpm run test-unit                     Tests  41350 passed | 423 expected fail | 174 skipped (41947)
pnpm run test-e2e-chromium             661 passed, 57 skipped
pnpm run test-e2e-firefox              627 passed, 91 skipped
pnpm run test-e2e-webkit               641 passed, 77 skipped

The browser test also passes in Firefox and WebKit. In the playground (Chromium, Firefox and WebKit), Shift+ArrowUp or a mouse drag from below into the table, then Backspace, word Backspace or Ctrl+H, removes the whole table. With nested tables on, a range from the paragraph above into a table whose last cell ends with a table gives the same result as main for Backspace and for a cut (Cmd+X) in all three.

On a Galaxy S25 (Chrome 158), the Samsung Keyboard's delete key sends a Backspace keydown, so the range from below already removed the whole table before this change, and still does. With two adjacent tables and a range from the paragraph above into the first one, the same key removed both tables before and removes only the first one now. With HeliBoard, which sends only beforeinput, the range had shrunk back into the paragraph before the delete, and both builds deleted one character. On an iPhone 12 (iOS 26.5, Safari), a range dragged with the selection handles from the first cell to the end of the paragraph below, deleted with the keyboard's delete key, removes the whole table before and after. With two adjacent tables, a range dragged from the paragraph above into the first one removed both tables before and removes only the first one now, as on the Galaxy.

@vercel

vercel Bot commented Dec 5, 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 Dec 6, 2026 1:15pm UTC
lexical-playground Ready Ready Preview Dec 6, 2026 1:15pm 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 Dec 5, 2026
@rootnode-bot

rootnode-bot Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Dec 6, 2026, 1:45 PM UTC

@pkg-pr-new

pkg-pr-new Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
lexical

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

@lexical/a11y

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

@lexical/clipboard

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

@lexical/code

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

@lexical/code-core

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

@lexical/code-prism

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

@lexical/code-shiki

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

@lexical/compiler

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

@lexical/devtools-core

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

@lexical/dragon

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

@lexical/eslint-plugin

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

@lexical/extension

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

@lexical/fast-check

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

@lexical/file

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

@lexical/hashtag

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

@lexical/headless

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

@lexical/history

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

@lexical/html

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

@lexical/internal

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

@lexical/link

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

@lexical/list

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

@lexical/mark

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

@lexical/markdown

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

@lexical/mdast

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

@lexical/offset

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

@lexical/overflow

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

@lexical/plain-text

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

@lexical/react

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

@lexical/rich-text

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

@lexical/selection

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

@lexical/table

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

@lexical/tailwind

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

@lexical/text

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

@lexical/utils

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

@lexical/y

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

@lexical/yjs

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

commit: 531e7c0

@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, this looks good. Routing DELETE_CHARACTER_COMMAND and DELETE_WORD_COMMAND through $deleteCellHandler makes word delete and Ctrl+H/Ctrl+D behave like Backspace and Delete, and the parent-point fallback fixes the adjacent-table case and the table-at-the-edge-of-a-cell case. I reproduced the new unit and browser tests failing against main's implementation and passing with this change. The two suggestions below are optional and non-blocking.

  1. A table at the end of a container above is now deleted too (packages/lexical-table/src/LexicalTableSelectionHelpers.ts:926). The fallback runs only when the sibling is a TableNode. Suppose the sibling is instead an element whose canBeEmpty() returns false, so ElementNode.select() descends into its last child, and it ends with a table. Then selectPrevious() still lands inside that table. Because rich text's Backspace now re-enters the table listeners through DELETE_CHARACTER_COMMAND, that table widens around itself as well.

    Reproduced locally in Chromium with real keys. The document is p, a shadow-root container with canBeEmpty() === false holding k and a one-cell table, a table c/d, then qr. With the caret at the start of qr, press Shift+ArrowUp, then Backspace. main removes only c/d; this branch also removes the container's table.

    Deciding by where the moved point lands, rather than by the sibling's type, fixed it in my local run for Backspace, DELETE_CHARACTER_COMMAND and DELETE_WORD_COMMAND, and all 47 new cases still pass:

    const newSelection = tableNode[isBefore ? 'selectPrevious' : 'selectNext']();
    const landedIn = $findTableNode(newSelection[tablePoint].getNode());
    if (landedIn !== null && !landedIn.isParentOf(tableNode)) {
      const parent = tableNode.getParentOrThrow();
      const index = tableNode.getIndexWithinParent() + (isBefore ? 0 : 1);
      newSelection[tablePoint].set(parent.getKey(), index, 'element');
    }
    newSelection[outerPoint].set(key, offset, type);

    A case for this layout in CASES would keep it covered.

  2. Optional test addition: the "into the earlier of two tables" case (packages/lexical-table/src/__tests__/unit/DeleteRangeIntoTable.test.ts:188-199) runs only the forward commands. The changeset's Verify: paragraph also promises Backspace for that range, so a Backspace case for the same selection (it passes on this branch locally) would pin that claim.

Automated review (maintainer link)

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

This looks good — thanks for the rework since the last round. Moving DELETE_CHARACTER_COMMAND and DELETE_WORD_COMMAND onto $deleteCellHandler, and checking where the widened end actually lands instead of the sibling's type, makes word delete and Ctrl+H on a range into a table behave like Backspace, and also fixes the adjacent-table and edge-table cases Backspace already got wrong.

What I checked:

  • The new unit file fails 95 of its 176 cases against the main implementation of LexicalTableSelectionHelpers.ts and passes all 176 on this branch (reproduced locally), matching your test plan's Before and After output.
  • The real-key browser test passes in this PR's CI browser jobs for Chromium, Firefox and WebKit.
  • The changeset is a @lexical/table patch [fix] entry with a Verify: paragraph, which is what CONTRIBUTING.md and .changeset/README.md ask for a fix that changes editing behavior.

One optional, non-blocking suggestion:

  1. Optional: add a case for a shadow root the range only partly covers (packages/lexical-table/src/__tests__/unit/DeleteRangeIntoTable.test.ts:404-414). The climb in LexicalTableSelectionHelpers.ts:933-944 can move the end out of a shadow root other than a cell, but the only shadow-root case has the range cover the whole container. Something like $build: () => [$p('p'), $shadowBox($table([['x']]), $p('k')), $p('q')] with select: ['k', 0, 'x', 0], backward: true and expected: 'p(p) box(p(k)) p(q)' would pin that the container and the rest of its content stay. It already behaves that way when I tried it locally; the case would keep it so if the core removal logic changes.

Automated review (maintainer link)

@mayrang
mayrang force-pushed the fix/table-outside-in-delete-text branch from 4a8b676 to 30cb92a Compare December 5, 2026 22:37
@mayrang

mayrang commented Dec 5, 2026

Copy link
Copy Markdown
Collaborator Author

Added that case (into a table starting a shadow root from inside it). It passes here and fails on main, which leaves part of the table behind.

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

This looks good to me. Sending DELETE_WORD_COMMAND and DELETE_CHARACTER_COMMAND through $deleteCellHandler fixes the word-delete and Ctrl+H case. Moving the end beside the table when selectPrevious/selectNext lands in another table fixes the adjacent-table case. I reproduced both locally: the new unit file fails 103 of 184 with LexicalTableSelectionHelpers.ts from main and passes on this branch. The browser test fails for word Backspace and word Delete on main in Chromium and passes here. With two adjacent tables, real keys now remove only the selected table, and typing afterwards lands in the paragraph beside it.

One optional, non-blocking suggestion:

  • Optional: pin two more shapes this change fixes (packages/lexical-table/src/__tests__/unit/DeleteRangeIntoTable.test.ts:404-421). The shadow-root cases only cover a table that starts the root. Two other shapes are fixed here too. A table ending a shadow root, with the range running forward from inside it (shadow(p(k) table(x)), k@1 → x@0): main leaves an emptied table in the root for Backspace, Delete and both text commands. A range from a cell's paragraph into the first of two tables in that cell (td(p(A) table(a) table(c)), A@1 → a@0): main's Backspace also loses c. A case for each, for example into a table ending a shadow root from inside it and into the first of two tables in a cell, would keep them from regressing.

Automated review (maintainer link)

@mayrang

mayrang commented Dec 5, 2026

Copy link
Copy Markdown
Collaborator Author

Added both (into a table ending a shadow root from inside it and into the first of two tables in a cell). Each fails on main with Backspace, Delete and both text commands, and passes here.

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

This looks good, and thanks for the thorough test matrix: I ran the new unit and browser tests locally, and they fail against the main implementation and pass on this branch. Two optional, non-blocking suggestions:

  1. Consider: the end-repair climb passes through shadow roots (packages/lexical-table/src/LexicalTableSelectionHelpers.ts, lines 934-944). The loop stops only at a table cell or a top-level node, so when a table starts an element that is a shadow root and cannot be empty (the playground's PageContentNode is one), a range from the end of that element's content back into the table now deletes the shadow root itself, plus a single-child wrapper such as PageNode. Core keeps an emptied shadow root (the !$isRootOrShadowRoot(this) guard in ElementNode.splice, packages/lexical/src/nodes/LexicalElementNode.ts line 879), and the same range with a paragraph in place of the table leaves the container holding an empty paragraph. Reproduced locally with shadow(table(x), p(k)) and the range k@1 -> x@0: Backspace, both text commands and a cut all give p(p) p(q), while main leaves an emptied table, so this is not a regression from correct behavior. If keeping the container is the intent, stopping the climb at a shadow root that holds the other end, as the cell check right below it does for cells, would do it; either way, a case like into a table starting a shadow root from its end would pin the intended result.

  2. Nit: refresh the Test plan's counts. The Before and After blocks show 184 tests, but DeleteRangeIntoTable.test.ts now runs 200 after the two cases added in the last push (into a table ending a shadow root from inside it, into the first of two tables in a cell); locally it is 119 failed | 81 passed (200) against the main implementation and 200 passed (200) on this branch. AGENTS.md asks the Test plan to carry the real output of the command that was run.

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

Automated review (maintainer link)

@mayrang

mayrang commented Dec 6, 2026

Copy link
Copy Markdown
Collaborator Author

Done. The climb now stops at any element holding the other end (the same results as a shadow-root-only check on every layout I tried), so a shadow root holding both ends stays, emptied. into a table starting a shadow root from its end covers it, and the Test plan counts are already updated.

@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, this looks good. Routing DELETE_CHARACTER_COMMAND and DELETE_WORD_COMMAND through $deleteCellHandler makes word delete and Ctrl+H/Ctrl+D remove the whole table the way Backspace already does, and the landedIn repair also fixes the adjacent-table and nested-table cases main got wrong. Reproduced locally: the new unit tests fail 135 of 216 against main's handler and all pass with this change, and the browser test's two word-delete cases fail on main and pass here. The changeset follows CONTRIBUTING.md (patch, [fix], a Verify: paragraph).

One optional, non-blocking suggestion:

  • Nit: the climb does not stop at the element the other end sits on. In packages/lexical-table/src/LexicalTableSelectionHelpers.ts:942, !parent.isParentOf(outerNode) is false when the outer point is an element point on parent itself, which selection normalization keeps when that element's edge child is a decorator. With a non-shadow element whose canBeEmpty() is false, box(table(x), deco), and a range from (box, 2) back to x@0, Backspace and both text commands leave box(), an element that cannot be empty with no children (main leaves box(table(p()))). Writing !parent.is(outerNode) && !parent.isParentOf(outerNode) makes the loop stop there, as its comment describes ("an element holding the other end"), and the box then goes with the table as in into a table starting a box from its end; the 216 tests still pass with that change locally. A case for that shape in CASES would pin whichever result you intend.

Automated review (maintainer link)

…side it is deleted by character or word

## Description

A range with one end outside a table and the other inside it, which Shift+Arrow or a mouse drag from below the table into it leaves, is expanded to cover the whole table on Backspace and Delete. Deleting the same range by word (Option+Backspace on macOS, Ctrl+Backspace elsewhere) or with Ctrl+H / Ctrl+D on macOS dispatches `DELETE_WORD_COMMAND` or `DELETE_CHARACTER_COMMAND`, whose table handler did not expand it, so the editor removed part of the table and left an emptied fragment, or a cell spanning two rows in a one-row table.

`DELETE_CHARACTER_COMMAND` and `DELETE_WORD_COMMAND` now go through the same table handler as Backspace and Delete. When `selectPrevious` / `selectNext` moves the end in the table out of it, the end can land inside another table: an adjacent one, or one at the edge of an element that cannot be empty, because `ElementNode.select()` descends into such an element's first or last child. That table's own handler then expanded the range around it too, either in the same key command or when Backspace or Delete re-dispatches `DELETE_CHARACTER_COMMAND`. In that case the end now goes beside the table instead. The end can also land back in this table when the table is the first or last child of an element that cannot be empty; then it goes beside the outermost element the table is at that edge of, stopping at a table cell or at an element that holds the other end, so an element the range empties from outside is removed with the table instead of being left empty, and a cell keeps the rest of its content. A shadow root that holds both ends of the range stays even when the range empties it, as the core removal keeps it when the range holds no table. When it stops at a cell whose table does not hold the other end, the end stays where it landed: that table expands around itself too, and a cut, which only runs the first table's handler (meta#9281), behaves as it did before. This also fixes cases Backspace and Delete already got wrong: a range from the paragraph above into the first of two adjacent tables, or into a table followed by such an element that starts with a table, removed the other table too, and a range into a table that is the first or last child of a cell, or that starts or ends another element that cannot be empty, left part of the table behind. `DELETE_LINE_COMMAND` keeps its existing behavior.

## Test plan

### Before

`pnpm vitest run --project unit packages/lexical-table/src/__tests__/unit/DeleteRangeIntoTable.test.ts`: `Tests 138 failed | 81 passed (219)`

`VITEST_BROWSER=chromium pnpm vitest run --project browser packages/lexical-table/src/__tests__/browser/DeleteRangeIntoTableKeys.test.ts`: `Tests 2 failed | 1 passed (3)` (word Backspace / word Delete leave `paragraph:p, table:, paragraph:b` instead of `paragraph:pb`)

### After

The two files above: `Tests 219 passed (219)` and `Tests 3 passed (3)`. `pnpm run test-unit`: `Tests 41350 passed | 423 expected fail | 174 skipped`. `pnpm run test-e2e-chromium` / `-firefox` / `-webkit`: 661 / 627 / 641 passed.
@mayrang

mayrang commented Dec 6, 2026

Copy link
Copy Markdown
Collaborator Author

Done: the climb now also stops at the element the other end sits on, with a case for a box ending in a decorator.

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

This looks good to me. Thanks for the wide set of documents and for pasting the real Before/After output.

What I checked:

  • DELETE_WORD_COMMAND and DELETE_CHARACTER_COMMAND now go through $deleteCellHandler, while a TableSelection is still cleared with true returned and DELETE_LINE_COMMAND keeps the old handler (packages/lexical-table/src/LexicalTableSelectionHelpers.ts, lines 876-1003).
  • The climb at lines 937-949 stops at any ancestor that is or holds the other end, so the element-point case from the inline thread (box(table, decorator) with the range starting on the box) now keeps working, and the new decorator cases pin it.
  • Reproduced locally: the new unit file fails 138 of 219 cases with the main implementation and passes on this branch, and the browser test's two word-delete cases fail on main (paragraph:p, table:, paragraph:b) and pass here. In CI at this head the browser test passes in Chromium, Firefox and WebKit.
  • Also checked locally: one undo after the word delete restores the paragraph, the table and ab, and a range into a table that starts or ends the document removes the whole table too.

The changeset follows .changeset/README.md ([fix] summary plus a Verify: paragraph). No changes requested.

Automated review (maintainer link)

This branch was successfully deployed

2 active deployments
Preview – lexical — 531e7c08 Deployed Dec 6, 2026 by vercel[bot]
Preview – lexical-playground — 531e7c08 Deployed Dec 6, 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant