Skip to content

Commit 9fb9898

Browse files
committed
Re-introduce onclone option (Fix niklasvh#1434)
1 parent 952eb4c commit 9fb9898

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

docs/configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ These are all of the available configuration options.
1919
| imageTimeout | `15000` | Timeout for loading an image (in milliseconds). Set to `0` to disable timeout.
2020
| ignoreElements | `(element) => false` | Predicate function which removes the matching elements from the render.
2121
| logging | `true` | Enable logging for debug purposes
22+
| 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.
2223
| 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.
2324
| removeContainer | `true` | Whether to cleanup the cloned DOM elements html2canvas creates temporarily
2425
| scale | `window.devicePixelRatio` | The scale to use for rendering. Defaults to the browsers device pixel ratio.

src/Clone.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -611,14 +611,21 @@ export const cloneWindow = (
611611
documentClone.documentElement.style.left = -bounds.left + 'px';
612612
documentClone.documentElement.style.position = 'absolute';
613613
}
614+
615+
const result = Promise.resolve([
616+
cloneIframeContainer,
617+
cloner.clonedReferenceElement,
618+
cloner.resourceLoader
619+
]);
620+
621+
const onclone = options.onclone;
622+
614623
return cloner.clonedReferenceElement instanceof cloneWindow.HTMLElement ||
615624
cloner.clonedReferenceElement instanceof ownerDocument.defaultView.HTMLElement ||
616625
cloner.clonedReferenceElement instanceof HTMLElement
617-
? Promise.resolve([
618-
cloneIframeContainer,
619-
cloner.clonedReferenceElement,
620-
cloner.resourceLoader
621-
])
626+
? typeof onclone === 'function'
627+
? Promise.resolve().then(() => onclone(documentClone)).then(() => result)
628+
: result
622629
: Promise.reject(
623630
__DEV__
624631
? `Error finding the ${referenceElement.nodeName} in the cloned document`

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export type Options = {
1717
ignoreElements?: HTMLElement => boolean,
1818
imageTimeout: number,
1919
logging: boolean,
20+
onclone?: Document => void,
2021
proxy: ?string,
2122
removeContainer: ?boolean,
2223
scale: number,

tests/reftests/options/onclone.html

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>element render test</title>
5+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
6+
<script>
7+
h2cOptions = {onclone: function(document) {
8+
const remove = document.querySelector('.ignored');
9+
remove.parentNode.removeChild(remove);
10+
}};
11+
</script>
12+
<script type="text/javascript" src="../../test.js"></script>
13+
<style>
14+
#div1 {
15+
16+
width: 100px;
17+
height: 100px;
18+
background: green;
19+
}
20+
21+
#ignored, .ignored {
22+
background: red;
23+
width: 100px;
24+
height: 100px;
25+
}
26+
27+
body, html {
28+
margin: 0;
29+
padding: 0;
30+
}
31+
</style>
32+
33+
</head>
34+
<body>
35+
<div class="ignored">
36+
ignore during onclone
37+
</div>
38+
<div id="div1">
39+
great success
40+
</div>
41+
</body>
42+
</html>

0 commit comments

Comments
 (0)