Skip to content

Commit

Permalink
fix: image loading="lazy" fix #2312
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasvh committed Aug 8, 2020
1 parent 04e145b commit 8330e20
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/dom/document-cloner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
isElementNode,
isHTMLElementNode,
isIFrameElement,
isImageElement,
isScriptElement,
isSelectElement,
isStyleElement,
Expand Down Expand Up @@ -128,7 +129,15 @@ export class DocumentCloner {
return this.createStyleClone(node);
}

return node.cloneNode(false) as HTMLElement;
const clone = node.cloneNode(false) as HTMLElement;

// @ts-ignore
if (isImageElement(clone) && clone.loading === 'lazy') {
// @ts-ignore
clone.loading = 'eager';
}

return clone;
}

createStyleClone(node: HTMLStyleElement): HTMLStyleElement {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const html2canvas = (element: HTMLElement, options: Partial<Options> = {}): Prom

export default html2canvas;

if (typeof window !== "undefined") {
if (typeof window !== 'undefined') {
CacheStorage.setContext(window);
}

Expand Down
1 change: 1 addition & 0 deletions tests/reftests/images/images.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</head>
<body>
<img src="../../assets/image.jpg" />
<img src="../../assets/image.jpg" loading="lazy" />
<img src="../../assets/image.jpg" style="width:50px;height:400px;" />
<img src="../../assets/image.jpg" style="width:500px;" />
<img src="../../assets/image.jpg" style="width:100px;border-radius:50px;" />
Expand Down

0 comments on commit 8330e20

Please sign in to comment.