From 2818b9613fba1c1d2cdef08c646594146f2142e0 Mon Sep 17 00:00:00 2001 From: Jan Kassens Date: Tue, 15 Nov 2022 17:02:52 -0500 Subject: [PATCH] [react-float] feature detect getRootNode --- .../src/client/ReactDOMFloatClient.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/react-dom-bindings/src/client/ReactDOMFloatClient.js b/packages/react-dom-bindings/src/client/ReactDOMFloatClient.js index e16eeb7e6f43a..b09789d436fb7 100644 --- a/packages/react-dom-bindings/src/client/ReactDOMFloatClient.js +++ b/packages/react-dom-bindings/src/client/ReactDOMFloatClient.js @@ -167,9 +167,7 @@ let lastCurrentDocument: ?Document = null; let previousDispatcher = null; export function prepareToRenderResources(rootContainer: Container) { - // Flot thinks that getRootNode returns a Node but it actually returns a - // Document or ShadowRoot - const rootNode: FloatRoot = (rootContainer.getRootNode(): any); + const rootNode = getRootNode(rootContainer); lastCurrentDocument = getDocumentFromRoot(rootNode); previousDispatcher = Dispatcher.current; @@ -191,10 +189,20 @@ export type FloatRoot = Document | ShadowRoot; // global maps of Resources const preloadResources: Map = new Map(); +// getRootNode is missing from IE and old jsdom versions +function getRootNode(container: Container): FloatRoot { + // $FlowFixMe[method-unbinding] + return typeof container.getRootNode === 'function' + ? /* $FlowFixMe[incompatible-return] Flow types this as returning a `Node`, + * but it's either a `Document` or `ShadowRoot`. */ + container.getRootNode() + : container.ownerDocument; +} + function getCurrentResourceRoot(): null | FloatRoot { const currentContainer = getCurrentRootHostContainer(); // $FlowFixMe flow should know currentContainer is a Node and has getRootNode - return currentContainer ? currentContainer.getRootNode() : null; + return currentContainer ? getRootNode(currentContainer) : null; } // This resource type constraint can be loosened. It really is everything except PreloadResource @@ -204,7 +212,7 @@ function resetInstance(resource: ScriptResource | HeadResource) { } export function clearRootResources(rootContainer: Container): void { - const rootNode: FloatRoot = (rootContainer.getRootNode(): any); + const rootNode = getRootNode(rootContainer); const resources = getResourcesFromRoot(rootNode); // We can't actually delete the resource cache because this function is called