Skip to content

Commit

Permalink
fix: image loading="lazy" fix niklasvh#2312 (niklasvh#2314)
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasvh authored and oliveira-gust committed May 24, 2021
1 parent a7556d0 commit eb3ede1
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 @@ -135,7 +136,15 @@ export class DocumentCloner {
return this.createCustomElementClone(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;
}

// custom elements are cloned as divs so the renderer knows how to render them
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 eb3ede1

Please sign in to comment.