Skip to content

[lexical-selection] Bug Fix: Make $getSelectionStyleValueForProperty direction-independent - #8261

Merged
etrepum merged 2 commits into
meta:mainfrom
Sathvik-Chowdary-Veerapaneni:selection-style-direction-fix-8013
Mar 28, 2026
Merged

etrepum merged 2 commits into
meta:mainfrom
Sathvik-Chowdary-Veerapaneni:selection-style-direction-fix-8013

Conversation

@Sathvik-Chowdary-Veerapaneni

Copy link
Copy Markdown
Contributor

Bug

Fixes #8013

$getSelectionStyleValueForProperty returns different values depending on whether the selection is forward or backward when text nodes have mixed styles.

Root Cause

The function used null as a sentinel value to mean "no text node has been visited yet." But null can also be a legitimate defaultValue passed by the caller, which causes the sentinel check (styleValue === null) to conflate "first iteration" with "unstyled node returned the default." This makes the function direction-dependent: iterating from an unstyled node to a styled node (forward) incorrectly returns the styled node's value, while iterating from styled to unstyled (backward) correctly returns ''.

Fix

Replace the null sentinel with a boolean isFirst flag. This cleanly separates the "first text node" check from the style value comparison, ensuring consistent results regardless of selection direction.

  • styleValue is initialized to defaultValue instead of null
  • isFirst boolean tracks whether we've seen the first text node
  • The final return is simplified since styleValue already holds defaultValue when no text nodes are found

Test Plan

  • Added a unit test that creates two text nodes (one unstyled, one styled), selects them in both forward and backward directions, and verifies $getSelectionStyleValueForProperty returns the same value ('') for both
  • All 285 existing selection tests continue to pass
  • TypeScript, ESLint, and Prettier checks all pass

…return consistent values regardless of selection direction (meta#8013)

Replace null sentinel with boolean flag to avoid conflating "no nodes seen yet" with a valid default value, ensuring forward and backward selections return the same result.
@vercel

vercel Bot commented Mar 26, 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, Comment Mar 26, 2026 8:47pm
lexical-playground Ready Ready Preview, Comment Mar 26, 2026 8:47pm

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 Mar 26, 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.

I'm not sure this is the best fix, I think a more appropriate solution is to apply the same edge case handling to the start and end nodes (ignore them if zero characters are selected) rather than only the end (right) node.

@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Mar 26, 2026
Skip start node when cursor is at end of it, skip end node when
cursor is at beginning — handles both selection directions correctly.
@Sathvik-Chowdary-Veerapaneni

Copy link
Copy Markdown
Contributor Author

reworked — now skips start node when cursor is at the end of it, and end node when cursor is at the beginning, so both boundaries are handled regardless of direction

@etrepum
etrepum added this pull request to the merge queue Mar 28, 2026
Merged via the queue into meta:main with commit b3f5e16 Mar 28, 2026
32 checks passed
@etrepum etrepum mentioned this pull request Apr 9, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical-playground — 610d1793 Deployed Mar 26, 2026 by vercel[bot]
Preview – lexical — 610d1793 Deployed Mar 26, 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. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: $getSelectionStyleValueForProperty will report different values for backward selection when 'null' passed as default

2 participants