Skip to content

[lexical-selection] Bug Fix: apply a function-valued style patch only once to an empty element - #8912

Merged
etrepum merged 1 commit into
meta:mainfrom
LeSingh1:fix/patch-style-text-double-apply
Aug 5, 2026
Merged

etrepum merged 1 commit into
meta:mainfrom
LeSingh1:fix/patch-style-text-double-apply

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Description

For a collapsed selection in an empty block, $patchStyleText patches the same node twice. The collapsed branch patches the anchor element, and the trailing selection.getNodes() loop then patches it again. patchedElementKeys was declared inside that loop, so it never saw the first application.

That is harmless for a plain string patch, but $patchStyle re-reads the target's current style on every call, so a function-valued patch is evaluated against the value its own previous application just wrote.

This is user-visible in the playground. The font-size + button patches with a function of the previous size, so a single click with the caret on an empty line writes 18px to selection.style but 20px to the block's textStyle — the toolbar reads 18px while the line renders at 20px.

Hoisting the Set above the collapsed branch and recording the empty element's key there makes the later loop skip it.

The second loop was added in #8003 without accounting for the pre-existing collapsed branch from #7024; function patches arrived in #6472.

Test plan

Before

New test in LexicalSelectionHelpers.test.ts fails on main:

AssertionError: expected 'font-size: 20px;' to be 'font-size: 18px;'
Expected: "font-size: 18px;"
Received: "font-size: 20px;"

After

Passes. Full packages/lexical-selection suite: 4 files, 311 tests passed. eslint, prettier and tsc --noEmit clean.

… once to an empty element

## Description

`$patchStyleText` styles an empty block twice when the selection is collapsed
inside it. The collapsed branch patches `selection.anchor.getNode()` when that
node is an empty ElementNode, and the trailing loop over `selection.getNodes()`
patches every empty element it finds — for a collapsed selection in an empty
block those are the same node. The `patchedElementKeys` set that guards the loop
was created inside the loop's scope, so it never saw the node the collapsed
branch had already patched.

A constant patch is idempotent, which is why this went unnoticed, but
`$patchStyleText` also accepts a function of the current value, and `$patchStyle`
re-reads the target's style on every call. The function is therefore evaluated
against the value the first application wrote and applied a second time.

Concretely, the playground's font-size +/- buttons patch with
`getNextFontSize` (a function of the previous size). With the caret on an empty
line, one click on "+" moves 16px to 18px in `selection.style` but to 20px in the
block's `textStyle`: the toolbar reports 18px, the empty line renders at 20px,
and typing produces 18px text inside a 20px-styled block.

The fix hoists `patchedElementKeys` so the collapsed branch can record the node
it patched, which is exactly the deduplication the loop already performs for
itself.

## Test plan

### Before

New unit test in `LexicalSelectionHelpers.test.ts` fails on `main`:

```
FAIL  $patchStyleText > applies a function-valued patch exactly once to an empty element
AssertionError: expected 'font-size: 20px;' to be 'font-size: 18px;'

Expected: "font-size: 18px;"
Received: "font-size: 20px;"
```

### After

`pnpm exec vitest --project unit packages/lexical-selection` passes (311 tests).
@vercel

vercel Bot commented Aug 4, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview Aug 4, 2026 3:44am
lexical-playground Ready Ready Preview Aug 4, 2026 3:44am

Request Review

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

This branch was successfully deployed

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

2 participants