Skip to content

[lexical] Bug Fix: avoid redundant selection change commands - #9138

Closed
kavyansh18 wants to merge 1 commit into
meta:mainfrom
kavyansh18:fix/safari-selection-command
Closed

kavyansh18 wants to merge 1 commit into
meta:mainfrom
kavyansh18:fix/safari-selection-command

Conversation

@kavyansh18

Copy link
Copy Markdown
Contributor

Description

Safari/WebKit can dispatch a native selectionchange event when interacting with links outside the editor, even when the Lexical selection has not actually changed.

Previously, onSelectionChange unconditionally dispatched SELECTION_CHANGE_COMMAND for every selectionchange event. This caused an unnecessary SELECTION_CHANGE_COMMAND when clicking the link in the floating link menu in Safari.

This change checks the previous Lexical selection before dispatching the command. The command is dispatched when:

  • the selection value has changed;
  • the selection is marked dirty; or
  • the selection changes between null and a non-null selection.

A browser regression test was added to verify that redundant selectionchange events are ignored while legitimate caret and range selection changes continue to dispatch SELECTION_CHANGE_COMMAND.

Closes #8885

Test plan

Before

On the unmodified implementation, the regression test fails because a redundant selectionchange dispatches SELECTION_CHANGE_COMMAND:

Expected: 0
Received: 1

The failure was reproduced in WebKit against the unmodified base implementation.

After

The regression test passes with the fix in:

  • WebKit
  • Chromium
  • Firefox

The test verifies that:

  • redundant selectionchange events do not dispatch SELECTION_CHANGE_COMMAND;
  • legitimate caret movement still dispatches the command;
  • legitimate range expansion still dispatches the command;
  • redundant events after legitimate selection changes remain suppressed.

Additional validation:

  • pnpm run test-unit packages/lexical/src/__tests__/unit/LexicalSelection*.test.ts
  • pnpm run ci-check
  • Prettier
  • ESLint
  • git diff --check

@vercel

vercel Bot commented Sep 8, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
lexical Ready Ready Preview Sep 9, 2026 8:05am UTC
lexical-playground Ready Ready Preview Sep 9, 2026 8:05am UTC

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 Sep 8, 2026
@kavyansh18

Copy link
Copy Markdown
Contributor Author

hey @etrepum can you please review the PR

@etrepum

etrepum commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator

I don't review PRs with failing tests

@kavyansh18

Copy link
Copy Markdown
Contributor Author

@etrepum I’ve updated the selection-change handling to avoid redundant SELECTION_CHANGE_COMMAND dispatches while preserving Lexical/table-driven selection updates

@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Sep 9, 2026
@etrepum etrepum changed the title fix: avoid redundant selection change commands [lexical] Bug Fix: avoid redundant selection change commands Sep 9, 2026
@kavyansh18

Copy link
Copy Markdown
Contributor Author

hey @etrepum can we merge if all good?

@etrepum

etrepum commented Sep 10, 2026

Copy link
Copy Markdown
Collaborator

Be patient. There are other PRs to review and other priorities for the project and its maintainers. Generating extra notifications by begging for review will only lower the priority of your PRs.

@kavyansh18 kavyansh18 closed this Sep 10, 2026
@kavyansh18
kavyansh18 deleted the fix/safari-selection-command branch September 10, 2026 15:28
@levensta

Copy link
Copy Markdown
Contributor

@kavyansh18 It’s a shame you closed the PR. This change improves the editor's stability. I don't think there was any need to rush the review; a new version of Lexical is released about once a month on average, so the PR would certainly have been noticed within two or three weeks

This branch was successfully deployed

2 active deployments
Preview – lexical — 0b66e5c9 Deployed Sep 9, 2026 by vercel[bot]
Preview – lexical-playground — 0b66e5c9 Deployed Sep 9, 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: SELECTION_CHANGE_COMMAND is triggered after clicking a link in Safari

3 participants