Repository navigation
[lexical-table][lexical-website] Bug Fix: Table commands throw or misbehave on a ragged table - #9375
Merged
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
zurfyx
force-pushed
the
zurfyx/table-ragged-rows
branch
from
October 22, 2026 14:14
35f3041 to
9c44e58
Compare
zurfyx
marked this pull request as ready for review
October 22, 2026 14:14
zurfyx
requested review from
acywatson,
etrepum,
fantactuka,
ivailop7 and
potatowagon
as code owners
October 22, 2026 14:14
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/yjs
commit: |
potatowagon
approved these changes
Oct 22, 2026
…behave on a ragged table
## Description
Current behavior:
A table whose rows cover different numbers of grid columns ("ragged") is
normally repaired by the `TableNode` transform that TablePlugin /
TableExtension registers. That transform does not run when an update is
applied with `skipTransforms`, which is how the Yjs bindings (v1 and v2) apply
remote updates. So when collaborators race, e.g. one inserts a row while
another inserts a column, both peers converge on a ragged table such as
`[4, 4, 3, 4]` cells per row, and it stays ragged until a later local edit
dirties the table.
The structural commands index the table map as if it were rectangular, so on
such a table they throw or leave it inconsistent:
- `$insertTableColumnAtSelection` / `$insertTableColumnAtNode`:
`Cannot read properties of undefined (reading 'cell')`, or the table stays
ragged when inserting after the short row's last cell.
- `$insertTableRowAtSelection` / `$insertTableRowAtNode`:
`Cannot destructure property 'cell' of 'insertAfterEndRowMap[i]' as it is
undefined.`, or the table stays ragged.
- `$deleteTableColumnAtSelection` and `$deleteTableRowAtSelection`:
`Cannot destructure property 'cell' of 'gridMap[row][column]' as it is
undefined.`
- `$mergeCells`: throws when the merged block reaches past the end of a short
row, and otherwise leaves the table ragged.
- `$unmergeCell` / `$unmergeCellNode`: throws when computing the header
states of the split cells if another row is short.
- Pasting a table into a table (`$insertTableIntoGrid`): throws.
New behavior:
Each of these commands first pads short rows with empty cells, the same
repair the transform does (padding at the row end, inheriting the header-row
bit), and then runs on a rectangular grid ("repair on write"). The repair is
factored out of the transform into a new `@experimental` export,
`$padTableRows(tableNode): boolean`, which the transform now calls too, so the
transform's behavior is unchanged. On a rectangular table the commands behave
exactly as before; the only extra work is one table-map pass.
The `LexicalTablePlugin` docs page says that the table commands pad ragged
rows first and that `$padTableRows` is exported (`@experimental`).
This does not make ragged tables rectangular when they are received; they
stay ragged until the next structural command (or local edit that runs the
transform). Read paths that index the map (e.g. `TableSelection.getNodes()`
across a hole) are not changed here.
## Test plan
New `packages/lexical-table/src/__tests__/unit/RaggedTableCommands.test.ts`
builds a ragged table directly (rows of 3, 3 and 2 cells, transforms not
registered, as when remote collab updates are applied) and runs each command
on it, asserting the resulting grid widths and cell contents. It also covers
`$padTableRows` on its own, including a hole left of a rowSpan from an
earlier row.
### Before
```
$ npx vitest --project unit --no-watch packages/lexical-table/src/__tests__/unit/RaggedTableCommands.test.ts
× insert column after the last column of a full row
× insert column after the last cell of the short row
× insert row after the short row
× insert row before the first row
× delete the last column
× delete the short row
× merge a block that reaches past the end of the short row
× merge cells in the last column
× unmerge a header-column cell when another row is short
× paste a 2x2 table at the last cell of the short row
× pads short rows and leaves a rectangular table alone
× fills a hole left of a rowSpan from an earlier row
TypeError: Cannot read properties of undefined (reading 'cell')
AssertionError: expected [ 4, 4, 3 ] to deeply equal [ 4, 4, 4 ]
TypeError: Cannot destructure property 'cell' of 'insertAfterEndRowMap[i]' as it is undefined.
AssertionError: expected [ 3, 3, 3, 2 ] to deeply equal [ 3, 3, 3, 3 ]
TypeError: Cannot destructure property 'cell' of 'gridMap[row][column]' as it is undefined.
TypeError: Cannot destructure property 'cell' of 'gridMap[row][column]' as it is undefined.
TypeError: Cannot read properties of undefined (reading 'cell')
AssertionError: expected [ 3, 3, 2 ] to deeply equal [ 3, 3, 3 ]
TypeError: Cannot read properties of undefined (reading 'cell')
TypeError: Cannot read properties of undefined (reading 'cell')
TypeError: (0 , $padTableRows) is not a function
TypeError: (0 , $padTableRows) is not a function
Tests 12 failed (12)
```
### After
```
$ npx vitest --project unit --no-watch packages/lexical-table/src/__tests__/unit/RaggedTableCommands.test.ts
✓ |unit| packages/lexical-table/src/__tests__/unit/RaggedTableCommands.test.ts (12 tests)
Tests 12 passed (12)
$ npx vitest --project unit --no-watch packages/lexical-table packages/lexical-yjs packages/lexical-react/src/__tests__/unit/Collab* packages/lexical-react/src/__tests__/unit/LexicalCollaborationPlugin.test.tsx packages/lexical-playground/__tests__/unit
Test Files 67 passed (67)
Tests 658 passed (658)
```
End to end with two headless collab clients (row ‖ column race on a 3x3
table, then a follow-up command on one client; scratch test, not included),
on v1 and v2:
```
ragged after race follow-up before after
v1 [4,4,3,4] insert last column throws "reading 'cell'" [5,5,5,5] on both
v1 [4,4,3,4] delete last column throws "gridMap[row][column]" [3,3,3,3] on both
v2 [4,4,4,3] insert last column throws "reading 'cell'" [5,5,5,5] on both
v2 [4,4,4,3] delete last column throws "gridMap[row][column]" [3,3,3,3] on both
```
`pnpm run test-unit`: 393 files, 9549 tests passed (1 skipped).
`pnpm run tsc`, `pnpm run flow` (0 errors), ESLint and Prettier on the
changed files pass.
Not exercised: real browsers and a real network provider. If two clients run
a structural command on the same ragged table concurrently, both pad, and the
merge can over-correct by an extra empty column; this change does not
coordinate repairs between clients.
zurfyx
force-pushed
the
zurfyx/table-ragged-rows
branch
from
October 23, 2026 14:51
9c44e58 to
df6494e
Compare
Merged
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Current behavior:
A table whose rows cover different numbers of grid columns ("ragged") is
normally repaired by the
TableNodetransform that TablePlugin /TableExtension registers. That transform does not run when an update is
applied with
skipTransforms, which is how the Yjs bindings (v1 and v2) applyremote updates. So when collaborators race, e.g. one inserts a row while
another inserts a column, both peers converge on a ragged table such as
[4, 4, 3, 4]cells per row, and it stays ragged until a later local editdirties the table.
The structural commands index the table map as if it were rectangular, so on
such a table they throw or leave it inconsistent:
$insertTableColumnAtSelection/$insertTableColumnAtNode:Cannot read properties of undefined (reading 'cell'), or the table staysragged when inserting after the short row's last cell.
$insertTableRowAtSelection/$insertTableRowAtNode:Cannot destructure property 'cell' of 'insertAfterEndRowMap[i]' as it is undefined., or the table stays ragged.$deleteTableColumnAtSelectionand$deleteTableRowAtSelection:Cannot destructure property 'cell' of 'gridMap[row][column]' as it is undefined.$mergeCells: throws when the merged block reaches past the end of a shortrow, and otherwise leaves the table ragged.
$unmergeCell/$unmergeCellNode: throws when computing the headerstates of the split cells if another row is short.
$insertTableIntoGrid): throws.New behavior:
Each of these commands first pads short rows with empty cells, the same
repair the transform does (padding at the row end, inheriting the header-row
bit), and then runs on a rectangular grid ("repair on write"). The repair is
factored out of the transform into a new
@experimentalexport,$padTableRows(tableNode): boolean, which the transform now calls too, so thetransform's behavior is unchanged. On a rectangular table the commands behave
exactly as before; the only extra work is one table-map pass.
The
LexicalTablePlugindocs page says that the table commands pad raggedrows first and that
$padTableRowsis exported (@experimental).This does not make ragged tables rectangular when they are received; they
stay ragged until the next structural command (or local edit that runs the
transform). Read paths that index the map (e.g.
TableSelection.getNodes()across a hole) are not changed here.
Test plan
New
packages/lexical-table/src/__tests__/unit/RaggedTableCommands.test.tsbuilds a ragged table directly (rows of 3, 3 and 2 cells, transforms not
registered, as when remote collab updates are applied) and runs each command
on it, asserting the resulting grid widths and cell contents. It also covers
$padTableRowson its own, including a hole left of a rowSpan from anearlier row.
Before
After
End to end with two headless collab clients (row ‖ column race on a 3x3
table, then a follow-up command on one client; scratch test, not included),
on v1 and v2:
pnpm run test-unit: 393 files, 9549 tests passed (1 skipped).pnpm run tsc,pnpm run flow(0 errors), ESLint and Prettier on thechanged files pass.
Not exercised: real browsers and a real network provider. If two clients run
a structural command on the same ragged table concurrently, both pad, and the
merge can over-correct by an extra empty column; this change does not
coordinate repairs between clients.