|
| 1 | +var log = require('./log'); |
| 2 | + |
| 3 | +function restoreOwnerScroll(ownerDocument, x, y) { |
| 4 | + if (ownerDocument.defaultView && (x !== ownerDocument.defaultView.pageXOffset || y !== ownerDocument.defaultView.pageYOffset)) { |
| 5 | + ownerDocument.defaultView.scrollTo(x, y); |
| 6 | + } |
| 7 | +} |
| 8 | + |
| 9 | +function cloneCanvasContents(canvas, clonedCanvas) { |
| 10 | + try { |
| 11 | + if (clonedCanvas) { |
| 12 | + clonedCanvas.width = canvas.width; |
| 13 | + clonedCanvas.height = canvas.height; |
| 14 | + clonedCanvas.getContext("2d").putImageData(canvas.getContext("2d").getImageData(0, 0, canvas.width, canvas.height), 0, 0); |
| 15 | + } |
| 16 | + } catch(e) { |
| 17 | + log("Unable to copy canvas content from", canvas, e); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +function cloneNode(node, javascriptEnabled) { |
| 22 | + var clone = node.nodeType === 3 ? document.createTextNode(node.nodeValue) : node.cloneNode(false); |
| 23 | + |
| 24 | + var child = node.firstChild; |
| 25 | + while(child) { |
| 26 | + if (javascriptEnabled === true || child.nodeType !== 1 || child.nodeName !== 'SCRIPT') { |
| 27 | + clone.appendChild(cloneNode(child, javascriptEnabled)); |
| 28 | + } |
| 29 | + child = child.nextSibling; |
| 30 | + } |
| 31 | + |
| 32 | + if (node.nodeType === 1) { |
| 33 | + clone._scrollTop = node.scrollTop; |
| 34 | + clone._scrollLeft = node.scrollLeft; |
| 35 | + if (node.nodeName === "CANVAS") { |
| 36 | + cloneCanvasContents(node, clone); |
| 37 | + } else if (node.nodeName === "TEXTAREA" || node.nodeName === "SELECT") { |
| 38 | + clone.value = node.value; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + return clone; |
| 43 | +} |
| 44 | + |
| 45 | +function initNode(node) { |
| 46 | + if (node.nodeType === 1) { |
| 47 | + node.scrollTop = node._scrollTop; |
| 48 | + node.scrollLeft = node._scrollLeft; |
| 49 | + |
| 50 | + var child = node.firstChild; |
| 51 | + while(child) { |
| 52 | + initNode(child); |
| 53 | + child = child.nextSibling; |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +module.exports = function(ownerDocument, containerDocument, width, height, options, x ,y) { |
| 59 | + var documentElement = cloneNode(ownerDocument.documentElement, options.javascriptEnabled); |
| 60 | + var container = containerDocument.createElement("iframe"); |
| 61 | + |
| 62 | + container.className = "html2canvas-container"; |
| 63 | + container.style.visibility = "hidden"; |
| 64 | + container.style.position = "fixed"; |
| 65 | + container.style.left = "-10000px"; |
| 66 | + container.style.top = "0px"; |
| 67 | + container.style.border = "0"; |
| 68 | + container.width = width; |
| 69 | + container.height = height; |
| 70 | + container.scrolling = "no"; // ios won't scroll without it |
| 71 | + containerDocument.body.appendChild(container); |
| 72 | + |
| 73 | + return new Promise(function(resolve) { |
| 74 | + var documentClone = container.contentWindow.document; |
| 75 | + |
| 76 | + /* Chrome doesn't detect relative background-images assigned in inline <style> sheets when fetched through getComputedStyle |
| 77 | + if window url is about:blank, we can assign the url to current by writing onto the document |
| 78 | + */ |
| 79 | + container.contentWindow.onload = container.onload = function() { |
| 80 | + var interval = setInterval(function() { |
| 81 | + if (documentClone.body.childNodes.length > 0) { |
| 82 | + initNode(documentClone.documentElement); |
| 83 | + clearInterval(interval); |
| 84 | + if (options.type === "view") { |
| 85 | + container.contentWindow.scrollTo(x, y); |
| 86 | + if ((/(iPad|iPhone|iPod)/g).test(navigator.userAgent) && (container.contentWindow.scrollY !== y || container.contentWindow.scrollX !== x)) { |
| 87 | + documentClone.documentElement.style.top = (-y) + "px"; |
| 88 | + documentClone.documentElement.style.left = (-x) + "px"; |
| 89 | + documentClone.documentElement.style.position = 'absolute'; |
| 90 | + } |
| 91 | + } |
| 92 | + resolve(container); |
| 93 | + } |
| 94 | + }, 50); |
| 95 | + }; |
| 96 | + |
| 97 | + documentClone.open(); |
| 98 | + documentClone.write("<!DOCTYPE html><html></html>"); |
| 99 | + // Chrome scrolls the parent document for some reason after the write to the cloned window??? |
| 100 | + restoreOwnerScroll(ownerDocument, x, y); |
| 101 | + documentClone.replaceChild(documentClone.adoptNode(documentElement), documentClone.documentElement); |
| 102 | + documentClone.close(); |
| 103 | + }); |
| 104 | +}; |
0 commit comments