Skip to content

[lexical] Bug Fix: Dispatch SELECTION_CHANGE_COMMAND for skipped DOM selection updates - #9214

Closed
kwy404 wants to merge 2 commits into
meta:mainfrom
kwy404:fix-selection-change-after-dom-update
Closed

kwy404 wants to merge 2 commits into
meta:mainfrom
kwy404:fix-selection-change-after-dom-update

Conversation

@kwy404

@kwy404 kwy404 commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

Description

When Lexical moves the DOM selection itself, for example after deleting a range inside a single text node, the next selectionchange event is skipped as an echo when both points land strictly inside a text node. That early return also skipped SELECTION_CHANGE_COMMAND, so the command fired or not depending on whether the new caret happened to sit at a text node boundary. The non-skipped path already dispatches the command for these DOM update echoes, so this dispatches it in the skipped path too.

Closes #9179

Test plan

Before

Added a browser test to Issue8885SafariLinkSelectionChange.test.ts that selects llo wo in Hello world, dispatches DELETE_CHARACTER_COMMAND and waits for SELECTION_CHANGE_COMMAND.

npx vitest --project browser --no-watch packages/lexical/src/__tests__/browser/Issue8885SafariLinkSelectionChange.test.ts

 × dispatches when a deletion moves the selection inside a text node (#9179)
AssertionError: expected "vi.fn()" to be called 1 times, but got 0 times
 Test Files  1 failed (1)
      Tests  1 failed | 1 passed (2)

After

npx vitest --project browser --no-watch packages/lexical/src/__tests__/browser/Issue8885SafariLinkSelectionChange.test.ts

 Test Files  1 passed (1)
      Tests  2 passed (2)

npx vitest --project unit --project browser --no-watch has 4844 passing tests. The only failures are 3 tests in packages/lexical-compiler (inline.test.ts, subpathImports.test.ts), which fail the same way on main on my Windows machine. tsc, eslint and prettier pass. The browser tests ran in Chromium only, and I did not run the Playwright e2e suite.

…selection updates

## Description

When Lexical moves the DOM selection itself and both points land inside a
text node, the following selectionchange event is skipped as an echo. That
early return also skipped SELECTION_CHANGE_COMMAND, so deleting a range
inside a single text node never dispatched it. Dispatch the command in the
skipped path too, matching the non-skipped path for DOM update echoes.

Closes meta#9179

## Test plan

### Before

npx vitest --project browser --no-watch packages/lexical/src/__tests__/browser/Issue8885SafariLinkSelectionChange.test.ts
AssertionError: expected "vi.fn()" to be called 1 times, but got 0 times
Tests  1 failed | 1 passed (2)

### After

npx vitest --project browser --no-watch packages/lexical/src/__tests__/browser/Issue8885SafariLinkSelectionChange.test.ts
Tests  2 passed (2)
@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 24, 2026
@vercel

vercel Bot commented Sep 24, 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 24, 2026 4:19am UTC
lexical-playground Ready Ready Preview Sep 24, 2026 4:19am UTC

Request Review

@mayrang mayrang 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.

Verified locally — both tests pass on the PR branch (Chromium).

The fix is correct. The root cause is clear: shouldSkipSelectionChange returning true caused an early exit before updateEditorSync was called, and since SELECTION_CHANGE_COMMAND is dispatched inside updateEditorSync in the non-skipped path, it was silently dropped.

One thing worth noting:

In the non-skipped path, SELECTION_CHANGE_COMMAND fires inside an updateEditorSync callback (L483), so listeners run as part of an active update — any editor.update() call inside a listener is batched synchronously with it.

With this fix, dispatchCommand is called at the event-handler level (outside any update), so listeners run in a separate update cycle. For the common case ($getSelection() lookups, toolbar state updates) this is fine — the selection committed by the delete operation is already readable. But listeners that call editor.update() will get a separate async cycle rather than batching.

This pattern does appear elsewhere in LexicalEvents.ts (e.g. L604, L962), so it's not unprecedented. Just worth a comment so future readers don't wonder why the dispatch lands before the return:

// Dispatch before returning: the non-skipped path emits this command
// inside updateEditorSync, but the selection was already committed by
// the prior update (e.g. DELETE_CHARACTER_COMMAND), so listeners will
// observe the correct state via $getSelection().
dispatchCommand(editor, SELECTION_CHANGE_COMMAND);
return;

@kwy404

kwy404 commented Sep 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for verifying and for the detailed note. Added a short comment above the dispatch in 5bdbb59 explaining that the selection was already committed by the update that moved it, so listeners can read it outside of an update.

@etrepum

etrepum commented Sep 24, 2026

Copy link
Copy Markdown
Collaborator

dispatch always creates an update, so it's not outside of an update, but it is outside of the update where the selection was initially changed. While this is more correct than main, I think the most correct solution would fire the dispatch in the update where the selection is changed because that's when other selectionchange events for RangeSelection happen. I have a draft branch that consolidates all of the SELECTION_CHANGE_COMMAND dispatches to happen with consistent in-update timing across all selection types (the non-RangeSelection instances were handled differently). I just opened a draft PR #9219 which will supersede this PR.

@kwy404

kwy404 commented Sep 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the correction on the update timing, that makes sense. Consolidating the dispatch into the update where the selection changes is the better fix, happy for #9219 to supersede this one.

This branch was successfully deployed

2 active deployments
Preview – lexical — 5bdbb59d Deployed Sep 24, 2026 by vercel[bot]
Preview – lexical-playground — 5bdbb59d Deployed Sep 24, 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.

Bug: SELECTION_CHANGE_COMMAND is not triggered if the anchor and focus are on nodes of the same type

3 participants