From 34958f7c0632068006524fd510c8fa1aa0d179de Mon Sep 17 00:00:00 2001 From: Josh Story Date: Tue, 1 Nov 2022 17:14:46 -0700 Subject: [PATCH] refactor isHostResourceType to not receive the context from reconciler and not leak types (#25610) type validateDOMNesting move `isHostResourceType` to ReactDOMHostConfig type `AncestorInfo` refactor `resourceFormOnly` into `ancestorInfo.containerTagInScope` provide hostContext from reconciler --- .../src/client/ReactDOMFloatClient.js | 128 +------------- .../src/client/ReactDOMHostConfig.js | 157 +++++++++++++++--- .../src/client/validateDOMNesting.js | 79 ++++++--- .../react-reconciler/src/ReactFiber.new.js | 7 +- .../react-reconciler/src/ReactFiber.old.js | 7 +- 5 files changed, 206 insertions(+), 172 deletions(-) diff --git a/packages/react-dom-bindings/src/client/ReactDOMFloatClient.js b/packages/react-dom-bindings/src/client/ReactDOMFloatClient.js index 55a37d240351b..e16eeb7e6f43a 100644 --- a/packages/react-dom-bindings/src/client/ReactDOMFloatClient.js +++ b/packages/react-dom-bindings/src/client/ReactDOMFloatClient.js @@ -28,14 +28,8 @@ import { getResourcesFromRoot, markNodeAsResource, } from './ReactDOMComponentTree'; -import {HTML_NAMESPACE} from '../shared/DOMNamespaces'; -import { - getCurrentRootHostContainer, - getHostContext, -} from 'react-reconciler/src/ReactFiberHostContext'; -import {getResourceFormOnly} from './validateDOMNesting'; -import {getNamespace} from './ReactDOMHostConfig'; -import {SVG_NAMESPACE} from '../shared/DOMNamespaces'; +import {HTML_NAMESPACE, SVG_NAMESPACE} from '../shared/DOMNamespaces'; +import {getCurrentRootHostContainer} from 'react-reconciler/src/ReactFiberHostContext'; // The resource types we support. currently they match the form for the as argument. // In the future this may need to change, especially when modules / scripts are supported @@ -1426,124 +1420,6 @@ function insertResourceInstanceBefore( } } -export function isHostResourceType(type: string, props: Props): boolean { - let resourceFormOnly: boolean; - let namespace: string; - if (__DEV__) { - const hostContext = getHostContext(); - resourceFormOnly = getResourceFormOnly(hostContext); - namespace = getNamespace(hostContext); - } - switch (type) { - case 'base': - case 'meta': { - return true; - } - case 'title': { - const hostContext = getHostContext(); - return getNamespace(hostContext) !== SVG_NAMESPACE; - } - case 'link': { - const {onLoad, onError} = props; - if (onLoad || onError) { - if (__DEV__) { - if (resourceFormOnly) { - console.error( - 'Cannot render a with onLoad or onError listeners outside the main document.' + - ' Try removing onLoad={...} and onError={...} or moving it into the root tag or' + - ' somewhere in the .', - ); - } else if (namespace === SVG_NAMESPACE) { - console.error( - 'Cannot render a with onLoad or onError listeners as a descendent of .' + - ' Try removing onLoad={...} and onError={...} or moving it above the ancestor.', - ); - } - } - return false; - } - switch (props.rel) { - case 'stylesheet': { - const {href, precedence, disabled} = props; - if (__DEV__) { - validateLinkPropsForStyleResource(props); - if (typeof precedence !== 'string') { - if (resourceFormOnly) { - console.error( - 'Cannot render a outside the main document without knowing its precedence.' + - ' Consider adding precedence="default" or moving it into the root tag.', - ); - } else if (namespace === SVG_NAMESPACE) { - console.error( - 'Cannot render a as a descendent of an element without knowing its precedence.' + - ' Consider adding precedence="default" or moving it above the ancestor.', - ); - } - } - } - return ( - typeof href === 'string' && - typeof precedence === 'string' && - disabled == null - ); - } - default: { - const {rel, href} = props; - return typeof href === 'string' && typeof rel === 'string'; - } - } - } - case 'script': { - // We don't validate because it is valid to use async with onLoad/onError unlike combining - // precedence with these for style resources - const {src, async, onLoad, onError} = props; - if (__DEV__) { - if (async !== true) { - if (resourceFormOnly) { - console.error( - 'Cannot render a sync or defer tag.', - ); - } else if (namespace === SVG_NAMESPACE) { - console.error( - 'Cannot render a sync or defer