From e385f5f94d395e23008e8947a477e388c6d6c7b0 Mon Sep 17 00:00:00 2001 From: ZunSThy Date: Wed, 16 Oct 2024 20:41:06 +0800 Subject: [PATCH] Fix error svg output when no xmlns attr --- src/js/page/ui/svg-output.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/js/page/ui/svg-output.js b/src/js/page/ui/svg-output.js index c79a5db8f..f09b1f237 100644 --- a/src/js/page/ui/svg-output.js +++ b/src/js/page/ui/svg-output.js @@ -23,12 +23,21 @@ export default class SvgOutput { } setSvg({ text, width, height }) { - // TODO: revisit this - // I would rather use blob urls, but they don't work in Firefox - // All the internal refs break. - // https://bugzilla.mozilla.org/show_bug.cgi?id=1125667 const nextLoad = this._nextLoadPromise(); - this._svgFrame.src = `data:image/svg+xml,${encodeURIComponent(text)}`; + const parser = new DOMParser(); + const doc = parser.parseFromString(text, 'image/svg+xml'); + const noNamespace = !doc.documentElement.attributes.xmlns; + if (noNamespace) { + this._svgFrame.src = `data:text/html,${encodeURIComponent( + '' + + '' + + `${text}` + + '' + )}`; + } else { + const blob = new Blob([text], { type: 'image/svg+xml' }); + this._svgFrame.src = URL.createObjectURL(blob); + } this._svgFrame.style.width = `${width}px`; this._svgFrame.style.height = `${height}px`; return nextLoad;