I would like to replace all my SVG’s <rect>
elements with foreignObject
which does work fine FireFox and Chromium-based Browser but fails in Safari.
MWE:
HTML/SVG
<!DOCTYPE html>
<html lang="en">
<body>
<svg>
<rect id="rect1" width="100" height="100" />
</svg>
</body>
</html>
JavaScript
let myRect = document.getElementById("rect1")
myRect.outerHTML = myRect.outerHTML.replace("<rect", "<foreignObject").replace("</rect>", "</foreignObject>")
Safari fails with: NoModificationAllowedError: The object can not be modified.
I know the issue is due to outerHTML
is on the HTML namespace while SVG is on XML, yet is there a way to make this work in SVG?
Thanks for any insight into this issue.
Replacing strings is very error prone
Replace the element .. after copying attributes
PS unreadable
rect.parentNode.replaceChild(obj, rect);
for IE11