From 8350c1df850933396dd450789bd3e10a1f7f0d5a Mon Sep 17 00:00:00 2001 From: hocine BENKHADRA Date: Sat, 27 Mar 2021 08:35:07 +0100 Subject: [PATCH] fix(NodeContainer) : fix issue with responsive SVG for image generation. --- src/NodeContainer.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/NodeContainer.js b/src/NodeContainer.js index f233a38..eb86353 100644 --- a/src/NodeContainer.js +++ b/src/NodeContainer.js @@ -273,6 +273,23 @@ const getImage = (node: HTMLElement | SVGSVGElement, resourceLoader: ResourceLoa node instanceof node.ownerDocument.defaultView.SVGSVGElement || node instanceof SVGSVGElement ) { + const percentValueTest: RegExp = /^[0-9]{1,3}\%$/i; + if ( + percentValueTest.test(node.getAttribute('width')) || + percentValueTest.test(node.getAttribute('height')) || + percentValueTest.test(node.style.width) || + percentValueTest.test(node.style.height) + ) { + if (!node.clientWidth && !node.clientHeight && node.parentNode) { + const parentStyle = window.getComputedStyle(node.parentNode); + node.setAttribute('width', parentStyle.width); + node.setAttribute('height', parentStyle.height); + } else { + node.setAttribute('width', node.clientWidth); + node.setAttribute('height', node.clientHeight); + } + } + const s = new XMLSerializer(); return resourceLoader.loadImage( `data:image/svg+xml,${encodeURIComponent(s.serializeToString(node))}`