From f3e17c90d380e4ae7bf6c441b7a28f3d6a1383a3 Mon Sep 17 00:00:00 2001 From: David Luzar Date: Fri, 15 Apr 2022 12:05:10 +0200 Subject: [PATCH] fix: ensure svg image dimensions are always set (#5044) --- src/element/image.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/element/image.ts b/src/element/image.ts index 17ba245e10..bd9bcd6270 100644 --- a/src/element/image.ts +++ b/src/element/image.ts @@ -106,6 +106,20 @@ export const normalizeSVG = async (SVGString: string) => { svg.setAttribute("xmlns", SVG_NS); } + if (!svg.hasAttribute("width") || !svg.hasAttribute("height")) { + const viewBox = svg.getAttribute("viewBox"); + let width = svg.getAttribute("width") || "50"; + let height = svg.getAttribute("height") || "50"; + if (viewBox) { + const match = viewBox.match(/\d+ +\d+ +(\d+) +(\d+)/); + if (match) { + [, width, height] = match; + } + } + svg.setAttribute("width", width); + svg.setAttribute("height", height); + } + return svg.outerHTML; } };