Skip to content

Commit c343f80

Browse files
authored
[react-float] feature detect getRootNode (#25689)
Some old environments like IE11 or very old versions of jsdom are missing `getRootNode()`. Use feature detection to fall back to `ownerDocuments` in these environments that also won't be supporting shadow DOM anyway.
1 parent e1dd0a2 commit c343f80

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

packages/react-dom-bindings/src/client/ReactDOMFloatClient.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,7 @@ let lastCurrentDocument: ?Document = null;
167167

168168
let previousDispatcher = null;
169169
export function prepareToRenderResources(rootContainer: Container) {
170-
// Flot thinks that getRootNode returns a Node but it actually returns a
171-
// Document or ShadowRoot
172-
const rootNode: FloatRoot = (rootContainer.getRootNode(): any);
170+
const rootNode = getRootNode(rootContainer);
173171
lastCurrentDocument = getDocumentFromRoot(rootNode);
174172

175173
previousDispatcher = Dispatcher.current;
@@ -191,10 +189,20 @@ export type FloatRoot = Document | ShadowRoot;
191189
// global maps of Resources
192190
const preloadResources: Map<string, PreloadResource> = new Map();
193191

192+
// getRootNode is missing from IE and old jsdom versions
193+
function getRootNode(container: Container): FloatRoot {
194+
// $FlowFixMe[method-unbinding]
195+
return typeof container.getRootNode === 'function'
196+
? /* $FlowFixMe[incompatible-return] Flow types this as returning a `Node`,
197+
* but it's either a `Document` or `ShadowRoot`. */
198+
container.getRootNode()
199+
: container.ownerDocument;
200+
}
201+
194202
function getCurrentResourceRoot(): null | FloatRoot {
195203
const currentContainer = getCurrentRootHostContainer();
196204
// $FlowFixMe flow should know currentContainer is a Node and has getRootNode
197-
return currentContainer ? currentContainer.getRootNode() : null;
205+
return currentContainer ? getRootNode(currentContainer) : null;
198206
}
199207

200208
// This resource type constraint can be loosened. It really is everything except PreloadResource
@@ -204,7 +212,7 @@ function resetInstance(resource: ScriptResource | HeadResource) {
204212
}
205213

206214
export function clearRootResources(rootContainer: Container): void {
207-
const rootNode: FloatRoot = (rootContainer.getRootNode(): any);
215+
const rootNode = getRootNode(rootContainer);
208216
const resources = getResourcesFromRoot(rootNode);
209217

210218
// We can't actually delete the resource cache because this function is called

0 commit comments

Comments
 (0)