Skip to content

[lexical] Bug Fix: Update selection format and style on programmatic select#8819

Open
1012ayush wants to merge 2 commits into
facebook:mainfrom
1012ayush:fix-programmatic-selection-format
Open

[lexical] Bug Fix: Update selection format and style on programmatic select#8819
1012ayush wants to merge 2 commits into
facebook:mainfrom
1012ayush:fix-programmatic-selection-format

Conversation

@1012ayush

Copy link
Copy Markdown

Description

What is the current behavior that you are modifying?
When moving a selection programmatically (e.g., using $getRoot().selectStart()), the RangeSelection object updates its anchor and focus properties to the new location, but it fails to update the format and style caches. This results in the new selection holding stale formatting data from the previous cursor position.

What are the behavior or changes that are being added by this PR?
This PR ensures that programmatic selection updates correctly synchronize the format and style caches:

  1. In LexicalElementNode.ts, the select() method's fallback logic now resets selection.format = 0 and selection.style = '' to clear stale data when a new element is selected.
  2. In LexicalTextNode.ts, the select() method now explicitly inherits the target node's formatting by setting selection.format = this.getFormat() and selection.style = this.getStyle().

Closes #8817

Test plan

Before

When selecting text with a specific format (e.g., bold) and then triggering a programmatic selection change (like clicking a button that calls selectStart()), $getSelection().hasFormat('bold') incorrectly remains true because the bitmask is never cleared.

After

Programmatic selection changes now correctly flush the format cache. Moving the selection via selectStart() to an unformatted node correctly returns false for $getSelection().hasFormat('bold').

Note: Verified locally using the reproduction StackBlitz provided in the original issue.

@vercel

vercel Bot commented Jul 8, 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 Jul 9, 2026 4:28am
lexical-playground Ready Ready Preview Jul 9, 2026 4:28am

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 Jul 8, 2026

@etrepum etrepum left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach fails existing tests and doesn't add any new tests

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.

Bug: Programmatically moving a Selection does not update the Formatting nor Style

2 participants