From 737c0236b1e1aba7a16f039fc61f2e0aac0d05de Mon Sep 17 00:00:00 2001 From: chenyong Date: Thu, 14 Oct 2021 20:53:29 +0800 Subject: [PATCH] Support replacing node when filter --- src/dom-to-image.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/dom-to-image.js b/src/dom-to-image.js index 27201ac9..b07643a2 100644 --- a/src/dom-to-image.js +++ b/src/dom-to-image.js @@ -175,7 +175,17 @@ } function cloneNode(node, filter, root) { - if (!root && filter && !filter(node)) return Promise.resolve(); + if (!root && filter) { + const filterRet = filter(node) + if (!filterRet) { + // false or undefined or null + return Promise.resolve(); + } + + // if filter returns boolean true, keep node unchanged + // else we use filter result as template node to clone + node = filterRet === true ? node : filterRet; + } return Promise.resolve(node) .then(makeNodeCopy)