Skip to content

[lexical-table] Bug Fix: toggleHeaderStyle stays inside TableCellHeaderStates - #9038

Merged
etrepum merged 3 commits into
meta:mainfrom
LeSingh1:table-toggle-header-style-bitwise
Aug 10, 2026
Merged

etrepum merged 3 commits into
meta:mainfrom
LeSingh1:table-toggle-header-style-bitwise

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Description

TableCellNode.toggleHeaderStyle tests its argument bitwise and then mutates
arithmetically:

if ((self.__headerState & headerStateToToggle) === headerStateToToggle) {
  self.__headerState -= headerStateToToggle;
} else {
  self.__headerState += headerStateToToggle;
}

The two agree only while the argument is a single bit. The parameter is typed
TableCellHeaderState, which includes BOTH (3), and BOTH is a
first-class member of the enum everywhere else in the same class —
setHeaderStyles(headerState, mask) masks with it, hasHeaderState(BOTH)
tests it, getHeaderState in LexicalTableUtils compares against it.

Passing it here overflows: a cell with ROW (1) that is toggled with BOTH
(3) ends at __headerState === 4, which is not NO_STATUS, ROW, COLUMN
or BOTH. hasHeaderState(ROW) and hasHeaderState(COLUMN) both go false
while hasHeader() stays true, so the cell renders as a <th> that belongs to
neither a header row nor a header column, and 4 is what exportJSON writes
out.

This keeps the existing branch — "does it already have all of these bits" —
and makes the mutation bitwise to match: clear those bits, or set them.
Single-bit arguments resolve to exactly the same values as before, which the
second test pins.

Test plan

npx vitest run packages/lexical-table/src/__tests__/unit/ToggleHeaderStyle.test.ts

Before

 ❯ |unit| packages/lexical-table/src/__tests__/unit/ToggleHeaderStyle.test.ts (2 tests | 1 failed) 12ms
     × stays inside the enum when toggling BOTH 9ms

 FAIL  ... > TableCellNode.toggleHeaderStyle > stays inside the enum when toggling BOTH
AssertionError: expected 4 to be 3 // Object.is equality

- Expected
+ Received

- 3
+ 4

After

 Test Files  1 passed (1)
      Tests  2 passed (2)

npx vitest run packages/lexical-table is green (12 files, 159 tests).

…erStates

## Description

`TableCellNode.toggleHeaderStyle` tests its argument bitwise and then mutates
arithmetically:

```ts
if ((self.__headerState & headerStateToToggle) === headerStateToToggle) {
  self.__headerState -= headerStateToToggle;
} else {
  self.__headerState += headerStateToToggle;
}
```

The two agree only while the argument is a single bit. The parameter is typed
`TableCellHeaderState`, which includes `BOTH` (`3`), and `BOTH` is a
first-class member of the enum everywhere else in the same class —
`setHeaderStyles(headerState, mask)` masks with it, `hasHeaderState(BOTH)`
tests it, `getHeaderState` in `LexicalTableUtils` compares against it.

Passing it here overflows: a cell with `ROW` (`1`) that is toggled with `BOTH`
(`3`) ends at `__headerState === 4`, which is not `NO_STATUS`, `ROW`, `COLUMN`
or `BOTH`. `hasHeaderState(ROW)` and `hasHeaderState(COLUMN)` both go false
while `hasHeader()` stays true, so the cell renders as a `<th>` that belongs to
neither a header row nor a header column, and `4` is what `exportJSON` writes
out.

This keeps the existing branch — "does it already have all of these bits" —
and makes the mutation bitwise to match: clear those bits, or set them.
Single-bit arguments resolve to exactly the same values as before, which the
second test pins.

## Test plan

`npx vitest run packages/lexical-table/src/__tests__/unit/ToggleHeaderStyle.test.ts`

### Before

```
 ❯ |unit| packages/lexical-table/src/__tests__/unit/ToggleHeaderStyle.test.ts (2 tests | 1 failed) 12ms
     × stays inside the enum when toggling BOTH 9ms

 FAIL  ... > TableCellNode.toggleHeaderStyle > stays inside the enum when toggling BOTH
AssertionError: expected 4 to be 3 // Object.is equality

- Expected
+ Received

- 3
+ 4
```

### After

```
 Test Files  1 passed (1)
      Tests  2 passed (2)
```

`npx vitest run packages/lexical-table` is green (12 files, 159 tests).
@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 on main — toggle(ROW, BOTH) produces __headerState === 4, outside the enum. The bitwise fix is correct and all 159 table tests pass on this branch.

@etrepum
etrepum added this pull request to the merge queue Aug 10, 2026
Merged via the queue into meta:main with commit 41d58e2 Aug 10, 2026
44 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