Repository navigation
[lexical-table] Bug Fix: A table selection on a cell whose row span reaches past the last row no longer throws - #9781
Conversation
…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.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
lexical
@lexical/a11y
@lexical/clipboard
@lexical/code
@lexical/code-core
@lexical/code-prism
@lexical/code-shiki
@lexical/compiler
@lexical/devtools-core
@lexical/dragon
@lexical/eslint-plugin
@lexical/extension
@lexical/fast-check
@lexical/file
@lexical/hashtag
@lexical/headless
@lexical/history
@lexical/html
@lexical/internal
@lexical/link
@lexical/list
@lexical/mark
@lexical/markdown
@lexical/mdast
@lexical/offset
@lexical/overflow
@lexical/plain-text
@lexical/react
@lexical/rich-text
@lexical/selection
@lexical/table
@lexical/tailwind
@lexical/text
@lexical/utils
@lexical/y
@lexical/yjs
commit: |
|
AI review status for this pull request.
|
There was a problem hiding this comment.
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 indocument.bodyand its listeners registered for the rest of the file. The neighbouring table tests clean up, usingusing source = build(...)inRaggedTableSelectionRowSpans.test.tsorafterEach(() => editor.dispose())inShiftArrowOffCornerAnchor.test.ts, as the testing notes in AGENTS.md describe. AddingonTestFinished(() => editor.dispose())insidebuild()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[]} { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
```
There was a problem hiding this comment.
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.
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.
$computeTableMapSkipCellCheckstops filling such a span at the last row, while$computeTableCellRectBoundarytook the cell's full row span for the rectangle, soTableSelection.getNodesread rows of the map that do not exist and threwTypeError: Cannot read properties of undefined (reading '0'). Because the table extension readsgetNodes()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
$computeTableCellRectBoundarystops 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:$computeTableCellRectSpanscounts 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/tablepatch,[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 aretest.fails.Before
After
The
@lexical/table,@lexical/clipboard,@lexical/utilsand 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.