Skip to content

Commit b39654f

Browse files
committed
Validate content sent into InPortal to ensure it matches elementType
1 parent 07db592 commit b39654f

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/index.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,32 @@ class InPortal extends React.PureComponent<InPortalProps, { nodeProps: {} }> {
133133
});
134134
};
135135

136+
validateElementTypesOfChildren() {
137+
const {
138+
elementType,
139+
element: { childNodes },
140+
} = this.props.node;
141+
142+
// To support SVG and other non-html elements, every element that gets rendered
143+
// into the InPortal needs to match the portalNode's elementType.
144+
// Non-elements like text and comments are fine with any portal type.
145+
childNodes.forEach((childNode) => {
146+
if (childNode instanceof Element) {
147+
if ((elementType === ELEMENT_TYPE_HTML && !(childNode instanceof HTMLElement)) ||
148+
(elementType === ELEMENT_TYPE_SVG && !(childNode instanceof SVGElement))) {
149+
throw new Error(`Invalid content for portal: "${elementType}" portalNodes must be used with ${elementType} elements, but InPortal received <${childNode.tagName}>.`);
150+
}
151+
}
152+
})
153+
}
154+
136155
componentDidMount() {
156+
this.validateElementTypesOfChildren();
137157
this.addPropsChannel();
138158
}
139159

140160
componentDidUpdate() {
161+
this.validateElementTypesOfChildren();
141162
this.addPropsChannel();
142163
}
143164

0 commit comments

Comments
 (0)