Skip to content

[lexical][lexical-table][lexical-playground][lexical-website] Bug Fix: Standardize selection notification timing - #32

Closed
etrepum wants to merge 2 commits into
mainfrom
codex/fix-9179-selection-change
Closed

etrepum wants to merge 2 commits into
mainfrom
codex/fix-9179-selection-change

Conversation

@etrepum

@etrepum etrepum commented Sep 18, 2026 •

Copy link
Copy Markdown
Owner

Description

SELECTION_CHANGE_COMMAND can be missed when native selectionchange events are suppressed or coalesced, including deletion inside a text node. Detect changes in the pending editor state and notify before reconciliation, independently of DOM events.

Use one pre-reconciliation contract for native, programmatic, and non-range selections. Listeners see the pending selection and the actual previous committed selection, and their edits and transforms join the same commit. Deduplicate unchanged selections while retaining native dirty-selection workarounds and the connected-root requirement for automatic range/null notifications.

Migrate DOM-dependent table and playground handlers to $onUpdate, with read('latest') when reading reconciled model state. Table normalization uses model nodes, so newly created tables and cells can be selected in the same update. Register the table observer's selection-command handler only while a root exists, with cleanup on removal and replacement.

Bound non-converging selection listeners without rolling back the user's edit: commit the pending state before reporting through onWarn. Document the notification timing and DOM availability contract, including migration guidance for third-party DOM-reading handlers.

Closes meta#9179

Test plan

Before

Recorded during development against f836ea1, before the notification-limit and DOM-consumer fixes:

pnpm run test-unit LexicalSelectionChangeCommand -t 'caps notifications|node selections notify'
 Test Files  1 failed (1)
      Tests  5 failed | 21 skipped (26)

FONTCONFIG_PATH=/etc/fonts pnpm run test-browser --config vitest.local.config.mts --no-file-parallelism SelectionToolbarTiming
AssertionError: expected Set{ …(2) } to deeply equal Set{ 'CHANGEDline twoline three' }
 Test Files  1 failed (1)
      Tests  1 failed (1)

The toolbar measured both old and new DOM during one programmatic text/selection update.

After

Full validation at e956c97, before the small root-lifecycle and configuration follow-ups:

pnpm run test-unit
 Test Files  349 passed (349)
      Tests  8559 passed | 1 skipped (8560)

FONTCONFIG_PATH=/etc/fonts pnpm run test-browser --config vitest.local.config.mts --no-file-parallelism packages/lexical/src/__tests__/browser packages/lexical-table/src/__tests__/browser packages/lexical-playground/src/__tests__/browser packages/lexical-react/src/__tests__/browser
 Test Files  23 passed (23)
      Tests  152 passed | 1 skipped (153)

Validation after the root-lifecycle follow-up:

pnpm run test-unit LexicalTableSelectionHelpers LexicalTableExtension LexicalSelectionChangeCommand
 Test Files  3 passed (3)
      Tests  60 passed (60)

FONTCONFIG_PATH=/etc/fonts pnpm run test-browser --config vitest.local.config.mts --no-file-parallelism packages/lexical-table/src/__tests__/browser
 Test Files  4 passed (4)
      Tests  14 passed (14)

The final configuration cleanup also passes SelectionToolbarTiming (1 passed), without the dependency-resolution warning. TypeScript and ESLint passed; Prettier reported: All matched files use Prettier code style!

Coverage includes deletion and replacement typing, notification deduplication, previous-selection semantics, listener edits and transforms, undo merging with onUpdate/$onUpdate, newly inserted tables/cells, table escape handling, root lifecycle, and non-converging listeners.

Browser validation used Chromium on Linux with a temporary local executable configuration. Firefox, WebKit, and other operating systems were not exercised. This squash preserves the final tree exactly; tests were not rerun for the history-only change.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Sep 18, 2026 •

Copy link
Copy Markdown

Deploying lexical with  Cloudflare Pages  Cloudflare Pages

Latest commit: 53ba250
Status:⚡️  Build in progress...

View logs

@etrepum etrepum changed the title [lexical][lexical-table] Bug Fix: Notify selection changes independently of DOM events [lexical] Bug Fix: Notify selection changes independently of DOM events Sep 18, 2026
@etrepum etrepum changed the title [lexical] Bug Fix: Notify selection changes independently of DOM events [lexical][lexical-table] Bug Fix: Notify selection changes independently of DOM events Sep 18, 2026
@etrepum etrepum changed the title [lexical][lexical-table] Bug Fix: Notify selection changes independently of DOM events [lexical][lexical-table][lexical-playground][lexical-website] Bug Fix: Standardize selection notification timing Sep 18, 2026
…: Standardize selection notification timing

## Description

SELECTION_CHANGE_COMMAND can be missed when native selectionchange events are suppressed or coalesced, including deletion inside a text node. Detect changes in the pending editor state and notify before reconciliation, independently of DOM events.

Use one pre-reconciliation contract for native, programmatic, and non-range selections. Listeners see the pending selection and the actual previous committed selection, and their edits and transforms join the same commit. Deduplicate unchanged selections while retaining native dirty-selection workarounds and the connected-root requirement for automatic range/null notifications.

Migrate DOM-dependent table and playground handlers to $onUpdate, with read('latest') when reading reconciled model state. Table normalization uses model nodes, so newly created tables and cells can be selected in the same update. Register the table observer's selection-command handler only while a root exists, with cleanup on removal and replacement.

Bound non-converging selection listeners without rolling back the user's edit: commit the pending state before reporting through onWarn. Document the notification timing and DOM availability contract, including migration guidance for third-party DOM-reading handlers.

Closes meta#9179

## Test plan

### Before

Recorded during development against f836ea1, before the notification-limit and DOM-consumer fixes:

```
pnpm run test-unit LexicalSelectionChangeCommand -t 'caps notifications|node selections notify'
 Test Files  1 failed (1)
      Tests  5 failed | 21 skipped (26)

FONTCONFIG_PATH=/etc/fonts pnpm run test-browser --config vitest.local.config.mts --no-file-parallelism SelectionToolbarTiming
AssertionError: expected Set{ …(2) } to deeply equal Set{ 'CHANGEDline twoline three' }
 Test Files  1 failed (1)
      Tests  1 failed (1)
```

The toolbar measured both old and new DOM during one programmatic text/selection update.

### After

Full validation at e956c97, before the small root-lifecycle and configuration follow-ups:

```
pnpm run test-unit
 Test Files  349 passed (349)
      Tests  8559 passed | 1 skipped (8560)

FONTCONFIG_PATH=/etc/fonts pnpm run test-browser --config vitest.local.config.mts --no-file-parallelism packages/lexical/src/__tests__/browser packages/lexical-table/src/__tests__/browser packages/lexical-playground/src/__tests__/browser packages/lexical-react/src/__tests__/browser
 Test Files  23 passed (23)
      Tests  152 passed | 1 skipped (153)
```

Validation after the root-lifecycle follow-up:

```
pnpm run test-unit LexicalTableSelectionHelpers LexicalTableExtension LexicalSelectionChangeCommand
 Test Files  3 passed (3)
      Tests  60 passed (60)

FONTCONFIG_PATH=/etc/fonts pnpm run test-browser --config vitest.local.config.mts --no-file-parallelism packages/lexical-table/src/__tests__/browser
 Test Files  4 passed (4)
      Tests  14 passed (14)
```

The final configuration cleanup also passes SelectionToolbarTiming (1 passed), without the dependency-resolution warning. TypeScript and ESLint passed; Prettier reported: `All matched files use Prettier code style!`

Coverage includes deletion and replacement typing, notification deduplication, previous-selection semantics, listener edits and transforms, undo merging with onUpdate/$onUpdate, newly inserted tables/cells, table escape handling, root lifecycle, and non-converging listeners.

Browser validation used Chromium on Linux with a temporary local executable configuration. Firefox, WebKit, and other operating systems were not exercised. This squash preserves the final tree exactly; tests were not rerun for the history-only change.
@etrepum
etrepum force-pushed the codex/fix-9179-selection-change branch from 7f1b3e6 to 53ba250 Compare September 24, 2026 15:17
… main

## Description

Merge facebook/lexical main at 6fdbffa into the selection-change branch without rebasing.

Firefox can leave the test editor as document.activeElement while its iframe has no focus, so keyboard input never reaches the editor. Explicitly focus the iframe immediately before keyboard input in the deletion and selection-command tests.

Drain setup mutations before the nested shadow-editor attribution test changes the native selection. Otherwise the outer editor's pending MutationObserver update imports WebKit's host-retargeted selection and emits a separate notification. Keep the zero-outer-notifications assertion and also assert that the outer editor's selection remains null.

Define tableScrollableWrapper in the browser test themes to remove the table configuration warnings.

## Test plan

### Before

`VITEST_BROWSER=firefox pnpm run test-browser`

```text
 Test Files  1 failed | 28 passed | 1 skipped (30)
      Tests  24 failed | 167 passed | 4 skipped (195)
```

`VITEST_BROWSER=webkit pnpm run test-browser ShadowRootSelection -t 'attributes to the inner'`

```text
AssertionError: expected 1 to be +0 // Object.is equality
 Test Files  1 failed (1)
      Tests  1 failed | 30 skipped (31)
```

### After

`VITEST_BROWSER=chromium,firefox,webkit pnpm run test-browser`

```text
 Test Files  89 passed | 1 skipped (90)
      Tests  578 passed | 7 skipped (585)
```

TypeScript, ESLint, Prettier, and git diff --check passed. All browser runs above were on Linux; macOS and Windows are left to CI.
@etrepum etrepum closed this Sep 24, 2026
etrepum pushed a commit that referenced this pull request Oct 6, 2026
…paces beside line breaks in Markdown table cells

## Description

Fixes from a /code-review of the playground's `TABLE` Markdown transformer:

- An empty row is written `|  |  |`, which `isTableRowDivider` also accepts, so an empty body row was read as a delimiter row: it was dropped and the row above it became a header, and a table with an empty header row stayed text. A delimiter row now needs a dash in every cell, as GFM requires. `isTableRowDivider` itself is unchanged.
- An empty paragraph in a cell was lost: `a`, an empty paragraph and `b` exported as `a<br>b`, the same as two paragraphs. Each paragraph is now a line, so an empty one is an empty line (`a<br><br>b`), which reads back as a blank line between them. After a list or quote it adds a line beyond the one that ends the block.
- Spaces next to a line break were trimmed on import as the cell's padding (`a  ` then a break then `b` read back as `a` and `b`). The space or tab next to a break is now written as a character reference (`a &#32;<br>b`).
- A cell with more line breaks than a call can take arguments threw `RangeError: Maximum call stack size exceeded` from `splitText(...offsets)`, and reading one was quadratic. The text under a cell is now rebuilt in one pass and spliced in, and the decoder joins its lines once, so a 64,000-line cell reads in under a second instead of about ten.
- Text between code marks that had no mark of its own (in a node between the start and end marks) wasn't made code.

The upstream review asked that pipes after a run of backslashes be split by the parity of the run (`\|` escaped, `\\|` a separator, `\\\|` escaped). `splitTableRow` reads each backslash with the character after it, which gives exactly that; new tests cover those rows and `| a | \a | b |`.

## Test plan

### Before

```
$ vitest run --project unit packages/lexical-playground/__tests__/unit/MarkdownTransformers.test.ts
     × keeps an empty body row that reads like a delimiter row 16ms
     × reads a table whose header cells are empty 9ms
     × keeps the line of an empty paragraph between paragraphs in a cell 3ms
     × keeps the line of an empty paragraph after a heading in a cell 2ms
     × keeps the line of an empty paragraph first in a cell 2ms
     × keeps the line of an empty paragraph last in a cell 2ms
     × keeps the line of an empty paragraph alone in a cell 2ms
     × keeps the line of an empty paragraph after a list in a cell 2ms
     × keeps the spaces beside a line break in "a  \nb" 4ms
     × keeps the spaces beside a line break in "a\n  b" 3ms
     × keeps the spaces beside a line break in "a\t\n\tb" 3ms
     × keeps the spaces beside a line break in "a \n \nb" 3ms
     × reads a cell with more line breaks than a call can take arguments 15468ms
AssertionError: expected [ [ [ 'a' ], [ 'b' ] ], …(1) ] to deeply equal [ [ [ 'a' ], [ 'b' ] ], …(2) ]
AssertionError: Root child must be a table
AssertionError: expected '| a<br>b |' to be '| a<br><br>b |' // Object.is equality
AssertionError: expected [ [ 'a\nb' ] ] to deeply equal [ [ 'a  \nb' ] ]
RangeError: Maximum call stack size exceeded
```

### After

```
$ vitest run --project unit packages/lexical-playground
      Tests  504 passed (504)
```

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
etrepum pushed a commit that referenced this pull request Oct 7, 2026
…l and guard missing offsets

## Description

- A line of only spaces right after a block in a cell (`<ul><li>a</li></ul>&#32;&#32;<br>`) was dropped on import, since any whitespace there was taken as padding. Only whitespace written literally is dropped now.
- Telling literal spaces from references sliced the source by a node's offsets without checking that they exist, so a tree whose positions have only lines and columns sliced the whole document and stopped trimming `a <br>  b`. Without offsets, all edge spaces are taken as literal, as without a source.
- Each line before a block in a cell is trimmed once instead of twice.

## Test plan

### Before

```
$ vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastTableHtml.test.ts
     × keeps a line of only spaces after a block in a cell
     × trims the spaces around a <br> in a tree without offsets
```

### After

```
$ vitest run --project unit packages/lexical-markdown packages/lexical-playground/__tests__/unit packages/lexical-code-core packages/lexical-react packages/lexical-mdast
      Tests  1628 passed (1628)
$ vitest run --project browser packages/lexical-mdast dev-examples/mdast-editor
      Tests  44 passed (44)
$ pnpm run tsc && pnpm run flow
Found 0 errors
```

Only Chromium was available, so the browser tests did not run in Firefox or WebKit.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
etrepum pushed a commit that referenced this pull request Oct 7, 2026
…nd columns

## Description

Telling the spaces a cell line was written with from those written as character references needs a node's offsets in the source. A tree whose positions have only lines and columns, as a tree transform may leave them, took every edge space as literal, so `a<br>&#32;b` read back as `a` and `b`. Offsets are now worked out from the line and column when a point has none. The three places that ended a line in `cellItems` are one helper again, and its doc comment states which whitespace after a block makes a line: anything but spaces and tabs written as they are, so a no-break space after a block, which GFM doesn't trim, is kept as a line too.

## Test plan

### Before

```
$ vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastTableHtml.test.ts
     × reads "a<br>&#32;b" in a tree without offsets as ["a"," b"]
      Tests  1 failed | 23 passed (24)
```

### After

```
$ vitest run --project unit packages/lexical-mdast
      Tests  252 passed (252)
$ vitest run --project browser packages/lexical-mdast dev-examples/mdast-editor
      Tests  44 passed (44)
```

Only Chromium was available, so the browser tests did not run in Firefox or WebKit.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
etrepum pushed a commit that referenced this pull request Oct 8, 2026
…oes and pad code spans only for spaces

## Description

- `unescapeText` read backslash escapes and character references in two passes, so an escaped `\&#65;` came back as `A` instead of the text `&#65;`. It now reads both in one pass. Export writes `\&` before text that would read as a reference, so text like `&#32;` round-trips.
- A decimal reference with more than 7 digits (`&#99999999;`) is not a reference in CommonMark and now stays text instead of becoming U+FFFD.
- A code span was padded when it started and ended with any whitespace, but a reader strips only U+0020, so spans like a tab-wrapped `x` or a lone no-break space are now written without padding.

## Test plan

### Before

```
$ vitest run --project unit packages/lexical-markdown/src/__tests__/unit/CodeSpanEscapes.test.ts
     × round-trips "\tx\t" bare
     × round-trips " " bare
     × reads "&#99999999;" as the text "&#99999999;"
     × reads "\\&#65;" as the text "&#65;"
     × round-trips text that reads as one
⎯⎯⎯⎯⎯⎯⎯ Failed Tests 5 ⎯⎯⎯⎯⎯⎯⎯
      Tests  5 failed | 13 passed (18)
```

The second line is the no-break space case, which the terminal shows as a space.

### After

```
$ vitest run --project unit packages/lexical-markdown/src/__tests__/unit/CodeSpanEscapes.test.ts
      Tests  18 passed (18)
```

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
etrepum pushed a commit that referenced this pull request Oct 8, 2026
## Description

A tree transform can leave points with only a line and column. The table extension worked out offsets itself with a module-level cache that kept the last source alive and miscounted a bare newline followed by another, while every other handler that reads the source by offset (heading style, list markers, code fences) fell back to its no-source behavior. The import (and the streaming parser) now fill in each missing offset once, after parsing, from the line and column, so every reader gets it and nothing is cached across imports.

## Test plan

### Before

```
$ vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastTableHtml.test.ts
     × reads "h\n=\n\n\n""a<br>&#32;b" in a tree without offsets
AssertionError: expected '# h\n\n| a           |\n| -----------…' to match /^h\n=+\n/
```

### After

```
$ vitest run --project unit packages/lexical-markdown packages/lexical-playground/__tests__/unit packages/lexical-code-core packages/lexical-react packages/lexical-mdast
      Tests  1636 passed (1636)
$ vitest run --project browser packages/lexical-mdast dev-examples/mdast-editor
      Tests  44 passed (44)
$ vitest run --project scripts-unit scripts/__tests__/unit/treeShakingSource.test.ts
      Tests  342 passed (342)
$ pnpm run tsc && pnpm run flow
Found 0 errors
```

Only Chromium was available, so the browser tests did not run in Firefox or WebKit.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
etrepum pushed a commit that referenced this pull request Oct 18, 2026
…ts them

## Description

`MdastTableExtension` trimmed spaces and tabs from the edges of every line of a cell, including a cell with no `<br>`, so spaces written as character references (`&#32;`), which GFM keeps, were dropped. Only the whitespace beside a `<br>` is trimmed now; the cell's own edges keep what the GFM parser left, as on main.

## Test plan

### Before

```
$ vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastImportExport.test.ts -t "references at the edges"
       × keeps spaces written as references at the edges of a cell 45ms
AssertionError: expected 'b' to be '  b ' // Object.is equality
      Tests  1 failed | 113 skipped (114)
```

### After

```
$ vitest run --project unit packages/lexical-mdast
      Tests  225 passed (225)
```

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

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

1 participant