Repository navigation
[lexical-selection] Bug Fix: apply a function-valued style patch only once to an empty element - #8912
Merged
Conversation
… 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).
LeSingh1
requested review from
acywatson,
etrepum,
fantactuka,
ivailop7,
potatowagon and
zurfyx
as code owners
August 4, 2026 03:43
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
etrepum
approved these changes
Aug 5, 2026
Merged
This branch was successfully deployed
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
For a collapsed selection in an empty block,
$patchStyleTextpatches the same node twice. The collapsed branch patches the anchor element, and the trailingselection.getNodes()loop then patches it again.patchedElementKeyswas declared inside that loop, so it never saw the first application.That is harmless for a plain string patch, but
$patchStylere-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 writes18pxtoselection.stylebut20pxto the block'stextStyle— the toolbar reads 18px while the line renders at 20px.Hoisting the
Setabove 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.tsfails onmain:After
Passes. Full
packages/lexical-selectionsuite: 4 files, 311 tests passed.eslint,prettierandtsc --noEmitclean.