Repository navigation
[lexical] Bug Fix: keep the select-all highlight visible next to boundary decorators - #8931
Merged
Merged
Conversation
…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]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
…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
marked this pull request as ready for review
August 7, 2026 20:26
etrepum
requested review from
acywatson,
fantactuka,
ivailop7,
potatowagon and
zurfyx
as code owners
August 7, 2026 20:26
ivailop7
approved these changes
Aug 8, 2026
Merged
This was referenced Sep 9, 2026
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-levelcontenteditable=falsechild, 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 ownexecCommand('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: absolutekeeps 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.LexicalDOMSlotgainsget/setDecoratorBoundaryAnchorand steps over the anchors so child-index math is untouched;LexicalReconcilerreconciles them on every dirty non-inline element including the root (where select-all lands);LexicalMutationsrecognizes them as keyless scaffolding so a flush doesn't evict them as foreign DOM. The playground e2e helpers normalize the anchors out ofassertHTML/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;
$updateDOMSelectiondeliberately re-sets element-anchored selections and then marksisSelectionChangeFromDOMUpdate, but WebKit fires noselectionchangefor the no-op re-set, so the stale flag swallowed the next real event via the skip-optimization inonSelectionChange. 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 againstselectionchangecoalescing.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.tscounts pixels painted in the::selectioncolour before/after$selectAll()(a real highlight adds thousands; unfixed browsers add exactly zero). With the reconciler change disabled:Issue8922SelectionSyncAfterSelectAll.test.ts(select-all twice, then collapse the DOM selection mid-text) without the selection-sync fix, on webkit:WebkitLinebreakImg.test.tslikewise 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:
Unit suite (
pnpm run test-unit, includesIssue8922Repro.test.tspinning anchor DOM shape and child-offset bookkeeping):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.