[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.
Josh Story committed
Feb 6, 2025 at 14:30 UTC
b48e739998432fc9672a42d0d04515980b8cae82
1 file changed
+6
-3
packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js
+6
-3
@@ -2523,10 +2523,13 @@ export type HoistableRoot = Document | ShadowRoot;
2523
export function getHoistableRoot(container: Container): HoistableRoot {
2524
// $FlowFixMe[method-unbinding]
2525
return typeof container.getRootNode === 'function'
2526
- ? /* $FlowFixMe[incompatible-return] Flow types this as returning a `Node`,
2526
+ ? /* $FlowFixMe[incompatible-cast] Flow types this as returning a `Node`,
2527
* but it's either a `Document` or `ShadowRoot`. */
2528
- container.getRootNode()
2529
- : container.ownerDocument;
2528
+ (container.getRootNode(): Document | ShadowRoot)
2529
+ : container.nodeType === DOCUMENT_NODE
2530
+ ? // $FlowFixMe[incompatible-cast] We've constrained this to be a Document which satisfies the return type
2531
+ (container: Document)
2532
+ : container.ownerDocument;
2533
}
2534
2535
function getCurrentResourceRoot(): null | HoistableRoot {