Skip to content

Commit fad4f83

Browse files
committed
Add ignoreElements predicate function option
1 parent e6c44af commit fad4f83

File tree

5 files changed

+16
-7
lines changed

5 files changed

+16
-7
lines changed

docs/configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ These are all of the available configuration options.
1717
| canvas | `null` | Existing `canvas` element to use as a base for drawing on
1818
| foreignObjectRendering | `false` | Whether to use ForeignObject rendering if the browser supports it
1919
| imageTimeout | `15000` | Timeout for loading an image (in milliseconds). Set to `0` to disable timeout.
20+
| ignoreElements | `(element) => false` | Predicate function which removes the matching elements from the render.
2021
| logging | `true` | Enable logging for debug purposes
2122
| 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.
2223
| removeContainer | `true` | Whether to cleanup the cloned DOM elements html2canvas creates temporarily

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"build": "rimraf dist/ && node scripts/create-reftest-list && npm run build:npm && npm run build:browser",
6666
"build:npm": "babel src/ -d dist/npm/ --plugins=dev-expression,transform-es2015-modules-commonjs && replace-in-file __VERSION__ '\"$npm_package_version\"' dist/npm/index.js",
6767
"build:browser": "webpack",
68-
"format": "prettier --single-quote --no-bracket-spacing --tab-width 4 --print-width 100 --write \"{src,www,tests,scripts}/**/*.js\"",
68+
"format": "prettier --single-quote --no-bracket-spacing --tab-width 4 --print-width 100 --write \"{src,www/src,tests,scripts}/**/*.js\"",
6969
"flow": "flow",
7070
"lint": "eslint src/**",
7171
"test": "npm run flow && npm run lint && npm run test:node && npm run karma",

src/Clone.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,12 @@ export class DocumentCloner {
268268
for (let child = node.firstChild; child; child = child.nextSibling) {
269269
if (
270270
child.nodeType !== Node.ELEMENT_NODE ||
271-
// $FlowFixMe
272-
(child.nodeName !== 'SCRIPT' && !child.hasAttribute(IGNORE_ATTRIBUTE))
271+
(child.nodeName !== 'SCRIPT' &&
272+
// $FlowFixMe
273+
!child.hasAttribute(IGNORE_ATTRIBUTE) &&
274+
(typeof this.options.ignoreElements !== 'function' ||
275+
// $FlowFixMe
276+
!this.options.ignoreElements(child)))
273277
) {
274278
if (!this.copyStyles || child.nodeName !== 'STYLE') {
275279
clone.appendChild(this.cloneNode(child));

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export type Options = {
1414
backgroundColor: string,
1515
canvas: ?HTMLCanvasElement,
1616
foreignObjectRendering: boolean,
17+
ignoreElements?: HTMLElement => boolean,
1718
imageTimeout: number,
1819
logging: boolean,
1920
proxy: ?string,

tests/reftests/options/ignore.html

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
<title>element render test</title>
55
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
66
<script>
7-
7+
h2cOptions = {ignoreElements: function(element) {
8+
return element.className === 'ignored';
9+
}};
810
</script>
911
<script type="text/javascript" src="../../test.js"></script>
1012
<style>
@@ -15,7 +17,7 @@
1517
background: green;
1618
}
1719

18-
#ignored {
20+
#ignored, .ignored {
1921
background: red;
2022
width: 100px;
2123
height: 100px;
@@ -32,10 +34,11 @@
3234
<div id="ignored" data-html2canvas-ignore>
3335
great failure
3436
</div>
37+
<div class="ignored">
38+
ignore predicate
39+
</div>
3540
<div id="div1">
3641
great success
3742
</div>
38-
39-
4043
</body>
4144
</html>

0 commit comments

Comments
 (0)