ray-template/js/es-DRsCFgZT.js

2 lines
13 KiB
JavaScript

function e(e,t){if(e.match(/^[a-z]+:\/\//i))return e;if(e.match(/^\/\//))return window.location.protocol+e;if(e.match(/^[a-z]+:/i))return e;let n=document.implementation.createHTMLDocument(),r=n.createElement(`base`),i=n.createElement(`a`);return n.head.appendChild(r),n.body.appendChild(i),t&&(r.href=t),i.href=e,i.href}var t=(()=>{let e=0,t=()=>`0000${(Math.random()*36**4|0).toString(36)}`.slice(-4);return()=>(e+=1,`u${t()}${e}`)})();function n(e){let t=[];for(let n=0,r=e.length;n<r;n++)t.push(e[n]);return t}var r=null;function i(e={}){return r||(e.includeStyleProperties?(r=e.includeStyleProperties,r):(r=n(window.getComputedStyle(document.documentElement)),r))}function a(e,t){let n=(e.ownerDocument.defaultView||window).getComputedStyle(e).getPropertyValue(t);return n?parseFloat(n.replace(`px`,``)):0}function o(e){let t=a(e,`border-left-width`),n=a(e,`border-right-width`);return e.clientWidth+t+n}function s(e){let t=a(e,`border-top-width`),n=a(e,`border-bottom-width`);return e.clientHeight+t+n}function c(e,t={}){return{width:t.width||o(e),height:t.height||s(e)}}function ee(){let e,t;try{t=process}catch{}let n=t&&t.env?t.env.devicePixelRatio:null;return n&&(e=parseInt(n,10),Number.isNaN(e)&&(e=1)),e||window.devicePixelRatio||1}var l=16384;function te(e){(e.width>l||e.height>l)&&(e.width>l&&e.height>l?e.width>e.height?(e.height*=l/e.width,e.width=l):(e.width*=l/e.height,e.height=l):e.width>l?(e.height*=l/e.width,e.width=l):(e.width*=l/e.height,e.height=l))}function u(e,t={}){return e.toBlob?new Promise(n=>{e.toBlob(n,t.type?t.type:`image/png`,t.quality?t.quality:1)}):new Promise(n=>{let r=window.atob(e.toDataURL(t.type?t.type:void 0,t.quality?t.quality:void 0).split(`,`)[1]),i=r.length,a=new Uint8Array(i);for(let e=0;e<i;e+=1)a[e]=r.charCodeAt(e);n(new Blob([a],{type:t.type?t.type:`image/png`}))})}function d(e){return new Promise((t,n)=>{let r=new Image;r.onload=()=>{r.decode().then(()=>{requestAnimationFrame(()=>t(r))})},r.onerror=n,r.crossOrigin=`anonymous`,r.decoding=`async`,r.src=e})}async function f(e){return Promise.resolve().then(()=>new XMLSerializer().serializeToString(e)).then(encodeURIComponent).then(e=>`data:image/svg+xml;charset=utf-8,${e}`)}async function p(e,t,n){let r=`http://www.w3.org/2000/svg`,i=document.createElementNS(r,`svg`),a=document.createElementNS(r,`foreignObject`);return i.setAttribute(`width`,`${t}`),i.setAttribute(`height`,`${n}`),i.setAttribute(`viewBox`,`0 0 ${t} ${n}`),a.setAttribute(`width`,`100%`),a.setAttribute(`height`,`100%`),a.setAttribute(`x`,`0`),a.setAttribute(`y`,`0`),a.setAttribute(`externalResourcesRequired`,`true`),i.appendChild(a),a.appendChild(e),f(i)}var m=(e,t)=>{if(e instanceof t)return!0;let n=Object.getPrototypeOf(e);return n===null?!1:n.constructor.name===t.name||m(n,t)};function ne(e){let t=e.getPropertyValue(`content`);return`${e.cssText} content: '${t.replace(/'|"/g,``)}';`}function re(e,t){return i(t).map(t=>`${t}: ${e.getPropertyValue(t)}${e.getPropertyPriority(t)?` !important`:``};`).join(` `)}function ie(e,t,n,r){let i=`.${e}:${t}`,a=n.cssText?ne(n):re(n,r);return document.createTextNode(`${i}{${a}}`)}function h(e,n,r,i){let a=window.getComputedStyle(e,r),o=a.getPropertyValue(`content`);if(o===``||o===`none`)return;let s=t();try{n.className=`${n.className} ${s}`}catch{return}let c=document.createElement(`style`);c.appendChild(ie(s,r,a,i)),n.appendChild(c)}function ae(e,t,n){h(e,t,`:before`,n),h(e,t,`:after`,n)}var g=`application/font-woff`,_=`image/jpeg`,v={woff:g,woff2:g,ttf:`application/font-truetype`,eot:`application/vnd.ms-fontobject`,png:`image/png`,jpg:_,jpeg:_,gif:`image/gif`,tiff:`image/tiff`,svg:`image/svg+xml`,webp:`image/webp`};function y(e){let t=/\.([^./]*?)$/g.exec(e);return t?t[1]:``}function b(e){return v[y(e).toLowerCase()]||``}function oe(e){return e.split(/,/)[1]}function x(e){return e.search(/^(data:)/)!==-1}function S(e,t){return`data:${t};base64,${e}`}async function C(e,t,n){let r=await fetch(e,t);if(r.status===404)throw Error(`Resource "${r.url}" not found`);let i=await r.blob();return new Promise((e,t)=>{let a=new FileReader;a.onerror=t,a.onloadend=()=>{try{e(n({res:r,result:a.result}))}catch(e){t(e)}},a.readAsDataURL(i)})}var w={};function se(e,t,n){let r=e.replace(/\?.*/,``);return n&&(r=e),/ttf|otf|eot|woff2?/i.test(r)&&(r=r.replace(/.*\//,``)),t?`[${t}]${r}`:r}async function T(e,t,n){let r=se(e,t,n.includeQueryParams);if(w[r]!=null)return w[r];n.cacheBust&&(e+=(/\?/.test(e)?`&`:`?`)+new Date().getTime());let i;try{i=S(await C(e,n.fetchRequestInit,({res:e,result:n})=>(t||=e.headers.get(`Content-Type`)||``,oe(n))),t)}catch(t){i=n.imagePlaceholder||``,`${e}`,t&&(typeof t==`string`||t.message)}return w[r]=i,i}async function ce(e){let t=e.toDataURL();return t===`data:,`?e.cloneNode(!1):d(t)}async function le(e,t){if(e.currentSrc){let t=document.createElement(`canvas`),n=t.getContext(`2d`);return t.width=e.clientWidth,t.height=e.clientHeight,n?.drawImage(e,0,0,t.width,t.height),d(t.toDataURL())}let n=e.poster;return d(await T(n,b(n),t))}async function ue(e,t){try{if(e?.contentDocument?.body)return await N(e.contentDocument.body,t,!0)}catch{}return e.cloneNode(!1)}async function de(e,t){return m(e,HTMLCanvasElement)?ce(e):m(e,HTMLVideoElement)?le(e,t):m(e,HTMLIFrameElement)?ue(e,t):e.cloneNode(E(e))}var fe=e=>e.tagName!=null&&e.tagName.toUpperCase()===`SLOT`,E=e=>e.tagName!=null&&e.tagName.toUpperCase()===`SVG`;async function D(e,t,r){if(E(t))return t;let i=[];return i=fe(e)&&e.assignedNodes?n(e.assignedNodes()):m(e,HTMLIFrameElement)&&e.contentDocument?.body?n(e.contentDocument.body.childNodes):n((e.shadowRoot??e).childNodes),i.length===0||m(e,HTMLVideoElement)||await i.reduce((e,n)=>e.then(()=>N(n,r)).then(e=>{e&&t.appendChild(e)}),Promise.resolve()),t}function O(e,t,n){let r=t.style;if(!r)return;let a=window.getComputedStyle(e);a.cssText?(r.cssText=a.cssText,r.transformOrigin=a.transformOrigin):i(n).forEach(n=>{let i=a.getPropertyValue(n);n===`font-size`&&i.endsWith(`px`)&&(i=`${Math.floor(parseFloat(i.substring(0,i.length-2)))-.1}px`),m(e,HTMLIFrameElement)&&n===`display`&&i===`inline`&&(i=`block`),n===`d`&&t.getAttribute(`d`)&&(i=`path(${t.getAttribute(`d`)})`),r.setProperty(n,i,a.getPropertyPriority(n))})}function k(e,t){m(e,HTMLTextAreaElement)&&(t.innerHTML=e.value),m(e,HTMLInputElement)&&t.setAttribute(`value`,e.value)}function A(e,t){if(m(e,HTMLSelectElement)){let n=t,r=Array.from(n.children).find(t=>e.value===t.getAttribute(`value`));r&&r.setAttribute(`selected`,``)}}function j(e,t,n){return m(t,Element)&&(O(e,t,n),ae(e,t,n),k(e,t),A(e,t)),t}async function M(e,t){let n=e.querySelectorAll?e.querySelectorAll(`use`):[];if(n.length===0)return e;let r={};for(let i=0;i<n.length;i++){let a=n[i].getAttribute(`xlink:href`);if(a){let n=e.querySelector(a),i=document.querySelector(a);!n&&i&&!r[a]&&(r[a]=await N(i,t,!0))}}let i=Object.values(r);if(i.length){let t=`http://www.w3.org/1999/xhtml`,n=document.createElementNS(t,`svg`);n.setAttribute(`xmlns`,t),n.style.position=`absolute`,n.style.width=`0`,n.style.height=`0`,n.style.overflow=`hidden`,n.style.display=`none`;let r=document.createElementNS(t,`defs`);n.appendChild(r);for(let e=0;e<i.length;e++)r.appendChild(i[e]);e.appendChild(n)}return e}async function N(e,t,n){return!n&&t.filter&&!t.filter(e)?null:Promise.resolve(e).then(e=>de(e,t)).then(n=>D(e,n,t)).then(n=>j(e,n,t)).then(e=>M(e,t))}var P=/url\((['"]?)([^'"]+?)\1\)/g,F=/url\([^)]+\)\s*format\((["']?)([^"']+)\1\)/g,I=/src:\s*(?:url\([^)]+\)\s*format\([^)]+\)[,;]\s*)+/g;function L(e){let t=e.replace(/([.*+?^${}()|\[\]\/\\])/g,`\\$1`);return RegExp(`(url\\(['"]?)(${t})(['"]?\\))`,`g`)}function R(e){let t=[];return e.replace(P,(e,n,r)=>(t.push(r),e)),t.filter(e=>!x(e))}async function z(t,n,r,i,a){try{let o=r?e(n,r):n,s=b(n),c;return c=a?S(await a(o),s):await T(o,s,i),t.replace(L(n),`$1${c}$3`)}catch{}return t}function B(e,{preferredFontFormat:t}){return t?e.replace(I,e=>{for(;;){let[n,,r]=F.exec(e)||[];if(!r)return``;if(r===t)return`src: ${n};`}}):e}function V(e){return e.search(P)!==-1}async function H(e,t,n){if(!V(e))return e;let r=B(e,n);return R(r).reduce((e,r)=>e.then(e=>z(e,r,t,n)),Promise.resolve(r))}async function U(e,t,n){let r=t.style?.getPropertyValue(e);if(r){let i=await H(r,null,n);return t.style.setProperty(e,i,t.style.getPropertyPriority(e)),!0}return!1}async function pe(e,t){await U(`background`,e,t)||await U(`background-image`,e,t),await U(`mask`,e,t)||await U(`-webkit-mask`,e,t)||await U(`mask-image`,e,t)||await U(`-webkit-mask-image`,e,t)}async function me(e,t){let n=m(e,HTMLImageElement);if((!n||x(e.src))&&(!m(e,SVGImageElement)||x(e.href.baseVal)))return;let r=n?e.src:e.href.baseVal,i=await T(r,b(r),t);await new Promise((r,a)=>{e.onload=r,e.onerror=t.onImageErrorHandler?(...e)=>{try{r(t.onImageErrorHandler(...e))}catch(e){a(e)}}:a;let o=e;o.decode&&=r,o.loading===`lazy`&&(o.loading=`eager`),n?(e.srcset=``,e.src=i):e.href.baseVal=i})}async function he(e,t){let r=n(e.childNodes).map(e=>W(e,t));await Promise.all(r).then(()=>e)}async function W(e,t){m(e,Element)&&(await pe(e,t),await me(e,t),await he(e,t))}function ge(e,t){let{style:n}=e;t.backgroundColor&&(n.backgroundColor=t.backgroundColor),t.width&&(n.width=`${t.width}px`),t.height&&(n.height=`${t.height}px`);let r=t.style;return r!=null&&Object.keys(r).forEach(e=>{n[e]=r[e]}),e}var G={};async function K(e){let t=G[e];return t??(t={url:e,cssText:await(await fetch(e)).text()},G[e]=t),t}async function q(e,t){let n=e.cssText,r=/url\(["']?([^"')]+)["']?\)/g,i=(n.match(/url\([^)]+\)/g)||[]).map(async i=>{let a=i.replace(r,`$1`);return a.startsWith(`https://`)||(a=new URL(a,e.url).href),C(a,t.fetchRequestInit,({result:e})=>(n=n.replace(i,`url(${e})`),[i,e]))});return Promise.all(i).then(()=>n)}function J(e){if(e==null)return[];let t=[],n=e.replace(/(\/\*[\s\S]*?\*\/)/gi,``),r=RegExp(`((@.*?keyframes [\\s\\S]*?){([\\s\\S]*?}\\s*?)})`,`gi`);for(;;){let e=r.exec(n);if(e===null)break;t.push(e[0])}n=n.replace(r,``);let i=/@import[\s\S]*?url\([^)]*\)[\s\S]*?;/gi,a=RegExp(`((\\s*?(?:\\/\\*[\\s\\S]*?\\*\\/)?\\s*?@media[\\s\\S]*?){([\\s\\S]*?)}\\s*?})|(([\\s\\S]*?){([\\s\\S]*?)})`,`gi`);for(;;){let e=i.exec(n);if(e===null){if(e=a.exec(n),e===null)break;i.lastIndex=a.lastIndex}else a.lastIndex=i.lastIndex;t.push(e[0])}return t}async function _e(e,t){let r=[],i=[];return e.forEach(r=>{if(`cssRules`in r)try{n(r.cssRules||[]).forEach((e,n)=>{if(e.type===CSSRule.IMPORT_RULE){let a=n+1,o=e.href,s=K(o).then(e=>q(e,t)).then(e=>J(e).forEach(e=>{try{r.insertRule(e,e.startsWith(`@import`)?a+=1:r.cssRules.length)}catch{}})).catch(e=>{});i.push(s)}})}catch{let n=e.find(e=>e.href==null)||document.styleSheets[0];r.href!=null&&i.push(K(r.href).then(e=>q(e,t)).then(e=>J(e).forEach(e=>{n.insertRule(e,n.cssRules.length)})).catch(e=>{}))}}),Promise.all(i).then(()=>(e.forEach(e=>{if(`cssRules`in e)try{n(e.cssRules||[]).forEach(e=>{r.push(e)})}catch{}}),r))}function ve(e){return e.filter(e=>e.type===CSSRule.FONT_FACE_RULE).filter(e=>V(e.style.getPropertyValue(`src`)))}async function ye(e,t){if(e.ownerDocument==null)throw Error(`Provided element is not within a Document`);return ve(await _e(n(e.ownerDocument.styleSheets),t))}function Y(e){return e.trim().replace(/["']/g,``)}function be(e){let t=new Set;function n(e){(e.style.fontFamily||getComputedStyle(e).fontFamily).split(`,`).forEach(e=>{t.add(Y(e))}),Array.from(e.children).forEach(e=>{e instanceof HTMLElement&&n(e)})}return n(e),t}async function X(e,t){let n=await ye(e,t),r=be(e);return(await Promise.all(n.filter(e=>r.has(Y(e.style.fontFamily))).map(e=>{let n=e.parentStyleSheet?e.parentStyleSheet.href:null;return H(e.cssText,n,t)}))).join(`
`)}async function xe(e,t){let n=t.fontEmbedCSS==null?t.skipFonts?null:await X(e,t):t.fontEmbedCSS;if(n){let t=document.createElement(`style`),r=document.createTextNode(n);t.appendChild(r),e.firstChild?e.insertBefore(t,e.firstChild):e.appendChild(t)}}async function Z(e,t={}){let{width:n,height:r}=c(e,t),i=await N(e,t,!0);return await xe(i,t),await W(i,t),ge(i,t),await p(i,n,r)}async function Q(e,t={}){let{width:n,height:r}=c(e,t),i=await d(await Z(e,t)),a=document.createElement(`canvas`),o=a.getContext(`2d`),s=t.pixelRatio||ee(),l=t.canvasWidth||n,u=t.canvasHeight||r;return a.width=l*s,a.height=u*s,t.skipAutoScale||te(a),a.style.width=`${l}`,a.style.height=`${u}`,t.backgroundColor&&(o.fillStyle=t.backgroundColor,o.fillRect(0,0,a.width,a.height)),o.drawImage(i,0,0,a.width,a.height),a}async function Se(e,t={}){let{width:n,height:r}=c(e,t);return(await Q(e,t)).getContext(`2d`).getImageData(0,0,n,r).data}async function Ce(e,t={}){return(await Q(e,t)).toDataURL()}async function $(e,t={}){return(await Q(e,t)).toDataURL(`image/jpeg`,t.quality||1)}async function we(e,t={}){return await u(await Q(e,t))}async function Te(e,t={}){return X(e,t)}export{Te as getFontEmbedCSS,we as toBlob,Q as toCanvas,$ as toJpeg,Se as toPixelData,Ce as toPng,Z as toSvg};