html2canvas/dist/html2canvas.min.js

8 lines
60 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) 2015 Niklas von Hertzen
2014-09-01 21:22:56 +04:00
Released under MIT License
*/
2015-01-19 23:28:10 +03:00
(function(a,b,c,d,e,f,g){function h(a,b,c,d,e){return q(a,a,c,d,b,a.defaultView.pageXOffset,a.defaultView.pageYOffset).then(function(f){J("Document cloned");var g=Wb+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 Mb(g.document),i=new H(d,h),n=R(a),o="view"===d.type?e:l(g.document),p="view"===d.type?f:m(g.document),q=new d.renderer(o,p,i,d,b),r=new T(a,q,h,i,d);return r.ready.then(function(){J("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),J("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,J("Cropping canvas at:","left:",c.left,"top:",c.top,"width:",f-e,"height:",h-g),J("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(){return b.documentMode&&b.documentMode<=9}function p(a,c){for(var d=3===a.nodeType?b.createTextNode(a.nodeValue):a.cloneNode(!1),e=a.firstChild;e;)(c===!0||1!==e.nodeType||"SCRIPT"!==e.nodeName)&&d.appendChild(p(e,c)),e=e.nextSibling;return d}function q(a,b,c,d,e,f,g){v(a);var h=o()?p(a.documentElement,e.javascriptEnabled):a.documentElement.cloneNode(!0),i=b.createElement("iframe");return i.className="html2canvas-container",i.style.visibility="hidden",i.style.position="fixed",i.style.left="-10000px",i.style.top="0px",i.style.border="0",i.width=c,i.height=d,i.scrolling="no",b.body.appendChild(i),new Promise(function(b){var c=i.contentWindow.document;r(a.documentElement,h,"textarea"),r(a.documentElement,h,"select"),i.contentWindow.onload=i.onload=function(){var d=setInterval(function(){c.body.childNodes.length>0&&(w(a,c),clearInterval(d),"view"===e.type&&i.contentWindow.scrollTo(f,g),b(i))},50)},c.open(),c.write("<!DOCTYPE html><html></html>"),s(a,f,g),c.replaceChild(e.javascriptEnabled===!0?c.adoptNode(h):x(c.adoptNode(h)),c.documentElement),c.close()})}function r(a,b,c){for(var d=a.getElementsByTagName(c),e=b.getElementsByTagName(c),f=d.length,g=0;f>g;g++)e[g].value=d[g].value}function s(a,b,c){!a.defaultView||b===a.defaultView.pageXOffset&&c===a.defaultView.pageYOffset||a.defaultView.scrollTo(b,c)}function t(b,c,d,e,f,g){return new Db(b,c,a.document).then(u(b)).then(function(a){return q(a,d,e,f,g,0,0)})}function u(a){return function(c){var d,e=new DOMParser;try{d=e.parseFromString(c,"text/html")}catch(f){J("DOMParser not supported, falling back to createHTMLDocument"),d=b.implementation.createHTMLDocument("");try{d.open(),d.write(c),d.close()}catch(g){J("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 v(a){[].slice.call(a.querySelectorAll("canvas"
D.prototype.getMetrics=function(a,b){return this.data[a+"-"+b]===g&&(this.data[a+"-"+b]=new C(a,b)),this.data[a+"-"+b]},E.prototype.proxyLoad=function(a,b,c){var d=this.src;return t(d.src,a,d.ownerDocument,b.width,b.height,c)},F.prototype.TYPES={LINEAR:1,RADIAL:2},H.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},H.prototype.findBackgroundImage=function(a,b){return b.parseBackgroundImages().filter(this.hasImageBackground).forEach(this.addImage(a,this.loadImage),this),a},H.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)),J("Added image #"+a.length,"string"==typeof d?d.substring(0,100):d))},this)}},H.prototype.hasImageBackground=function(a){return"none"!==a.method},H.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 G(b.replace(/url\(['"]{0,}|['"]{0,}\)$/gi,""),!1):this.isSameOrigin(b)||this.options.allowTaint===!0||this.isSVG(b)?new G(b,!1):this.support.cors&&!this.options.allowTaint&&this.options.useCORS?new G(b,!0):this.options.proxy?new Ib(b,this.options.proxy):new B(b):new Nb(b)}return"linear-gradient"===a.method?new I(a):"gradient"===a.method?new Sb(a):"svg"===a.method?new Pb(a.args[0],this.support.svg):"IFRAME"===a.method?new E(a.args[0],this.isSameOrigin(a.args[0].src),this.options):new B(a)},H.prototype.isSVG=function(a){return"svg"===a.substring(a.length-3).toLowerCase()||Nb.prototype.isInline(a)},H.prototype.imageExists=function(a,b){return a.some(function(a){return a.src===b})},H.prototype.isSameOrigin=function(a){return this.getOrigin(a)===this.origin},H.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},H.prototype.getPromise=function(a){return this.timeout(a,this.options.imageTimeout)["catch"](function(){var b=new B(a.src);return b.promise.then(function(b){a.image=b})})},H.prototype.get=function(a){var b=null;return this.images.some(function(c){return(b=c).src===a})?b:null},H.prototype.fetch=function(a){return this.images=a.reduce(ub(this.findBackgroundImage,this),this.findImages(a)),this.images.forEach(function(a,b){a.promise.then(function(){J("Succesfully loaded image #"+(b+1),a)},function(c){J("Failed loading image #"+(b+1),a,c)})}),this.ready=Promise.all(this.images.map(this.getPromise,this)),J("Finished searching images"),this},H.prototype.timeout=function(a,b){var c,d=Promise.race([a.promise,new Promise(function(d,e){c=setTimeout(function(){J("Timed out loading image",a),e(a)},b)})]).then(function(a){return clearTimeout(c),a});return d["catch"](function(){clearTimeout(c)}),d},I.prototype=Object.create(F.prototype),I.prototype.stepRegExp=/((?:rgb|rgba)\(\d{1,3},\s\d{1,3},\s\d{1,3}(?:,\s[0-9\.]+)?\))\s*(\d{1,3})?(%|px)?/,K.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},K.prototype.getOpacity=function(){return null===this.opacity?this.opacity=this.cssFloat("opacity"):this.opacity},K.prototype.assignStack=function(a){this.stack=a,a.children.push(this)},K.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"))},K.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.s