Skip to content

[lexical] Bug Fix: keep the select-all highlight visible next to boundary decorators - #8931

Merged
etrepum merged 9 commits into
meta:mainfrom
etrepum:claude/lexical-issue-8922-01eofa
Aug 8, 2026
Merged

etrepum merged 9 commits into
meta:mainfrom
etrepum:claude/lexical-issue-8922-01eofa

Conversation

@etrepum

@etrepum etrepum commented Aug 5, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Selecting all content in a document whose first or last top-level node is a block DecoratorNode renders no highlight at all. Browsers drop the selection highlight for the whole document when a range endpoint is an element-boundary DOM position ((element, 0) / (element, childNodes.length)) immediately next to a block-level contenteditable=false child, and select-all produces exactly that shape. The Lexical selection and DOM Range both survive — it just looks like the keystroke did nothing. WebKit drops the highlight when either endpoint has that shape (its own execCommand('selectAll') hits the same wall), Chromium when both do, Firefox never — interior element points paint fine everywhere, which is why the fix is not gated on WebKit.

This PR parks a zero-size, out-of-flow <img data-lexical-decorator-boundary> on the outside of such a boundary decorator, giving the browser an editable inline box to canonicalize the boundary position against, which restores the highlight (including over the decorator). position: absolute keeps it out of the inline flow so layout is unchanged. The anchors are reconciler scaffolding with the DOM as the source of truth — identified by attribute plus their fixed position in the managed range, no per-element cache to go stale, self-healing on the next reconcile if the browser wipes one. LexicalDOMSlot gains get/setDecoratorBoundaryAnchor and steps over the anchors so child-index math is untouched; LexicalReconciler reconciles them on every dirty non-inline element including the root (where select-all lands); LexicalMutations recognizes them as keyless scaffolding so a flush doesn't evict them as foreign DOM. The playground e2e helpers normalize the anchors out of assertHTML / assertSelection.

Also fixes a Safari-only follow-up found on this PR's preview deploy: after Cmd-A twice, clicking back into text moved the DOM caret but left the Lexical selection frozen on the select-all range. The second select-all re-applies a DOM selection the DOM already has; $updateDOMSelection deliberately re-sets element-anchored selections and then marks isSelectionChangeFromDOMUpdate, but WebKit fires no selectionchange for the no-op re-set, so the stale flag swallowed the next real event via the skip-optimization in onSelectionChange. The reconciler now records the DOM points it applied alongside the flag, and the skip only fires when the observed selection is those same points — a stale flag self-heals, and the same guard hardens all engines against selectionchange coalescing.

Related hardening: a browser-mode characterization test now pins the previously untested WebKit img+br managed-linebreak DOM contract per engine. The two img hacks are deliberately not unified: one is in-flow and WebKit-only, the other out-of-flow, cross-browser, and position-sensitive.

Closes #8922

Test plan

Before

Issue8922SelectAllPaint.test.ts counts pixels painted in the ::selection colour before/after $selectAll() (a real highlight adds thousands; unfixed browsers add exactly zero). With the reconciler change disabled:

webkit    no decorators ✓   leading ✗   trailing ✗   both ✗
chromium  no decorators ✓   leading ✓   trailing ✓   both ✗
firefox   no decorators ✓   leading ✓   trailing ✓   both ✓

Issue8922SelectionSyncAfterSelectAll.test.ts (select-all twice, then collapse the DOM selection mid-text) without the selection-sync fix, on webkit:

- Expected
+ Received

  {
-   "anchorKey": "2",
-   "anchorOffset": 3,
-   "anchorType": "text",
-   "collapsed": true,
+   "anchorKey": "root",
+   "anchorOffset": 0,
+   "anchorType": "element",
+   "collapsed": false,
  }

 Test Files  1 failed | 16 passed (17)

WebkitLinebreakImg.test.ts likewise fails 3 of 5 cases on webkit if the managed-linebreak img hack is removed (previously nothing did).

After

All browser-mode tests pass in all three engines (17 files / 123 tests each), including the paint matrix, the selection-sync regression, and the linebreak characterization:

webkit,chromium   Test Files  34 passed (34)   Tests  243 passed | 3 skipped
firefox           Test Files  17 passed (17)   Tests  122 passed | 1 skipped

Unit suite (pnpm run test-unit, includes Issue8922Repro.test.ts pinning anchor DOM shape and child-offset bookkeeping):

 Test Files  235 passed (235)
      Tests  5002 passed | 1 skipped (5003)

tsc, flow, eslint, prettier all pass. E2E in chromium (182 passed: regression, slot, selection, decorator specs) and webkit (122 passed: same selection plus decorator/slot specs); the full e2e suite does not fit in the dev container's time budget, so it was not run end to end. Verified in headless Linux engines; native Safari 26.6 was the original reporter for the selection-sync fix and is worth a confirm on the next preview deploy.

…dary decorators

## Description

Currently, selecting all content in a document whose first or last top-level
node is a block DecoratorNode renders no highlight at all. Browsers drop the
selection highlight for the whole document when a range endpoint is an
element-boundary DOM position (`(element, 0)` / `(element, childNodes.length)`)
sitting immediately next to a block-level `contenteditable=false` child, and
select-all produces exactly that shape. The Lexical selection and the DOM Range
both survive, so the content really is selected — it just looks like the
keystroke did nothing.

WebKit drops the highlight as soon as either endpoint has that shape; its own
`document.execCommand('selectAll')` hits the same wall, so this is a browser bug
rather than something the selection layer can express its way out of. Chromium
drops it when both endpoints do (a document that both starts and ends with a
decorator). Firefox is unaffected. Interior element points next to the same
decorator paint fine everywhere, so only the first / last child matters — which
is why the fix is not gated on WebKit.

This PR parks a zero-size, out-of-flow `<img>` on the outside of such a boundary
decorator, giving the browser an editable inline box to canonicalize the
boundary position against. That restores the highlight, including over the
decorator itself. `position: absolute` keeps the anchor out of the inline flow
so it contributes no line box and the element's layout is unchanged — an in-flow
`<br>` or `<img>`, like the one `insertManagedLineBreak` uses for inline
decorators, would add a stray blank line here.

The anchors are reconciler scaffolding, so:

- `LexicalDOMSlot` gains `get`/`setDecoratorBoundaryAnchor`, and the leading /
  trailing boundary helpers step over the anchors so child-index math is
  untouched. A detached anchor reads as absent, so a browser evicting one can't
  make a later reconcile throw on `removeChild`.
- `LexicalReconciler` reconciles the anchors on every dirty non-inline element,
  including the root — unlike the terminating line break, which skips the root,
  and the root is exactly where select-all lands.
- `LexicalMutations` recognizes them as keyless scaffolding so a mutation flush
  does not evict them as foreign DOM.

The playground e2e helpers normalize the anchors out of `assertHTML` /
`assertSelection`, matching how they already handle the Safari linebreak img.

Closes meta#8922

## Test plan

### Before

`packages/lexical/src/__tests__/browser/Issue8922SelectAllPaint.test.ts` mounts
an editor with a block decorator on each edge, runs `$selectAll()`, and counts
pixels painted in the `::selection` colour. With the reconciler change disabled:

```
webkit    no decorators              ✓     leading ✗   trailing ✗   both ✗
chromium  no decorators              ✓     leading ✓   trailing ✓   both ✗
firefox   no decorators              ✓     leading ✓   trailing ✓   both ✓
```

Every ✗ is `expected 0 to be greater than 0` — zero highlighted pixels, matching
the report.

### After

All four cases paint in all three engines:

```
chromium  Tests  4 passed (4)
webkit    Tests  4 passed (4)
firefox   Tests  4 passed (4)
```

Also added `packages/lexical/src/__tests__/unit/Issue8922Repro.test.ts` (12
jsdom tests) pinning the DOM shape and the child-offset bookkeeping: anchors
appear on a leading / trailing block decorator and on both edges, never on an
interior or inline decorator, and are dropped when the boundary decorator moves
inward or is removed.

Full suites: unit (228 files / 4071 tests), browser-mode in chromium + webkit +
firefox, `tsc`, `flow`, eslint, prettier. E2E in chromium (182 passed) and
webkit (122 passed) across `__tests__/regression`, the slot specs (which share
`getFirstChildAnchor` with this change), the selection specs, and the
decorator-bearing specs. The full e2e suite does not fit in the dev container's
time budget, so it was not run end to end.

Co-Authored-By: Claude Opus 5 <[email protected]>
@vercel

vercel Bot commented Aug 5, 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 Aug 7, 2026 8:27pm
lexical-playground Ready Ready Preview Aug 7, 2026 8:27pm

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 Aug 5, 2026
@etrepum etrepum added extended-tests Run extended e2e tests on a PR and removed CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. labels Aug 5, 2026
@etrepum etrepum 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 Aug 5, 2026
…t against zero

The meta#8922 browser regression test asserted that no pixel matched the
`::selection` colour before running `$selectAll()`. That baseline held on the
Linux and macOS font stacks but not on Windows CI, where subpixel text
antialiasing rasterizes the decorator's label into ~16 same-hued fringe pixels
(32 with a decorator on each edge), failing the precondition on all three
decorator cases.

The anchor `<img>` is not the source — a probe across chromium / webkit /
firefox produced byte-identical counts for the srcless anchor, an
`overflow: hidden` one, a clip-path one, and one with a transparent `src`, so
it paints nothing on any engine. Only the fixture's own text does.

Compare the before / after counts against a floor between the two scales
instead: a real highlight adds thousands of pixels, antialiasing noise adds
tens, and the unfixed browsers add exactly zero. Re-verified that the test
still fails without the reconciler change — webkit on all three decorator
cases, chromium on the both-edges case — and passes with it.

Co-Authored-By: Claude Opus 5 <[email protected]>
… not a cache

Drop the `__lexicalDecoratorBoundary` private-DOM cache: a boundary anchor's
`data-lexical-decorator-boundary` attribute plus its fixed position in the
managed range (leading: the head, after the `after` boundary and any slot
containers; trailing: the very end, just inside `before`) identify it
unambiguously, so the DOM itself is the source of truth.

This removes the whole stale-cache surface — the detached-anchor recovery in
`getDecoratorBoundaryAnchor`, the cache writes in `setDecoratorBoundaryAnchor`,
and the target-keyed lookups in the mutation-observer guards, which are now a
plain attribute check shared from LexicalDOMSlot. It also makes the anchors
self-healing by construction: if the browser or the `textContent = ''`
children fast path wipes one, the next reconcile of that element simply sees
no anchor and re-parks it.

The trailing read skips the transient block cursor, which
`$updateDOMBlockCursorElement` appends after the anchor (a collapsed element
selection at the end, beside the same boundary decorator) and which can still
be in the DOM during the next reconcile.

Co-Authored-By: Claude Opus 5 <[email protected]>
…n browser tests

No test exercised the img+br managed line break that WebKit engines get when a
block's last child is an inline DecoratorNode — disabling the `webkitHack`
gate in `ElementDOMSlot.insertManagedLineBreak` passed the full browser-mode
suite on webkit and the webkit decorator e2e specs (`HorizontalRule`,
`Images`) untouched, so a regression there would ship silently. The symptom
the hack fixes involves native Safari caret behavior that headless Linux
WebKit does not reproduce, so what can be pinned portably is the mechanism:
the DOM contract of `setManagedLineBreak('decorator')` on each real engine.

Add a browser-mode test that derives the expected scaffold from the live
environment flags — the webkit instance asserts the in-flow img precedes the
terminating br (and that the img is in-flow, unlike the absolute-positioned
meta#8922 boundary anchor), while chromium / firefox assert the plain-br shape —
plus the shared invariants: 'line-break' and 'empty' last-child kinds never
get the img, and the scaffold is dropped or swapped when the decorator stops
being the last child. Verified the webkit run fails on 3 of 5 tests with the
hack disabled and passes with it.

Also assert in Issue8922Repro (jsdom, non-Safari) that the inline-decorator
managed line break carries no img there.

Co-Authored-By: Claude Opus 5 <[email protected]>
…lse result narrows nothing

isDecoratorBoundaryAnchorDOM claimed `node is HTMLImageElement`, which
lets TypeScript conclude on a false return that the value is NOT an
HTMLImageElement — untrue for any <img> without the boundary attribute.
Follow the isSlotContainerDOM pattern: intersect with a unique-symbol
brand so failure excludes only the (uninhabited) branded type, and base
it on Element since the guard checks nodeType and attribute, not the tag.

Co-Authored-By: Claude Fable 5 <[email protected]>
… from-DOM-update flag

## Description

Currently, in Safari, clicking back into text after pressing Cmd-A twice in a
document that starts with a block DecoratorNode moves the DOM caret but leaves
the Lexical selection frozen on the select-all range (reported against meta#8922's
preview deploy).

The second select-all re-applies a DOM selection the DOM already has:
$updateDOMSelection deliberately re-sets element-anchored selections (the
shape select-all produces next to a boundary decorator) and then sets
isSelectionChangeFromDOMUpdate, expecting the browser to deliver one
selectionchange event for it. WebKit fires no selectionchange for that no-op
re-set, so the flag outlives its event, and the *next* real selectionchange —
the user's click landing mid-text — is consumed by the skip-optimization in
onSelectionChange. Chromium and Firefox deliver an event either way, which is
why only Safari users saw it.

This change records the DOM boundary points the reconciler applied alongside
the flag, and only skips a selectionchange when the observed DOM selection is
those same points. A stale flag now self-heals: a user selection observed with
different points is processed normally. This also hardens the same skip against
event coalescing, where one delivered event can represent a newer selection
than the one the reconciler applied.

## Test plan

Regression test:
`packages/lexical/src/__tests__/browser/Issue8922SelectionSyncAfterSelectAll.test.ts`
builds a leading-block-decorator document, applies $selectAll twice, then
collapses the DOM selection mid-text and asserts the Lexical selection follows.

### Before

`VITEST_BROWSER=webkit pnpm run test-browser -- Issue8922SelectionSyncAfterSelectAll`

```
- Expected
+ Received

  {
-   "anchorKey": "2",
-   "anchorOffset": 3,
-   "anchorType": "text",
-   "collapsed": true,
+   "anchorKey": "root",
+   "anchorOffset": 0,
+   "anchorType": "element",
+   "collapsed": false,
  }

 Test Files  1 failed | 16 passed (17)
      Tests  1 failed | 120 passed | 2 skipped (123)
```

### After

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

```
 Test Files  34 passed (34)
      Tests  243 passed | 3 skipped (246)
```

`VITEST_BROWSER=firefox pnpm run test-browser`

```
 Test Files  17 passed (17)
      Tests  122 passed | 1 skipped (123)
```

`pnpm run test-unit`

```
 Test Files  235 passed (235)
      Tests  5002 passed | 1 skipped (5003)
```

`pnpm run tsc` passes. Verified in headless Linux WebKit/Chromium/Firefox via
the Vitest browser mode; native Safari was not exercised directly.

Co-Authored-By: Claude Fable 5 <[email protected]>
@etrepum
etrepum added this pull request to the merge queue Aug 8, 2026
Merged via the queue into meta:main with commit 455ffc7 Aug 8, 2026
44 checks passed
@etrepum etrepum mentioned this pull request Aug 31, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — 871d9665 Deployed Aug 7, 2026 by vercel[bot]
Preview – lexical-playground — 871d9665 Deployed Aug 7, 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: DecoratorNode resets the selection of all content

3 participants