Skip to content

[lexical-table] Bug Fix: Deleting a row no longer drops a merged cell changed earlier in the same update - #9851

Merged
potatowagon merged 1 commit into
meta:mainfrom
vishalhabib99:fix/delete-row-merged-cell-same-update
Nov 12, 2026
Merged

potatowagon merged 1 commit into
meta:mainfrom
vishalhabib99:fix/delete-row-merged-cell-same-update

Conversation

@vishalhabib99

Copy link
Copy Markdown
Contributor

Description

$deleteTableRowAtSelection trims the row span of a merged cell that reaches past the deleted rows, and moves the cell into the next row when the deleted rows include its first row. It read cell.__rowSpan again after cell.setRowSpan(...), and a cell spanning several deleted rows was visited once per row.

When the cell is not yet writable in the update, setRowSpan clones it, so cell keeps the old span and this works. When the cell is already writable (for example $mergeCells and $deleteTableRowAtSelection called in one editor.update), setRowSpan changes it in place:

  • Deleting the first row of a 2-row merged cell skips the move, so the merged cell and its text are removed with the row. With TableExtension, the row is padded with empty cells.
  • Deleting two middle rows of a 4-row merged cell trims the span twice (4 -> 2 -> 1), leaving the row below one cell short.

Each cell is now handled once, from the span it had before the delete. Deleting rows in their own update, as the table menu does, already worked and is unchanged. $deleteTableColumnAtSelection reads the span before changing it and does not have this problem.

Changeset

.changeset/table-delete-row-merged-cell-same-update.md (@lexical/table patch, [fix] with a Verify: paragraph).

Test plan

New DeleteRowMergedCellSameUpdate.test.ts runs three cases (2-row merge, 2x2 merge, 4-row merge with two rows deleted) with the merge and the delete in separate updates and in the same update.

pnpm exec vitest run --project unit packages/lexical-table/src/__tests__/unit/DeleteRowMergedCellSameUpdate.test.ts

Before

     × deleting the top row of a 2-row merged …, in the same update as the merge
     × deleting the top row of a 2x2 merged ce…, in the same update as the merge
     × deleting two middle rows of a 4-row mer…, in the same update as the merge
      Tests  3 failed | 3 passed (6)

- Expected
+ Received
  [
-   "A2 | B1 B2",
+   "A2",
  ]

  [
-   "A2 | B1 C1 B2 C2 cs2",
+   "A2 |  | ",
    "A3 | B3 | C3",
  ]

  [
-   "A1 | B1 B2 B3 B4 rs2",
-   "A4",
+   "A1 | B1 B2 B3 B4",
+   "A4 | ",
  ]

After

      Tests  6 passed (6)

pnpm exec vitest run --project unit packages/lexical-table: 38 files passed, 395 passed | 9 expected fail. pnpm run tsc, ESLint and Prettier on the changed files, and pnpm run check-changesets pass. Flow, browser and e2e tests were not run; the change has no API or DOM impact.

… changed earlier in the same update

## Description

`$deleteTableRowAtSelection` trims the row span of a merged cell that reaches past the deleted rows, and moves the cell into the next row when the deleted rows include its first row. It read `cell.__rowSpan` again after `cell.setRowSpan(...)`, and a cell spanning several deleted rows was visited once per row.

When the cell is not yet writable in the update, `setRowSpan` clones it, so `cell` keeps the old span and this works. When the cell is already writable (for example `$mergeCells` and `$deleteTableRowAtSelection` called in one `editor.update`), `setRowSpan` changes it in place:

- Deleting the first row of a 2-row merged cell skips the move, so the merged cell and its text are removed with the row. With `TableExtension`, the row is padded with empty cells.
- Deleting two middle rows of a 4-row merged cell trims the span twice (4 -> 2 -> 1), leaving the row below one cell short.

Each cell is now handled once, from the span it had before the delete. Deleting rows in their own update, as the table menu does, already worked and is unchanged. `$deleteTableColumnAtSelection` reads the span before changing it and does not have this problem.

## Test plan

New `DeleteRowMergedCellSameUpdate.test.ts` runs three cases (2-row merge, 2x2 merge, 4-row merge with two rows deleted) with the merge and the delete in separate updates and in the same update.

`pnpm exec vitest run --project unit packages/lexical-table/src/__tests__/unit/DeleteRowMergedCellSameUpdate.test.ts`

### Before

```
     × deleting the top row of a 2-row merged …, in the same update as the merge
     × deleting the top row of a 2x2 merged ce…, in the same update as the merge
     × deleting two middle rows of a 4-row mer…, in the same update as the merge
      Tests  3 failed | 3 passed (6)

- Expected
+ Received
  [
-   "A2 | B1 B2",
+   "A2",
  ]

  [
-   "A2 | B1 C1 B2 C2 cs2",
+   "A2 |  | ",
    "A3 | B3 | C3",
  ]

  [
-   "A1 | B1 B2 B3 B4 rs2",
-   "A4",
+   "A1 | B1 B2 B3 B4",
+   "A4 | ",
  ]
```

### After

```
      Tests  6 passed (6)
```

`pnpm exec vitest run --project unit packages/lexical-table`: 38 files passed, 395 passed | 9 expected fail. `pnpm run tsc`, ESLint and Prettier on the changed files, and `pnpm run check-changesets` pass. Flow, browser and e2e tests were not run; the change has no API or DOM impact.
@vercel

vercel Bot commented Nov 8, 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 8, 2026 5:19pm UTC
lexical-playground Ready Ready Preview Nov 8, 2026 5:19pm 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 Nov 8, 2026
@rootnode-bot

rootnode-bot Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 9, 2026, 7:03 PM UTC

@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, this looks good to me. Reading __rowSpan once before setRowSpan and handling each cell once in $deleteTableRowAtSelection (packages/lexical-table/src/LexicalTableUtils.ts:669-706) gives the same result the separate-update path already produced, and the new DeleteRowMergedCellSameUpdate.test.ts fails on the old implementation with exactly the output in your Before block and passes with the change. The changeset follows .changeset/README.md, including the Verify: paragraph AGENTS.md asks for on a fix that changes observable output.

One optional, non-blocking suggestion:

  • The same pattern is still in the sibling commands. The description says $deleteTableColumnAtSelection does not have this problem, but its loop visits a row-spanning cell once per row and re-reads cell.__colSpan each time (LexicalTableUtils.ts:783-806). Merging B1:C2 in a 3x3 table and deleting column B (selection in B3) in the same editor.update removes the merged cell and its text through the else branch at line 803; with an A1:C2 merge in a 3x4 table the span is trimmed twice (colSpan 1 instead of 2). $insertTableRowAtNode (setRowSpan(cell.__rowSpan + 1) at lines 345 and 374, once per spanned column) and $insertTableColumnAtNode (setColSpan(currentCell.__colSpan + 1) at line 590, once per spanned row) over-grow a merged cell the same way: rowSpan 4 instead of 3 and colSpan 4 instead of 3 after merging B1:C2 in the same update. These behave the same on main, so this PR does not cause them and a follow-up is fine; please drop the sentence about $deleteTableColumnAtSelection from the description, or extend the same read-once, once-per-cell fix to these three commands here.

@vishalhabib99

Copy link
Copy Markdown
Contributor Author

Hi, could a maintainer approve the workflow runs on this one when you have a moment? The Lexical Tests runs are still waiting for approval because this is my first PR here. The fix is in $deleteTableRowAtSelection. The new unit test fails 3 of 6 cases on main and passes all 6 with the change. Happy to adjust anything.

@vishalhabib99

Copy link
Copy Markdown
Contributor Author

Thanks for the approval, @potatowagon! @zurfyx @acywatson @ivailop7 @etrepum, the workflow runs on this PR (Lexical Tests, Extended, Changesets and others) are still waiting for approval because it's my first PR here, so no checks have run yet. Could one of you approve them when you have a moment? The fix is in $deleteTableRowAtSelection, and the new unit test fails 3 of 6 cases on main and passes all 6 with the change.

@pkg-pr-new

pkg-pr-new Bot commented Nov 11, 2026

Copy link
Copy Markdown
lexical

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

@lexical/a11y

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

@lexical/clipboard

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

@lexical/code

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

@lexical/code-core

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

@lexical/code-prism

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

@lexical/code-shiki

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

@lexical/compiler

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

@lexical/devtools-core

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

@lexical/dragon

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

@lexical/eslint-plugin

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

@lexical/extension

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

@lexical/fast-check

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

@lexical/file

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

@lexical/hashtag

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

@lexical/headless

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

@lexical/history

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

@lexical/html

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

@lexical/internal

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

@lexical/link

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

@lexical/list

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

@lexical/mark

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

@lexical/markdown

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

@lexical/mdast

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

@lexical/offset

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

@lexical/overflow

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

@lexical/plain-text

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

@lexical/react

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

@lexical/rich-text

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

@lexical/selection

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

@lexical/table

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

@lexical/tailwind

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

@lexical/text

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

@lexical/utils

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

@lexical/y

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

@lexical/yjs

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

commit: ca487d2

@potatowagon
potatowagon added this pull request to the merge queue Nov 12, 2026
Merged via the queue into meta:main with commit 3bb0e26 Nov 12, 2026
90 of 91 checks passed
@potatowagon potatowagon mentioned this pull request Dec 2, 2026

This branch was successfully deployed

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

2 participants