Skip to content

[lexical-table] Bug Fix: Plain text pasted into a table selection made right to left or upwards starts at its top-left cell - #9774

Merged
zurfyx merged 2 commits into
mainfrom
zurfyx/table-plain-paste-top-left
Nov 4, 2026
Merged

zurfyx merged 2 commits into
mainfrom
zurfyx/table-plain-paste-top-left

Conversation

@zurfyx

@zurfyx zurfyx commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Description

Plain text pasted into a table selection (tab- and newline-separated values, as "paste and match style" delivers them) is spread over the cells by TableSelection.insertRawText, growing the table when the text has more rows or columns. It started at the selection's anchor, the cell the selection was started from. For a selection made from its right or bottom end the anchor is that end, so the values landed right of or below the selection, over cells it does not hold (their text was lost), and the table grew. For example, selecting three cells of a row from right to left and pasting X⇥Y⇥Z wrote over the selected right cell and the two cells after it.

Now a grid of values is laid over the selection's rectangle from its top-left cell (after padding a ragged table, as $insertTableIntoGrid does anyway), whichever corner the selection was made from, as it already was for a selection made from its top-left cell and as a pasted table is. Non-table content that the table's clipboard handler turns into such a grid (several paragraphs of HTML) is placed the same way. A single value, and a paste into a single selected cell, still go to the anchor cell. A rectangle that a merged cell stretches beyond both ends of the selection starts at the rectangle's top-left.

Changeset

.changeset/table-plain-paste-top-left.md (@lexical/table patch, [fix]).

Test plan

  • packages/lexical-table/src/__tests__/unit/TableSelectionPlainPasteStart.test.ts (new, 16 tests): a row, a column and a block selected from every corner; a selection made right to left starting on a merged cell; a selection made upwards in a ragged table; a rectangle a row span stretches upwards; a grid larger than the selection pasted whole; HTML without a table (one formatted word, several paragraphs); a single value and a single selected cell keep the anchor; the caret lands at the start of the pasted grid after a paste from the bottom right.

Before

$ ./node_modules/.bin/vitest --run --project unit packages/lexical-table/src/__tests__/unit/TableSelectionPlainPasteStart.test.ts
AssertionError: expected [ [ 'a', 'b', 'X', 'Y', 'Z' ], …(2) ] to deeply equal [ [ 'X', 'Y', 'Z', 'd' ], …(2) ]
AssertionError: expected [ [ 't0', 't1', 'X', 'Y', 'Z' ], …(1) ] to deeply equal [ [ 'X', 'Y', 'Z', '', 't3' ], …(1) ]
      Tests  11 failed | 5 passed (16)

After

$ ./node_modules/.bin/vitest --run --project unit packages/lexical-table/src/__tests__/unit/TableSelectionPlainPasteStart.test.ts
      Tests  16 passed (16)

The @lexical/table, @lexical/clipboard, lexical and playground unit tests pass with no change from main. Fingerprints of every anchor/focus pair (556) on 11 table shapes (plain, merged cells, covered rows, ragged rows, cells with lists and nested tables), covering the table selection's nodes, text, all three clipboard formats, delete, backspace, cut, paste back of its own Lexical copy and of its own plain text: identical to main except plain-text pastes from selections made from a corner other than their top-left, plus 5 made from the top-left whose rectangle a merged cell stretches. WebKit, Chromium and Firefox e2e were not run for this change (the paths are not reached by the playground's e2e specs).

Review

Independent review with gpt-6.1-sol over two rounds (the last clean); its first round found that single values pasted as HTML moved, which this keeps at the anchor.

…e right to left or upwards starts at its top-left cell

## Description

Plain text pasted into a table selection (tab- and newline-separated values, as "paste and match style" delivers them) is spread over the cells by `TableSelection.insertRawText`, growing the table when the text has more rows or columns. It started at the selection's anchor, the cell the selection was started from. For a selection made from its right or bottom end the anchor is that end, so the values landed right of or below the selection, over cells it does not hold (their text was lost), and the table grew. For example, selecting three cells of a row from right to left and pasting `X⇥Y⇥Z` wrote over the selected right cell and the two cells after it.

Now a grid of values is laid over the selection's rectangle from its top-left cell (after padding a ragged table, as `$insertTableIntoGrid` does anyway), whichever corner the selection was made from, as it already was for a selection made from its top-left cell and as a pasted table is. Non-table content that the table's clipboard handler turns into such a grid (several paragraphs of HTML) is placed the same way. A single value, and a paste into a single selected cell, still go to the anchor cell. A rectangle that a merged cell stretches beyond both ends of the selection starts at the rectangle's top-left.

## Changeset

`.changeset/table-plain-paste-top-left.md` (`@lexical/table` patch, `[fix]`).

## Test plan

- `packages/lexical-table/src/__tests__/unit/TableSelectionPlainPasteStart.test.ts` (new, 15 tests): a row, a column and a block selected from every corner; a selection made right to left starting on a merged cell; a selection made upwards in a ragged table; a rectangle a row span stretches upwards; a grid larger than the selection pasted whole; HTML without a table (one formatted word, several paragraphs); a single value and a single selected cell keep the anchor.

### Before

```
$ ./node_modules/.bin/vitest --run --project unit packages/lexical-table/src/__tests__/unit/TableSelectionPlainPasteStart.test.ts
AssertionError: expected [ [ 'a', 'b', 'X', 'Y', 'Z' ], …(2) ] to deeply equal [ [ 'X', 'Y', 'Z', 'd' ], …(2) ]
AssertionError: expected [ [ 't0', 't1', 'X', 'Y', 'Z' ], …(1) ] to deeply equal [ [ 'X', 'Y', 'Z', '', 't3' ], …(1) ]
      Tests  10 failed | 5 passed (15)
```

### After

```
$ ./node_modules/.bin/vitest --run --project unit packages/lexical-table/src/__tests__/unit/TableSelectionPlainPasteStart.test.ts
      Tests  15 passed (15)
```

The `@lexical/table`, `@lexical/clipboard`, `lexical` and playground unit tests pass with no change from main. Fingerprints of every anchor/focus pair (556) on 11 table shapes (plain, merged cells, covered rows, ragged rows, cells with lists and nested tables), covering the table selection's nodes, text, all three clipboard formats, delete, backspace, cut, paste back of its own Lexical copy and of its own plain text: identical to main except plain-text pastes from selections made from a corner other than their top-left, plus 5 made from the top-left whose rectangle a merged cell stretches. WebKit, Chromium and Firefox e2e were not run for this change (the paths are not reached by the playground's e2e specs).

## Review

Independent review with gpt-6.1-sol over two rounds (the last clean); its first round found that single values pasted as HTML moved, which this keeps at the anchor.
@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 3:32am UTC
lexical-playground Ready Ready Preview Nov 4, 2026 3:32am UTC

Request Review

@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, 3:52 AM UTC

@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@9774

@lexical/a11y

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

@lexical/clipboard

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

@lexical/code

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

@lexical/code-core

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

@lexical/code-prism

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

@lexical/code-shiki

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

@lexical/compiler

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

@lexical/devtools-core

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

@lexical/dragon

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

@lexical/eslint-plugin

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

@lexical/extension

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

@lexical/fast-check

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

@lexical/file

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

@lexical/hashtag

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

@lexical/headless

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

@lexical/history

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

@lexical/html

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

@lexical/internal

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

@lexical/link

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

@lexical/list

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

@lexical/mark

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

@lexical/markdown

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

@lexical/mdast

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

@lexical/offset

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

@lexical/overflow

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

@lexical/plain-text

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

@lexical/react

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

@lexical/rich-text

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

@lexical/selection

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

@lexical/table

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

@lexical/tailwind

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

@lexical/text

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

@lexical/utils

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

@lexical/y

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

@lexical/yjs

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

commit: 9874461

@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. Thanks for the clear reproduction and test plan.

TableSelection.insertRawText now starts the pasted grid at the top-left cell of the selection's rectangle, so a selection made right to left or upwards no longer overwrites cells outside it or grows the table. The new TableSelectionPlainPasteStart.test.ts cases that cover selections made from other corners fail against main's implementation and pass with this change, and the cases that must not move (top-left selections, a single value, a single cell) pass on both. The changeset follows CONTRIBUTING.md: [fix], patch, and a Verify: paragraph.

Optional, non-blocking:

  • For a selection made from a corner other than the top-left, the caret after the paste now lands in the top-left cell rather than the anchor cell (packages/lexical-table/src/LexicalTableSelection.ts, line 271). One assertion on $getSelection() after a backward paste in the new test would pin that too.

… from the bottom right of a table selection

## Description

Adds a test to this pull request's plain-paste tests: after plain text is pasted into a table selection made from its bottom-right cell, the caret is collapsed at the start of the selection rectangle's top-left cell, where the pasted grid starts. Test-only; no source change.

## Test plan

### Before

```
$ ./node_modules/.bin/vitest --run --project unit packages/lexical-table/src/__tests__/unit/TableSelectionPlainPasteStart.test.ts
     × the caret lands at the start of the pasted grid after a paste from the bottom right
      Tests  11 failed | 5 passed (16)
```

### After

```
$ ./node_modules/.bin/vitest --run --project unit packages/lexical-table/src/__tests__/unit/TableSelectionPlainPasteStart.test.ts
      Tests  16 passed (16)
```
@zurfyx

zurfyx commented Nov 4, 2026

Copy link
Copy Markdown
Member Author

The optional item from the review is taken in 9874461 (test only, no source change): TableSelectionPlainPasteStart.test.ts now also checks that after a plain paste into a selection made from its bottom-right cell, the caret is collapsed at the start of the selection's top-left cell, where the pasted grid starts. The file has 16 tests: 11 fail on main, 16 pass here.

@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, thanks for the careful reproduction.

TableSelection.insertRawText now starts a pasted grid at the top-left cell of the selection's rectangle, so a selection made right to left or upwards no longer overwrites cells outside it or grows the table, matching how a pasted table is already placed. The new TableSelectionPlainPasteStart.test.ts cases for selections made from other corners fail against main and pass with this change, while the cases that must not move (a selection made from its top-left, a single value, a single selected cell) pass on both. The changeset follows CONTRIBUTING.md: @lexical/table as patch, a [fix] summary, and a Verify: paragraph.

Optional, non-blocking:

  • The Test plan in the description still describes the first commit ("new, 15 tests", Before 10 failed | 5 passed (15), After 15 passed (15)). With the caret test from 9874461 the file has 16 tests, 11 of which fail on main, as your later comment says. AGENTS.md asks the Test plan to carry the real Before/After output, and the description usually becomes the squash commit message, so updating those three lines keeps the record accurate.

@zurfyx

zurfyx commented Nov 4, 2026

Copy link
Copy Markdown
Member Author

The Test plan in the description now matches the file at 9874461: 16 tests, 11 fail on main, 16 pass here.

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

This branch was successfully deployed

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