import {SVG_NAMESPACE} from './DOMNamespaces';
import {enableTrustedTypesIntegration} from 'shared/ReactFeatureFlags';
let reusableSVGContainer: HTMLElement;
function setInnerHTMLImpl(
node: Element,
html: {valueOf(): {toString(): string, ...}, ...},
): void {
if (node.namespaceURI === SVG_NAMESPACE) {
if (__DEV__) {
if (enableTrustedTypesIntegration) {
if (typeof trustedTypes !== 'undefined') {
console.error(
"Using 'dangerouslySetInnerHTML' in an svg element with " +
'Trusted Types enabled in an Internet Explorer will cause ' +
'the trusted value to be converted to string. Assigning string ' +
"to 'innerHTML' will throw an error if Trusted Types are enforced. " +
"You can try to wrap your svg element inside a div and use 'dangerouslySetInnerHTML' " +
'on the enclosing div instead.',
);
}
}
}
if (!('innerHTML' in node)) {
reusableSVGContainer =
reusableSVGContainer || document.createElement('div');
reusableSVGContainer.innerHTML =
'<svg>' + html.valueOf().toString() + '</svg>';
const svgNode = reusableSVGContainer.firstChild;
while (node.firstChild) {
node.removeChild(node.firstChild);
}
while (svgNode.firstChild) {
node.appendChild(svgNode.firstChild);
}
return;
}
}
node.innerHTML = (html: any);
}
let setInnerHTML: (
node: Element,
html: {valueOf(): {toString(): string, ...}, ...},
) => void = setInnerHTMLImpl;
if (typeof MSApp !== 'undefined' && MSApp.execUnsafeLocalFunction) {
setInnerHTML = function (
node: Element,
html: {valueOf(): {toString(): string, ...}, ...},
): void {
return MSApp.execUnsafeLocalFunction(function () {
return setInnerHTMLImpl(node, html);
});
};
}
export default setInnerHTML;