html2canvas/dist/html2canvas.min.js

8 lines
59 KiB
JavaScript
Raw Normal View History

2014-09-01 21:22:56 +04:00
/*
html2canvas 0.5.0-alpha <http://html2canvas.hertzen.com>
Copyright (c) 2014 Niklas von Hertzen
Released under MIT License
*/
(function(a,b,c,d,e,f,g){function h(a,b,c,d,e){return o(a,a,c,d,b).then(function(f){G("Document cloned");var g=Tb+e,h="["+g+"='"+e+"']";a.querySelector(h).removeAttribute(g);var j=f.contentWindow,k=j.document.querySelector(h),l=Promise.resolve("function"==typeof b.onclone?b.onclone(j.document):!0);return l.then(function(){return i(k,f,b,c,d)})})}function i(a,c,d,e,f){var g=c.contentWindow,h=new Jb(g.document),i=new E(d,h),n=O(a),o="view"===d.type?e:l(g.document),p="view"===d.type?f:m(g.document),q=new Rb(o,p,i,d,b),r=new Q(a,q,h,i,d);return r.ready.then(function(){G("Finished rendering");var b;return b="view"===d.type?k(q.canvas,{width:q.canvas.width,height:q.canvas.height,top:0,left:0,x:0,y:0}):a===g.document.body||a===g.document.documentElement||null!=d.canvas?q.canvas:k(q.canvas,{width:null!=d.width?d.width:n.width,height:null!=d.height?d.height:n.height,top:n.top,left:n.left,x:g.pageXOffset,y:g.pageYOffset}),j(c,d),b})}function j(a,b){b.removeContainer&&(a.parentNode.removeChild(a),G("Cleaned up container"))}function k(a,c){var d=b.createElement("canvas"),e=Math.min(a.width-1,Math.max(0,c.left)),f=Math.min(a.width,Math.max(1,c.left+c.width)),g=Math.min(a.height-1,Math.max(0,c.top)),h=Math.min(a.height,Math.max(1,c.top+c.height));return d.width=c.width,d.height=c.height,G("Cropping canvas at:","left:",c.left,"top:",c.top,"width:",f-e,"height:",h-g),G("Resulting crop with width",c.width,"and height",c.height," with x",e,"and y",g),d.getContext("2d").drawImage(a,e,g,f-e,h-g,c.x,c.y,f-e,h-g),d}function l(a){return Math.max(Math.max(a.body.scrollWidth,a.documentElement.scrollWidth),Math.max(a.body.offsetWidth,a.documentElement.offsetWidth),Math.max(a.body.clientWidth,a.documentElement.clientWidth))}function m(a){return Math.max(Math.max(a.body.scrollHeight,a.documentElement.scrollHeight),Math.max(a.body.offsetHeight,a.documentElement.offsetHeight),Math.max(a.body.clientHeight,a.documentElement.clientHeight))}function n(){return"data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"}function o(a,b,c,d,e){s(a);var f=a.documentElement.cloneNode(!0),g=b.createElement("iframe");return g.className="html2canvas-container",g.style.visibility="hidden",g.style.position="fixed",g.style.left="-10000px",g.style.top="0px",g.style.border="0",g.width=c,g.height=d,g.scrolling="no",b.body.appendChild(g),new Promise(function(b){var c=g.contentWindow.document;g.contentWindow.onload=g.onload=function(){var f=setInterval(function(){c.body.childNodes.length>0&&(t(a,c),clearInterval(f),"view"===e.type&&g.contentWindow.scrollTo(d,h),b(g))},50)};var d=a.defaultView.pageXOffset,h=a.defaultView.pageYOffset;c.open(),c.write("<!DOCTYPE html><html></html>"),p(a,d,h),c.replaceChild(e.javascriptEnabled===!0?c.adoptNode(f):u(c.adoptNode(f)),c.documentElement),c.close()})}function p(a,b,c){(b!==a.defaultView.pageXOffset||c!==a.defaultView.pageYOffset)&&a.defaultView.scrollTo(b,c)}function q(b,c,d,e,f,g){return new Ab(b,c,a.document).then(r(b)).then(function(a){return o(a,d,e,f,g)})}function r(a){return function(c){var d,e=new DOMParser;try{d=e.parseFromString(c,"text/html")}catch(f){G("DOMParser not supported, falling back to createHTMLDocument"),d=b.implementation.createHTMLDocument("");try{d.open(),d.write(c),d.close()}catch(g){G("createHTMLDocument write not supported, falling back to document.body.innerHTML"),d.body.innerHTML=c}}var h=d.querySelector("base");if(!h||!h.href.host){var i=d.createElement("base");i.href=a,d.head.insertBefore(i,d.head.firstChild)}return d}}function s(a){[].slice.call(a.querySelectorAll("canvas"),0).forEach(function(a){a.setAttribute(Ub,"canvas-"+Vb++)})}function t(a,b){[].slice.call(a.querySelectorAll("["+Ub+"]"),0).forEach(function(a){try{var c=b.querySelector("["+Ub+'="'+a.getAttribute(Ub)+'"]');c&&(c.width=a.width,c.height=a.height,c.getContext("2d").putImageData(a.getContext("2d").getImageData(0,0,a.width,a.height),0,0))}catch(d){G("Unable to copy canvas content from",a,d)}a.removeAttribute(Ub)})}function u(a){return[].slice.call(a.childNodes,0).filter(v).forEach(function(b){"SCRIPT"===b.tagName
A.prototype.getMetrics=function(a,b){return this.data[a+"-"+b]===g&&(this.data[a+"-"+b]=new z(a,b)),this.data[a+"-"+b]},B.prototype.proxyLoad=function(a,b,c){var d=this.src;return q(d.src,a,d.ownerDocument,b.width,b.height,c)},C.prototype.TYPES={LINEAR:1,RADIAL:2},E.prototype.findImages=function(a){var b=[];return a.reduce(function(a,b){switch(b.node.nodeName){case"IMG":return a.concat([{args:[b.node.src],method:"url"}]);case"svg":case"IFRAME":return a.concat([{args:[b.node],method:b.node.nodeName}])}return a},[]).forEach(this.addImage(b,this.loadImage),this),b},E.prototype.findBackgroundImage=function(a,b){return b.parseBackgroundImages().filter(this.hasImageBackground).forEach(this.addImage(a,this.loadImage),this),a},E.prototype.addImage=function(a,b){return function(c){c.args.forEach(function(d){this.imageExists(a,d)||(a.splice(0,0,b.call(this,c)),G("Added image #"+a.length,"string"==typeof d?d.substring(0,100):d))},this)}},E.prototype.hasImageBackground=function(a){return"none"!==a.method},E.prototype.loadImage=function(a){if("url"===a.method){var b=a.args[0];return!this.isSVG(b)||this.support.svg||this.options.allowTaint?b.match(/data:image\/.*;base64,/i)?new D(b.replace(/url\(['"]{0,}|['"]{0,}\)$/gi,""),!1):this.isSameOrigin(b)||this.options.allowTaint===!0||this.isSVG(b)?new D(b,!1):this.support.cors&&!this.options.allowTaint&&this.options.useCORS?new D(b,!0):this.options.proxy?new Fb(b,this.options.proxy):new y(b):new Kb(b)}return"linear-gradient"===a.method?new F(a):"gradient"===a.method?new Pb(a):"svg"===a.method?new Mb(a.args[0],this.support.svg):"IFRAME"===a.method?new B(a.args[0],this.isSameOrigin(a.args[0].src),this.options):new y(a)},E.prototype.isSVG=function(a){return"svg"===a.substring(a.length-3).toLowerCase()||Kb.prototype.isInline(a)},E.prototype.imageExists=function(a,b){return a.some(function(a){return a.src===b})},E.prototype.isSameOrigin=function(a){return this.getOrigin(a)===this.origin},E.prototype.getOrigin=function(a){var c=this.link||(this.link=b.createElement("a"));return c.href=a,c.href=c.href,c.protocol+c.hostname+c.port},E.prototype.getPromise=function(a){return this.timeout(a,this.options.imageTimeout)["catch"](function(){var b=new y(a.src);return b.promise.then(function(b){a.image=b})})},E.prototype.get=function(a){var b=null;return this.images.some(function(c){return(b=c).src===a})?b:null},E.prototype.fetch=function(a){return this.images=a.reduce(rb(this.findBackgroundImage,this),this.findImages(a)),this.images.forEach(function(a,b){a.promise.then(function(){G("Succesfully loaded image #"+(b+1),a)},function(c){G("Failed loading image #"+(b+1),a,c)})}),this.ready=Promise.all(this.images.map(this.getPromise,this)),G("Finished searching images"),this},E.prototype.timeout=function(a,b){var c;return Promise.race([a.promise,new Promise(function(d,e){c=setTimeout(function(){G("Timed out loading image",a),e(a)},b)})]).then(function(a){return clearTimeout(c),a})},F.prototype=Object.create(C.prototype),F.prototype.stepRegExp=/((?:rgb|rgba)\(\d{1,3},\s\d{1,3},\s\d{1,3}(?:,\s[0-9\.]+)?\))\s*(\d{1,3})?(%|px)?/,H.prototype.cloneTo=function(a){a.visible=this.visible,a.borders=this.borders,a.bounds=this.bounds,a.clip=this.clip,a.backgroundClip=this.backgroundClip,a.computedStyles=this.computedStyles,a.styles=this.styles,a.backgroundImages=this.backgroundImages,a.opacity=this.opacity},H.prototype.getOpacity=function(){return null===this.opacity?this.opacity=this.cssFloat("opacity"):this.opacity},H.prototype.assignStack=function(a){this.stack=a,a.children.push(this)},H.prototype.isElementVisible=function(){return this.node.nodeType===Node.TEXT_NODE?this.parent.visible:"none"!==this.css("display")&&"hidden"!==this.css("visibility")&&!this.node.hasAttribute("data-html2canvas-ignore")&&("INPUT"!==this.node.nodeName||"hidden"!==this.node.getAttribute("type"))},H.prototype.css=function(a){return this.computedStyles||(this.computedStyles=this.isPseudoElement?this.parent.computedStyle(this.before?":before":":after"):this.computedStyle(null)),this.styles[a]||(this.styles[a]=this.computedStyles[a])},H.prototyp