Skip to content

[lexical-table] Bug Fix: align every cell a table selection covers, not just the naive rect - #9043

Merged
etrepum merged 3 commits into
meta:mainfrom
LeSingh1:table-format-element-rect-boundary
Aug 9, 2026
Merged

etrepum merged 3 commits into
meta:mainfrom
LeSingh1:table-format-element-rect-boundary

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

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:

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.

…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.
@vercel

vercel Bot commented Aug 9, 2026

Copy link
Copy Markdown

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

@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 Aug 9, 2026
@mayrang

mayrang commented Aug 9, 2026

Copy link
Copy Markdown
Collaborator

Confirmed the bug and tested the fix. $computeTableCellRectBoundary is exactly what this handler should have been using, and the test catches the regression clearly. Two small notes inline.

@etrepum
etrepum added this pull request to the merge queue Aug 9, 2026
Merged via the queue into meta:main with commit aa7e3de Aug 9, 2026
40 checks passed
@etrepum etrepum mentioned this pull request Aug 31, 2026
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.

3 participants