Skip to content

Commit

Permalink
Clean up root handling and skip sandbox
Browse files Browse the repository at this point in the history
Root is detected when `cloneNode` is passed `null` for  `parentComputedStyles`
Remove spurious `util.delay(0) ` in rendering chain
Hoisted `sandbox` up higher and don't ever clone the sandbox
  • Loading branch information
IDisposable committed Jan 12, 2023
1 parent 8a80c31 commit 3ed4ef5
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions src/dom-to-image-more.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@
copyOptions(options);
return Promise.resolve(node)
.then(function (clonee) {
const root = true;
return cloneNode(clonee, options.filter, root, null, ownerWindow);
return cloneNode(clonee, options.filter, null, ownerWindow);
})
.then(embedFonts)
.then(inlineImages)
Expand Down Expand Up @@ -206,7 +205,6 @@
options = options || {};
return toSvg(domNode, options)
.then(util.makeImage)
.then(util.delay(0))
.then(function (image) {
const scale = typeof options.scale !== 'number' ? 1 : options.scale;
const canvas = newCanvas(domNode, scale);
Expand Down Expand Up @@ -237,8 +235,10 @@
}
}

function cloneNode(node, filter, root, parentComputedStyles, ownerWindow) {
if (!root && filter && !filter(node)) {
let sandbox = null;

function cloneNode(node, filter, parentComputedStyles, ownerWindow) {
if (node === sandbox || (parentComputedStyles !== null && filter && !filter(node))) {
return Promise.resolve();
}

Expand All @@ -259,6 +259,7 @@

function cloneChildren(original, clone) {
const children = original.childNodes;

if (children.length === 0) {
return Promise.resolve(clone);
}
Expand All @@ -273,13 +274,7 @@
childs.forEach(function (child) {
done = done
.then(function () {
return cloneNode(
child,
filter,
false,
computedStyles,
ownerWindow
);
return cloneNode(child, filter, computedStyles, ownerWindow);
})
.then(function (childClone) {
if (childClone) {
Expand Down Expand Up @@ -337,8 +332,8 @@
targetElement
);

// Remove positioning of root elements, which stops them from being captured correctly
if (root) {
// Remove positioning of initial element, which stops them from being captured correctly
if (parentComputedStyles === null) {
[
'inset-block',
'inset-block-start',
Expand Down Expand Up @@ -994,7 +989,6 @@
}

let removeDefaultStylesTimeoutId = null;
let sandbox = null;
let tagNameDefaultStyles = {};

function getDefaultStyle(tagName) {
Expand Down

0 comments on commit 3ed4ef5

Please sign in to comment.