1. /**
    
  2.  * Copyright (c) Meta Platforms, Inc. and affiliates.
    
  3.  *
    
  4.  * This source code is licensed under the MIT license found in the
    
  5.  * LICENSE file in the root directory of this source tree.
    
  6.  *
    
  7.  * @flow
    
  8.  */
    
  9. 
    
  10. /* globals MSApp */
    
  11. 
    
  12. import {SVG_NAMESPACE} from './DOMNamespaces';
    
  13. import {enableTrustedTypesIntegration} from 'shared/ReactFeatureFlags';
    
  14. 
    
  15. // SVG temp container for IE lacking innerHTML
    
  16. let reusableSVGContainer: HTMLElement;
    
  17. 
    
  18. function setInnerHTMLImpl(
    
  19.   node: Element,
    
  20.   html: {valueOf(): {toString(): string, ...}, ...},
    
  21. ): void {
    
  22.   if (node.namespaceURI === SVG_NAMESPACE) {
    
  23.     if (__DEV__) {
    
  24.       if (enableTrustedTypesIntegration) {
    
  25.         // TODO: reconsider the text of this warning and when it should show
    
  26.         // before enabling the feature flag.
    
  27.         if (typeof trustedTypes !== 'undefined') {
    
  28.           console.error(
    
  29.             "Using 'dangerouslySetInnerHTML' in an svg element with " +
    
  30.               'Trusted Types enabled in an Internet Explorer will cause ' +
    
  31.               'the trusted value to be converted to string. Assigning string ' +
    
  32.               "to 'innerHTML' will throw an error if Trusted Types are enforced. " +
    
  33.               "You can try to wrap your svg element inside a div and use 'dangerouslySetInnerHTML' " +
    
  34.               'on the enclosing div instead.',
    
  35.           );
    
  36.         }
    
  37.       }
    
  38.     }
    
  39.     if (!('innerHTML' in node)) {
    
  40.       // IE does not have innerHTML for SVG nodes, so instead we inject the
    
  41.       // new markup in a temp node and then move the child nodes across into
    
  42.       // the target node
    
  43.       reusableSVGContainer =
    
  44.         reusableSVGContainer || document.createElement('div');
    
  45.       reusableSVGContainer.innerHTML =
    
  46.         '<svg>' + html.valueOf().toString() + '</svg>';
    
  47.       const svgNode = reusableSVGContainer.firstChild;
    
  48.       while (node.firstChild) {
    
  49.         node.removeChild(node.firstChild);
    
  50.       }
    
  51.       // $FlowFixMe[incompatible-use]
    
  52.       // $FlowFixMe[incompatible-type]
    
  53.       while (svgNode.firstChild) {
    
  54.         node.appendChild(svgNode.firstChild);
    
  55.       }
    
  56.       return;
    
  57.     }
    
  58.   }
    
  59.   node.innerHTML = (html: any);
    
  60. }
    
  61. 
    
  62. let setInnerHTML: (
    
  63.   node: Element,
    
  64.   html: {valueOf(): {toString(): string, ...}, ...},
    
  65. ) => void = setInnerHTMLImpl;
    
  66. // $FlowFixMe[cannot-resolve-name]
    
  67. if (typeof MSApp !== 'undefined' && MSApp.execUnsafeLocalFunction) {
    
  68.   /**
    
  69.    * Create a function which has 'unsafe' privileges (required by windows8 apps)
    
  70.    */
    
  71.   setInnerHTML = function (
    
  72.     node: Element,
    
  73.     html: {valueOf(): {toString(): string, ...}, ...},
    
  74.   ): void {
    
  75.     // $FlowFixMe[cannot-resolve-name]
    
  76.     return MSApp.execUnsafeLocalFunction(function () {
    
  77.       return setInnerHTMLImpl(node, html);
    
  78.     });
    
  79.   };
    
  80. }
    
  81. 
    
  82. export default setInnerHTML;