Skip to content

Commit

Permalink
Short-circuit style filter steps
Browse files Browse the repository at this point in the history
  • Loading branch information
zm-cttae authored Apr 28, 2023
1 parent 9228a87 commit 59df18e
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/dom-to-image-more.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,29 +573,31 @@
// The filter op is subtractive and goes upward (to only splice out inheritable style declarations).
const walker = document.createTreeWalker(clone, NodeFilter.SHOW_ELEMENT);
const tree = [walker.currentNode];
const depths = [];
let node;
while ((node = walker.nextNode())) tree.push(node);

function getNodeDepth(node, root, depth) {
const parent = node.parentElement;
return parent === clone.parentElement ? depth : getNodeDepth(parent, root, ++depth);
}

const depths = tree.map((element) => getNodeDepth(element, clone, 1));

const pyramid = [];
let depth = Math.max.apply(Math, depths);
while (depth) {
for (let index = 0; index < tree.length; index++) {
if (depths[index] === depth) pyramid.push(tree[index]);
}
depth--;
while ((node = walker.nextNode())) {
tree.push(node);
depths.push(getNodeDepth(element, clone, 1));
}

let height = Math.max.apply(Math, depths);
let delta;
while (delta !== 0) {
delta = 0;
pyramid.forEach(filterWinningInlineStyles);
while (height !== 0) {
tree.forEach(function(node, index) {
if (depths[index] === height) {
filterWinningInlineStyles(node);
}
});
height--;
}
}

sandboxWindow.document.body.removeChild(clone);
Expand Down

1 comment on commit 59df18e

@IDisposable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noice!

Please sign in to comment.