Repository navigation
[lexical-table] Bug Fix: align every cell a table selection covers, not just the naive rect - #9043
Merged
Merged
Conversation
…ot just the naive rect
## Description
A `TableSelection` is two cells, and the set of cells it covers is the smallest
rect containing both — but that rect has to be *grown* whenever a merged cell
straddles its edge, otherwise the rect would cut a cell in half.
`$computeTableCellRectBoundary` does that expansion, iterating until the rect
stops growing, and `TableSelection.getNodes()` uses it to decide what is
selected. `$adjustFocusInDirection` and `$extractRectCorners` use it too.
The `FORMAT_ELEMENT_COMMAND` handler is the one place that reimplements the
rect, and it reimplements only the first half of it:
```ts
const maxRow = Math.max(
anchorCell.startRow + anchorCell.cell.__rowSpan - 1,
focusCell.startRow + focusCell.cell.__rowSpan - 1,
);
...
const minColumn = Math.min(anchorCell.startColumn, focusCell.startColumn);
```
That is the un-expanded rect, so whenever a merged cell pulls the boundary
outwards the handler formats a strict subset of what the selection contains.
The extra cells are selected and highlighted, but pressing an alignment button
leaves them untouched.
This uses `$computeTableCellRectBoundary` so the handler walks the same rect
the selection reports. The "whole table selected" shortcut just above now also
tests the expanded rect, which is the rect that actually covers the table.
## Test plan
`npx vitest run packages/lexical-table/src/__tests__/unit/FormatElementTableRect.test.ts`
The test merges grid columns 0–1 of row 1 in a 3x3 table, selects
(row 0, column 1) → (row 1, column 2), centres it, and asserts every cell
`selection.getNodes()` reports came out centred.
(The natural test file, `LexicalTableSelectionHelpers.test.ts`, is already
being edited by an open PR, so the repro lives in its own file.)
### Before
```
FAIL ... > formats every cell the selection contains, including one pulled in by a merge
AssertionError: expected [ '', 'center', 'center', …(2) ] to deeply equal [ 'center', 'center', 'center', …(2) ]
- Expected
+ Received
[
- "center",
+ "",
"center",
"center",
"center",
"center",
]
```
### After
```
Test Files 1 passed (1)
Tests 1 passed (1)
```
Also green:
- `npx vitest run packages/lexical-table` — 13 files, 158 tests
- `E2E_BROWSER=chromium npx playwright test --project=chromium Tables.spec.mjs` — 88 passed, 1 skipped
Only chromium was exercised locally for the e2e run.
LeSingh1
requested review from
acywatson,
etrepum,
fantactuka,
ivailop7,
potatowagon and
zurfyx
as code owners
August 9, 2026 06:08
|
@LeSingh1 is attempting to deploy a commit to the Meta Open Source Team on Vercel. A member of the Team first needs to authorize it. |
Collaborator
|
Confirmed the bug and tested the fix. |
mayrang
reviewed
Aug 9, 2026
mayrang
reviewed
Aug 9, 2026
etrepum
approved these changes
Aug 9, 2026
Merged
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
A
TableSelectionis two cells, and the set of cells it covers is the smallestrect containing both — but that rect has to be grown whenever a merged cell
straddles its edge, otherwise the rect would cut a cell in half.
$computeTableCellRectBoundarydoes that expansion, iterating until the rectstops growing, and
TableSelection.getNodes()uses it to decide what isselected.
$adjustFocusInDirectionand$extractRectCornersuse it too.The
FORMAT_ELEMENT_COMMANDhandler is the one place that reimplements therect, and it reimplements only the first half of it:
That is the un-expanded rect, so whenever a merged cell pulls the boundary
outwards the handler formats a strict subset of what the selection contains.
The extra cells are selected and highlighted, but pressing an alignment button
leaves them untouched.
This uses
$computeTableCellRectBoundaryso the handler walks the same rectthe selection reports. The "whole table selected" shortcut just above now also
tests the expanded rect, which is the rect that actually covers the table.
Test plan
npx vitest run packages/lexical-table/src/__tests__/unit/FormatElementTableRect.test.tsThe test merges grid columns 0–1 of row 1 in a 3x3 table, selects
(row 0, column 1) → (row 1, column 2), centres it, and asserts every cell
selection.getNodes()reports came out centred.(The natural test file,
LexicalTableSelectionHelpers.test.ts, is alreadybeing edited by an open PR, so the repro lives in its own file.)
Before
After
Also green:
npx vitest run packages/lexical-table— 13 files, 158 testsE2E_BROWSER=chromium npx playwright test --project=chromium Tables.spec.mjs— 88 passed, 1 skippedOnly chromium was exercised locally for the e2e run.