Skip to content

[lexical-table] Bug Fix: A table selection on a cell whose row span reaches past the last row no longer throws - #9781

Merged
zurfyx merged 2 commits into
mainfrom
zurfyx/table-row-span-past-last-row
Nov 4, 2026
Merged

zurfyx merged 2 commits into
mainfrom
zurfyx/table-row-span-past-last-row

Conversation

@zurfyx

@zurfyx zurfyx commented Nov 4, 2026

Copy link
Copy Markdown
Member

Description

The table commands keep row spans within the table, but a remote update (for example, one collaborator merging cells down to the last row while another deletes a row) or imported JSON can leave a cell whose row span reaches past the table's last row. $computeTableMapSkipCellCheck stops filling such a span at the last row, while $computeTableCellRectBoundary took the cell's full row span for the rectangle, so TableSelection.getNodes read rows of the map that do not exist and threw TypeError: Cannot read properties of undefined (reading '0'). Because the table extension reads getNodes() on every update that has a table selection, merely selecting such a cell threw, and the cell could not be selected, deleted or copied.

Now $computeTableCellRectBoundary stops the rectangle at the map's last row, as the map and the rendered table already do. Extending or shrinking the selection with Shift and an arrow key uses the same rows: $computeTableCellRectSpans counts the rows a cell covers in the map, and the corner the anchor sits on accepts a cell whose span reaches the rectangle's last row or beyond. The cell's row span is not changed, so a copy keeps it. For every table whose spans fit within its rows, all three give the same result as before.

Inserting or deleting rows, merging, unmerging and plain-text paste on such a selection read the span in their own code and still throw; what an overlong span should mean for them (clamp it, or create the missing rows) is not decided here. They are pinned as expected failures.

Fixes part of #9759.

Changeset

.changeset/table-row-span-past-last-row.md (@lexical/table patch, [fix]).

Test plan

  • packages/lexical-table/src/__tests__/unit/TableRowSpanPastLastRow.test.ts (new, 26 tests): a four-row table whose first cell spans two columns and five rows, built without transforms; for a selection of that cell and one from it to the last row: the selection can be set, getNodes() returns the selected cells, Delete clears them (or removes the table when the selection covers all of it), copy holds them, inserting and deleting a column work, and deleting the rows of the whole table works. Four more: a selection between two ordinary cells grows to an overlong cell between them and stops at the last row; Shift+Up shrinks a selection ending on an overlong cell by the rows that cell covers; Shift+Up from an overlong cell on the last row keeps its corner; a selection of an overlong cell on the last row holds only that cell. Plain paste, inserting a row after, deleting a row (single cell), merging and unmerging are test.fails.

Before

$ ./node_modules/.bin/vitest --run --project unit packages/lexical-table/src/__tests__/unit/TableRowSpanPastLastRow.test.ts
TypeError: Cannot read properties of undefined (reading '0')
AssertionError: expected [ Array(1) ] to deeply equal []
      Tests  12 failed | 5 passed | 9 expected fail (26)

After

$ ./node_modules/.bin/vitest --run --project unit packages/lexical-table/src/__tests__/unit/TableRowSpanPastLastRow.test.ts
      Tests  17 passed | 9 expected fail (26)

The @lexical/table, @lexical/clipboard, @lexical/utils and playground unit tests pass with no change from main. A fingerprint of every table shape we generate (plain, column and row spans, merged blocks, covered rows, ragged rows, contents; every anchor and focus pair; rich, HTML and plain-text paste and copy) is byte identical to main: 556 pairs. The rich-text end-to-end tests for tables and copy and paste give the same results as main in Chromium and Firefox.

Review

Independent review with gpt-6.1-sol over three rounds (the last clean); the first two found the keyboard extension and corner cases above, which this change covers, and their accepted mutants are rejected by the tests here.

…eaches past the last row no longer throws

## Description

The table commands keep row spans within the table, but a remote update (for example, one collaborator merging cells down to the last row while another deletes a row) or imported JSON can leave a cell whose row span reaches past the table's last row. `$computeTableMapSkipCellCheck` stops filling such a span at the last row, while `$computeTableCellRectBoundary` took the cell's full row span for the rectangle, so `TableSelection.getNodes` read rows of the map that do not exist and threw `TypeError: Cannot read properties of undefined (reading '0')`. Because the table extension reads `getNodes()` on every update that has a table selection, merely selecting such a cell threw, and the cell could not be selected, deleted or copied.

Now `$computeTableCellRectBoundary` stops the rectangle at the map's last row, as the map and the rendered table already do. Extending or shrinking the selection with Shift and an arrow key uses the same rows: `$computeTableCellRectSpans` counts the rows a cell covers in the map, and the corner the anchor sits on accepts a cell whose span reaches the rectangle's last row or beyond. The cell's row span is not changed, so a copy keeps it. For every table whose spans fit within its rows, all three give the same result as before.

Inserting or deleting rows, merging, unmerging and plain-text paste on such a selection read the span in their own code and still throw; what an overlong span should mean for them (clamp it, or create the missing rows) is not decided here. They are pinned as expected failures.

Fixes part of #9759.

## Changeset

`.changeset/table-row-span-past-last-row.md` (`@lexical/table` patch, `[fix]`).

## Test plan

- `packages/lexical-table/src/__tests__/unit/TableRowSpanPastLastRow.test.ts` (new, 26 tests): a four-row table whose first cell spans two columns and five rows, built without transforms; for a selection of that cell and one from it to the last row: the selection can be set, `getNodes()` returns the selected cells, Delete clears them (or removes the table when the selection covers all of it), copy holds them, inserting and deleting a column work, and deleting the rows of the whole table works. Four more: a selection between two ordinary cells grows to an overlong cell between them and stops at the last row; Shift+Up shrinks a selection ending on an overlong cell by the rows that cell covers; Shift+Up from an overlong cell on the last row keeps its corner; a selection of an overlong cell on the last row holds only that cell. Plain paste, inserting a row after, deleting a row (single cell), merging and unmerging are `test.fails`.

### Before

```
$ ./node_modules/.bin/vitest --run --project unit packages/lexical-table/src/__tests__/unit/TableRowSpanPastLastRow.test.ts
TypeError: Cannot read properties of undefined (reading '0')
AssertionError: expected [ Array(1) ] to deeply equal []
      Tests  12 failed | 5 passed | 9 expected fail (26)
```

### After

```
$ ./node_modules/.bin/vitest --run --project unit packages/lexical-table/src/__tests__/unit/TableRowSpanPastLastRow.test.ts
      Tests  17 passed | 9 expected fail (26)
```

The `@lexical/table`, `@lexical/clipboard`, `@lexical/utils` and playground unit tests pass with no change from main. A fingerprint of every table shape we generate (plain, column and row spans, merged blocks, covered rows, ragged rows, contents; every anchor and focus pair; rich, HTML and plain-text paste and copy) is byte identical to main: 556 pairs. The rich-text end-to-end tests for tables and copy and paste give the same results as main in Chromium and Firefox.

## Review

Independent review with gpt-6.1-sol over three rounds (the last clean); the first two found the keyboard extension and corner cases above, which this change covers, and their accepted mutants are rejected by the tests here.
@vercel

vercel Bot commented Nov 4, 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 4, 2026 9:14am UTC
lexical-playground Ready Ready Preview Nov 4, 2026 9:14am 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 4, 2026
@pkg-pr-new

pkg-pr-new Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
lexical

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

@lexical/a11y

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

@lexical/clipboard

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

@lexical/code

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

@lexical/code-core

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

@lexical/code-prism

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

@lexical/code-shiki

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

@lexical/compiler

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

@lexical/devtools-core

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

@lexical/dragon

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

@lexical/eslint-plugin

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

@lexical/extension

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

@lexical/fast-check

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

@lexical/file

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

@lexical/hashtag

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

@lexical/headless

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

@lexical/history

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

@lexical/html

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

@lexical/internal

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

@lexical/link

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

@lexical/list

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

@lexical/mark

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

@lexical/markdown

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

@lexical/mdast

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

@lexical/offset

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

@lexical/overflow

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

@lexical/plain-text

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

@lexical/react

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

@lexical/rich-text

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

@lexical/selection

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

@lexical/table

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

@lexical/tailwind

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

@lexical/text

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

@lexical/utils

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

@lexical/y

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

@lexical/yjs

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

commit: d3c83e7

@meta-code-review

meta-code-review Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 4, 2026, 9:29 AM UTC

@meta-code-review meta-code-review 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. Clamping the selection rectangle in $computeTableCellRectBoundary to the table's last row matches what $computeTableMapSkipCellCheck and the rendered table already do. Tables whose row spans fit within their rows are unaffected. The new tests reproduce the crash and cover each part of the fix: the rect clamp, the Shift+arrow span count in $computeTableCellRectSpans, and the >= corner check in getCorner.

One optional, non-blocking suggestion:

  • packages/lexical-table/src/__tests__/unit/TableRowSpanPastLastRow.test.ts:51: build() creates a new editor for every test and never disposes it, so each test leaves its root element in document.body and its listeners registered for the rest of the file. The neighbouring table tests clean up, using using source = build(...) in RaggedTableSelectionRowSpans.test.ts or afterEach(() => editor.dispose()) in ShiftArrowOffCornerAnchor.test.ts, as the testing notes in AGENTS.md describe. Adding onTestFinished(() => editor.dispose()) inside build() would match them.

This PR leaves the stored row span alone and pins the commands that still throw as test.fails for #9759, which seems like the right scope.

// can leave one. The table map stops at the last row, and so must the
// rectangle a table selection covers.

function build(): {editor: LexicalEditor; errors: string[]} {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Optional, non-blocking: every test gets a fresh editor from build() and none is disposed, so each leaves its root element in document.body and its listeners registered for the rest of the file. The neighbouring table tests clean up (using source = build(...) in RaggedTableSelectionRowSpans.test.ts, afterEach(() => editor.dispose()) in ShiftArrowOffCornerAnchor.test.ts); an onTestFinished(() => editor.dispose()) here would match them.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done in d3c83e7 (test only): build() registers onTestFinished(() => editor.dispose()).

…st.ts builds

## Description

Each test in `TableRowSpanPastLastRow.test.ts` now disposes the editor it builds when it finishes (`onTestFinished(() => editor.dispose())`), as the neighbouring table tests do, so no root element or listener outlives its test. Test-only.

## Test plan

### Before

```
$ ./node_modules/.bin/vitest --run --project unit packages/lexical-table/src/__tests__/unit/TableRowSpanPastLastRow.test.ts
      Tests  17 passed | 9 expected fail (26)
```

### After

```
$ ./node_modules/.bin/vitest --run --project unit packages/lexical-table/src/__tests__/unit/TableRowSpanPastLastRow.test.ts
      Tests  17 passed | 9 expected fail (26)
```

@meta-code-review meta-code-review 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. Capping the selection rectangle in $computeTableCellRectBoundary at the table's last row matches what $computeTableMapSkipCellCheck and the rendered table already do, so a cell whose row span reaches past the last row can be selected, cleared and copied again, while tables whose spans fit select exactly as before.

The new tests in packages/lexical-table/src/__tests__/unit/TableRowSpanPastLastRow.test.ts fail on main and pass here, and each of the three changes (the rect clamp, the row count in $computeTableCellRectSpans, and the >= corner check in getCorner) is pinned by a test that fails without it. The changeset follows CONTRIBUTING.md, including the Verify: paragraph a behavior fix needs.

Leaving the stored row span alone and pinning row insert/delete, merge, unmerge and plain paste as test.fails for #9759 is a sensible scope for this PR. Thanks for disposing the test editors as well.

@zurfyx
zurfyx marked this pull request as ready for review November 4, 2026 10:20
@zurfyx
zurfyx added this pull request to the merge queue Nov 4, 2026
Merged via the queue into main with commit a983160 Nov 4, 2026
61 checks passed
@potatowagon potatowagon mentioned this pull request Dec 2, 2026

This branch was successfully deployed

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