Skip to content

Commit 5137e5f

Browse files
committed
Render correct element
1 parent 314d26f commit 5137e5f

5 files changed

Lines changed: 21 additions & 11 deletions

File tree

build/html2canvas.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
(function(window, document, undefined){
99

10+
var html2canvasNodeAttribute = "data-html2canvas-node";
11+
1012
window.html2canvas = function(nodeList, options) {
1113
options = options || {};
1214
if (options.logging) {
@@ -17,7 +19,9 @@ window.html2canvas = function(nodeList, options) {
1719
options.async = typeof(options.async) === "undefined" ? true : options.async;
1820
options.removeContainer = typeof(options.removeContainer) === "undefined" ? true : options.removeContainer;
1921

20-
return renderDocument(document, options, window.innerWidth, window.innerHeight).then(function(canvas) {
22+
var node = ((nodeList === undefined) ? [document.documentElement] : ((nodeList.length) ? nodeList : [nodeList]))[0];
23+
node.setAttribute(html2canvasNodeAttribute, "true");
24+
return renderDocument(node.ownerDocument, options, window.innerWidth, window.innerHeight).then(function(canvas) {
2125
if (typeof(options.onrendered) === "function") {
2226
log("options.onrendered is deprecated, html2canvas returns a Promise containing the canvas");
2327
options.onrendered(canvas);
@@ -29,9 +33,10 @@ window.html2canvas = function(nodeList, options) {
2933
function renderDocument(document, options, windowWidth, windowHeight) {
3034
return createWindowClone(document, windowWidth, windowHeight, options).then(function(container) {
3135
log("Document cloned");
36+
var selector = "[" + html2canvasNodeAttribute + "='true']";
37+
document.querySelector(selector).removeAttribute(html2canvasNodeAttribute);
3238
var clonedWindow = container.contentWindow;
33-
//var element = (nodeList === undefined) ? document.body : nodeList[0];
34-
var node = clonedWindow.document.documentElement;
39+
var node = clonedWindow.document.querySelector(selector);
3540
var support = new Support();
3641
var imageLoader = new ImageLoader(options, support);
3742
var bounds = NodeParser.prototype.getBounds(node);
@@ -735,7 +740,7 @@ NodeParser.prototype.isBodyWithTransparentRoot = function(container) {
735740
};
736741

737742
NodeParser.prototype.isRootElement = function(container) {
738-
return container.node.nodeName === "HTML";
743+
return container.parent === null;
739744
};
740745

741746
NodeParser.prototype.sortStackingContexts = function(stack) {

build/html2canvas.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var html2canvasNodeAttribute = "data-html2canvas-node";
2+
13
window.html2canvas = function(nodeList, options) {
24
options = options || {};
35
if (options.logging) {
@@ -8,7 +10,9 @@ window.html2canvas = function(nodeList, options) {
810
options.async = typeof(options.async) === "undefined" ? true : options.async;
911
options.removeContainer = typeof(options.removeContainer) === "undefined" ? true : options.removeContainer;
1012

11-
return renderDocument(document, options, window.innerWidth, window.innerHeight).then(function(canvas) {
13+
var node = ((nodeList === undefined) ? [document.documentElement] : ((nodeList.length) ? nodeList : [nodeList]))[0];
14+
node.setAttribute(html2canvasNodeAttribute, "true");
15+
return renderDocument(node.ownerDocument, options, window.innerWidth, window.innerHeight).then(function(canvas) {
1216
if (typeof(options.onrendered) === "function") {
1317
log("options.onrendered is deprecated, html2canvas returns a Promise containing the canvas");
1418
options.onrendered(canvas);
@@ -20,9 +24,10 @@ window.html2canvas = function(nodeList, options) {
2024
function renderDocument(document, options, windowWidth, windowHeight) {
2125
return createWindowClone(document, windowWidth, windowHeight, options).then(function(container) {
2226
log("Document cloned");
27+
var selector = "[" + html2canvasNodeAttribute + "='true']";
28+
document.querySelector(selector).removeAttribute(html2canvasNodeAttribute);
2329
var clonedWindow = container.contentWindow;
24-
//var element = (nodeList === undefined) ? document.body : nodeList[0];
25-
var node = clonedWindow.document.documentElement;
30+
var node = clonedWindow.document.querySelector(selector);
2631
var support = new Support();
2732
var imageLoader = new ImageLoader(options, support);
2833
var bounds = NodeParser.prototype.getBounds(node);

src/nodeparser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ NodeParser.prototype.isBodyWithTransparentRoot = function(container) {
149149
};
150150

151151
NodeParser.prototype.isRootElement = function(container) {
152-
return container.node.nodeName === "HTML";
152+
return container.parent === null;
153153
};
154154

155155
NodeParser.prototype.sortStackingContexts = function(stack) {

tests/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var h2cSelector, h2cOptions;
1717
document.write(srcStart + '/src/' + html2canvas[i] + '.js?' + Math.random() + scrEnd);
1818
}
1919
window.onload = function() {
20-
h2cSelector = [document.body];
20+
h2cSelector = [document.documentElement];
2121

2222
if (window.setUp) {
2323
window.setUp();

0 commit comments

Comments
 (0)