Skip to content

Commit

Permalink
see pr: niklasvh#3048
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nangelina committed Feb 9, 2024
1 parent 95ea260 commit 79a0924
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/dom/replaced-elements/svg-element-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,26 @@ export class SVGElementContainer extends ElementContainer {
intrinsicHeight: number;

constructor(context: Context, img: SVGSVGElement) {
super(context, img);
const s = new XMLSerializer();
const bounds = parseBounds(context, img);
img.setAttribute('width', `${bounds.width}px`);
img.setAttribute('height', `${bounds.height}px`);
super(context, img)
const s = new XMLSerializer()
const bounds = parseBounds(context, img)
let originPosition: string = img.style.position
img.setAttribute('width', `${bounds.width}px`)
img.setAttribute('height', `${bounds.height}px`)

// fix: resolve missing svg serialized content
// if svg's tag has absolute position, when converting through serializeToString, it will result in missing SVG content.
// so, it is necessary to eliminate positioning before serialization.
img.style.position = 'initial'

this.svg = `data:image/svg+xml,${encodeURIComponent(s.serializeToString(img))}`;
this.intrinsicWidth = img.width.baseVal.value;
this.intrinsicHeight = img.height.baseVal.value;
this.svg = `data:image/svg+xml,${encodeURIComponent(s.serializeToString(img))}`

// reset position
img.style.position = originPosition

this.intrinsicWidth = img.width.baseVal.value
this.intrinsicHeight = img.height.baseVal.value

this.context.cache.addImage(this.svg);
this.context.cache.addImage(this.svg)
}
}

0 comments on commit 79a0924

Please sign in to comment.