Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit injected SVG styles to user cascade origin #37

Merged
merged 3 commits into from Aug 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions src/dom-to-image-more.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(function(global) {
(function(window) {
'use strict';

var util = newUtil();
Expand Down Expand Up @@ -35,7 +35,7 @@
if (typeof exports === "object" && typeof module === "object")
module.exports = domtoimage;
else
global.domtoimage = domtoimage;
window.domtoimage = domtoimage;

/**
* @param {Node} node - The DOM Node object to render
Expand Down Expand Up @@ -261,7 +261,7 @@
});

function cloneStyle() {
copyStyle(window.getComputedStyle(original), clone.style);
copyStyle(getUserComputedStyle(original, root), clone.style);

function copyFont(source, target) {
target.font = source.font;
Expand Down Expand Up @@ -842,4 +842,29 @@
}
}
}

function getUserComputedStyle(element, root) {
var computedStyles = window.getComputedStyle(element);
var inlineStyles = element.style;

for (var i = 0; i < computedStyles.length; i++) {
var key = computedStyles[i];
var value = computedStyles.getPropertyValue(key);
var inlineValue = inlineStyles.getPropertyValue(key);

if (!inlineValue.length) {
inlineStyles.setProperty(key, root ? 'initial' : 'unset');

var initialValue = computedStyles.getPropertyValue(key);

if (initialValue !== value) {
inlineStyles.setProperty(key, value);
} else {
inlineStyles.removeProperty(key);
}
}
}

return inlineStyles;
}
})(this);