@inscreen/html-to-image
Advanced tools
Comparing version 1.11.11-is11 to 1.11.11-is12
@@ -1,2 +0,2 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).htmlToImage={})}(this,(function(t){"use strict";function e(t,e,n,r){return new(n||(n=Promise))((function(i,o){function c(t){try{a(r.next(t))}catch(t){o(t)}}function u(t){try{a(r.throw(t))}catch(t){o(t)}}function a(t){var e;t.done?i(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(c,u)}a((r=r.apply(t,e||[])).next())}))}function n(t,e){var n,r,i,o,c={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function u(u){return function(a){return function(u){if(n)throw new TypeError("Generator is already executing.");for(;o&&(o=0,u[0]&&(c=0)),c;)try{if(n=1,r&&(i=2&u[0]?r.return:u[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,u[1])).done)return i;switch(r=0,i&&(u=[2&u[0],i.value]),u[0]){case 0:case 1:i=u;break;case 4:return c.label++,{value:u[1],done:!1};case 5:c.label++,r=u[1],u=[0];continue;case 7:u=c.ops.pop(),c.trys.pop();continue;default:if(!(i=c.trys,(i=i.length>0&&i[i.length-1])||6!==u[0]&&2!==u[0])){c=0;continue}if(3===u[0]&&(!i||u[1]>i[0]&&u[1]<i[3])){c.label=u[1];break}if(6===u[0]&&c.label<i[1]){c.label=i[1],i=u;break}if(i&&c.label<i[2]){c.label=i[2],c.ops.push(u);break}i[2]&&c.ops.pop(),c.trys.pop();continue}u=e.call(t,c)}catch(t){u=[6,t],r=0}finally{n=i=0}if(5&u[0])throw u[1];return{value:u[0]?u[1]:void 0,done:!0}}([u,a])}}}var r,i=(r=0,function(){return r+=1,"u".concat("0000".concat((Math.random()*Math.pow(36,4)<<0).toString(36)).slice(-4)).concat(r)});function o(t){return Array.from(t)}function c(t,e){var n=(t.ownerDocument.defaultView||window).getComputedStyle(t).getPropertyValue(e);return n?parseFloat(n.replace("px","")):0}function u(t,e){void 0===e&&(e={});var n,r,i,o,u=e.width||(i=c(n=t,"border-left-width"),o=c(n,"border-right-width"),((null===(r=n.getBoundingClientRect())||void 0===r?void 0:r.width)||n.clientWidth)+i+o),a=e.height||function(t){var e,n=c(t,"border-top-width"),r=c(t,"border-bottom-width");return((null===(e=t.getBoundingClientRect())||void 0===e?void 0:e.height)||t.clientHeight)+n+r}(t);return{width:u,height:a}}var a=16384;function s(t,e){return void 0===e&&(e={}),t.toBlob?new Promise((function(n){t.toBlob(n,e.type?e.type:"image/png",e.quality?e.quality:1)})):new Promise((function(n){for(var r=window.atob(t.toDataURL(e.type?e.type:void 0,e.quality?e.quality:void 0).split(",")[1]),i=r.length,o=new Uint8Array(i),c=0;c<i;c+=1)o[c]=r.charCodeAt(c);n(new Blob([o],{type:e.type?e.type:"image/png"}))}))}function l(t){return e(this,void 0,void 0,(function(){return n(this,(function(e){return[2,Promise.resolve().then((function(){return(new XMLSerializer).serializeToString(t)})).then(encodeURIComponent).then((function(t){return"data:image/svg+xml;charset=utf-8,".concat(t)}))]}))}))}function f(t,r,i){return e(this,void 0,void 0,(function(){var e,o,c;return n(this,(function(n){return e="http://www.w3.org/2000/svg",o=document.createElementNS(e,"svg"),c=document.createElementNS(e,"foreignObject"),o.setAttribute("width","".concat(r)),o.setAttribute("height","".concat(i)),o.setAttribute("viewBox","0 0 ".concat(r," ").concat(i)),c.setAttribute("width","100%"),c.setAttribute("height","100%"),c.setAttribute("x","0"),c.setAttribute("y","0"),c.setAttribute("externalResourcesRequired","true"),o.appendChild(c),c.appendChild(t),[2,l(o)]}))}))}var h=function(t,e){return t instanceof t.ownerDocument.defaultView[e.name]};function d(t,e,n){var r=".".concat(t,":").concat(e),i=n.cssText?function(t){var e=t.getPropertyValue("content");return"".concat(t.cssText," content: '").concat(e.replace(/'|"/g,""),"';")}(n):function(t){return o(t).map((function(e){var n=t.getPropertyValue(e),r=t.getPropertyPriority(e);return"".concat(e,": ").concat(n).concat(r?" !important":"",";")})).join(" ")}(n);return document.createTextNode("".concat(r,"{").concat(i,"}"))}function v(t,e,n){var r=window.getComputedStyle(t,n),o=r.getPropertyValue("content");if(""!==o&&"none"!==o){var c=i();try{e.className="".concat(e.className," ").concat(c)}catch(t){return}var u=document.createElement("style");u.appendChild(d(c,n,r)),e.appendChild(u)}}function g(t){try{var e=t.toDataURL();if("data:,"===e)return t.cloneNode(!1);var n=document.createElement("img");return n.src=e,n}catch(e){return console.error("Unable to inline canvas contents, canvas is tainted",t),null}}function p(t){try{var e=void 0;if(t.currentSrc){var n=document.createElement("canvas"),r=n.getContext("2d");n.width=t.clientWidth,n.height=t.clientHeight,null==r||r.drawImage(t,0,0,n.width,n.height),e=n.toDataURL()}var i=document.createElement("img");return i.src=null!=e?e:t.poster,i}catch(e){return console.error("Unable to clone video as it is tainted",t),null}}function m(t){var r;return e(this,void 0,void 0,(function(){return n(this,(function(e){switch(e.label){case 0:return e.trys.push([0,3,,4]),(null===(r=null==t?void 0:t.contentDocument)||void 0===r?void 0:r.body)?[4,b(t.contentDocument.body,{})]:[3,2];case 1:return[2,e.sent()];case 2:return[3,4];case 3:return e.sent(),[3,4];case 4:return[2,t.cloneNode(!1)]}}))}))}function w(t){return e(this,void 0,void 0,(function(){return n(this,(function(e){return h(t,HTMLCanvasElement)?[2,g(t)]:h(t,HTMLVideoElement)?[2,p(t)]:h(t,HTMLIFrameElement)?[2,m(t)]:[2,t.cloneNode(!1)]}))}))}function y(t,r,i){var c,u;return e(this,void 0,void 0,(function(){var e;return n(this,(function(n){switch(n.label){case 0:return e=[],0===(e=null!=(a=t).tagName&&"SLOT"===a.tagName.toUpperCase()&&t.assignedNodes?o(t.assignedNodes()):h(t,HTMLIFrameElement)&&(null===(c=t.contentDocument)||void 0===c?void 0:c.body)?o(t.contentDocument.body.childNodes):o((null!==(u=t.shadowRoot)&&void 0!==u?u:t).childNodes)).length||h(t,HTMLVideoElement)?[2,r]:[4,e.reduce((function(t,e){return t.then((function(){return b(e,i)})).then((function(t){t&&r.appendChild(t)}))}),Promise.resolve())];case 1:return n.sent(),[2,r]}var a}))}))}function b(t,r){return e(this,void 0,void 0,(function(){var e,i;return n(this,(function(n){switch(n.label){case 0:return function(t){return 3===t.nodeType}(t)?[2,document.createTextNode(t.data)]:function(t){return 1===t.nodeType}(t)?r.filter&&!r.filter(t)||"none"===(e=window.getComputedStyle(t)).getPropertyValue("display")?[2,null]:[4,w(t)]:[2,t.cloneNode(!1)];case 1:return(i=n.sent())?(function(t,e,n){var r=e.style;r&&(n.cssText?(r.cssText=n.cssText,r.transformOrigin=n.transformOrigin):o(n).forEach((function(i){var o=n.getPropertyValue(i);if("font-size"===i&&o.endsWith("px")){var c=Math.floor(parseFloat(o.substring(0,o.length-2)))-.1;o="".concat(c,"px")}"display"===i&&"inline"===o&&h(t,HTMLIFrameElement)&&(o="block"),"d"===i&&e.getAttribute("d")&&(o="path(".concat(e.getAttribute("d"),")")),r.setProperty(i,o,n.getPropertyPriority(i))})))}(t,i,e),function(t,e){v(t,e,":before"),v(t,e,":after")}(t,i),function(t,e){h(t,HTMLTextAreaElement)&&(e.innerHTML=t.value),h(t,HTMLInputElement)&&e.setAttribute("value",t.value)}(t,i),function(t,e){if(h(t,HTMLSelectElement)){var n=e,r=Array.from(n.children).find((function(e){return t.value===e.getAttribute("value")}));r&&r.setAttribute("selected","")}}(t,i),[4,y(t,i,r)]):[2,null];case 2:return n.sent(),function(t,e){if(0!==t.scrollTop||0!==t.scrollLeft)for(var n=0;n<e.children.length;n++){var r=e.children[n];if(!("style"in r))return;r.style.transform=new DOMMatrix(r.style.transform).translateSelf(-t.scrollLeft,-t.scrollTop).toString()}}(t,i),[2,i]}}))}))}var x="application/font-woff",S="image/jpeg",E={woff:x,woff2:x,ttf:"application/font-truetype",eot:"application/vnd.ms-fontobject",png:"image/png",jpg:S,jpeg:S,gif:"image/gif",tiff:"image/tiff",svg:"image/svg+xml",webp:"image/webp"};function C(t){var e=function(t){var e=/\.([^./]*?)$/g.exec(t);return e?e[1]:""}(t).toLowerCase();return E[e]||""}function R(t){return-1!==t.search(/^(data:)/)}function T(t,e){return"data:".concat(e,";base64,").concat(t)}function P(t,r,i){return e(this,void 0,void 0,(function(){var e,o;return n(this,(function(n){switch(n.label){case 0:return[4,fetch(t,r)];case 1:if(404===(e=n.sent()).status)throw new Error('Resource "'.concat(e.url,'" not found'));return[4,e.blob()];case 2:return o=n.sent(),[2,new Promise((function(t,n){var r=new FileReader;r.onerror=n,r.onloadend=function(){try{t(i({res:e,result:r.result}))}catch(t){n(t)}},r.readAsDataURL(o)}))]}}))}))}var L={};function N(t,r,i){return e(this,void 0,void 0,(function(){var e,o,c,u,a;return n(this,(function(n){switch(n.label){case 0:if(e=function(t,e,n){var r=t.replace(/\?.*/,"");return n&&(r=t),/ttf|otf|eot|woff2?/i.test(r)&&(r=r.replace(/.*\//,"")),e?"[".concat(e,"]").concat(r):r}(t,r,i.includeQueryParams),null!=L[e])return[2,L[e]];i.cacheBust&&(t+=(/\?/.test(t)?"&":"?")+(new Date).getTime()),n.label=1;case 1:return n.trys.push([1,3,,4]),[4,P(t,i.fetchRequestInit,(function(t){var e=t.res,n=t.result;return r||(r=e.headers.get("Content-Type")||""),function(t){return t.split(/,/)[1]}(n)}))];case 2:return c=n.sent(),o=T(c,r),[3,4];case 3:return u=n.sent(),o=i.imagePlaceholder||"",a="Failed to fetch resource: ".concat(t),u&&(a="string"==typeof u?u:u.message),a&&console.warn(a),[3,4];case 4:return L[e]=o,[2,o]}}))}))}var A=/url\((['"]?)([^'"]+?)\1\)/g,k=/url\([^)]+\)\s*format\((["']?)([^"']+)\1\)/g,D=/src:\s*(?:url\([^)]+\)\s*format\([^)]+\)[,;]\s*)+/g;function I(t,r,i,o,c){return e(this,void 0,void 0,(function(){var e,u,a,s;return n(this,(function(n){switch(n.label){case 0:return n.trys.push([0,5,,6]),e=i?function(t,e){if(t.match(/^[a-z]+:\/\//i))return t;if(t.match(/^\/\//))return window.location.protocol+t;if(t.match(/^[a-z]+:/i))return t;var n=document.implementation.createHTMLDocument(),r=n.createElement("base"),i=n.createElement("a");return n.head.appendChild(r),n.body.appendChild(i),e&&(r.href=e),i.href=t,i.href}(r,i):r,u=C(r),a=void 0,c?[4,c(e)]:[3,2];case 1:return s=n.sent(),a=T(s,u),[3,4];case 2:return[4,N(e,u,o)];case 3:a=n.sent(),n.label=4;case 4:return[2,t.replace((l=r,f=l.replace(/([.*+?^${}()|\[\]\/\\])/g,"\\$1"),new RegExp("(url\\(['\"]?)(".concat(f,")(['\"]?\\))"),"g")),"$1".concat(a,"$3"))];case 5:return n.sent(),[3,6];case 6:return[2,t]}var l,f}))}))}function M(t){return-1!==t.search(A)}function V(t,r,i){return e(this,void 0,void 0,(function(){var e,o;return n(this,(function(n){return M(t)?(e=function(t,e){var n=e.preferredFontFormat;return n?t.replace(D,(function(t){for(;;){var e=k.exec(t)||[],r=e[0],i=e[2];if(!i)return"";if(i===n)return"src: ".concat(r,";")}})):t}(t,i),o=function(t){var e=[];return t.replace(A,(function(t,n,r){return e.push(r),t})),e.filter((function(t){return!R(t)}))}(e),[2,o.reduce((function(t,e){return t.then((function(t){return I(t,e,r,i)}))}),Promise.resolve(e))]):[2,t]}))}))}function H(t,r,i){var o;return e(this,void 0,void 0,(function(){var e,c;return n(this,(function(n){switch(n.label){case 0:return(e=null===(o=r.style)||void 0===o?void 0:o.getPropertyValue(t))?[4,V(e,null,i)]:[3,2];case 1:return c=n.sent(),r.style.setProperty(t,c,r.style.getPropertyPriority(t)),[2,!0];case 2:return[2,!1]}}))}))}function U(t,r){return e(this,void 0,void 0,(function(){return n(this,(function(e){switch(e.label){case 0:return[4,H("background",t,r)];case 1:return e.sent()?[3,3]:[4,H("background-image",t,r)];case 2:e.sent(),e.label=3;case 3:return[4,H("mask",t,r)];case 4:return e.sent()?[3,6]:[4,H("mask-image",t,r)];case 5:e.sent(),e.label=6;case 6:return[2]}}))}))}function F(t,r){return e(this,void 0,void 0,(function(){var e,i,o;return n(this,(function(n){switch(n.label){case 0:return(e=h(t,HTMLImageElement))&&!R(t.src)||h(t,SVGImageElement)&&!R(t.href.baseVal)?[4,N(i=e?t.src:t.href.baseVal,C(i),r)]:[2];case 1:return o=n.sent(),[4,new Promise((function(n,r){t.onload=n,t.onerror=r;var i=t;i.decode&&(i.decode=n),"lazy"===i.loading&&(i.loading="eager"),e?(t.srcset="",t.src=o):t.href.baseVal=o}))];case 2:return n.sent(),[2]}}))}))}function j(t,r){return e(this,void 0,void 0,(function(){var e,i;return n(this,(function(n){switch(n.label){case 0:return e=o(t.childNodes),i=e.map((function(t){return B(t,r)})),[4,Promise.all(i).then((function(){return t}))];case 1:return n.sent(),[2]}}))}))}function B(t,r){return e(this,void 0,void 0,(function(){return n(this,(function(e){switch(e.label){case 0:return h(t,Element)?[4,U(t,r)]:[3,4];case 1:return e.sent(),[4,F(t,r)];case 2:return e.sent(),[4,j(t,r)];case 3:e.sent(),e.label=4;case 4:return[2]}}))}))}var O={};function q(t){return e(this,void 0,void 0,(function(){var e,r;return n(this,(function(n){switch(n.label){case 0:return null!=(e=O[t])?[2,e]:[4,fetch(t)];case 1:return[4,n.sent().text()];case 2:return r=n.sent(),e={url:t,cssText:r},O[t]=e,[2,e]}}))}))}function z(t,r){return e(this,void 0,void 0,(function(){var i,o,c,u,a=this;return n(this,(function(s){return i=t.cssText,o=/url\(["']?([^"')]+)["']?\)/g,c=i.match(/url\([^)]+\)/g)||[],u=c.map((function(c){return e(a,void 0,void 0,(function(){var e;return n(this,(function(n){return(e=c.replace(o,"$1")).startsWith("https://")||(e=new URL(e,t.url).href),[2,P(e,r.fetchRequestInit,(function(t){var e=t.result;return i=i.replace(c,"url(".concat(e,")")),[c,e]}))]}))}))})),[2,Promise.all(u).then((function(){return i}))]}))}))}function W(t){if(null==t)return[];for(var e=[],n=t.replace(/(\/\*[\s\S]*?\*\/)/gi,""),r=new RegExp("((@.*?keyframes [\\s\\S]*?){([\\s\\S]*?}\\s*?)})","gi");;){if(null===(c=r.exec(n)))break;e.push(c[0])}n=n.replace(r,"");for(var i=/@import[\s\S]*?url\([^)]*\)[\s\S]*?;/gi,o=new RegExp("((\\s*?(?:\\/\\*[\\s\\S]*?\\*\\/)?\\s*?@media[\\s\\S]*?){([\\s\\S]*?)}\\s*?})|(([\\s\\S]*?){([\\s\\S]*?)})","gi");;){var c;if(null===(c=i.exec(n))){if(null===(c=o.exec(n)))break;i.lastIndex=o.lastIndex}else o.lastIndex=i.lastIndex;e.push(c[0])}return e}function $(t,r){return e(this,void 0,void 0,(function(){var e,i;return n(this,(function(n){return e=[],i=[],t.filter((function(t){try{return"cssRules"in t&&t.cssRules}catch(t){return!1}})).forEach((function(e){try{o(e.cssRules||[]).forEach((function(t,n){if(t.type===CSSRule.IMPORT_RULE){var o=n+1,c=q(t.href).then((function(t){return z(t,r)})).then((function(t){return W(t).forEach((function(t){try{e.insertRule(t,t.startsWith("@import")?o+=1:e.cssRules.length)}catch(e){console.error("Error inserting rule from remote css",{rule:t,error:e})}}))})).catch((function(t){console.error("Error loading remote css",t.toString())}));i.push(c)}}))}catch(o){var n=t.find((function(t){return null==t.href}))||document.styleSheets[0];null!=e.href&&i.push(q(e.href).then((function(t){return z(t,r)})).then((function(t){return W(t).forEach((function(t){n.insertRule(t,e.cssRules.length)}))})).catch((function(t){console.error("Error loading remote stylesheet",t)}))),console.error("Error inlining remote css file",o)}})),[2,Promise.all(i).then((function(){return t.forEach((function(t){if("cssRules"in t)try{o(t.cssRules||[]).forEach((function(t){e.push(t)}))}catch(e){console.error("Error while reading CSS rules from ".concat(t.href),e)}})),e}))]}))}))}function _(t){return t.filter((function(t){return t.type===CSSRule.FONT_FACE_RULE})).filter((function(t){return M(t.style.getPropertyValue("src"))}))}function G(t,r){return e(this,void 0,void 0,(function(){return n(this,(function(e){switch(e.label){case 0:if(null==t.ownerDocument)throw new Error("Provided element is not within a Document");return[4,$(o(t.ownerDocument.styleSheets),r)];case 1:return[2,_(e.sent())]}}))}))}function J(t,r){return e(this,void 0,void 0,(function(){var e;return n(this,(function(n){switch(n.label){case 0:return[4,G(t,r)];case 1:return e=n.sent(),[4,Promise.all(e.map((function(t){var e=t.parentStyleSheet?t.parentStyleSheet.href:null;return V(t.cssText,e,r)})))];case 2:return[2,n.sent().join("\n")]}}))}))}function Q(t,r){return e(this,void 0,void 0,(function(){var e,i,o,c,u;return n(this,(function(n){switch(n.label){case 0:return null==r.fontEmbedCSS?[3,1]:(i=r.fontEmbedCSS,[3,5]);case 1:return r.skipFonts?(o=null,[3,4]):[3,2];case 2:return[4,J(t,r)];case 3:o=n.sent(),n.label=4;case 4:i=o,n.label=5;case 5:return(e=i)&&(c=document.createElement("style"),u=document.createTextNode(e),c.appendChild(u),t.firstChild?t.insertBefore(c,t.firstChild):t.appendChild(c)),[2]}}))}))}function X(t,r){return void 0===r&&(r={}),e(this,void 0,void 0,(function(){var e,i,o,c;return n(this,(function(n){switch(n.label){case 0:return e=u(t,r),i=e.width,o=e.height,[4,b(t,r)];case 1:return[4,Q(c=n.sent(),r)];case 2:return n.sent(),[4,B(c,r)];case 3:return n.sent(),function(t,e){var n=t.style;e.backgroundColor&&(n.backgroundColor=e.backgroundColor),e.width&&(n.width="".concat(e.width,"px")),e.height&&(n.height="".concat(e.height,"px"));var r=e.style;null!=r&&Object.keys(r).forEach((function(t){n[t]=r[t]}))}(c,r),[4,f(c,i,o)];case 4:return[2,n.sent()]}}))}))}function K(t,r){return void 0===r&&(r={}),e(this,void 0,void 0,(function(){var e,i,o,c,s,l,f,h,d,v;return n(this,(function(n){switch(n.label){case 0:return e=u(t,r),i=e.width,o=e.height,[4,X(t,r)];case 1:return c=n.sent(),[4,(g=c,new Promise((function(t,e){var n=new Image;n.decode=function(){return t(n)},n.onload=function(){return t(n)},n.onerror=e,n.crossOrigin="anonymous",n.decoding="async",n.src=g})))];case 2:return s=n.sent(),l=document.createElement("canvas"),f=l.getContext("2d"),h=r.pixelRatio||function(){var t,e;try{e=process}catch(t){}var n=e&&e.env?e.env.devicePixelRatio:null;return n&&(t=parseInt(n,10),Number.isNaN(t)&&(t=1)),t||window.devicePixelRatio||1}(),d=r.canvasWidth||i,v=r.canvasHeight||o,l.width=d*h,l.height=v*h,r.skipAutoScale||function(t){(t.width>a||t.height>a)&&(t.width>a&&t.height>a?t.width>t.height?(t.height*=a/t.width,t.width=a):(t.width*=a/t.height,t.height=a):t.width>a?(t.height*=a/t.width,t.width=a):(t.width*=a/t.height,t.height=a))}(l),l.style.width="".concat(d),l.style.height="".concat(v),r.backgroundColor&&(f.fillStyle=r.backgroundColor,f.fillRect(0,0,l.width,l.height)),f.drawImage(s,0,0,l.width,l.height),[2,l]}var g}))}))}t.getFontEmbedCSS=function(t,r){return void 0===r&&(r={}),e(this,void 0,void 0,(function(){return n(this,(function(e){return[2,J(t,r)]}))}))},t.toBlob=function(t,r){return void 0===r&&(r={}),e(this,void 0,void 0,(function(){return n(this,(function(e){switch(e.label){case 0:return[4,K(t,r)];case 1:return[4,s(e.sent(),r)];case 2:return[2,e.sent()]}}))}))},t.toCanvas=K,t.toJpeg=function(t,r){return void 0===r&&(r={}),e(this,void 0,void 0,(function(){return n(this,(function(e){switch(e.label){case 0:return[4,K(t,r)];case 1:return[2,e.sent().toDataURL("image/jpeg",r.quality||1)]}}))}))},t.toPixelData=function(t,r){return void 0===r&&(r={}),e(this,void 0,void 0,(function(){var e,i,o,c;return n(this,(function(n){switch(n.label){case 0:return e=u(t,r),i=e.width,o=e.height,[4,K(t,r)];case 1:return c=n.sent(),[2,c.getContext("2d").getImageData(0,0,i,o).data]}}))}))},t.toPng=function(t,r){return void 0===r&&(r={}),e(this,void 0,void 0,(function(){return n(this,(function(e){switch(e.label){case 0:return[4,K(t,r)];case 1:return[2,e.sent().toDataURL()]}}))}))},t.toSvg=X})); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).htmlToImage={})}(this,(function(t){"use strict";function e(t,e,n,r){return new(n||(n=Promise))((function(o,i){function c(t){try{a(r.next(t))}catch(t){i(t)}}function u(t){try{a(r.throw(t))}catch(t){i(t)}}function a(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(c,u)}a((r=r.apply(t,e||[])).next())}))}function n(t,e){var n,r,o,i,c={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(u){return function(a){return function(u){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,u[0]&&(c=0)),c;)try{if(n=1,r&&(o=2&u[0]?r.return:u[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,u[1])).done)return o;switch(r=0,o&&(u=[2&u[0],o.value]),u[0]){case 0:case 1:o=u;break;case 4:return c.label++,{value:u[1],done:!1};case 5:c.label++,r=u[1],u=[0];continue;case 7:u=c.ops.pop(),c.trys.pop();continue;default:if(!(o=c.trys,(o=o.length>0&&o[o.length-1])||6!==u[0]&&2!==u[0])){c=0;continue}if(3===u[0]&&(!o||u[1]>o[0]&&u[1]<o[3])){c.label=u[1];break}if(6===u[0]&&c.label<o[1]){c.label=o[1],o=u;break}if(o&&c.label<o[2]){c.label=o[2],c.ops.push(u);break}o[2]&&c.ops.pop(),c.trys.pop();continue}u=e.call(t,c)}catch(t){u=[6,t],r=0}finally{n=o=0}if(5&u[0])throw u[1];return{value:u[0]?u[1]:void 0,done:!0}}([u,a])}}}var r,o=(r=0,function(){return r+=1,"u".concat("0000".concat((Math.random()*Math.pow(36,4)<<0).toString(36)).slice(-4)).concat(r)});function i(t){return Array.from(t)}function c(t,e){var n=(t.ownerDocument.defaultView||window).getComputedStyle(t).getPropertyValue(e);return n?parseFloat(n.replace("px","")):0}function u(t,e){void 0===e&&(e={});var n,r,o,i,u=e.width||(o=c(n=t,"border-left-width"),i=c(n,"border-right-width"),((null===(r=n.getBoundingClientRect())||void 0===r?void 0:r.width)||n.clientWidth)+o+i),a=e.height||function(t){var e,n=c(t,"border-top-width"),r=c(t,"border-bottom-width");return((null===(e=t.getBoundingClientRect())||void 0===e?void 0:e.height)||t.clientHeight)+n+r}(t);return{width:u,height:a}}var a=16384;function s(t,e){return void 0===e&&(e={}),t.toBlob?new Promise((function(n){t.toBlob(n,e.type?e.type:"image/png",e.quality?e.quality:1)})):new Promise((function(n){for(var r=window.atob(t.toDataURL(e.type?e.type:void 0,e.quality?e.quality:void 0).split(",")[1]),o=r.length,i=new Uint8Array(o),c=0;c<o;c+=1)i[c]=r.charCodeAt(c);n(new Blob([i],{type:e.type?e.type:"image/png"}))}))}function l(t){return e(this,void 0,void 0,(function(){return n(this,(function(e){return[2,Promise.resolve().then((function(){return(new XMLSerializer).serializeToString(t)})).then(encodeURIComponent).then((function(t){return"data:image/svg+xml;charset=utf-8,".concat(t)}))]}))}))}function f(t,r,o){return e(this,void 0,void 0,(function(){var e,i,c;return n(this,(function(n){return e="http://www.w3.org/2000/svg",i=document.createElementNS(e,"svg"),c=document.createElementNS(e,"foreignObject"),i.setAttribute("width","".concat(r)),i.setAttribute("height","".concat(o)),i.setAttribute("viewBox","0 0 ".concat(r," ").concat(o)),c.setAttribute("width","100%"),c.setAttribute("height","100%"),c.setAttribute("x","0"),c.setAttribute("y","0"),c.setAttribute("externalResourcesRequired","true"),i.appendChild(c),c.appendChild(t),[2,l(i)]}))}))}var h=function(t,e){return t instanceof t.ownerDocument.defaultView[e.name]};function d(t,e,n){var r=".".concat(t,":").concat(e),o=n.cssText?function(t){var e=t.getPropertyValue("content");return"".concat(t.cssText," content: '").concat(e.replace(/'|"/g,""),"';")}(n):function(t){return i(t).map((function(e){var n=t.getPropertyValue(e),r=t.getPropertyPriority(e);return"".concat(e,": ").concat(n).concat(r?" !important":"",";")})).join(" ")}(n);return document.createTextNode("".concat(r,"{").concat(o,"}"))}function v(t,e,n){var r=window.getComputedStyle(t,n),i=r.getPropertyValue("content");if(""!==i&&"none"!==i){var c=o();try{e.className="".concat(e.className," ").concat(c)}catch(t){return}var u=document.createElement("style");u.appendChild(d(c,n,r)),e.appendChild(u)}}function g(t){return h(t,HTMLCanvasElement)?function(t){try{var e=t.toDataURL();if("data:,"===e)return t.cloneNode(!1);var n=document.createElement("img");return n.src=e,n}catch(e){return console.error("Unable to inline canvas contents, canvas is tainted",t),null}}(t):h(t,HTMLVideoElement)?function(t){try{var e=void 0;if(t.currentSrc){var n=document.createElement("canvas"),r=n.getContext("2d");n.width=t.clientWidth,n.height=t.clientHeight,null==r||r.drawImage(t,0,0,n.width,n.height),e=n.toDataURL()}var o=document.createElement("img");return o.src=null!=e?e:t.poster,o}catch(e){return console.error("Unable to clone video as it is tainted",t),null}}(t):h(t,HTMLIFrameElement)?function(t){var e;try{if(null===(e=null==t?void 0:t.contentDocument)||void 0===e?void 0:e.body)return p(t.contentDocument.body,{})}catch(e){console.error("Failed to clone iframe",t)}return t.cloneNode(!1)}(t):t.cloneNode(!1)}function p(t,e){if(function(t){return 3===t.nodeType}(t))return document.createTextNode(t.data);if(!function(t){return 1===t.nodeType}(t))return t.cloneNode(!1);if(e.filter&&!e.filter(t))return null;var n=window.getComputedStyle(t);if("none"===n.getPropertyValue("display"))return null;var r=g(t);return r?(function(t,e,n){var r=e.style;r&&(n.cssText?(r.cssText=n.cssText,r.transformOrigin=n.transformOrigin):i(n).forEach((function(o){var i=n.getPropertyValue(o);if("font-size"===o&&i.endsWith("px")){var c=Math.floor(parseFloat(i.substring(0,i.length-2)))-.1;i="".concat(c,"px")}"display"===o&&"inline"===i&&h(t,HTMLIFrameElement)&&(i="block"),"d"===o&&e.getAttribute("d")&&(i="path(".concat(e.getAttribute("d"),")")),r.setProperty(o,i,n.getPropertyPriority(o))})))}(t,r,n),function(t,e){v(t,e,":before"),v(t,e,":after")}(t,r),function(t,e){h(t,HTMLTextAreaElement)&&(e.innerHTML=t.value),h(t,HTMLInputElement)&&e.setAttribute("value",t.value)}(t,r),function(t,e){if(h(t,HTMLSelectElement)){var n=e,r=Array.from(n.children).find((function(e){return t.value===e.getAttribute("value")}));r&&r.setAttribute("selected","")}}(t,r),function(t,e,n){var r,o,c,u=[];if(0===(u=null!=(c=t).tagName&&"SLOT"===c.tagName.toUpperCase()&&t.assignedNodes?i(t.assignedNodes()):h(t,HTMLIFrameElement)&&(null===(r=t.contentDocument)||void 0===r?void 0:r.body)?i(t.contentDocument.body.childNodes):i((null!==(o=t.shadowRoot)&&void 0!==o?o:t).childNodes)).length||h(t,HTMLVideoElement))return e;for(var a=0;a<u.length;a++){var s=p(u[a],n);s&&e.appendChild(s)}}(t,r,e),function(t,e){if(0!==t.scrollTop||0!==t.scrollLeft)for(var n=0;n<e.children.length;n++){var r=e.children[n];if(!("style"in r))return;r.style.transform=new DOMMatrix(r.style.transform).translateSelf(-t.scrollLeft,-t.scrollTop).toString()}}(t,r),r):null}var m="application/font-woff",w="image/jpeg",y={woff:m,woff2:m,ttf:"application/font-truetype",eot:"application/vnd.ms-fontobject",png:"image/png",jpg:w,jpeg:w,gif:"image/gif",tiff:"image/tiff",svg:"image/svg+xml",webp:"image/webp"};function b(t){var e=function(t){var e=/\.([^./]*?)$/g.exec(t);return e?e[1]:""}(t).toLowerCase();return y[e]||""}function x(t){return-1!==t.search(/^(data:)/)}function S(t,e){return"data:".concat(e,";base64,").concat(t)}function E(t,r,o){return e(this,void 0,void 0,(function(){var e,i;return n(this,(function(n){switch(n.label){case 0:return[4,fetch(t,r)];case 1:if(404===(e=n.sent()).status)throw new Error('Resource "'.concat(e.url,'" not found'));return[4,e.blob()];case 2:return i=n.sent(),[2,new Promise((function(t,n){var r=new FileReader;r.onerror=n,r.onloadend=function(){try{t(o({res:e,result:r.result}))}catch(t){n(t)}},r.readAsDataURL(i)}))]}}))}))}var C={};function R(t,r,o){return e(this,void 0,void 0,(function(){var e,i,c,u,a;return n(this,(function(n){switch(n.label){case 0:if(e=function(t,e,n){var r=t.replace(/\?.*/,"");return n&&(r=t),/ttf|otf|eot|woff2?/i.test(r)&&(r=r.replace(/.*\//,"")),e?"[".concat(e,"]").concat(r):r}(t,r,o.includeQueryParams),null!=C[e])return[2,C[e]];o.cacheBust&&(t+=(/\?/.test(t)?"&":"?")+(new Date).getTime()),n.label=1;case 1:return n.trys.push([1,3,,4]),[4,E(t,o.fetchRequestInit,(function(t){var e=t.res,n=t.result;return r||(r=e.headers.get("Content-Type")||""),function(t){return t.split(/,/)[1]}(n)}))];case 2:return c=n.sent(),i=S(c,r),[3,4];case 3:return u=n.sent(),i=o.imagePlaceholder||"",a="Failed to fetch resource: ".concat(t),u&&(a="string"==typeof u?u:u.message),a&&console.warn(a),[3,4];case 4:return C[e]=i,[2,i]}}))}))}var T=/url\((['"]?)([^'"]+?)\1\)/g,P=/url\([^)]+\)\s*format\((["']?)([^"']+)\1\)/g,L=/src:\s*(?:url\([^)]+\)\s*format\([^)]+\)[,;]\s*)+/g;function N(t,r,o,i,c){return e(this,void 0,void 0,(function(){var e,u,a,s;return n(this,(function(n){switch(n.label){case 0:return n.trys.push([0,5,,6]),e=o?function(t,e){if(t.match(/^[a-z]+:\/\//i))return t;if(t.match(/^\/\//))return window.location.protocol+t;if(t.match(/^[a-z]+:/i))return t;var n=document.implementation.createHTMLDocument(),r=n.createElement("base"),o=n.createElement("a");return n.head.appendChild(r),n.body.appendChild(o),e&&(r.href=e),o.href=t,o.href}(r,o):r,u=b(r),a=void 0,c?[4,c(e)]:[3,2];case 1:return s=n.sent(),a=S(s,u),[3,4];case 2:return[4,R(e,u,i)];case 3:a=n.sent(),n.label=4;case 4:return[2,t.replace((l=r,f=l.replace(/([.*+?^${}()|\[\]\/\\])/g,"\\$1"),new RegExp("(url\\(['\"]?)(".concat(f,")(['\"]?\\))"),"g")),"$1".concat(a,"$3"))];case 5:return n.sent(),[3,6];case 6:return[2,t]}var l,f}))}))}function A(t){return-1!==t.search(T)}function k(t,r,o){return e(this,void 0,void 0,(function(){var e,i;return n(this,(function(n){return A(t)?(e=function(t,e){var n=e.preferredFontFormat;return n?t.replace(L,(function(t){for(;;){var e=P.exec(t)||[],r=e[0],o=e[2];if(!o)return"";if(o===n)return"src: ".concat(r,";")}})):t}(t,o),i=function(t){var e=[];return t.replace(T,(function(t,n,r){return e.push(r),t})),e.filter((function(t){return!x(t)}))}(e),[2,i.reduce((function(t,e){return t.then((function(t){return N(t,e,r,o)}))}),Promise.resolve(e))]):[2,t]}))}))}function D(t,r,o){var i;return e(this,void 0,void 0,(function(){var e,c;return n(this,(function(n){switch(n.label){case 0:return(e=null===(i=r.style)||void 0===i?void 0:i.getPropertyValue(t))?[4,k(e,null,o)]:[3,2];case 1:return c=n.sent(),r.style.setProperty(t,c,r.style.getPropertyPriority(t)),[2,!0];case 2:return[2,!1]}}))}))}function I(t,r){return e(this,void 0,void 0,(function(){return n(this,(function(e){switch(e.label){case 0:return[4,D("background",t,r)];case 1:return e.sent()?[3,3]:[4,D("background-image",t,r)];case 2:e.sent(),e.label=3;case 3:return[4,D("mask",t,r)];case 4:return e.sent()?[3,6]:[4,D("mask-image",t,r)];case 5:e.sent(),e.label=6;case 6:return[2]}}))}))}function M(t,r){return e(this,void 0,void 0,(function(){var e,o,i;return n(this,(function(n){switch(n.label){case 0:return(e=h(t,HTMLImageElement))&&!x(t.src)||h(t,SVGImageElement)&&!x(t.href.baseVal)?[4,R(o=e?t.src:t.href.baseVal,b(o),r)]:[2];case 1:return i=n.sent(),[4,new Promise((function(n,r){t.onload=n,t.onerror=r;var o=t;o.decode&&(o.decode=n),"lazy"===o.loading&&(o.loading="eager"),e?(t.srcset="",t.src=i):t.href.baseVal=i}))];case 2:return n.sent(),[2]}}))}))}function V(t,r){return e(this,void 0,void 0,(function(){var e,o;return n(this,(function(n){switch(n.label){case 0:return e=i(t.childNodes),o=e.map((function(t){return H(t,r)})),[4,Promise.all(o).then((function(){return t}))];case 1:return n.sent(),[2]}}))}))}function H(t,r){return e(this,void 0,void 0,(function(){return n(this,(function(e){switch(e.label){case 0:return h(t,Element)?[4,I(t,r)]:[3,4];case 1:return e.sent(),[4,M(t,r)];case 2:return e.sent(),[4,V(t,r)];case 3:e.sent(),e.label=4;case 4:return[2]}}))}))}var F={};function U(t){return e(this,void 0,void 0,(function(){var e,r;return n(this,(function(n){switch(n.label){case 0:return null!=(e=F[t])?[2,e]:[4,fetch(t)];case 1:return[4,n.sent().text()];case 2:return r=n.sent(),e={url:t,cssText:r},F[t]=e,[2,e]}}))}))}function j(t,r){return e(this,void 0,void 0,(function(){var o,i,c,u,a=this;return n(this,(function(s){return o=t.cssText,i=/url\(["']?([^"')]+)["']?\)/g,c=o.match(/url\([^)]+\)/g)||[],u=c.map((function(c){return e(a,void 0,void 0,(function(){var e;return n(this,(function(n){return(e=c.replace(i,"$1")).startsWith("https://")||(e=new URL(e,t.url).href),[2,E(e,r.fetchRequestInit,(function(t){var e=t.result;return o=o.replace(c,"url(".concat(e,")")),[c,e]}))]}))}))})),[2,Promise.all(u).then((function(){return o}))]}))}))}function B(t){if(null==t)return[];for(var e=[],n=t.replace(/(\/\*[\s\S]*?\*\/)/gi,""),r=new RegExp("((@.*?keyframes [\\s\\S]*?){([\\s\\S]*?}\\s*?)})","gi");;){if(null===(c=r.exec(n)))break;e.push(c[0])}n=n.replace(r,"");for(var o=/@import[\s\S]*?url\([^)]*\)[\s\S]*?;/gi,i=new RegExp("((\\s*?(?:\\/\\*[\\s\\S]*?\\*\\/)?\\s*?@media[\\s\\S]*?){([\\s\\S]*?)}\\s*?})|(([\\s\\S]*?){([\\s\\S]*?)})","gi");;){var c;if(null===(c=o.exec(n))){if(null===(c=i.exec(n)))break;o.lastIndex=i.lastIndex}else i.lastIndex=o.lastIndex;e.push(c[0])}return e}function O(t,r){return e(this,void 0,void 0,(function(){var e,o;return n(this,(function(n){return e=[],o=[],t.filter((function(t){try{return"cssRules"in t&&t.cssRules}catch(t){return!1}})).forEach((function(e){try{i(e.cssRules||[]).forEach((function(t,n){if(t.type===CSSRule.IMPORT_RULE){var i=n+1,c=U(t.href).then((function(t){return j(t,r)})).then((function(t){return B(t).forEach((function(t){try{e.insertRule(t,t.startsWith("@import")?i+=1:e.cssRules.length)}catch(e){console.error("Error inserting rule from remote css",{rule:t,error:e})}}))})).catch((function(t){console.error("Error loading remote css",t.toString())}));o.push(c)}}))}catch(i){var n=t.find((function(t){return null==t.href}))||document.styleSheets[0];null!=e.href&&o.push(U(e.href).then((function(t){return j(t,r)})).then((function(t){return B(t).forEach((function(t){n.insertRule(t,e.cssRules.length)}))})).catch((function(t){console.error("Error loading remote stylesheet",t)}))),console.error("Error inlining remote css file",i)}})),[2,Promise.all(o).then((function(){return t.forEach((function(t){if("cssRules"in t)try{i(t.cssRules||[]).forEach((function(t){e.push(t)}))}catch(e){console.error("Error while reading CSS rules from ".concat(t.href),e)}})),e}))]}))}))}function q(t){return t.filter((function(t){return t.type===CSSRule.FONT_FACE_RULE})).filter((function(t){return A(t.style.getPropertyValue("src"))}))}function z(t,r){return e(this,void 0,void 0,(function(){return n(this,(function(e){switch(e.label){case 0:if(null==t.ownerDocument)throw new Error("Provided element is not within a Document");return[4,O(i(t.ownerDocument.styleSheets),r)];case 1:return[2,q(e.sent())]}}))}))}function W(t,r){return e(this,void 0,void 0,(function(){var e;return n(this,(function(n){switch(n.label){case 0:return[4,z(t,r)];case 1:return e=n.sent(),[4,Promise.all(e.map((function(t){var e=t.parentStyleSheet?t.parentStyleSheet.href:null;return k(t.cssText,e,r)})))];case 2:return[2,n.sent().join("\n")]}}))}))}function $(t,r){return e(this,void 0,void 0,(function(){var e,o,i,c,u;return n(this,(function(n){switch(n.label){case 0:return null==r.fontEmbedCSS?[3,1]:(o=r.fontEmbedCSS,[3,5]);case 1:return r.skipFonts?(i=null,[3,4]):[3,2];case 2:return[4,W(t,r)];case 3:i=n.sent(),n.label=4;case 4:o=i,n.label=5;case 5:return(e=o)&&(c=document.createElement("style"),u=document.createTextNode(e),c.appendChild(u),t.firstChild?t.insertBefore(c,t.firstChild):t.appendChild(c)),[2]}}))}))}function _(t,r){return void 0===r&&(r={}),e(this,void 0,void 0,(function(){var e,o,i,c;return n(this,(function(n){switch(n.label){case 0:return e=u(t,r),o=e.width,i=e.height,[4,$(c=p(t,r),r)];case 1:return n.sent(),[4,H(c,r)];case 2:return n.sent(),function(t,e){var n=t.style;e.backgroundColor&&(n.backgroundColor=e.backgroundColor),e.width&&(n.width="".concat(e.width,"px")),e.height&&(n.height="".concat(e.height,"px"));var r=e.style;null!=r&&Object.keys(r).forEach((function(t){n[t]=r[t]}))}(c,r),[4,f(c,o,i)];case 3:return[2,n.sent()]}}))}))}function G(t,r){return void 0===r&&(r={}),e(this,void 0,void 0,(function(){var e,o,i,c,s,l,f,h,d,v;return n(this,(function(n){switch(n.label){case 0:return e=u(t,r),o=e.width,i=e.height,[4,_(t,r)];case 1:return c=n.sent(),[4,(g=c,new Promise((function(t,e){var n=new Image;n.decode=function(){return t(n)},n.onload=function(){return t(n)},n.onerror=e,n.crossOrigin="anonymous",n.decoding="async",n.src=g})))];case 2:return s=n.sent(),l=document.createElement("canvas"),f=l.getContext("2d"),h=r.pixelRatio||function(){var t,e;try{e=process}catch(t){}var n=e&&e.env?e.env.devicePixelRatio:null;return n&&(t=parseInt(n,10),Number.isNaN(t)&&(t=1)),t||window.devicePixelRatio||1}(),d=r.canvasWidth||o,v=r.canvasHeight||i,l.width=d*h,l.height=v*h,r.skipAutoScale||function(t){(t.width>a||t.height>a)&&(t.width>a&&t.height>a?t.width>t.height?(t.height*=a/t.width,t.width=a):(t.width*=a/t.height,t.height=a):t.width>a?(t.height*=a/t.width,t.width=a):(t.width*=a/t.height,t.height=a))}(l),l.style.width="".concat(d),l.style.height="".concat(v),r.backgroundColor&&(f.fillStyle=r.backgroundColor,f.fillRect(0,0,l.width,l.height)),f.drawImage(s,0,0,l.width,l.height),[2,l]}var g}))}))}t.getFontEmbedCSS=function(t,r){return void 0===r&&(r={}),e(this,void 0,void 0,(function(){return n(this,(function(e){return[2,W(t,r)]}))}))},t.toBlob=function(t,r){return void 0===r&&(r={}),e(this,void 0,void 0,(function(){return n(this,(function(e){switch(e.label){case 0:return[4,G(t,r)];case 1:return[4,s(e.sent(),r)];case 2:return[2,e.sent()]}}))}))},t.toCanvas=G,t.toJpeg=function(t,r){return void 0===r&&(r={}),e(this,void 0,void 0,(function(){return n(this,(function(e){switch(e.label){case 0:return[4,G(t,r)];case 1:return[2,e.sent().toDataURL("image/jpeg",r.quality||1)]}}))}))},t.toPixelData=function(t,r){return void 0===r&&(r={}),e(this,void 0,void 0,(function(){var e,o,i,c;return n(this,(function(n){switch(n.label){case 0:return e=u(t,r),o=e.width,i=e.height,[4,G(t,r)];case 1:return c=n.sent(),[2,c.getContext("2d").getImageData(0,0,o,i).data]}}))}))},t.toPng=function(t,r){return void 0===r&&(r={}),e(this,void 0,void 0,(function(){return n(this,(function(e){switch(e.label){case 0:return[4,G(t,r)];case 1:return[2,e.sent().toDataURL()]}}))}))},t.toSvg=_})); | ||
//# sourceMappingURL=html-to-image.js.map |
import type { Options } from './types'; | ||
export declare function cloneNode<T extends Node>(node: T, options: Options): Promise<Node | null>; | ||
export declare function cloneNode<T extends Node>(node: T, options: Options): Node | null; |
@@ -38,16 +38,15 @@ import { clonePseudoElements } from './clone-pseudos'; | ||
} | ||
async function cloneIFrameElement(iframe) { | ||
function cloneIFrameElement(iframe) { | ||
var _a; | ||
try { | ||
if ((_a = iframe === null || iframe === void 0 ? void 0 : iframe.contentDocument) === null || _a === void 0 ? void 0 : _a.body) { | ||
return (await cloneNode(iframe.contentDocument.body, {})); | ||
return cloneNode(iframe.contentDocument.body, {}); | ||
} | ||
} | ||
catch (_b) { | ||
// Failed to clone iframe | ||
console.error('Failed to clone iframe', iframe); | ||
} | ||
return iframe.cloneNode(false); | ||
} | ||
// TODO: make function synchronous | ||
async function cloneSingleNode(node) { | ||
function cloneSingleNode(node) { | ||
if (isInstanceOfElement(node, HTMLCanvasElement)) { | ||
@@ -65,3 +64,3 @@ return cloneCanvasElement(node); | ||
const isSlotElement = (node) => node.tagName != null && node.tagName.toUpperCase() === 'SLOT'; | ||
async function cloneChildren(nativeNode, clonedNode, options) { | ||
function cloneChildren(nativeNode, clonedNode, options) { | ||
var _a, _b; | ||
@@ -83,9 +82,8 @@ let children = []; | ||
} | ||
await children.reduce((deferred, child) => deferred | ||
.then(() => cloneNode(child, options)) | ||
.then((clonedChild) => { | ||
for (let i = 0; i < children.length; i++) { | ||
const clonedChild = cloneNode(children[i], options); | ||
if (clonedChild) { | ||
clonedNode.appendChild(clonedChild); | ||
} | ||
}), Promise.resolve()); | ||
} | ||
return clonedNode; | ||
@@ -161,3 +159,3 @@ } | ||
const isElementNode = (node) => node.nodeType === 1; // Node.ELEMENT_NODE | ||
export async function cloneNode(node, options) { | ||
export function cloneNode(node, options) { | ||
if (isTextNode(node)) { | ||
@@ -176,3 +174,3 @@ return document.createTextNode(node.data); | ||
} | ||
const clonedNode = await cloneSingleNode(node); | ||
const clonedNode = cloneSingleNode(node); | ||
if (!clonedNode) { | ||
@@ -185,3 +183,3 @@ return null; | ||
cloneSelectValue(node, clonedNode); | ||
await cloneChildren(node, clonedNode, options); | ||
cloneChildren(node, clonedNode, options); | ||
cloneScrollPosition(node, clonedNode); | ||
@@ -188,0 +186,0 @@ return clonedNode; |
@@ -8,3 +8,3 @@ import { cloneNode } from './clone-node'; | ||
const { width, height } = getImageSize(node, options); | ||
const clonedNode = (await cloneNode(node, options)); | ||
const clonedNode = cloneNode(node, options); | ||
await embedWebFonts(clonedNode, options); | ||
@@ -11,0 +11,0 @@ await embedImages(clonedNode, options); |
import type { Options } from './types'; | ||
export declare function cloneNode<T extends Node>(node: T, options: Options): Promise<Node | null>; | ||
export declare function cloneNode<T extends Node>(node: T, options: Options): Node | null; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (g && (g = 0, op[0] && (_ = 0)), _) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -79,36 +43,23 @@ exports.cloneNode = void 0; | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _b; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
_c.trys.push([0, 3, , 4]); | ||
if (!((_a = iframe === null || iframe === void 0 ? void 0 : iframe.contentDocument) === null || _a === void 0 ? void 0 : _a.body)) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, cloneNode(iframe.contentDocument.body, {})]; | ||
case 1: return [2 /*return*/, (_c.sent())]; | ||
case 2: return [3 /*break*/, 4]; | ||
case 3: | ||
_b = _c.sent(); | ||
return [3 /*break*/, 4]; | ||
case 4: return [2 /*return*/, iframe.cloneNode(false)]; | ||
} | ||
}); | ||
}); | ||
try { | ||
if ((_a = iframe === null || iframe === void 0 ? void 0 : iframe.contentDocument) === null || _a === void 0 ? void 0 : _a.body) { | ||
return cloneNode(iframe.contentDocument.body, {}); | ||
} | ||
} | ||
catch (_b) { | ||
console.error('Failed to clone iframe', iframe); | ||
} | ||
return iframe.cloneNode(false); | ||
} | ||
// TODO: make function synchronous | ||
function cloneSingleNode(node) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
if ((0, util_1.isInstanceOfElement)(node, HTMLCanvasElement)) { | ||
return [2 /*return*/, cloneCanvasElement(node)]; | ||
} | ||
if ((0, util_1.isInstanceOfElement)(node, HTMLVideoElement)) { | ||
return [2 /*return*/, cloneVideoElement(node)]; | ||
} | ||
if ((0, util_1.isInstanceOfElement)(node, HTMLIFrameElement)) { | ||
return [2 /*return*/, cloneIFrameElement(node)]; | ||
} | ||
return [2 /*return*/, node.cloneNode(false)]; | ||
}); | ||
}); | ||
if ((0, util_1.isInstanceOfElement)(node, HTMLCanvasElement)) { | ||
return cloneCanvasElement(node); | ||
} | ||
if ((0, util_1.isInstanceOfElement)(node, HTMLVideoElement)) { | ||
return cloneVideoElement(node); | ||
} | ||
if ((0, util_1.isInstanceOfElement)(node, HTMLIFrameElement)) { | ||
return cloneIFrameElement(node); | ||
} | ||
return node.cloneNode(false); | ||
} | ||
@@ -120,37 +71,24 @@ var isSlotElement = function (node) { | ||
var _a, _b; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var children; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
children = []; | ||
if (isSlotElement(nativeNode) && nativeNode.assignedNodes) { | ||
children = (0, util_1.toArray)(nativeNode.assignedNodes()); | ||
} | ||
else if ((0, util_1.isInstanceOfElement)(nativeNode, HTMLIFrameElement) && | ||
((_a = nativeNode.contentDocument) === null || _a === void 0 ? void 0 : _a.body)) { | ||
children = (0, util_1.toArray)(nativeNode.contentDocument.body.childNodes); | ||
} | ||
else { | ||
children = (0, util_1.toArray)(((_b = nativeNode.shadowRoot) !== null && _b !== void 0 ? _b : nativeNode).childNodes); | ||
} | ||
if (children.length === 0 || | ||
(0, util_1.isInstanceOfElement)(nativeNode, HTMLVideoElement)) { | ||
return [2 /*return*/, clonedNode]; | ||
} | ||
return [4 /*yield*/, children.reduce(function (deferred, child) { | ||
return deferred | ||
.then(function () { return cloneNode(child, options); }) | ||
.then(function (clonedChild) { | ||
if (clonedChild) { | ||
clonedNode.appendChild(clonedChild); | ||
} | ||
}); | ||
}, Promise.resolve())]; | ||
case 1: | ||
_c.sent(); | ||
return [2 /*return*/, clonedNode]; | ||
} | ||
}); | ||
}); | ||
var children = []; | ||
if (isSlotElement(nativeNode) && nativeNode.assignedNodes) { | ||
children = (0, util_1.toArray)(nativeNode.assignedNodes()); | ||
} | ||
else if ((0, util_1.isInstanceOfElement)(nativeNode, HTMLIFrameElement) && | ||
((_a = nativeNode.contentDocument) === null || _a === void 0 ? void 0 : _a.body)) { | ||
children = (0, util_1.toArray)(nativeNode.contentDocument.body.childNodes); | ||
} | ||
else { | ||
children = (0, util_1.toArray)(((_b = nativeNode.shadowRoot) !== null && _b !== void 0 ? _b : nativeNode).childNodes); | ||
} | ||
if (children.length === 0 || | ||
(0, util_1.isInstanceOfElement)(nativeNode, HTMLVideoElement)) { | ||
return clonedNode; | ||
} | ||
for (var i = 0; i < children.length; i++) { | ||
var clonedChild = cloneNode(children[i], options); | ||
if (clonedChild) { | ||
clonedNode.appendChild(clonedChild); | ||
} | ||
} | ||
return clonedNode; | ||
} | ||
@@ -226,40 +164,28 @@ function cloneCSSStyle(nativeNode, clonedNode, sourceStyle) { | ||
function cloneNode(node, options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var style, clonedNode; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (isTextNode(node)) { | ||
return [2 /*return*/, document.createTextNode(node.data)]; | ||
} | ||
if (!isElementNode(node)) { | ||
return [2 /*return*/, node.cloneNode(false)]; | ||
} | ||
if (options.filter && !options.filter(node)) { | ||
return [2 /*return*/, null]; | ||
} | ||
style = window.getComputedStyle(node); | ||
if (style.getPropertyValue('display') === 'none') { | ||
return [2 /*return*/, null]; | ||
} | ||
return [4 /*yield*/, cloneSingleNode(node)]; | ||
case 1: | ||
clonedNode = _a.sent(); | ||
if (!clonedNode) { | ||
return [2 /*return*/, null]; | ||
} | ||
cloneCSSStyle(node, clonedNode, style); | ||
(0, clone_pseudos_1.clonePseudoElements)(node, clonedNode); | ||
cloneInputValue(node, clonedNode); | ||
cloneSelectValue(node, clonedNode); | ||
return [4 /*yield*/, cloneChildren(node, clonedNode, options)]; | ||
case 2: | ||
_a.sent(); | ||
cloneScrollPosition(node, clonedNode); | ||
return [2 /*return*/, clonedNode]; | ||
} | ||
}); | ||
}); | ||
if (isTextNode(node)) { | ||
return document.createTextNode(node.data); | ||
} | ||
if (!isElementNode(node)) { | ||
return node.cloneNode(false); | ||
} | ||
if (options.filter && !options.filter(node)) { | ||
return null; | ||
} | ||
var style = window.getComputedStyle(node); | ||
if (style.getPropertyValue('display') === 'none') { | ||
return null; | ||
} | ||
var clonedNode = cloneSingleNode(node); | ||
if (!clonedNode) { | ||
return null; | ||
} | ||
cloneCSSStyle(node, clonedNode, style); | ||
(0, clone_pseudos_1.clonePseudoElements)(node, clonedNode); | ||
cloneInputValue(node, clonedNode); | ||
cloneSelectValue(node, clonedNode); | ||
cloneChildren(node, clonedNode, options); | ||
cloneScrollPosition(node, clonedNode); | ||
return clonedNode; | ||
} | ||
exports.cloneNode = cloneNode; | ||
//# sourceMappingURL=clone-node.js.map |
@@ -53,14 +53,12 @@ "use strict"; | ||
_a = (0, util_1.getImageSize)(node, options), width = _a.width, height = _a.height; | ||
return [4 /*yield*/, (0, clone_node_1.cloneNode)(node, options)]; | ||
clonedNode = (0, clone_node_1.cloneNode)(node, options); | ||
return [4 /*yield*/, (0, embed_webfonts_1.embedWebFonts)(clonedNode, options)]; | ||
case 1: | ||
clonedNode = (_b.sent()); | ||
return [4 /*yield*/, (0, embed_webfonts_1.embedWebFonts)(clonedNode, options)]; | ||
case 2: | ||
_b.sent(); | ||
return [4 /*yield*/, (0, embed_images_1.embedImages)(clonedNode, options)]; | ||
case 3: | ||
case 2: | ||
_b.sent(); | ||
(0, apply_style_1.applyStyle)(clonedNode, options); | ||
return [4 /*yield*/, (0, util_1.nodeToDataURL)(clonedNode, width, height)]; | ||
case 4: | ||
case 3: | ||
datauri = _b.sent(); | ||
@@ -67,0 +65,0 @@ return [2 /*return*/, datauri]; |
{ | ||
"name": "@inscreen/html-to-image", | ||
"version": "1.11.11-is11", | ||
"version": "1.11.11-is12", | ||
"description": "Generates an image from a DOM node using HTML5 canvas and SVG.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -40,12 +40,9 @@ import type { Options } from './types' | ||
async function cloneIFrameElement(iframe: HTMLIFrameElement) { | ||
function cloneIFrameElement(iframe: HTMLIFrameElement) { | ||
try { | ||
if (iframe?.contentDocument?.body) { | ||
return (await cloneNode( | ||
iframe.contentDocument.body, | ||
{}, | ||
)) as HTMLBodyElement | ||
return cloneNode(iframe.contentDocument.body, {}) as HTMLBodyElement | ||
} | ||
} catch { | ||
// Failed to clone iframe | ||
console.error('Failed to clone iframe', iframe) | ||
} | ||
@@ -56,6 +53,3 @@ | ||
// TODO: make function synchronous | ||
async function cloneSingleNode<T extends HTMLElement>( | ||
node: T, | ||
): Promise<HTMLElement | null> { | ||
function cloneSingleNode<T extends HTMLElement>(node: T): HTMLElement | null { | ||
if (isInstanceOfElement(node, HTMLCanvasElement)) { | ||
@@ -79,7 +73,7 @@ return cloneCanvasElement(node) | ||
async function cloneChildren<T extends HTMLElement>( | ||
function cloneChildren<T extends HTMLElement>( | ||
nativeNode: T, | ||
clonedNode: T, | ||
options: Options, | ||
): Promise<T> { | ||
): T { | ||
let children: T[] = [] | ||
@@ -105,13 +99,8 @@ | ||
await children.reduce( | ||
(deferred, child) => | ||
deferred | ||
.then(() => cloneNode(child, options)) | ||
.then((clonedChild: HTMLElement | null) => { | ||
if (clonedChild) { | ||
clonedNode.appendChild(clonedChild) | ||
} | ||
}), | ||
Promise.resolve(), | ||
) | ||
for (let i = 0; i < children.length; i++) { | ||
const clonedChild = cloneNode(children[i], options) | ||
if (clonedChild) { | ||
clonedNode.appendChild(clonedChild) | ||
} | ||
} | ||
@@ -218,6 +207,6 @@ return clonedNode | ||
export async function cloneNode<T extends Node>( | ||
export function cloneNode<T extends Node>( | ||
node: T, | ||
options: Options, | ||
): Promise<Node | null> { | ||
): Node | null { | ||
if (isTextNode(node)) { | ||
@@ -241,3 +230,3 @@ return document.createTextNode(node.data) | ||
const clonedNode = await cloneSingleNode(node) | ||
const clonedNode = cloneSingleNode(node) | ||
@@ -256,3 +245,3 @@ if (!clonedNode) { | ||
await cloneChildren(node, clonedNode, options) | ||
cloneChildren(node, clonedNode, options) | ||
@@ -259,0 +248,0 @@ cloneScrollPosition(node, clonedNode) |
@@ -20,3 +20,3 @@ import { Options } from './types' | ||
const { width, height } = getImageSize(node, options) | ||
const clonedNode = (await cloneNode(node, options)) as HTMLElement | ||
const clonedNode = cloneNode(node, options) as HTMLElement | ||
await embedWebFonts(clonedNode, options) | ||
@@ -23,0 +23,0 @@ await embedImages(clonedNode, options) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
289106
3842