Skip to content

Commit

Permalink
fix CSS copying for browsers that don't support computedStyle.cssText
Browse files Browse the repository at this point in the history
  • Loading branch information
tsayen committed Apr 22, 2015
1 parent 2bdc0a9 commit 0555184
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/*.min.js -diff
68 changes: 68 additions & 0 deletions demo/firefox/firefox.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<style>
* {
box-sizing: border-box;
}

#root {
height: 100px;
width: 100px;
}

#dom-content {
border: 1px solid black;
height: 100%;
}

.red {
background-color: red;
}

.green {
background-color: lightgreen;
}

.blue {
background-color: lightblue;
}

.red, .green, .blue {
height: 33.333333%;
width: 100%;
}

.image {
width: 100px;
height: 100px;
}
</style>
<script src="jquery.js"></script>
<script src="../../src/domvas.js"></script>
</head>
<body>
<script>
exportToImage = function () {
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');

domvas.toImage(document.getElementById('root'), function (image) {
context.drawImage(image, 0, 0);
});
};
</script>
<div id="root">
<div id="dom-content">
<div class="red">red</div>
<div class="green">green</div>
<div class="blue">blue</div>
</div>
</div>
<button onclick="exportToImage()">draw</button>
<div class="image">
<canvas id="canvas" width="100" height="100"></canvas>
</div>
</body>
</html>
4 changes: 4 additions & 0 deletions demo/firefox/jquery.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/dom-to-image.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 17 additions & 17 deletions src/domvas.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
(function (global) {
"use strict";

var supportsCSSText = getComputedStyle(document.body).cssText !== "";
function copyCss(source, target) {
var sourceStyle = global.window.getComputedStyle(source);

function copyCSS(elem, origElem, log) {

var computedStyle = getComputedStyle(origElem);

if (supportsCSSText) {
elem.style.cssText = computedStyle.cssText;
} else {
// Really, Firefox?
for (var prop in computedStyle) {
if (isNaN(parseInt(prop, 10)) && typeof computedStyle[prop] !== 'function' && !(/^(cssText|length|parentRule)$/).test(prop)) {
elem.style[prop] = computedStyle[prop];
}
}
if (sourceStyle.cssText) {
target.style.cssText = sourceStyle.cssText;
return;
}

for (var i = 0; i < sourceStyle.length; i++) {
var propertyName = sourceStyle[i];
target.style.setProperty(
propertyName,
sourceStyle.getPropertyValue(propertyName),
sourceStyle.getPropertyPriority(propertyName)
);
}
}

Expand All @@ -25,11 +25,11 @@
var origChildren = origElem.querySelectorAll('*');

// copy the current style to the clone
copyCSS(elem, origElem, 1);
copyCss(origElem, elem);

// collect all nodes within the element, copy the current style to the clone
Array.prototype.forEach.call(children, function (child, i) {
copyCSS(child, origChildren[i]);
copyCss(origChildren[i], child);
});

// strip margins from the outer element
Expand Down Expand Up @@ -70,7 +70,7 @@
// when loaded, fire onload callback with actual image node
img.onload = function () {
if (callback) {
callback.call(this, this);
callback.call(img, img);
}
};
}
Expand Down
Loading

0 comments on commit 0555184

Please sign in to comment.