Skip to content

[lexical-mark] Bug Fix: keep the markOverlap class in sync for every id-count change - #8987

Closed
LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:fix/marknode-overlap-class
Closed

LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:fix/marknode-overlap-class

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Description

MarkNode.createDOM states the rule plainly — a mark carries
theme.markOverlap when it has more than one id:

if (this.__ids.length > 1) {
  addClassNamesToElement(element, config.theme.markOverlap);
}

updateDOM enumerated transitions instead of re-applying that rule, and only
covered two of them:

if (prevIDsCount !== nextIDsCount) {
  if (prevIDsCount === 1) {
    if (nextIDsCount === 2) { addClassNamesToElement(element, overlapTheme); }
  } else if (nextIDsCount === 1) {
    removeClassNamesFromElement(element, overlapTheme);
  }
}

So any other crossing of the > 1 boundary desynced the DOM from the rule:

  • 1 -> 3 (or 0 -> 2, 1 -> 4, …) crosses into overlap but the class is
    never added — the inner nextIDsCount === 2 check fails and the else if is
    unreachable;
  • 2 -> 0 crosses out of overlap but the class is never removed, leaving
    markOverlap on a mark with no ids.

1 -> 3 is reachable from the shipped playground comment feature: merging
nested marks does from.getIDs().forEach(id => to.addID(id))
(CommentPlugin/index.tsx), so merging a 2-id mark into a 1-id mark lands on 3
ids in a single update and the overlap styling is silently lost. Re-creating
the same node from scratch does style it, which is the contradiction.

Compute the overlap state from the same > 1 rule createDOM uses and toggle
the class only when that state changes.

Test plan

Three new cases in packages/lexical-mark/__tests__/unit/LexicalMarkNode.test.ts
under a theme that defines mark/markOverlap. The createDOM case passes
before and after — it is the reference the two updateDOM cases are held to.

Before

$ npx vitest run packages/lexical-mark

 FAIL  |unit| .../LexicalMarkNode.test.ts > MarkNode overlap theme class > updateDOM adds the overlap class when going from 1 to 3 ids
AssertionError: expected 'mk' to contain 'mk-overlap'

 FAIL  |unit| .../LexicalMarkNode.test.ts > MarkNode overlap theme class > updateDOM removes the overlap class when going from 2 to 0 ids
AssertionError: expected 'mk mk-overlap' not to contain 'mk-overlap'

Expected: "mk-overlap"
Received: "mk mk-overlap"

 Test Files  1 failed | 2 passed (3)
      Tests  2 failed | 437 passed (439)

After

$ npx vitest run packages/lexical-mark

 Test Files  3 passed (3)
      Tests  439 passed (439)

…id-count change

## Description

`MarkNode.createDOM` states the rule plainly — a mark carries
`theme.markOverlap` when it has more than one id:

```ts
if (this.__ids.length > 1) {
  addClassNamesToElement(element, config.theme.markOverlap);
}
```

`updateDOM` enumerated transitions instead of re-applying that rule, and only
covered two of them:

```ts
if (prevIDsCount !== nextIDsCount) {
  if (prevIDsCount === 1) {
    if (nextIDsCount === 2) { addClassNamesToElement(element, overlapTheme); }
  } else if (nextIDsCount === 1) {
    removeClassNamesFromElement(element, overlapTheme);
  }
}
```

So any other crossing of the `> 1` boundary desynced the DOM from the rule:

- `1 -> 3` (or `0 -> 2`, `1 -> 4`, …) crosses into overlap but the class is
  never added — the inner `nextIDsCount === 2` check fails and the `else if` is
  unreachable;
- `2 -> 0` crosses out of overlap but the class is never removed, leaving
  `markOverlap` on a mark with no ids.

`1 -> 3` is reachable from the shipped playground comment feature: merging
nested marks does `from.getIDs().forEach(id => to.addID(id))`
(`CommentPlugin/index.tsx`), so merging a 2-id mark into a 1-id mark lands on 3
ids in a single update and the overlap styling is silently lost. Re-creating
the same node from scratch does style it, which is the contradiction.

Compute the overlap state from the same `> 1` rule `createDOM` uses and toggle
the class only when that state changes.

## Test plan

Three new cases in `packages/lexical-mark/__tests__/unit/LexicalMarkNode.test.ts`
under a theme that defines `mark`/`markOverlap`. The `createDOM` case passes
before and after — it is the reference the two `updateDOM` cases are held to.

### Before

```
$ npx vitest run packages/lexical-mark

 FAIL  |unit| .../LexicalMarkNode.test.ts > MarkNode overlap theme class > updateDOM adds the overlap class when going from 1 to 3 ids
AssertionError: expected 'mk' to contain 'mk-overlap'

 FAIL  |unit| .../LexicalMarkNode.test.ts > MarkNode overlap theme class > updateDOM removes the overlap class when going from 2 to 0 ids
AssertionError: expected 'mk mk-overlap' not to contain 'mk-overlap'

Expected: "mk-overlap"
Received: "mk mk-overlap"

 Test Files  1 failed | 2 passed (3)
      Tests  2 failed | 437 passed (439)
```

### After

```
$ npx vitest run packages/lexical-mark

 Test Files  3 passed (3)
      Tests  439 passed (439)
```
@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
@LeSingh1

Copy link
Copy Markdown
Contributor Author

Consolidated into #9060 with the other PRs that share this defect, per @etrepum's note on #9027 and @mayrang's on #9035. Same fix and same tests, one review.

@LeSingh1 LeSingh1 closed this Aug 10, 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.

1 participant