ray-template/js/html-to-image@1.11.13-C6a6vdc2.js

3 lines
13 KiB
JavaScript

function W(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;const n=document.implementation.createHTMLDocument(),r=n.createElement("base"),s=n.createElement("a");return n.head.appendChild(r),n.body.appendChild(s),e&&(r.href=e),s.href=t,s.href}const B=(()=>{let t=0;const e=()=>`0000${(Math.random()*36**4<<0).toString(36)}`.slice(-4);return()=>(t+=1,`u${e()}${t}`)})();function m(t){const e=[];for(let n=0,r=t.length;n<r;n++)e.push(t[n]);return e}let g=null;function v(t={}){return g||(t.includeStyleProperties?(g=t.includeStyleProperties,g):(g=m(window.getComputedStyle(document.documentElement)),g))}function y(t,e){const r=(t.ownerDocument.defaultView||window).getComputedStyle(t).getPropertyValue(e);return r?parseFloat(r.replace("px","")):0}function j(t){const e=y(t,"border-left-width"),n=y(t,"border-right-width");return t.clientWidth+e+n}function z(t){const e=y(t,"border-top-width"),n=y(t,"border-bottom-width");return t.clientHeight+e+n}function R(t,e={}){const n=e.width||j(t),r=e.height||z(t);return{width:n,height:r}}function G(){let t,e;try{e=process}catch{}const n=e&&e.env?e.env.devicePixelRatio:null;return n&&(t=parseInt(n,10),Number.isNaN(t)&&(t=1)),t||window.devicePixelRatio||1}const u=16384;function X(t){(t.width>u||t.height>u)&&(t.width>u&&t.height>u?t.width>t.height?(t.height*=u/t.width,t.width=u):(t.width*=u/t.height,t.height=u):t.width>u?(t.height*=u/t.width,t.width=u):(t.width*=u/t.height,t.height=u))}function J(t,e={}){return t.toBlob?new Promise(n=>{t.toBlob(n,e.type?e.type:"image/png",e.quality?e.quality:1)}):new Promise(n=>{const r=window.atob(t.toDataURL(e.type?e.type:void 0,e.quality?e.quality:void 0).split(",")[1]),s=r.length,i=new Uint8Array(s);for(let a=0;a<s;a+=1)i[a]=r.charCodeAt(a);n(new Blob([i],{type:e.type?e.type:"image/png"}))})}function w(t){return new Promise((e,n)=>{const r=new Image;r.onload=()=>{r.decode().then(()=>{requestAnimationFrame(()=>e(r))})},r.onerror=n,r.crossOrigin="anonymous",r.decoding="async",r.src=t})}async function K(t){return Promise.resolve().then(()=>new XMLSerializer().serializeToString(t)).then(encodeURIComponent).then(e=>`data:image/svg+xml;charset=utf-8,${e}`)}async function Q(t,e,n){const r="http://www.w3.org/2000/svg",s=document.createElementNS(r,"svg"),i=document.createElementNS(r,"foreignObject");return s.setAttribute("width",`${e}`),s.setAttribute("height",`${n}`),s.setAttribute("viewBox",`0 0 ${e} ${n}`),i.setAttribute("width","100%"),i.setAttribute("height","100%"),i.setAttribute("x","0"),i.setAttribute("y","0"),i.setAttribute("externalResourcesRequired","true"),s.appendChild(i),i.appendChild(t),K(s)}const l=(t,e)=>{if(t instanceof e)return!0;const n=Object.getPrototypeOf(t);return n===null?!1:n.constructor.name===e.name||l(n,e)};function Y(t){const e=t.getPropertyValue("content");return`${t.cssText} content: '${e.replace(/'|"/g,"")}';`}function Z(t,e){return v(e).map(n=>{const r=t.getPropertyValue(n),s=t.getPropertyPriority(n);return`${n}: ${r}${s?" !important":""};`}).join(" ")}function N(t,e,n,r){const s=`.${t}:${e}`,i=n.cssText?Y(n):Z(n,r);return document.createTextNode(`${s}{${i}}`)}function $(t,e,n,r){const s=window.getComputedStyle(t,n),i=s.getPropertyValue("content");if(i===""||i==="none")return;const a=B();try{e.className=`${e.className} ${a}`}catch{return}const c=document.createElement("style");c.appendChild(N(a,n,s,r)),e.appendChild(c)}function tt(t,e,n){$(t,e,":before",n),$(t,e,":after",n)}const T="application/font-woff",L="image/jpeg",et={woff:T,woff2:T,ttf:"application/font-truetype",eot:"application/vnd.ms-fontobject",png:"image/png",jpg:L,jpeg:L,gif:"image/gif",tiff:"image/tiff",svg:"image/svg+xml",webp:"image/webp"};function nt(t){const e=/\.([^./]*?)$/g.exec(t);return e?e[1]:""}function C(t){const e=nt(t).toLowerCase();return et[e]||""}function rt(t){return t.split(/,/)[1]}function x(t){return t.search(/^(data:)/)!==-1}function it(t,e){return`data:${e};base64,${t}`}async function U(t,e,n){const r=await fetch(t,e);if(r.status===404)throw new Error(`Resource "${r.url}" not found`);const s=await r.blob();return new Promise((i,a)=>{const c=new FileReader;c.onerror=a,c.onloadend=()=>{try{i(n({res:r,result:c.result}))}catch(o){a(o)}},c.readAsDataURL(s)})}const E={};function st(t,e,n){let r=t.replace(/\?.*/,"");return n&&(r=t),/ttf|otf|eot|woff2?/i.test(r)&&(r=r.replace(/.*\//,"")),e?`[${e}]${r}`:r}async function P(t,e,n){const r=st(t,e,n.includeQueryParams);if(E[r]!=null)return E[r];n.cacheBust&&(t+=(/\?/.test(t)?"&":"?")+new Date().getTime());let s;try{const i=await U(t,n.fetchRequestInit,({res:a,result:c})=>(e||(e=a.headers.get("Content-Type")||""),rt(c)));s=it(i,e)}catch(i){s=n.imagePlaceholder||"";let a=`Failed to fetch resource: ${t}`;i&&(a=typeof i=="string"?i:i.message),a&&console.warn(a)}return E[r]=s,s}async function at(t){const e=t.toDataURL();return e==="data:,"?t.cloneNode(!1):w(e)}async function ct(t,e){if(t.currentSrc){const i=document.createElement("canvas"),a=i.getContext("2d");i.width=t.clientWidth,i.height=t.clientHeight,a==null||a.drawImage(t,0,0,i.width,i.height);const c=i.toDataURL();return w(c)}const n=t.poster,r=C(n),s=await P(n,r,e);return w(s)}async function ot(t,e){var n;try{if(!((n=t==null?void 0:t.contentDocument)===null||n===void 0)&&n.body)return await p(t.contentDocument.body,e,!0)}catch{}return t.cloneNode(!1)}async function lt(t,e){return l(t,HTMLCanvasElement)?at(t):l(t,HTMLVideoElement)?ct(t,e):l(t,HTMLIFrameElement)?ot(t,e):t.cloneNode(k(t))}const ut=t=>t.tagName!=null&&t.tagName.toUpperCase()==="SLOT",k=t=>t.tagName!=null&&t.tagName.toUpperCase()==="SVG";async function ft(t,e,n){var r,s;if(k(e))return e;let i=[];return ut(t)&&t.assignedNodes?i=m(t.assignedNodes()):l(t,HTMLIFrameElement)&&(!((r=t.contentDocument)===null||r===void 0)&&r.body)?i=m(t.contentDocument.body.childNodes):i=m(((s=t.shadowRoot)!==null&&s!==void 0?s:t).childNodes),i.length===0||l(t,HTMLVideoElement)||await i.reduce((a,c)=>a.then(()=>p(c,n)).then(o=>{o&&e.appendChild(o)}),Promise.resolve()),e}function ht(t,e,n){const r=e.style;if(!r)return;const s=window.getComputedStyle(t);s.cssText?(r.cssText=s.cssText,r.transformOrigin=s.transformOrigin):v(n).forEach(i=>{let a=s.getPropertyValue(i);i==="font-size"&&a.endsWith("px")&&(a=`${Math.floor(parseFloat(a.substring(0,a.length-2)))-.1}px`),l(t,HTMLIFrameElement)&&i==="display"&&a==="inline"&&(a="block"),i==="d"&&e.getAttribute("d")&&(a=`path(${e.getAttribute("d")})`),r.setProperty(i,a,s.getPropertyPriority(i))})}function mt(t,e){l(t,HTMLTextAreaElement)&&(e.innerHTML=t.value),l(t,HTMLInputElement)&&e.setAttribute("value",t.value)}function gt(t,e){if(l(t,HTMLSelectElement)){const n=e,r=Array.from(n.children).find(s=>t.value===s.getAttribute("value"));r&&r.setAttribute("selected","")}}function dt(t,e,n){return l(e,Element)&&(ht(t,e,n),tt(t,e,n),mt(t,e),gt(t,e)),e}async function yt(t,e){const n=t.querySelectorAll?t.querySelectorAll("use"):[];if(n.length===0)return t;const r={};for(let i=0;i<n.length;i++){const c=n[i].getAttribute("xlink:href");if(c){const o=t.querySelector(c),f=document.querySelector(c);!o&&f&&!r[c]&&(r[c]=await p(f,e,!0))}}const s=Object.values(r);if(s.length){const i="http://www.w3.org/1999/xhtml",a=document.createElementNS(i,"svg");a.setAttribute("xmlns",i),a.style.position="absolute",a.style.width="0",a.style.height="0",a.style.overflow="hidden",a.style.display="none";const c=document.createElementNS(i,"defs");a.appendChild(c);for(let o=0;o<s.length;o++)c.appendChild(s[o]);t.appendChild(a)}return t}async function p(t,e,n){return!n&&e.filter&&!e.filter(t)?null:Promise.resolve(t).then(r=>lt(r,e)).then(r=>ft(t,r,e)).then(r=>dt(t,r,e)).then(r=>yt(r,e))}const H=/url\((['"]?)([^'"]+?)\1\)/g,wt=/url\([^)]+\)\s*format\((["']?)([^"']+)\1\)/g,pt=/src:\s*(?:url\([^)]+\)\s*format\([^)]+\)[,;]\s*)+/g;function bt(t){const e=t.replace(/([.*+?^${}()|\[\]\/\\])/g,"\\$1");return new RegExp(`(url\\(['"]?)(${e})(['"]?\\))`,"g")}function St(t){const e=[];return t.replace(H,(n,r,s)=>(e.push(s),n)),e.filter(n=>!x(n))}async function Et(t,e,n,r,s){try{const i=n?W(e,n):e,a=C(e);let c;return s||(c=await P(i,a,r)),t.replace(bt(e),`$1${c}$3`)}catch{}return t}function xt(t,{preferredFontFormat:e}){return e?t.replace(pt,n=>{for(;;){const[r,,s]=wt.exec(n)||[];if(!s)return"";if(s===e)return`src: ${r};`}}):t}function V(t){return t.search(H)!==-1}async function M(t,e,n){if(!V(t))return t;const r=xt(t,n);return St(r).reduce((i,a)=>i.then(c=>Et(c,a,e,n)),Promise.resolve(r))}async function d(t,e,n){var r;const s=(r=e.style)===null||r===void 0?void 0:r.getPropertyValue(t);if(s){const i=await M(s,null,n);return e.style.setProperty(t,i,e.style.getPropertyPriority(t)),!0}return!1}async function Rt(t,e){await d("background",t,e)||await d("background-image",t,e),await d("mask",t,e)||await d("-webkit-mask",t,e)||await d("mask-image",t,e)||await d("-webkit-mask-image",t,e)}async function Ct(t,e){const n=l(t,HTMLImageElement);if(!(n&&!x(t.src))&&!(l(t,SVGImageElement)&&!x(t.href.baseVal)))return;const r=n?t.src:t.href.baseVal,s=await P(r,C(r),e);await new Promise((i,a)=>{t.onload=i,t.onerror=e.onImageErrorHandler?(...o)=>{try{i(e.onImageErrorHandler(...o))}catch(f){a(f)}}:a;const c=t;c.decode&&(c.decode=i),c.loading==="lazy"&&(c.loading="eager"),n?(t.srcset="",t.src=s):t.href.baseVal=s})}async function Pt(t,e){const r=m(t.childNodes).map(s=>O(s,e));await Promise.all(r).then(()=>t)}async function O(t,e){l(t,Element)&&(await Rt(t,e),await Ct(t,e),await Pt(t,e))}function $t(t,e){const{style:n}=t;e.backgroundColor&&(n.backgroundColor=e.backgroundColor),e.width&&(n.width=`${e.width}px`),e.height&&(n.height=`${e.height}px`);const r=e.style;return r!=null&&Object.keys(r).forEach(s=>{n[s]=r[s]}),t}const F={};async function I(t){let e=F[t];if(e!=null)return e;const r=await(await fetch(t)).text();return e={url:t,cssText:r},F[t]=e,e}async function A(t,e){let n=t.cssText;const r=/url\(["']?([^"')]+)["']?\)/g,i=(n.match(/url\([^)]+\)/g)||[]).map(async a=>{let c=a.replace(r,"$1");return c.startsWith("https://")||(c=new URL(c,t.url).href),U(c,e.fetchRequestInit,({result:o})=>(n=n.replace(a,`url(${o})`),[a,o]))});return Promise.all(i).then(()=>n)}function D(t){if(t==null)return[];const e=[],n=/(\/\*[\s\S]*?\*\/)/gi;let r=t.replace(n,"");const s=new RegExp("((@.*?keyframes [\\s\\S]*?){([\\s\\S]*?}\\s*?)})","gi");for(;;){const o=s.exec(r);if(o===null)break;e.push(o[0])}r=r.replace(s,"");const i=/@import[\s\S]*?url\([^)]*\)[\s\S]*?;/gi,a="((\\s*?(?:\\/\\*[\\s\\S]*?\\*\\/)?\\s*?@media[\\s\\S]*?){([\\s\\S]*?)}\\s*?})|(([\\s\\S]*?){([\\s\\S]*?)})",c=new RegExp(a,"gi");for(;;){let o=i.exec(r);if(o===null){if(o=c.exec(r),o===null)break;i.lastIndex=c.lastIndex}else c.lastIndex=i.lastIndex;e.push(o[0])}return e}async function Tt(t,e){const n=[],r=[];return t.forEach(s=>{if("cssRules"in s)try{m(s.cssRules||[]).forEach((i,a)=>{if(i.type===CSSRule.IMPORT_RULE){let c=a+1;const o=i.href,f=I(o).then(h=>A(h,e)).then(h=>D(h).forEach(S=>{try{s.insertRule(S,S.startsWith("@import")?c+=1:s.cssRules.length)}catch(q){console.error("Error inserting rule from remote css",{rule:S,error:q})}})).catch(h=>{console.error("Error loading remote css",h.toString())});r.push(f)}})}catch(i){const a=t.find(c=>c.href==null)||document.styleSheets[0];s.href!=null&&r.push(I(s.href).then(c=>A(c,e)).then(c=>D(c).forEach(o=>{a.insertRule(o,a.cssRules.length)})).catch(c=>{console.error("Error loading remote stylesheet",c)})),console.error("Error inlining remote css file",i)}}),Promise.all(r).then(()=>(t.forEach(s=>{if("cssRules"in s)try{m(s.cssRules||[]).forEach(i=>{n.push(i)})}catch(i){console.error(`Error while reading CSS rules from ${s.href}`,i)}}),n))}function Lt(t){return t.filter(e=>e.type===CSSRule.FONT_FACE_RULE).filter(e=>V(e.style.getPropertyValue("src")))}async function Ft(t,e){if(t.ownerDocument==null)throw new Error("Provided element is not within a Document");const n=m(t.ownerDocument.styleSheets),r=await Tt(n,e);return Lt(r)}function _(t){return t.trim().replace(/["']/g,"")}function It(t){const e=new Set;function n(r){(r.style.fontFamily||getComputedStyle(r).fontFamily).split(",").forEach(i=>{e.add(_(i))}),Array.from(r.children).forEach(i=>{i instanceof HTMLElement&&n(i)})}return n(t),e}async function At(t,e){const n=await Ft(t,e),r=It(t);return(await Promise.all(n.filter(i=>r.has(_(i.style.fontFamily))).map(i=>{const a=i.parentStyleSheet?i.parentStyleSheet.href:null;return M(i.cssText,a,e)}))).join(`
`)}async function Dt(t,e){const n=e.fontEmbedCSS!=null?e.fontEmbedCSS:e.skipFonts?null:await At(t,e);if(n){const r=document.createElement("style"),s=document.createTextNode(n);r.appendChild(s),t.firstChild?t.insertBefore(r,t.firstChild):t.appendChild(r)}}async function vt(t,e={}){const{width:n,height:r}=R(t,e),s=await p(t,e,!0);return await Dt(s,e),await O(s,e),$t(s,e),await Q(s,n,r)}async function b(t,e={}){const{width:n,height:r}=R(t,e),s=await vt(t,e),i=await w(s),a=document.createElement("canvas"),c=a.getContext("2d"),o=e.pixelRatio||G(),f=e.canvasWidth||n,h=e.canvasHeight||r;return a.width=f*o,a.height=h*o,e.skipAutoScale||X(a),a.style.width=`${f}`,a.style.height=`${h}`,e.backgroundColor&&(c.fillStyle=e.backgroundColor,c.fillRect(0,0,a.width,a.height)),c.drawImage(i,0,0,a.width,a.height),a}async function Ut(t,e={}){const{width:n,height:r}=R(t,e);return(await b(t,e)).getContext("2d").getImageData(0,0,n,r).data}async function kt(t,e={}){return(await b(t,e)).toDataURL()}async function Ht(t,e={}){return(await b(t,e)).toDataURL("image/jpeg",e.quality||1)}async function Vt(t,e={}){const n=await b(t,e);return await J(n)}export{Ut as a,Vt as b,Ht as c,kt as d,vt as e,b as t};