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.  * @emails react-core
    
  8.  */
    
  9. 
    
  10. 'use strict';
    
  11. 
    
  12. const React = require('react');
    
  13. const ReactDOM = require('react-dom');
    
  14. 
    
  15. describe('dangerouslySetInnerHTML', () => {
    
  16.   describe('when the node has innerHTML property', () => {
    
  17.     it('sets innerHTML on it', () => {
    
  18.       const container = document.createElement('div');
    
  19.       const node = ReactDOM.render(
    
  20.         <div dangerouslySetInnerHTML={{__html: '<h1>Hello</h1>'}} />,
    
  21.         container,
    
  22.       );
    
  23.       expect(node.innerHTML).toBe('<h1>Hello</h1>');
    
  24.     });
    
  25.   });
    
  26. 
    
  27.   describe('when the node does not have an innerHTML property', () => {
    
  28.     let innerHTMLDescriptor;
    
  29. 
    
  30.     // In some versions of IE (TODO: which ones?) SVG nodes don't have
    
  31.     // innerHTML. To simulate this, we will take it off the Element prototype
    
  32.     // and put it onto the HTMLDivElement prototype. We expect that the logic
    
  33.     // checks for existence of innerHTML on SVG, and if one doesn't exist, falls
    
  34.     // back to using appendChild and removeChild.
    
  35. 
    
  36.     beforeEach(() => {
    
  37.       innerHTMLDescriptor = Object.getOwnPropertyDescriptor(
    
  38.         Element.prototype,
    
  39.         'innerHTML',
    
  40.       );
    
  41.       delete Element.prototype.innerHTML;
    
  42.       Object.defineProperty(
    
  43.         HTMLDivElement.prototype,
    
  44.         'innerHTML',
    
  45.         innerHTMLDescriptor,
    
  46.       );
    
  47.     });
    
  48. 
    
  49.     afterEach(() => {
    
  50.       delete HTMLDivElement.prototype.innerHTML;
    
  51.       Object.defineProperty(
    
  52.         Element.prototype,
    
  53.         'innerHTML',
    
  54.         innerHTMLDescriptor,
    
  55.       );
    
  56.     });
    
  57. 
    
  58.     // @gate !disableIEWorkarounds
    
  59.     it('sets innerHTML on it', () => {
    
  60.       const html = '<circle></circle>';
    
  61.       const container = document.createElementNS(
    
  62.         'http://www.w3.org/2000/svg',
    
  63.         'svg',
    
  64.       );
    
  65.       ReactDOM.render(
    
  66.         <g dangerouslySetInnerHTML={{__html: html}} />,
    
  67.         container,
    
  68.       );
    
  69.       const circle = container.firstChild.firstChild;
    
  70.       expect(circle.tagName).toBe('circle');
    
  71.     });
    
  72. 
    
  73.     // @gate !disableIEWorkarounds
    
  74.     it('clears previous children', () => {
    
  75.       const firstHtml = '<rect></rect>';
    
  76.       const secondHtml = '<circle></circle>';
    
  77. 
    
  78.       const container = document.createElementNS(
    
  79.         'http://www.w3.org/2000/svg',
    
  80.         'svg',
    
  81.       );
    
  82.       ReactDOM.render(
    
  83.         <g dangerouslySetInnerHTML={{__html: firstHtml}} />,
    
  84.         container,
    
  85.       );
    
  86.       const rect = container.firstChild.firstChild;
    
  87.       expect(rect.tagName).toBe('rect');
    
  88.       ReactDOM.render(
    
  89.         <g dangerouslySetInnerHTML={{__html: secondHtml}} />,
    
  90.         container,
    
  91.       );
    
  92.       const circle = container.firstChild.firstChild;
    
  93.       expect(circle.tagName).toBe('circle');
    
  94.     });
    
  95.   });
    
  96. });