Skip to content

[lexical-playground] Chore: Use ownerDocument and $getDocument() for shadow DOM safety - #8878

Merged
etrepum merged 2 commits into
meta:mainfrom
mayrang:chore/playground-shadow-dom-safety
Jul 30, 2026
Merged

etrepum merged 2 commits into
meta:mainfrom
mayrang:chore/playground-shadow-dom-safety

Conversation

@mayrang

@mayrang mayrang commented Jul 26, 2026

Copy link
Copy Markdown
Collaborator

Description

The playground serves as reference code for developers embedding Lexical in shadow DOM or iframes, but many files used raw document references that silently break in those contexts.

This PR converts all playground document.createElement/createTextNode/createDocumentFragment calls to $getDocument() (35 node and plugin files), scopes event listeners to ownerDocument instead of the global document, and points createPortal targets at editor.getRootElement()?.ownerDocument?.body. Event listener cleanup uses registerEventListener with cleanup refs and double-down guards to prevent leaks during drag/resize interactions.

Files intentionally left unchanged: Modal.tsx, ExcalidrawModal.tsx, FlashMessage.tsx (app-level overlays without editor context), ImagesExtension module-level constant, TestRecorderPlugin (standalone utility), ColorPicker.toHex() (canvas color utility), TweetNode Twitter widget script injection (scripts don't execute inside shadow roots), and PagesExtension :root CSS custom properties.

Test plan

  • pnpm test-unit — 3996 pass
  • pnpm test-e2e-chromium — 771 pass
  • tsc clean
  • Manual playground testing:
    • S1: Image resize — drag handles, cursor changes during drag, cursor restore on release
    • S2: Color picker — saturation area drag, hue bar drag, real-time color update
    • S3: Sticky note — header drag to move, position fixed on release
    • S4: Table cell resize — column border hover, drag to resize width
    • S5: Table action menu — cell selection menu button, menu items, outside click dismiss
    • S6: Comments — text select, inline comment UI, comment marker + sidebar
    • S7: Draggable block menu — hover + button, component picker popup, item insertion
    • S8: Dropdown menus — toolbar dropdowns open/position, item selection, outside click dismiss, scroll tracking
    • S9: Image node — insert, display, copy-paste round-trip
    • S10: Equation node — insert, render math expression
    • S11: Sticky note rendering — display check (via S3)
    • S12: Columns layout — 2/3/4 column insert, text input per column
    • S13: Poll node — insert, question + options render
    • S14: Page break — insert, <hr> display
    • S15: Emoji node — shortcut trigger (:)), conversion display
    • S16: Excalidraw node — insert, draw, save, render
    • S17: YouTube node — insert URL, iframe display
    • S18: Tweet node — insert ID, tweet render
    • S19: Mention node — @ trigger, autocomplete select, badge display
    • S20: DateTime node — insert, date display
    • S21: SlotContainer — collapsible insert, toggle expand/collapse

@vercel

vercel Bot commented Jul 26, 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 Jul 30, 2026 6:26pm
lexical-playground Ready Ready Preview Jul 30, 2026 6:26pm

Request Review

@mayrang
mayrang force-pushed the chore/playground-shadow-dom-safety branch from 4045c68 to b161e88 Compare July 27, 2026 06:57

@etrepum etrepum left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, is there anything else in-progress before it's ready for final review and merge? I only noticed a small additional simplification that could be done along the way

document.addEventListener('pointerup', handlePointerUp);
const doc = stickContainer.ownerDocument;
dragCleanupRef.current?.();
dragCleanupRef.current = mergeRegister(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could use registerEventListeners which would be a little more concise

document.addEventListener('mousemove', onMouseMove, false);
document.addEventListener('mouseup', onMouseUp, false);
dragCleanupRef.current?.();
dragCleanupRef.current = mergeRegister(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

registerEventListeners

document.addEventListener('pointerup', handlePointerUp);
const doc = image.ownerDocument;
resizeCleanupRef.current?.();
resizeCleanupRef.current = mergeRegister(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

registerEventListeners

@mayrang

mayrang commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks! I'll be back home on Thursday (KST) – will get that small tweak done and open the PR then.

@mayrang

mayrang commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator Author

Pushed — switched the three files to registerEventListeners as suggested.

@mayrang
mayrang marked this pull request as ready for review July 30, 2026 18:27
@etrepum
etrepum added this pull request to the merge queue Jul 30, 2026
Merged via the queue into meta:main with commit cad1c67 Jul 30, 2026
44 checks passed
@etrepum etrepum mentioned this pull request Aug 31, 2026

This branch was successfully deployed

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

2 participants