Skip to content

dangerouslySetInnerHTML in IE11 for svg elements #10013

@OriR

Description

@OriR

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
When using dangerouslySetInnerHtml of svg elements, the DOM nodes from the previous render aren't being removed.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://jsfiddle.net or similar (template: https://jsfiddle.net/84v837e9/).

See this fiddle

What is the expected behavior?
The DOM should only contain the last rendered value for dangerouslySetInnerHTML.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
React - 15.5.4+
Browser - IE11
Did this work in previous versions of React? Not sure

After digging around I found #6982 . Which made me think this is happening because of this line in setInnerHTML.js where it looks like this

  // IE does not have innerHTML for SVG nodes, so instead we inject the
  // new markup in a temp node and then move the child nodes across into
  // the target node
  if (node.namespaceURI === DOMNamespaces.svg && !('innerHTML' in node)) {
    reusableSVGContainer =
      reusableSVGContainer || document.createElement('div');
    reusableSVGContainer.innerHTML = '<svg>' + html + '</svg>';
    var svgNode = reusableSVGContainer.firstChild;
    while (svgNode.firstChild) {
      node.appendChild(svgNode.firstChild);
    }
  }

Because there's a call to node.appendChild(svgNode.firstChild); it will never remove all of the previous nodes but only add the new ones.
But that's just a guess...

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions