From b48e739998432fc9672a42d0d04515980b8cae82 Mon Sep 17 00:00:00 2001 From: Josh Story Date: Thu, 6 Feb 2025 14:30:41 -0800 Subject: [PATCH] [Fiber] `getHoistableRoot()` should account for Document containers (#32321) While modern DOM implementations all support getRootNode if you are running React in a runtime which does not the fallback logic which uses `.ownerDocument` works everywhere except when the container is a Document itself. This change corrects this by returning the container intsance if it is a Document type. --- .../react-dom-bindings/src/client/ReactFiberConfigDOM.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js b/packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js index 90c3855379fdc..012f7cd257f24 100644 --- a/packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js +++ b/packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js @@ -2523,10 +2523,13 @@ export type HoistableRoot = Document | ShadowRoot; export function getHoistableRoot(container: Container): HoistableRoot { // $FlowFixMe[method-unbinding] return typeof container.getRootNode === 'function' - ? /* $FlowFixMe[incompatible-return] Flow types this as returning a `Node`, + ? /* $FlowFixMe[incompatible-cast] Flow types this as returning a `Node`, * but it's either a `Document` or `ShadowRoot`. */ - container.getRootNode() - : container.ownerDocument; + (container.getRootNode(): Document | ShadowRoot) + : container.nodeType === DOCUMENT_NODE + ? // $FlowFixMe[incompatible-cast] We've constrained this to be a Document which satisfies the return type + (container: Document) + : container.ownerDocument; } function getCurrentResourceRoot(): null | HoistableRoot {