diff --git a/packages/sprotty/src/features/export/svg-exporter.ts b/packages/sprotty/src/features/export/svg-exporter.ts index 8439726..ba21e6d 100644 --- a/packages/sprotty/src/features/export/svg-exporter.ts +++ b/packages/sprotty/src/features/export/svg-exporter.ts @@ -50,12 +50,20 @@ export class SvgExporter { export(root: SModelRootImpl, request?: RequestAction): void { if (typeof document !== 'undefined') { - const div = document.getElementById(this.options.hiddenDiv); - if (div !== null && div.firstElementChild && div.firstElementChild.tagName === 'svg') { - const svgElement = div.firstElementChild as SVGSVGElement; - const svg = this.createSvg(svgElement, root); - this.actionDispatcher.dispatch(ExportSvgAction.create(svg, request ? request.requestId : '')); + const hiddenDiv = document.getElementById(this.options.hiddenDiv); + if (hiddenDiv === null) { + this.log.warn(this, `Element with id ${this.options.hiddenDiv} not found. Nothing to export.`); + return; + + } + + const svgElement = hiddenDiv.querySelector('svg'); + if (svgElement === null) { + this.log.warn(this, `No svg element found in ${this.options.hiddenDiv} div. Nothing to export.`); + return; } + const svg = this.createSvg(svgElement, root); + this.actionDispatcher.dispatch(ExportSvgAction.create(svg, request ? request.requestId : '')); } } @@ -70,7 +78,7 @@ export class SvgExporter { docCopy.open(); docCopy.write(svgCopy); docCopy.close(); - const svgElementNew = docCopy.getElementById(svgElementOrig.id)!; + const svgElementNew = docCopy.querySelector('svg')!; svgElementNew.removeAttribute('opacity'); // inline-size copied from sprotty-hidden svg shrinks the svg so it is not visible. this.copyStyles(svgElementOrig, svgElementNew, ['width', 'height', 'opacity', 'inline-size']);