Skip to content

[lexical] Bug Fix: select-all over a leading shadow root deletes the whole widget - #8955

Closed
LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:fix/6938-select-all-shadow-root-normalization
Closed

LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:fix/6938-select-all-shadow-root-normalization

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

When a columns layout (or any shadow root) is the first node in the document, select-all followed by delete left the widget behind with empty columns. With a paragraph before it, the same gesture deleted correctly.

As @etrepum diagnosed in the issue, the cause is point normalization in $selectAll. $normalizePoint descends element points toward the deepest text point, and it descends straight through shadow roots — so a selection spanning the whole document is rewritten into one scoped inside the widget, and the deletion that follows can no longer tell "select the widget" from "select the text in the widget".

$normalizeSelection takes an opt-in stopAtShadowRoot flag, which $selectAll passes. The descent stops before entering a shadow root, so select-all keeps element points at the container level. Select-all scoped inside a single column is unaffected, since that path never crosses a shadow root.

That alone lets a select-all delete empty the root, which the existing Issue8745Repro test rightly forbids, so $removeTextFromCaretRange now restores an empty paragraph when a removal leaves the root with no children.

One thing I left out of scope: forward-Delete over a select-all still leaves the root empty, because $ensureRootHasParagraph() only runs on the backward branch of deleteCharacter. That is pre-existing and generic — a lone decorator node behaves identically on unmodified main.

Fixes #6938

@vercel

vercel Bot commented Aug 7, 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 10:50am
lexical-playground Ready Ready Preview Aug 7, 2026 10:50am

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 7, 2026
@etrepum

etrepum commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator

Has failing tests

…widget itself

A columns layout that is the only top-level node survived select-all +
delete: the widget stayed behind holding two empty layout items instead of
being removed (meta#6938). A paragraph before the widget made it work, which is
the tell.

$selectAll sets its points at the element level and then normalizes them
down towards text points. When every point descends into the same top-level
shadow root, the result stops describing "select everything" and starts
describing "select the text inside the widget" - the range never covers the
widget itself, so deleting only empties it.

$normalizeSelectionForSelectAll keeps the element-level points in exactly
that case. A selection that merely starts in a shadow root, such as a
select-all anchored in a leading table, normalizes as before: it already
extends past the widget, so the widget is covered either way, and the table
cell observer keeps seeing the cell selection that Tables.spec.mjs asserts.

getTopLevelElement stops at the nearest shadow root, which for a nested
widget is an inner scope (a layout item rather than the layout container),
so the comparison walks out to the child of the RootNode instead.

$removeTextFromCaretRange restores the empty paragraph when the removal
empties the root, matching the result of deleting a document made of
ordinary paragraphs.

Fixes meta#6938
@LeSingh1
LeSingh1 force-pushed the fix/6938-select-all-shadow-root-normalization branch from 3f19a13 to f2eafdc Compare August 8, 2026 21:54
@vercel

vercel Bot commented Aug 8, 2026

Copy link
Copy Markdown

@LeSingh1 is attempting to deploy a commit to the Meta Open Source Team on Vercel.

A member of the Team first needs to authorize it.

@LeSingh1

LeSingh1 commented Aug 8, 2026

Copy link
Copy Markdown
Contributor Author

Reworked this after CI caught a regression, and the new shape is smaller.

The first version added a stopAtShadowRoot parameter to $normalizeSelection and passed it from every $selectAll path. That was too blunt: it also stopped normalization for a select-all anchored in a leading table, so the anchor became an element point on the TableNode instead of a point inside the cell. Tables.spec.mjs → "Can delete all with range selection anchored in table" failed on expect(page.locator(".table-cell-action-button")).toBeVisible() — the comment right above that line, "The observer is active", is asserting exactly the behaviour I broke.

The distinguishing property is narrower than "the point would enter a shadow root". It is the whole range collapsing inside one top-level shadow root:

  • Columns layout as the only top-level node → both normalized points land inside the layout container → the range never covers the container → delete empties it in place. That is Bug: Columns Layout if first node deletion is incorrect #6938.
  • Table followed by a paragraph → anchor lands in the table, focus lands in the trailing paragraph → the range already extends past the table, so the table is covered either way and normalization is harmless.

So the rule now lives in $selectAll alone, as $normalizeSelectionForSelectAll: normalize, and if both points ended up under the same top-level shadow root, restore the element-level points. $normalizeSelection is back to its original signature and is no longer part of this PR.

One detail worth flagging: getTopLevelElement() stops at the nearest shadow root, so for a nested widget it returns an inner scope — a LayoutItemNode rather than the LayoutContainerNode — and the two points compare unequal. The comparison walks out to the child of the RootNode instead.

Verification:

  • Issue6938Repro.test.ts: 5 passed (3 of these fail on main).
  • packages/lexical, lexical-table, lexical-selection, lexical-playground unit, lexical-list: 1709 passed, 1 skipped.
  • Rebased onto current main.

@LeSingh1

Copy link
Copy Markdown
Contributor Author

Consolidated into #9058 with the other PRs that share this defect, per @etrepum's note on #9027 and @mayrang's on #9035. Same fix and same tests, one review.

@LeSingh1 LeSingh1 closed this Aug 10, 2026
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: Columns Layout if first node deletion is incorrect

2 participants