From 2608ff278bd943f227b83a311fcedccae4325d94 Mon Sep 17 00:00:00 2001 From: Niklas von Hertzen Date: Tue, 13 Jul 2021 19:09:33 +0800 Subject: [PATCH 1/5] fix: iframe load to ensure images are loaded --- src/dom/document-cloner.ts | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/dom/document-cloner.ts b/src/dom/document-cloner.ts index a87221e7c..f425342d0 100644 --- a/src/dom/document-cloner.ts +++ b/src/dom/document-cloner.ts @@ -472,18 +472,15 @@ const iframeLoader = (iframe: HTMLIFrameElement): Promise => const documentClone = cloneWindow.document; - cloneWindow.onload = - iframe.onload = - documentClone.onreadystatechange = - () => { - cloneWindow.onload = iframe.onload = documentClone.onreadystatechange = null; - const interval = setInterval(() => { - if (documentClone.body.childNodes.length > 0 && documentClone.readyState === 'complete') { - clearInterval(interval); - resolve(iframe); - } - }, 50); - }; + cloneWindow.onload = iframe.onload = () => { + cloneWindow.onload = iframe.onload = null; + const interval = setInterval(() => { + if (documentClone.body.childNodes.length > 0 && documentClone.readyState === 'complete') { + clearInterval(interval); + resolve(iframe); + } + }, 50); + }; }); }; From 8546f1eaa28dbafa3d59febed8778d67b22245cb Mon Sep 17 00:00:00 2001 From: Niklas von Hertzen Date: Tue, 13 Jul 2021 20:01:43 +0800 Subject: [PATCH 2/5] fix: add image ready check --- src/dom/document-cloner.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/dom/document-cloner.ts b/src/dom/document-cloner.ts index f425342d0..3511b65eb 100644 --- a/src/dom/document-cloner.ts +++ b/src/dom/document-cloner.ts @@ -97,6 +97,8 @@ export class DocumentCloner { await documentClone.fonts.ready; } + await imagesReady(documentClone); + if (typeof onclone === 'function') { return Promise.resolve() .then(() => onclone(documentClone)) @@ -462,6 +464,25 @@ const createIFrameContainer = (ownerDocument: Document, bounds: Bounds): HTMLIFr return cloneIframeContainer; }; +const imageReady = (img: HTMLImageElement): Promise => { + return new Promise((resolve, reject) => { + if (img.complete) { + resolve(); + return; + } + if (!img.src) { + reject(); + return; + } + img.onload = resolve; + img.onerror = reject; + }); +}; + +const imagesReady = (document: HTMLDocument): Promise => { + return Promise.all([].slice.call(document.images, 0).map(imageReady)); +}; + const iframeLoader = (iframe: HTMLIFrameElement): Promise => { return new Promise((resolve, reject) => { const cloneWindow = iframe.contentWindow; From b1e30c806578008ec0ea5a6f3d3380688a229555 Mon Sep 17 00:00:00 2001 From: Niklas von Hertzen Date: Tue, 13 Jul 2021 20:20:25 +0800 Subject: [PATCH 3/5] fix: image check on apple webkit only --- src/dom/document-cloner.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/dom/document-cloner.ts b/src/dom/document-cloner.ts index 3511b65eb..621754edc 100644 --- a/src/dom/document-cloner.ts +++ b/src/dom/document-cloner.ts @@ -97,7 +97,9 @@ export class DocumentCloner { await documentClone.fonts.ready; } - await imagesReady(documentClone); + if (/(AppleWebKit)/g.test(navigator.userAgent)) { + await imagesReady(documentClone); + } if (typeof onclone === 'function') { return Promise.resolve() From d9cb4a8008f818685d4d7fbda80c87fac50fb329 Mon Sep 17 00:00:00 2001 From: Niklas von Hertzen Date: Tue, 13 Jul 2021 22:58:44 +0800 Subject: [PATCH 4/5] fix: error on failed image --- src/dom/document-cloner.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/dom/document-cloner.ts b/src/dom/document-cloner.ts index 621754edc..d7062e889 100644 --- a/src/dom/document-cloner.ts +++ b/src/dom/document-cloner.ts @@ -467,17 +467,17 @@ const createIFrameContainer = (ownerDocument: Document, bounds: Bounds): HTMLIFr }; const imageReady = (img: HTMLImageElement): Promise => { - return new Promise((resolve, reject) => { + return new Promise((resolve) => { if (img.complete) { resolve(); return; } if (!img.src) { - reject(); + resolve(); return; } img.onload = resolve; - img.onerror = reject; + img.onerror = resolve; }); }; From 26b9487f9a6688c7a92f55ebe67974f65fe34e10 Mon Sep 17 00:00:00 2001 From: Niklas von Hertzen Date: Tue, 13 Jul 2021 23:02:35 +0800 Subject: [PATCH 5/5] fix: type --- src/dom/document-cloner.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dom/document-cloner.ts b/src/dom/document-cloner.ts index d7062e889..2bf940d37 100644 --- a/src/dom/document-cloner.ts +++ b/src/dom/document-cloner.ts @@ -466,7 +466,7 @@ const createIFrameContainer = (ownerDocument: Document, bounds: Bounds): HTMLIFr return cloneIframeContainer; }; -const imageReady = (img: HTMLImageElement): Promise => { +const imageReady = (img: HTMLImageElement): Promise => { return new Promise((resolve) => { if (img.complete) { resolve();