Render correct element

This commit is contained in:
Niklas von Hertzen 2014-02-23 17:02:49 +02:00
parent 314d26f1f1
commit 5137e5f35a
5 changed files with 21 additions and 11 deletions

View File

@ -7,6 +7,8 @@
(function(window, document, undefined){ (function(window, document, undefined){
var html2canvasNodeAttribute = "data-html2canvas-node";
window.html2canvas = function(nodeList, options) { window.html2canvas = function(nodeList, options) {
options = options || {}; options = options || {};
if (options.logging) { if (options.logging) {
@ -17,7 +19,9 @@ window.html2canvas = function(nodeList, options) {
options.async = typeof(options.async) === "undefined" ? true : options.async; options.async = typeof(options.async) === "undefined" ? true : options.async;
options.removeContainer = typeof(options.removeContainer) === "undefined" ? true : options.removeContainer; options.removeContainer = typeof(options.removeContainer) === "undefined" ? true : options.removeContainer;
return renderDocument(document, options, window.innerWidth, window.innerHeight).then(function(canvas) { var node = ((nodeList === undefined) ? [document.documentElement] : ((nodeList.length) ? nodeList : [nodeList]))[0];
node.setAttribute(html2canvasNodeAttribute, "true");
return renderDocument(node.ownerDocument, options, window.innerWidth, window.innerHeight).then(function(canvas) {
if (typeof(options.onrendered) === "function") { if (typeof(options.onrendered) === "function") {
log("options.onrendered is deprecated, html2canvas returns a Promise containing the canvas"); log("options.onrendered is deprecated, html2canvas returns a Promise containing the canvas");
options.onrendered(canvas); options.onrendered(canvas);
@ -29,9 +33,10 @@ window.html2canvas = function(nodeList, options) {
function renderDocument(document, options, windowWidth, windowHeight) { function renderDocument(document, options, windowWidth, windowHeight) {
return createWindowClone(document, windowWidth, windowHeight, options).then(function(container) { return createWindowClone(document, windowWidth, windowHeight, options).then(function(container) {
log("Document cloned"); log("Document cloned");
var selector = "[" + html2canvasNodeAttribute + "='true']";
document.querySelector(selector).removeAttribute(html2canvasNodeAttribute);
var clonedWindow = container.contentWindow; var clonedWindow = container.contentWindow;
//var element = (nodeList === undefined) ? document.body : nodeList[0]; var node = clonedWindow.document.querySelector(selector);
var node = clonedWindow.document.documentElement;
var support = new Support(); var support = new Support();
var imageLoader = new ImageLoader(options, support); var imageLoader = new ImageLoader(options, support);
var bounds = NodeParser.prototype.getBounds(node); var bounds = NodeParser.prototype.getBounds(node);
@ -735,7 +740,7 @@ NodeParser.prototype.isBodyWithTransparentRoot = function(container) {
}; };
NodeParser.prototype.isRootElement = function(container) { NodeParser.prototype.isRootElement = function(container) {
return container.node.nodeName === "HTML"; return container.parent === null;
}; };
NodeParser.prototype.sortStackingContexts = function(stack) { NodeParser.prototype.sortStackingContexts = function(stack) {

File diff suppressed because one or more lines are too long

View File

@ -1,3 +1,5 @@
var html2canvasNodeAttribute = "data-html2canvas-node";
window.html2canvas = function(nodeList, options) { window.html2canvas = function(nodeList, options) {
options = options || {}; options = options || {};
if (options.logging) { if (options.logging) {
@ -8,7 +10,9 @@ window.html2canvas = function(nodeList, options) {
options.async = typeof(options.async) === "undefined" ? true : options.async; options.async = typeof(options.async) === "undefined" ? true : options.async;
options.removeContainer = typeof(options.removeContainer) === "undefined" ? true : options.removeContainer; options.removeContainer = typeof(options.removeContainer) === "undefined" ? true : options.removeContainer;
return renderDocument(document, options, window.innerWidth, window.innerHeight).then(function(canvas) { var node = ((nodeList === undefined) ? [document.documentElement] : ((nodeList.length) ? nodeList : [nodeList]))[0];
node.setAttribute(html2canvasNodeAttribute, "true");
return renderDocument(node.ownerDocument, options, window.innerWidth, window.innerHeight).then(function(canvas) {
if (typeof(options.onrendered) === "function") { if (typeof(options.onrendered) === "function") {
log("options.onrendered is deprecated, html2canvas returns a Promise containing the canvas"); log("options.onrendered is deprecated, html2canvas returns a Promise containing the canvas");
options.onrendered(canvas); options.onrendered(canvas);
@ -20,9 +24,10 @@ window.html2canvas = function(nodeList, options) {
function renderDocument(document, options, windowWidth, windowHeight) { function renderDocument(document, options, windowWidth, windowHeight) {
return createWindowClone(document, windowWidth, windowHeight, options).then(function(container) { return createWindowClone(document, windowWidth, windowHeight, options).then(function(container) {
log("Document cloned"); log("Document cloned");
var selector = "[" + html2canvasNodeAttribute + "='true']";
document.querySelector(selector).removeAttribute(html2canvasNodeAttribute);
var clonedWindow = container.contentWindow; var clonedWindow = container.contentWindow;
//var element = (nodeList === undefined) ? document.body : nodeList[0]; var node = clonedWindow.document.querySelector(selector);
var node = clonedWindow.document.documentElement;
var support = new Support(); var support = new Support();
var imageLoader = new ImageLoader(options, support); var imageLoader = new ImageLoader(options, support);
var bounds = NodeParser.prototype.getBounds(node); var bounds = NodeParser.prototype.getBounds(node);

View File

@ -149,7 +149,7 @@ NodeParser.prototype.isBodyWithTransparentRoot = function(container) {
}; };
NodeParser.prototype.isRootElement = function(container) { NodeParser.prototype.isRootElement = function(container) {
return container.node.nodeName === "HTML"; return container.parent === null;
}; };
NodeParser.prototype.sortStackingContexts = function(stack) { NodeParser.prototype.sortStackingContexts = function(stack) {

View File

@ -17,7 +17,7 @@ var h2cSelector, h2cOptions;
document.write(srcStart + '/src/' + html2canvas[i] + '.js?' + Math.random() + scrEnd); document.write(srcStart + '/src/' + html2canvas[i] + '.js?' + Math.random() + scrEnd);
} }
window.onload = function() { window.onload = function() {
h2cSelector = [document.body]; h2cSelector = [document.documentElement];
if (window.setUp) { if (window.setUp) {
window.setUp(); window.setUp();