Skip to content

[lexical-table][lexical-website] Bug Fix: Table commands throw or misbehave on a ragged table - #9375

Merged
zurfyx merged 1 commit into
mainfrom
zurfyx/table-ragged-rows
Oct 23, 2026
Merged

zurfyx merged 1 commit into
mainfrom
zurfyx/table-ragged-rows

Conversation

@zurfyx

@zurfyx zurfyx commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

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.

@vercel

vercel Bot commented Oct 19, 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 Oct 23, 2026 2:53pm UTC
lexical-playground Ready Ready Preview Oct 23, 2026 2:53pm UTC

Request Review

@pkg-pr-new

pkg-pr-new Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
lexical

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

@lexical/a11y

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

@lexical/clipboard

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

@lexical/code

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

@lexical/code-core

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

@lexical/code-prism

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

@lexical/code-shiki

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

@lexical/compiler

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

@lexical/devtools-core

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

@lexical/dragon

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

@lexical/eslint-plugin

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

@lexical/extension

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

@lexical/fast-check

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

@lexical/file

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

@lexical/hashtag

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

@lexical/headless

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

@lexical/history

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

@lexical/html

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

@lexical/internal

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

@lexical/link

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

@lexical/list

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

@lexical/mark

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

@lexical/markdown

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

@lexical/mdast

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

@lexical/offset

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

@lexical/overflow

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

@lexical/plain-text

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

@lexical/react

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

@lexical/rich-text

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

@lexical/selection

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

@lexical/table

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

@lexical/tailwind

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

@lexical/text

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

@lexical/utils

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

@lexical/yjs

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

commit: df6494e

…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 zurfyx changed the title [lexical-table] Bug Fix: Table commands throw or misbehave on a ragged table [lexical-table][lexical-website] Bug Fix: Table commands throw or misbehave on a ragged table Oct 23, 2026
@zurfyx
zurfyx force-pushed the zurfyx/table-ragged-rows branch from 9c44e58 to df6494e Compare October 23, 2026 14:51
@zurfyx
zurfyx added this pull request to the merge queue Oct 23, 2026
Merged via the queue into main with commit 157656c Oct 23, 2026
57 of 64 checks passed
@zurfyx zurfyx mentioned this pull request Oct 27, 2026

This branch was successfully deployed

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