From c74e0ec656899f14e0ca6602bc215730d9a6096b Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Fri, 31 Mar 2023 13:59:16 -0700 Subject: [PATCH 1/2] have watch mode parse svg message correctly --- d2cli/static/watch.js | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/d2cli/static/watch.js b/d2cli/static/watch.js index 156a2992d..71fa9436f 100644 --- a/d2cli/static/watch.js +++ b/d2cli/static/watch.js @@ -25,14 +25,9 @@ function init(reconnectDelay) { console.debug("watch websocket received data"); } if (msg.svg) { - // We could turn d2SVG into an actual SVG element and use outerHTML to fully replace it - // with the result from the renderer but unfortunately that overwrites the #d2-svg ID. - // Even if you add another line to set it afterwards. The parsing/interpretation of outerHTML must be async. - // - // There's no way around that short of parsing out the top level svg tag in the msg and - // setting innerHTML to only the actual svg innards. However then you also need to parse - // out the width, height and viewbox out of the top level SVG tag and update those manually. - d2SVG.innerHTML = msg.svg; + // we can't just set `d2SVG.innerHTML = msg.svg` need to parse this as xml not html + const parsedXML = new DOMParser().parseFromString(msg.svg, "text/xml"); + d2SVG.replaceChildren(parsedXML.documentElement); const svgEl = d2SVG.querySelector("#d2-svg"); // just use inner SVG in watch mode From f72e0ecbdd0e8147508c523a075e1dfc8f2fca8a Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Fri, 31 Mar 2023 14:17:02 -0700 Subject: [PATCH 2/2] changelog --- ci/release/changelogs/next.md | 1 + 1 file changed, 1 insertion(+) diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index a312ab5be..5651bfee0 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -7,3 +7,4 @@ #### Bugfixes ⛑️ - Fix a bug in ID parsing [#322](https://github.com/terrastruct/d2/issues/322) +- Fix a bug in watch mode parsing SVG [#1119](https://github.com/terrastruct/d2/issues/1119)