From b00510c74a98fbc46dea13ef42afff2218ad7ea9 Mon Sep 17 00:00:00 2001 From: Sherry Date: Wed, 8 Jul 2026 21:12:14 +0800 Subject: [PATCH 1/2] [lexical] Bug Fix: $getDocument() should fall back to the global document when there is no active editor (#8813) --- packages/lexical/src/LexicalUtils.ts | 17 ++++++++++++++--- .../src/__tests__/unit/LexicalUtils.test.ts | 9 +++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/packages/lexical/src/LexicalUtils.ts b/packages/lexical/src/LexicalUtils.ts index c42862cdf96..7c7bcba1475 100644 --- a/packages/lexical/src/LexicalUtils.ts +++ b/packages/lexical/src/LexicalUtils.ts @@ -99,6 +99,7 @@ import { errorOnReadOnly, getActiveEditor, getActiveEditorState, + internalGetActiveEditor, internalGetActiveEditorState, isCurrentlyReadOnlyMode, triggerCommandListeners, @@ -2120,15 +2121,25 @@ export function getRootOwnerDocument( /** * Returns the {@link Document} that owns the active editor's root element. - * Falls back to `globalThis.document` when the editor has no root element - * (e.g. headless mode with {@link @lexical/headless!withDOM | withDOM}). + * Falls back to `globalThis.document` when there is no active editor (e.g. + * a node method such as `createDOM` / `exportDOM` is invoked headlessly, + * outside of `editor.update()` / `editor.read()`), or when the active + * editor has no root element (e.g. headless mode with + * {@link @lexical/headless!withDOM | withDOM}). * * Use this inside `createDOM`, `updateDOM`, and `exportDOM` instead of the * bare `document` global so the node works correctly when the editor lives * inside a Shadow DOM or a cross-origin `