From ff7753e1d0ef332128d7fbb5d2e6336d65ad8718 Mon Sep 17 00:00:00 2001 From: MoyuScript Date: Thu, 15 Feb 2018 21:40:34 +0800 Subject: [PATCH] Re-introduce onclone option (Fix #1434) --- docs/configuration.md | 1 + src/Clone.js | 17 ++++++++---- src/index.js | 1 + tests/reftests/options/onclone.html | 42 +++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 tests/reftests/options/onclone.html diff --git a/docs/configuration.md b/docs/configuration.md index 8b2ecd1..88a61d8 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -19,6 +19,7 @@ These are all of the available configuration options. | imageTimeout | `15000` | Timeout for loading an image (in milliseconds). Set to `0` to disable timeout. | ignoreElements | `(element) => false` | Predicate function which removes the matching elements from the render. | logging | `true` | Enable logging for debug purposes +| onclone | `null` | Callback function which is called when the Document has been cloned for rendering, can be used to modify the contents that will be rendered without affecting the original source document. | proxy | `null` | Url to the [proxy](/proxy/) which is to be used for loading cross-origin images. If left empty, cross-origin images won't be loaded. | removeContainer | `true` | Whether to cleanup the cloned DOM elements html2canvas creates temporarily | scale | `window.devicePixelRatio` | The scale to use for rendering. Defaults to the browsers device pixel ratio. diff --git a/src/Clone.js b/src/Clone.js index d216956..018e6dc 100644 --- a/src/Clone.js +++ b/src/Clone.js @@ -611,14 +611,21 @@ export const cloneWindow = ( documentClone.documentElement.style.left = -bounds.left + 'px'; documentClone.documentElement.style.position = 'absolute'; } + + const result = Promise.resolve([ + cloneIframeContainer, + cloner.clonedReferenceElement, + cloner.resourceLoader + ]); + + const onclone = options.onclone; + return cloner.clonedReferenceElement instanceof cloneWindow.HTMLElement || cloner.clonedReferenceElement instanceof ownerDocument.defaultView.HTMLElement || cloner.clonedReferenceElement instanceof HTMLElement - ? Promise.resolve([ - cloneIframeContainer, - cloner.clonedReferenceElement, - cloner.resourceLoader - ]) + ? typeof onclone === 'function' + ? Promise.resolve().then(() => onclone(documentClone)).then(() => result) + : result : Promise.reject( __DEV__ ? `Error finding the ${referenceElement.nodeName} in the cloned document` diff --git a/src/index.js b/src/index.js index 0acc380..ef70a06 100644 --- a/src/index.js +++ b/src/index.js @@ -17,6 +17,7 @@ export type Options = { ignoreElements?: HTMLElement => boolean, imageTimeout: number, logging: boolean, + onclone?: Document => void, proxy: ?string, removeContainer: ?boolean, scale: number, diff --git a/tests/reftests/options/onclone.html b/tests/reftests/options/onclone.html new file mode 100644 index 0000000..4770036 --- /dev/null +++ b/tests/reftests/options/onclone.html @@ -0,0 +1,42 @@ + + + + element render test + + + + + + + +
+ ignore during onclone +
+
+ great success +
+ +