Skip to content

[lexical] Bug Fix: use ShadowRoot selection for editor roots - #8659

Closed
nujhong wants to merge 1 commit into
meta:mainfrom
KeplerBrowser:fix/shadow-root-dom-selection
Closed

nujhong wants to merge 1 commit into
meta:mainfrom
KeplerBrowser:fix/shadow-root-dom-selection

Conversation

@nujhong

@nujhong nujhong commented Jun 9, 2026

Copy link
Copy Markdown

Description

Editors mounted inside a ShadowRoot currently still read selection through window.getSelection() in several editor-scoped paths. In Chrome, that can return a selection outside the shadow tree while the actual caret is inside the editor, so updates can be applied at the wrong text position.

This adds an internal getDOMSelectionForEditor(editor) helper that prefers ShadowRoot.getSelection() when the editor root is mounted in a shadow root, then falls back to the existing window selection path. Editor-owned DOM selection reads now use that helper.

Related to #7790

Test plan

Before

Added focused unit coverage for ShadowRoot selection reads in LexicalUtils.test.ts.

After

  • pnpm vitest --project unit --no-watch packages/lexical/src/__tests__/unit/LexicalUtils.test.ts
  • pnpm exec eslint packages/lexical/src/LexicalEvents.ts packages/lexical/src/LexicalMutations.ts packages/lexical/src/LexicalSelection.ts packages/lexical/src/LexicalUpdates.ts packages/lexical/src/LexicalUtils.ts packages/lexical/src/__tests__/unit/LexicalUtils.test.ts
  • pnpm run tsc
  • pnpm run build

@meta-cla

meta-cla Bot commented Jun 9, 2026

Copy link
Copy Markdown

Hi @nujhong!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@vercel

vercel Bot commented Jun 9, 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, Comment Jun 9, 2026 4:41pm
lexical-playground Ready Ready Preview, Comment Jun 9, 2026 4:41pm

Request Review

@etrepum

etrepum commented Jun 9, 2026

Copy link
Copy Markdown
Collaborator

Shadow roots aren’t supported, this almost certainly isn’t sufficient to support them. Not really worth the churn until we have a PR with tests that adds full support, ideally without adding too much complexity (emulating selection methods for example). See also #7790

@nujhong

nujhong commented Jun 9, 2026 •

Copy link
Copy Markdown
Author

Shadow roots aren’t supported, this almost certainly isn’t sufficient to support them. Not really worth the churn until we have a PR with tests that adds full support, ideally without adding too much complexity (emulating selection methods for example). See also #7790

Hey thanks for the great library as always, I do believe this has much less coverage for what full shadow dom needs for support. I've been using the fork from #7790 for a while and just had codex to patch this as a fix for an internal bug. It seems to work fine for our use case, but anyway happy to close this!

@nujhong nujhong closed this Jun 9, 2026
@etrepum

etrepum commented Jun 9, 2026

Copy link
Copy Markdown
Collaborator

A full implementation would be worth considering but partial changes don’t really help

etrepum pushed a commit to etrepum/lexical that referenced this pull request Jun 9, 2026
…M: review hardening, library coverage, and a web component example

Deep-review follow-up to the initial DOM shadow root support, also closing
the coverage gaps relative to the earlier attempts (meta#7790, meta#8659) and the
scenarios in meta#2119, meta#6709 and meta#8125.

Core (lexical):
- getComposedStaticRange falls back to the legacy variadic
  getComposedRanges(...shadowRoots) call form used by Safari 17–18.1 before
  the spec switched to an options dictionary.
- New getDOMSelectionRange helper: a live Range resolved through shadow
  roots (getRangeAt(0) is retargeted to the host), for callers that need
  layout (getBoundingClientRect); falls back to getRangeAt(0) in the light
  DOM. Replaces the bespoke collapsed-range construction in
  $updateDOMSelection's scroll-into-view path.
- isSelectionCapturedInDecoratorInput resolves the focused element through
  shadow trees; previously decorator-input protection silently failed in a
  shadow root because document.activeElement reports the host.
- $updateDOMBlockCursorElement uses the tree-scoped activeElement so the
  block cursor renders inside a shadow root.
- $updateDOMSelection computes composed points after the early returns,
  preserving the documented deferred-selection-read optimization.
- onDocumentSelectionChange's active-editor fallback only engages when
  focus is actually inside a shadow tree, keeping light DOM behavior
  byte-identical.

@lexical/table: resolve composed points/ranges in
$fixTableSelectionForSelectedTable, the arrow-key table edge detection,
and $getTableEdgeCursorPosition.

@lexical/react: LexicalTypeaheadMenuPlugin positions the menu from
composed selection points so typeahead works in a shadow root.

Playground: getDOMRangeRect, FloatingTextFormatToolbarPlugin,
FloatingLinkEditorPlugin and TableActionMenuPlugin resolve composed
points/ranges so the floating UIs work with the Shadow DOM setting; the
dev error overlay no longer crashes on Error-less events (e.g. the benign
ResizeObserver loop warning).

Add dev-examples/shadow-dom-web-component: a framework-free
<lexical-editor> custom element with toolbar, styles and contentEditable
fully encapsulated in an open shadow root, form-associated via
ElementInternals, dispatching composed input events — the scenario from
the issues above, complementing the React example.

Add a browser regression test that drives real keyboard input (typing and
backspace) into an editor inside a web component's shadow root.

https://claude.ai/code/session_01X2Aa2rteqPsVhWs4W6DW6T
mayrang added a commit to mayrang/lexical that referenced this pull request Jun 14, 2026
… shadow roots via platform selection APIs

Based on the work in meta#8660 (etrepum's Claude agent), squashed and re-authored
per meta#8603 maintainer note ("structure commits however efficient, no need to
preserve provenance, squashed at merge anyway").

Make Lexical editors work when their contentEditable is inside a DOM ShadowRoot,
using platform APIs only — no emulation of browser selection facilities.

When a selection is inside a shadow tree the browser retargets
Selection.anchorNode/focusNode/getRangeAt to the shadow host, and
document.activeElement to the host, which previously prevented Lexical from
resolving the selection back to its nodes.

Core (lexical):
- Add getDOMShadowRoots, getDOMSelectionPoints (never-null), getActiveElement,
  getActiveElementDeep — resolve selection boundary points and focus through
  enclosing shadow roots with Selection.getComposedRanges + Selection.direction
  and (Document|ShadowRoot).activeElement.
- Route DOM->model reads (selection resolution, onSelectionChange, composition
  and mutation handlers) and the model->DOM write/diff through composed points,
  and resolve the active editor on selectionchange by descending shadow roots.
- RangeSelection.modify keeps using native Selection.modify and reads the
  result via the composed StaticRange.
- Helpers degrade to light-DOM reads when getComposedRanges is unavailable,
  so older browsers are unaffected.

@lexical/clipboard: resolve the copy guard's selection through shadow roots.

Library packages (lexical-dragon / lexical-list / lexical-extension /
lexical-react / lexical-rich-text): resolve focus and point-to-node through
shadow roots where needed.

Playground: add a "Render in Shadow DOM" setting that portals the editor into
an open shadow root, with an e2e spec covering typing, word selection,
formatting and deletion. Floating UI plugins resolve active element through
shadow roots.

Add dev-examples/shadow-dom and dev-examples/shadow-dom-web-component with
Playwright tests, and a browser unit suite (jsdom does not implement
shadow-DOM selection APIs).

Closes meta#7790
Closes meta#8125
RE meta#6709 meta#2119 meta#8659
mayrang added a commit to mayrang/lexical that referenced this pull request Jun 17, 2026
… shadow roots via platform selection APIs

Based on the work in meta#8660 (etrepum's Claude agent), squashed and re-authored
per meta#8603 maintainer note ("structure commits however efficient, no need to
preserve provenance, squashed at merge anyway").

Make Lexical editors work when their contentEditable is inside a DOM ShadowRoot,
using platform APIs only — no emulation of browser selection facilities.

When a selection is inside a shadow tree the browser retargets
Selection.anchorNode/focusNode/getRangeAt to the shadow host, and
document.activeElement to the host, which previously prevented Lexical from
resolving the selection back to its nodes.

Core (lexical):
- Add getDOMShadowRoots, getDOMSelectionPoints (never-null), getActiveElement,
  getActiveElementDeep — resolve selection boundary points and focus through
  enclosing shadow roots with Selection.getComposedRanges + Selection.direction
  and (Document|ShadowRoot).activeElement.
- Route DOM->model reads (selection resolution, onSelectionChange, composition
  and mutation handlers) and the model->DOM write/diff through composed points,
  and resolve the active editor on selectionchange by descending shadow roots.
- RangeSelection.modify keeps using native Selection.modify and reads the
  result via the composed StaticRange.
- Helpers degrade to light-DOM reads when getComposedRanges is unavailable,
  so older browsers are unaffected.

@lexical/clipboard: resolve the copy guard's selection through shadow roots.

Library packages (lexical-dragon / lexical-list / lexical-extension /
lexical-react / lexical-rich-text): resolve focus and point-to-node through
shadow roots where needed.

Playground: add a "Render in Shadow DOM" setting that portals the editor into
an open shadow root, with an e2e spec covering typing, word selection,
formatting and deletion. Floating UI plugins resolve active element through
shadow roots.

Add dev-examples/shadow-dom and dev-examples/shadow-dom-web-component with
Playwright tests, and a browser unit suite (jsdom does not implement
shadow-DOM selection APIs).

Closes meta#7790
Closes meta#8125
RE meta#6709 meta#2119 meta#8659

This branch was successfully deployed

2 active deployments
Preview – lexical — 7aa8de7b Deployed Jun 9, 2026 by vercel[bot]
Preview – lexical-playground — 7aa8de7b Deployed Jun 9, 2026 by vercel[bot]
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.

2 participants