Skip to content

Commit 79a0924

Browse files
committed
see pr: niklasvh#3048
Squashed commit of the following: commit 8301a3b Author: Halo <[email protected]> Date: Thu Apr 6 14:41:16 2023 +0800 fix: resolve missing svg serialized content dealing with the issue of incomplete rendering content when using 'position: absolute' to render SVG
1 parent 95ea260 commit 79a0924

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/dom/replaced-elements/svg-element-container.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,26 @@ export class SVGElementContainer extends ElementContainer {
88
intrinsicHeight: number;
99

1010
constructor(context: Context, img: SVGSVGElement) {
11-
super(context, img);
12-
const s = new XMLSerializer();
13-
const bounds = parseBounds(context, img);
14-
img.setAttribute('width', `${bounds.width}px`);
15-
img.setAttribute('height', `${bounds.height}px`);
11+
super(context, img)
12+
const s = new XMLSerializer()
13+
const bounds = parseBounds(context, img)
14+
let originPosition: string = img.style.position
15+
img.setAttribute('width', `${bounds.width}px`)
16+
img.setAttribute('height', `${bounds.height}px`)
17+
18+
// fix: resolve missing svg serialized content
19+
// if svg's tag has absolute position, when converting through serializeToString, it will result in missing SVG content.
20+
// so, it is necessary to eliminate positioning before serialization.
21+
img.style.position = 'initial'
1622

17-
this.svg = `data:image/svg+xml,${encodeURIComponent(s.serializeToString(img))}`;
18-
this.intrinsicWidth = img.width.baseVal.value;
19-
this.intrinsicHeight = img.height.baseVal.value;
23+
this.svg = `data:image/svg+xml,${encodeURIComponent(s.serializeToString(img))}`
24+
25+
// reset position
26+
img.style.position = originPosition
27+
28+
this.intrinsicWidth = img.width.baseVal.value
29+
this.intrinsicHeight = img.height.baseVal.value
2030

21-
this.context.cache.addImage(this.svg);
31+
this.context.cache.addImage(this.svg)
2232
}
2333
}

0 commit comments

Comments
 (0)