styimat 4.1.0 → 4.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +143 -201
- package/dist/styimat.js +367 -384
- package/dist/styimat.min.js +23 -20
- package/dist/styimat.min.mjs +23 -20
- package/dist/styimat.mjs +368 -385
- package/package.json +1 -1
package/dist/styimat.min.js
CHANGED
|
@@ -1,31 +1,34 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* MIT License
|
|
3
3
|
* Copyright (c) 2025 王小玗
|
|
4
|
-
*/const styimat=(function(){let d={rootSelector:":root",variablePrefix:"--",preserveOriginal:!1,indentSize:4,enableNesting:!0,autoProcessStyleTags:!0,styleTagAttribute:"e",convertLabToRGB:!0,convertLchToRGB:!0,enableP3:!0,enableMath:!0,mathPrecision:6,importBaseUrl:"",importCache:!0,importTimeout:5e3,enableAlias:!0},
|
|
5
|
-
`),s=[];for(let r of n){const o=r.trim();if(o.startsWith("#")){const
|
|
6
|
-
`)}}function
|
|
7
|
-
`),s=[];for(let r of n){const o=r.trim();if(o.startsWith("@alias")){const
|
|
8
|
-
`)}}function
|
|
9
|
-
`),
|
|
10
|
-
|
|
11
|
-
`}c=!1,i=null}o+=l
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
`
|
|
15
|
-
|
|
16
|
-
`});r
|
|
4
|
+
*/const styimat=(function(){let d={rootSelector:":root",variablePrefix:"--",preserveOriginal:!1,indentSize:4,enableNesting:!0,autoProcessStyleTags:!0,styleTagAttribute:"e",convertLabToRGB:!0,convertLchToRGB:!0,enableP3:!0,enableMath:!0,mathPrecision:6,importBaseUrl:"",importCache:!0,importTimeout:5e3,enableAlias:!0,enableMacros:!0},x=null;const V=new Map,B=new Map,v=new Map;function ee(e){const t={...d},n=e.split(`
|
|
5
|
+
`),s=[];for(let r of n){const o=r.trim();if(o.startsWith("#")){const a=o.substring(1).trim(),c=a.indexOf(" ");if(c!==-1){const l=a.substring(0,c).trim(),i=a.substring(c+1).trim(),u=se(l);u in t?i==="true"||i==="false"?t[u]=i==="true":!isNaN(i)&&i.trim()!==""?t[u]=Number(i):t[u]=i:console.warn(`\u672A\u77E5\u7684\u914D\u7F6E\u9879: ${l}`)}else console.warn(`\u65E0\u6548\u7684\u914D\u7F6E\u884C: ${o}`)}else s.push(r)}return{config:t,css:s.join(`
|
|
6
|
+
`)}}function te(e){const t=new Map,n=e.split(`
|
|
7
|
+
`),s=[];for(let r of n){const o=r.trim();if(o.startsWith("@alias")){const a=o.match(/^@alias\s+([a-zA-Z0-9_-]+)\s+(.+?)\s*;$/);if(a){const c=a[1],l=a[2];t.set(c,l);continue}}s.push(r)}return{aliases:t,css:s.join(`
|
|
8
|
+
`)}}function N(e,t){if(!t.enableMacros)return{macros:new Map,css:e};const n=new Map,s=e.split(`
|
|
9
|
+
`),r=[];let o=!1,a="",c=[],l=[],i=0;for(let u of s){const p=u.trim();if(!o&&p.startsWith("@macro")){const f=p.match(/^@macro\s+([a-zA-Z0-9_-]+)\s*\(([^)]*)\)\s*\{$/);if(f){o=!0,a=f[1],c=f[2].split(",").map(h=>h.trim()).filter(h=>h).map(h=>{const m=h.split(":").map(y=>y.trim());return{name:m[0].slice(1),defaultValue:m[1]||null}}),l=[],i=1;continue}}if(o){for(const f of u)f==="{"&&i++,f==="}"&&i--;i===0?(n.set(a,{params:c,body:l.join(`
|
|
10
|
+
`)}),o=!1,a="",c=[],l=[]):l.push(u);continue}r.push(u)}return{macros:n,css:r.join(`
|
|
11
|
+
`)}}function ne(e,t,n){if(!n.enableMacros||t.size===0)return e;const s=Array.from(t.keys()).map(a=>a.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")).join("|"),r=new RegExp(`@(${s})\\s*\\(([^)]*)\\)\\s*;?`,"g");return(a=>{let c=a,l=!1;do l=!1,c=c.replace(r,(i,u,p)=>{l=!0;const f=t.get(u);if(!f)return console.warn(`\u672A\u5B9A\u4E49\u7684\u5B8F: ${u}`),i;const h=H(p,f.params);return W(f.body,h,n)});while(l);return c})(e)}function H(e,t){const n=new Map,s=re(e);for(let r=0;r<t.length;r++){const o=t[r];let a;r<s.length?a=s[r]:o.defaultValue!==null?a=o.defaultValue:(console.warn(`\u5B8F\u8C03\u7528\u7F3A\u5C11\u5FC5\u9700\u53C2\u6570: ${o.name}`),a=""),n.set(o.name,a)}return s.length>t.length&&console.warn("\u5B8F\u8C03\u7528\u4F20\u9012\u4E86\u8FC7\u591A\u53C2\u6570\uFF0C\u591A\u4F59\u7684\u53C2\u6570\u5C06\u88AB\u5FFD\u7565"),n}function re(e){const t=[];let n="",s=!1,r="",o=0,a=0;for(let c=0;c<e.length;c++){const l=e[c];(l==='"'||l==="'")&&!s?(s=!0,r=l):l===r&&s&&(s=!1,r=""),s||(l==="("&&o++,l===")"&&o--,l==="["&&a++,l==="]"&&a--),l===","&&!s&&o===0&&a===0?(t.push(n.trim()),n=""):n+=l}return n.trim()&&t.push(n.trim()),t}function W(e,t,n){let s=e;for(const[r,o]of t){const a=new RegExp(`\\$${r}`,"g");s=s.replace(a,o)}if(n.enableMacros){const r=Array.from(v.keys()).map(o=>o.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")).join("|");if(r){const o=new RegExp(`@(${r})\\s*\\(([^)]*)\\)\\s*;?`,"g");let a;do a=!1,s=s.replace(o,(c,l,i)=>{a=!0;const u=v.get(l);if(!u)return c;const p=H(i,u.params);return W(u.body,p,n)});while(a)}}return s=R(s,n),s}function se(e){return e.replace(/-([a-z])/g,function(t,n){return n.toUpperCase()})}function O(){if(x!==null)return x;if(typeof window>"u"||!window.CSS)return x=!1,!1;try{x=CSS.supports("color","color(display-p3 1 0 0)")}catch(e){console.warn("P3\u652F\u6301\u68C0\u6D4B\u5931\u8D25:",e),x=!1}return x}function G(e,t=[]){let n=e.trim();if(n.endsWith(";")&&(n=n.slice(0,-1)),!n)return[];const s=[];let r="",o=0,a=!1,c="";for(let i=0;i<n.length;i++){const u=n[i];(u==='"'||u==="'")&&!a?(a=!0,c=u):u===c&&a&&(a=!1,c=""),a||(u==="("?o++:u===")"&&o--),u===";"&&!a&&o===0?r.trim()&&(s.push(r.trim()),r=""):r+=u}r.trim()&&s.push(r.trim());const l=[];for(const i of s){if(!i.trim())continue;const u=oe(i);if(u===-1){console.warn(`\u65E0\u6548\u7684CSS\u58F0\u660E: "${i}"`);continue}let p=i.substring(0,u).trim(),f=i.substring(u+1).trim();f.endsWith(";")&&(f=f.slice(0,-1).trim()),t.get(p)&&(p=t.get(p)),l.push({[p]:f})}return l}function oe(e){let t=!1,n="";for(let s=0;s<e.length;s++){const r=e[s];if((r==='"'||r==="'")&&!t?(t=!0,n=r):r===n&&t&&(t=!1,n=""),r===":"&&!t)return s}return-1}function _(e,t){if(!t.enableMath)return`math(${e})`;try{let n=e.replace(/\s+/g,"");const s=U(n,t);return ae(n)?ce(n,s):q(s,t.mathPrecision)}catch(n){return console.warn("math()\u8868\u8FBE\u5F0F\u89E3\u6790\u5931\u8D25:",e,n),`math(${e})`}}function U(e,t){for(;e.includes("(")&&e.includes(")");){const n=e.lastIndexOf("("),s=e.indexOf(")",n);if(s===-1)break;const r=e.substring(n+1,s),o=U(r,t);e=e.substring(0,n)+o+e.substring(s+1)}return X(e,t)}function X(e,t){const n=[{regex:/([\d.]+(?:[a-zA-Z%]+)?)\*([\d.]+(?:[a-zA-Z%]+)?)/,handler:le},{regex:/([\d.]+(?:[a-zA-Z%]+)?)\/([\d.]+(?:[a-zA-Z%]+)?)/,handler:ie},{regex:/([\d.]+(?:[a-zA-Z%]+)?)\+([\d.]+(?:[a-zA-Z%]+)?)/,handler:ue},{regex:/([\d.]+(?:[a-zA-Z%]+)?)-([\d.]+(?:[a-zA-Z%]+)?)/,handler:fe}];let s=e;for(const o of n){let a;for(;(a=e.match(o.regex))!==null;){const c=z(a[1]),l=z(a[2]),i=o.handler(c,l);e=e.substring(0,a.index)+i.value+e.substring(a.index+a[0].length)}}return e!==s?X(e,t):z(e).value}function z(e){const t=e.match(/^([\d.]+)([a-zA-Z%]*)$/);if(!t)throw new Error(`\u65E0\u6CD5\u89E3\u6790\u503C: ${e}`);const n=parseFloat(t[1]),s=t[2]||"";return{value:n,unit:s}}function ae(e){return/[a-zA-Z%]/.test(e)}function ce(e,t){const n=e.match(/([a-zA-Z%]+)(?!.*[a-zA-Z%])/);return n?t+n[1]:e.includes("%")?t+"%":t+"px"}function le(e,t){return e.unit===t.unit||!e.unit&&!t.unit?{value:e.value*t.value,unit:e.unit||t.unit}:{value:`${e.value}${e.unit}*${t.value}${t.unit}`,unit:""}}function ie(e,t){if(t.value===0)throw new Error("\u9664\u4EE5\u96F6");return e.unit&&!t.unit?{value:e.value/t.value,unit:e.unit}:e.unit===t.unit?{value:e.value/t.value,unit:""}:{value:`${e.value}${e.unit}/${t.value}${t.unit}`,unit:""}}function ue(e,t){return e.unit===t.unit?{value:e.value+t.value,unit:e.unit}:{value:`${e.value}${e.unit}+${t.value}${t.unit}`,unit:""}}function fe(e,t){return e.unit===t.unit?{value:e.value-t.value,unit:e.unit}:{value:`${e.value}${e.unit}-${t.value}${t.unit}`,unit:""}}function q(e,t=6){const n=Math.pow(10,t),r=(Math.round(e*n)/n).toString();return r.includes(".")?r.replace(/(\.\d*?)0+$/,"$1").replace(/\.$/,""):r}function pe(e,t){if(!t.enableMath)return e;let n=e;const s=/math\(([^)]+)\)/gi,r=a=>a.replace(s,(c,l)=>_(l,t));let o;do o=n,n=r(n);while(n!==o&&n.includes("math("));return n}function he(e,t){if(!t.convertLabToRGB&&!t.convertLchToRGB)return e;let n=e;n=de(n,t);const s=/(lab|lch)\([^)]+\)/gi,r=new Map,o=c=>c.replace(s,l=>{if(r.has(l))return r.get(l);let i=l;return l.toLowerCase().startsWith("lab(")?t.convertLabToRGB&&(i=me(l,t)):l.toLowerCase().startsWith("lch(")&&t.convertLchToRGB&&(i=ge(l,t)),r.set(l,i),i});let a;do a=n,n=o(n);while(n!==a);return n}function de(e,t){let n=e;const s=/lab#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/gi,r=/lch#([0-9a-f]{2})([0-9a-f]{2})(\d{1,3})/gi,o=new Map;return n=n.replace(s,(a,c,l,i)=>{if(o.has(a))return o.get(a);try{const u=parseInt(c,16)/255*100,p=(parseInt(l,16)-128)*1.5,f=(parseInt(i,16)-128)*1.5,h=L(u,p,f,t);return o.set(a,h),h}catch(u){return console.warn(`\u65E0\u6CD5\u89E3\u6790lab#\u5341\u516D\u8FDB\u5236\u989C\u8272: ${a}`,u),a}}),n=n.replace(r,(a,c,l,i)=>{if(o.has(a))return o.get(a);try{const u=parseInt(c,16)/255*100,p=parseInt(l,16)/255*150,f=parseInt(i)/100*360,h=A(u,p,f),m=L(h.L,h.a,h.b,t);return o.set(a,m),m}catch(u){return console.warn(`\u65E0\u6CD5\u89E3\u6790lch#\u5341\u516D\u8FDB\u5236\u989C\u8272: ${a}`,u),a}}),n}function me(e,t){const n=/lab\(\s*([\d.]+)(%?)\s+([\d.-]+)\s+([\d.-]+)(?:\s*\/\s*([\d.%]+))?\s*\)/i,s=e.match(n);if(!s)return e;try{let r=parseFloat(s[1]);s[2]==="%"?r=r:(r<0&&(r=0),r>100&&(r=100));const o=parseFloat(s[3]),a=parseFloat(s[4]),c=s[5]!==void 0?s[5].includes("%")?parseFloat(s[5])/100:parseFloat(s[5]):null;return L(r,o,a,t,c)}catch(r){return console.warn(`\u65E0\u6CD5\u8F6C\u6362LAB\u989C\u8272: ${e}`,r),e}}function ge(e,t){const n=/lch\(\s*([\d.]+)(%?)\s+([\d.]+)\s+([\d.]+)(deg)?(?:\s*\/\s*([\d.%]+))?\s*\)/i,s=e.match(n);if(!s)return e;try{let r=parseFloat(s[1]);s[2]==="%"?r=r:(r<0&&(r=0),r>100&&(r=100));const o=parseFloat(s[3]);let a=parseFloat(s[4]);o<0&&console.warn(`LCH\u4E2D\u7684C\u503C\u4E0D\u80FD\u4E3A\u8D1F: ${o}`),a=(a%360+360)%360;const c=A(r,o,a),l=s[6]!==void 0?s[6].includes("%")?parseFloat(s[6])/100:parseFloat(s[6]):null;return L(c.L,c.a,c.b,t,l)}catch(r){return console.warn(`\u65E0\u6CD5\u8F6C\u6362LCH\u989C\u8272: ${e}`,r),e}}function A(e,t,n){const s=n*Math.PI/180,r=t*Math.cos(s),o=t*Math.sin(s);return{L:e,a:r,b:o}}function C(e,t,n){const s=(g,b,$)=>{const I=(g+16)/116,Z=b/500+I,j=I-$/200,Le=Z**3>.008856?Z**3:(116*Z-16)/903.3,Re=g>903.3*.008856?((g+16)/116)**3:g/903.3,xe=j**3>.008856?j**3:(116*j-16)/903.3;return[Le*.95047,Re*1,xe*1.08883]},r=(g,b,$)=>{const w=[[3.2404542,-1.5371385,-.4985314],[-.969266,1.8760108,.041556],[.0556434,-.2040259,1.0572252]],M=w[0][0]*g+w[0][1]*b+w[0][2]*$,S=w[1][0]*g+w[1][1]*b+w[1][2]*$,F=w[2][0]*g+w[2][1]*b+w[2][2]*$;return[M,S,F]},o=g=>{const b=g<0?-1:1,$=Math.abs(g);return $<=.0031308?b*12.92*$:b*(1.055*Math.pow($,.4166666666666667)-.055)},a=g=>Math.max(0,Math.min(255,Math.round(g*255))),[c,l,i]=s(e,t,n),[u,p,f]=r(c,l,i),h=o(u),m=o(p),y=o(f);return{r:a(h),g:a(m),b:a(y)}}function E(e,t,n){const s=(c,l,i)=>{const y=(c+16)/116,g=l/500+y,b=y-i/200,$=g**3>.008856?g**3:(116*g-16)/903.3,w=c>903.3*.008856?((c+16)/116)**3:c/903.3,M=b**3>.008856?b**3:(116*b-16)/903.3;return[$*.95047,w*1,M*1.08883]},r=(c,l,i)=>{const u=[[2.493496911941425,-.9313836179191239,-.40271078445071684],[-.8294889695615747,1.7626640603183463,.023624685841943577],[.03584583024378447,-.07617238926804182,.9568845240076872]],p=u[0][0]*c+u[0][1]*l+u[0][2]*i,f=u[1][0]*c+u[1][1]*l+u[1][2]*i,h=u[2][0]*c+u[2][1]*l+u[2][2]*i;return[p,f,h]},o=c=>{const l=c<0?-1:1,i=Math.abs(c);return i<=.0031308?l*12.92*i:l*(1.055*Math.pow(i,.4166666666666667)-.055)},a=c=>Math.max(0,Math.min(255,Math.round(c*255)));try{const[c,l,i]=s(e,t,n),[u,p,f]=r(c,l,i),h=o(u),m=o(p),y=o(f);return{r:Math.max(0,Math.min(1,h)),g:Math.max(0,Math.min(1,m)),b:Math.max(0,Math.min(1,y))}}catch(c){console.warn("P3\u8F6C\u6362\u5931\u8D25:",c);const l=C(e,t,n);return{r:l.r/255,g:l.g/255,b:l.b/255}}}function L(e,t,n,s,r=null){if(!s.enableP3||!O()){const o=C(e,t,n);return r!==null?`rgba(${o.r}, ${o.g}, ${o.b}, ${r})`:`rgb(${o.r}, ${o.g}, ${o.b})`}else{const o=E(e,t,n);return r!==null?`color(display-p3 ${o.r.toFixed(4)} ${o.g.toFixed(4)} ${o.b.toFixed(4)} / ${r})`:`color(display-p3 ${o.r.toFixed(4)} ${o.g.toFixed(4)} ${o.b.toFixed(4)})`}}function R(e,t){let n=e;return t.enableMath&&(n=pe(n,t)),(t.convertLabToRGB||t.convertLchToRGB)&&(n=he(n,t)),n}function be(e,t){const n=e.split(`
|
|
12
|
+
`),s={},r=new Map;let o="",a=null,c=!1,l=0;for(let i of n){const u=i.trim(),p=u.match(/^\$([a-zA-Z0-9_-]+)\s*:\s*(.+?);?$/);if(p){const[,f,h]=p,m=R(Y(h.trim(),{...s,...a?r.get(a)||{}:{}}),t);a?(r.has(a)||r.set(a,{}),r.get(a)[f]=m):s[f]=m;continue}if(u.endsWith("{")){a=u.slice(0,-1).trim(),c=!0,o+=i+`
|
|
13
|
+
`;continue}if(u==="}"){if(c&&a&&r.has(a)){const f=r.get(a),h=" ".repeat(l);for(const[m,y]of Object.entries(f))o+=`${h} --${m}: ${y};
|
|
14
|
+
`}c=!1,a=null}o+=i+`
|
|
15
|
+
`,i.includes("{")&&(l+=t.indentSize),i.includes("}")&&(l=Math.max(0,l-t.indentSize))}return{globalVariables:s,selectorVariables:r,cssWithoutVars:o.trim()}}function ye(e,t,n){const s=e.split(`
|
|
16
|
+
`),r=[],o=[];let a=0;for(let c=0;c<s.length;c++){const l=s[c],i=l.trim();if(!i)continue;const u=l.match(/^(\s*)/)[0].length;if(i==="}"){if(r.length>0){const p=r.pop();if(r.length>0){const f=r[r.length-1];f.children||(f.children=[]),f.children.push(p)}else o.push(p)}continue}if(i.endsWith("{")){const f={selector:i.slice(0,-1).trim(),properties:[],children:[]};r.push(f);continue}if(!i.includes("{")&&!i.includes("}")&&i.includes(":")){if(r.length>0){const p=r[r.length-1];G(i,n).forEach(h=>{const m=Object.keys(h)[0],y=R(h[m],t);p.properties.push(`${m}: ${y}`)})}continue}}for(;r.length>0;){const c=r.pop();if(r.length===0)o.push(c);else{const l=r[r.length-1];l.children||(l.children=[]),l.children.push(c)}}return D(o,t,"",n)}function D(e,t,n="",s=[]){let r="";const o=n.startsWith("@");for(const a of e){const c=a.selector.startsWith("@");let l=(o?" ".repeat(t.indentSize):"")+a.selector;if(c&&(l=a.selector+` {
|
|
17
|
+
`),n&&(l.includes("&")?l=l.replace(/&/g,n):l.trim().startsWith(":")?l=n+l:l=n+(o?"":" ")+l),a.properties.length>0){r+=(c?"":l)+` {
|
|
18
|
+
`;for(const i of a.properties)G(i,s).forEach(p=>{const f=Object.keys(p)[0],h=R(p[f],t);r+=(o?" ".repeat(t.indentSize):"")+" ".repeat(t.indentSize)+`${f}: ${h};
|
|
19
|
+
`});r+=o?" ".repeat(t.indentSize)+`}
|
|
17
20
|
`:`}
|
|
18
21
|
|
|
19
|
-
`}
|
|
22
|
+
`}a.children&&a.children.length>0&&(r+=D(a.children,t,l,s)),o&&(r+=`}
|
|
20
23
|
|
|
21
24
|
`)}return r.trim()+`
|
|
22
25
|
|
|
23
|
-
`}function
|
|
24
|
-
`);return`${
|
|
26
|
+
`}function Y(e,t){return e.replace(/\$([a-zA-Z0-9_-]+)/g,(n,s)=>t[s]?`var(--${s})`:n)}function $e(e,t,n,s){let r=e;return r=r.replace(/(?:\$\$|\$)([a-zA-Z0-9_-]+)/g,(o,a)=>o.startsWith("$$")?`attr(${a})`:`var(--${a})`),r=R(r,s),r}function we(e,t){if(Object.keys(e).length===0)return"";const n=Object.entries(e).map(([s,r])=>{const o=R(Y(r,e),t);return" ".repeat(t.indentSize)+`--${s}: ${o};`}).join(`
|
|
27
|
+
`);return`${t.rootSelector} {
|
|
25
28
|
${n}
|
|
26
29
|
}
|
|
27
30
|
|
|
28
|
-
`}function
|
|
29
|
-
`);let o=null;for(let
|
|
30
|
-
`}o=null}s+=
|
|
31
|
-
`}return s.trim()}async function
|
|
31
|
+
`}function ve(e,t,n){let s="";const r=e.split(`
|
|
32
|
+
`);let o=null;for(let a of r){const c=a.trim();if(c.endsWith("{")&&(o=c.slice(0,-1).trim()),c==="}"&&o){if(t.has(o)){const l=t.get(o),i=a.match(/^(\s*)/)[0];for(const[u,p]of Object.entries(l))s+=" --"+u+": "+p+`;
|
|
33
|
+
`}o=null}s+=R(a,n)+`
|
|
34
|
+
`}return s.trim()}async function Q(e,t){const n=/@import\s+([^;]+?)\s*;/g;return(async r=>{const o=[],a=r.replace(n,(c,l)=>{const i=Me(l,t).then(u=>Q(u,t)).catch(u=>(console.warn(`\u65E0\u6CD5\u5BFC\u5165CSS\u6587\u4EF6: ${l}`,u),""));return o.push(i),`__IMPORT_PLACEHOLDER_${o.length-1}__`});if(o.length>0){const c=await Promise.all(o);let l=a;for(let i=0;i<c.length;i++)l=l.replace(`__IMPORT_PLACEHOLDER_${i}__`,c[i]);return l}return a})(e)}async function Me(e,t){if(t.importCache&&V.has(e))return V.get(e);const n=t.importBaseUrl?new URL(e,t.importBaseUrl).href:e;let s;if(typeof process<"u"&&process.versions&&process.versions.node&&typeof require<"u")try{const o=require("fs"),a=require("path");let c=n;n.startsWith(".")&&(c=a.join(process.cwd(),n)),s=o.readFileSync(c,"utf-8")}catch(o){throw new Error(`Node.js\u6587\u4EF6\u8BFB\u53D6\u5931\u8D25: ${n} - ${o.message}`)}else try{const o=new AbortController,a=setTimeout(()=>o.abort(),t.importTimeout),c=await fetch(n,{signal:o.signal,headers:{Accept:"text/css"}});if(clearTimeout(a),!c.ok)throw new Error(`HTTP\u9519\u8BEF: ${c.status} ${c.statusText}`);s=await c.text()}catch(o){throw o.name==="AbortError"?new Error(`\u5BFC\u5165\u8D85\u65F6: ${n}`):new Error(`\u65E0\u6CD5\u83B7\u53D6CSS: ${n} - ${o.message}`)}return t.importCache&&V.set(e,s),s}function P(e,t={}){const n=(o,a)=>{const{config:c,css:l}=ee(o),i={...d,...t,...c};let u=l,p=new Map;if(i.enableAlias){const{aliases:S,css:F}=te(u);p=S,u=F}let f=u,h=new Map;if(i.enableMacros){const{macros:S,css:F}=N(f,i);h=S,f=F;for(const[J,I]of h)v.set(J,I)}let m=f;i.enableMacros&&h.size>0&&(m=ne(m,h,i));const{globalVariables:y,selectorVariables:g,cssWithoutVars:b}=be(m,i);let $=b.trim();if(i.enableNesting&&b.includes("{"))try{$=ye(b,i,p)}catch(S){console.warn("\u5D4C\u5957\u89E3\u6790\u5931\u8D25\uFF0C\u4F7F\u7528\u539F\u59CBCSS:",S)}const w=we(y,i);let M=$;return g.size>0&&(M=ve($,g,i)),M=$e(M,y,g,i),w+M},s=e&&/@import\s+([^;]+?)\s*;/g.test(e),r={...d,...t};return s?(async()=>{try{const o=await Q(e,r);return n(o,r)}catch(o){return console.error("@import\u5904\u7406\u5931\u8D25:",o),n(e,r)}})():n(e,r)}function K(e={}){const t={...d,...e},n=document.querySelectorAll(`style[${t.styleTagAttribute||"e"}]`);return n.forEach(s=>{let r=s.textContent;(async()=>{try{s.getAttribute("src")&&(r="@import "+s.getAttribute("src")+";");const a=await P(r,t),c=document.createElement("style");c.textContent=a,s.parentNode.insertBefore(c,s.nextSibling),t.preserveOriginal?s.style.display="none":s.remove()}catch(a){console.error("\u5904\u7406style\u6807\u7B7E\u5931\u8D25:",a)}})()}),n.length}function Ce(e={}){d={...d,...e}}function k(e,t={}){const n={...d,...t};if(e){if(/@import\s+([^;]+?)\s*;/g.test(e))return(async()=>{try{const r=await P(e,n),o=document.createElement("style");return o.textContent=r,document.head.appendChild(o),o}catch(r){return console.error("\u5E94\u7528CSS\u5931\u8D25:",r),null}})();{const r=P(e,n),o=document.createElement("style");return o.textContent=r,document.head.appendChild(o),o}}else document.readyState==="loading"?document.addEventListener("DOMContentLoaded",()=>{K(n)}):K(n)}const Se={convert:P,apply:k,config:Ce,supportsP3:O(),detectP3Support:O,imports:{clearCache:function(){V.clear()},setBaseUrl:function(e){d.importBaseUrl=e},setCacheEnabled:function(e){d.importCache=e},setTimeout:function(e){d.importTimeout=e}},aliases:{add:function(e,t){B.set(e,t)},remove:function(e){B.delete(e)},getAll:function(){return Array.from(B.entries())},clear:function(){B.clear()}},macros:{define:function(e,t,n=[]){if(typeof t=="function"){const r=t.toString().match(/{([\s\S]*)}/);r&&v.set(e,{params:n.map(o=>typeof o=="string"?{name:o}:o),body:r[1].trim()})}else v.set(e,{params:n.map(s=>typeof s=="string"?{name:s}:s),body:t})},call:function(e,...t){const n=v.get(e);if(!n)throw new Error(`\u672A\u5B9A\u4E49\u7684\u5B8F: ${e}`);const s=new Map;for(let r=0;r<n.params.length;r++){const o=n.params[r],a=r<t.length?t[r]:o.defaultValue;if(a===void 0&&o.defaultValue===null)throw new Error(`\u7F3A\u5C11\u5FC5\u9700\u53C2\u6570: ${o.name}`);s.set(o.name,a!==void 0?a:"")}return W(n.body,s,d)},getAll:function(){return Array.from(v.entries())},remove:function(e){v.delete(e)},clear:function(){v.clear()},parse:function(e){const{macros:t}=N(e,d);for(const[n,s]of t)v.set(n,s)}},math:{evaluate:function(e){return _(e,d)},parseUnit:z,round:q,test:function(e,t={}){const n={...d,...t};try{const s=_(e,n);return{success:!0,expression:e,result:s,parsed:z(s.replace(/^calc\(|\)$/g,""))}}catch(s){return{success:!1,expression:e,error:s.message}}}},colorUtils:{labToRGB:C,lchToLab:A,lchToRGB:function(e,t,n){const s=A(e,t,n);return C(s.L,s.a,s.b)},labToP3:E,lchToP3:function(e,t,n){const s=A(e,t,n);return E(s.L,s.a,s.b)},parseHexLab:function(e){const t=e.match(/lab#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/i);if(!t)return null;const n=t[1],s=t[2],r=t[3],o=parseInt(n,16)/255*100,a=(parseInt(s,16)-128)*1.5,c=(parseInt(r,16)-128)*1.5;return C(o,a,c)},parseHexLch:function(e){const t=e.match(/lch#([0-9a-f]{2})([0-9a-f]{2})(\d{1,3})/i);if(!t)return null;const n=t[1],s=t[2],r=t[3],o=parseInt(n,16)/255*100,a=parseInt(s,16)/255*150,c=parseInt(r)/100*360,l=A(o,a,c);return C(l.L,l.a,l.b)},generateColor:function(e,t,n,s=null,r=!0){return L(e,t,n,{enableP3:r},s)},parseColor:function(e){try{const t=e.match(/lab\(\s*([\d.]+)(%?)\s+([\d.-]+)\s+([\d.-]+)(?:\s*\/\s*([\d.%]+))?\s*\)/i);if(t){let s=parseFloat(t[1]);const r=parseFloat(t[3]),o=parseFloat(t[4]),a=t[5]?t[5].includes("%")?parseFloat(t[5])/100:parseFloat(t[5]):null,c=L(s,r,o,d,a);return{L:s,A:r,B:o,alpha:a,rgb:C(s,r,o),p3:E(s,r,o),colorString:c}}const n=e.match(/lch\(\s*([\d.]+)(%?)\s+([\d.]+)\s+([\d.]+)(deg)?(?:\s*\/\s*([\d.%]+))?\s*\)/i);if(n){let s=parseFloat(n[1]);const r=parseFloat(n[3]);let o=parseFloat(n[4]);const a=n[6]?n[6].includes("%")?parseFloat(n[6])/100:parseFloat(n[6]):null,c=A(s,r,o),l=L(c.L,c.a,c.b,d,a);return{L:s,C:r,H:o,alpha:a,lab:c,rgb:C(c.L,c.a,c.b),p3:E(c.L,c.a,c.b),colorString:l}}return null}catch(t){return console.warn("\u65E0\u6CD5\u89E3\u6790\u989C\u8272:",e,t),null}}}},T=function(...e){if(e.length>1||e[0]&&e[0].raw)return Ae(...e);const t=e[0];if(typeof t=="string"){const n=P(t,{...d,...e[1]});return n&&typeof n.then=="function",n}return typeof t=="object"&&t!==null?(d={...d,...t},T):e.length===0?k():T};function Ae(e,...t){let n=e[0];for(let r=0;r<t.length;r++){const o=t[r];let a="";typeof o=="function"?a=o():Array.isArray(o)?a=o.join(" "):a=String(o??""),n+=a+e[r+1]}const s=P(n,d);return s&&typeof s.then=="function",s}return Object.assign(T,Se),Object.setPrototypeOf(T,Function.prototype),typeof window<"u"&&(k(),Object.defineProperty(window.HTMLElement.prototype,"cssVar",{get(){const e=this;return new Proxy(()=>{},{get(t,n){const s=n.startsWith("--")?n:`--${n}`;return e.style.getPropertyValue(s)},set(t,n,s){const r=n.startsWith("--")?n:`--${n}`;return e.style.setProperty(r,s),!0},apply(t,n,s){const r=s[0],o=s[1],a=r.startsWith("--")?r:`--${r}`;if(o===void 0)return e.style.getPropertyValue(a);e.style.setProperty(a,o)}})}})),T})();if(typeof define=="function"&&define.amd)define([],()=>styimat);else if(typeof module=="object"&&module.exports)module.exports=styimat;else{const d=globalThis??(typeof self<"u"&&self)??(typeof window<"u"&&window)??global??{};d.styimat=styimat}
|
package/dist/styimat.min.mjs
CHANGED
|
@@ -1,31 +1,34 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* MIT License
|
|
3
3
|
* Copyright (c) 2025 王小玗
|
|
4
|
-
*/const
|
|
5
|
-
`),s=[];for(let r of n){const o=r.trim();if(o.startsWith("#")){const
|
|
6
|
-
`)}}function
|
|
7
|
-
`),s=[];for(let r of n){const o=r.trim();if(o.startsWith("@alias")){const
|
|
8
|
-
`)}}function
|
|
9
|
-
`),
|
|
10
|
-
|
|
11
|
-
`}i=!1,c=null}o+=l
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
`
|
|
15
|
-
|
|
16
|
-
`});r
|
|
4
|
+
*/const V=(function(){let d={rootSelector:":root",variablePrefix:"--",preserveOriginal:!1,indentSize:4,enableNesting:!0,autoProcessStyleTags:!0,styleTagAttribute:"e",convertLabToRGB:!0,convertLchToRGB:!0,enableP3:!0,enableMath:!0,mathPrecision:6,importBaseUrl:"",importCache:!0,importTimeout:5e3,enableAlias:!0,enableMacros:!0},x=null;const B=new Map,I=new Map,v=new Map;function te(e){const t={...d},n=e.split(`
|
|
5
|
+
`),s=[];for(let r of n){const o=r.trim();if(o.startsWith("#")){const a=o.substring(1).trim(),c=a.indexOf(" ");if(c!==-1){const l=a.substring(0,c).trim(),i=a.substring(c+1).trim(),u=oe(l);u in t?i==="true"||i==="false"?t[u]=i==="true":!isNaN(i)&&i.trim()!==""?t[u]=Number(i):t[u]=i:console.warn(`\u672A\u77E5\u7684\u914D\u7F6E\u9879: ${l}`)}else console.warn(`\u65E0\u6548\u7684\u914D\u7F6E\u884C: ${o}`)}else s.push(r)}return{config:t,css:s.join(`
|
|
6
|
+
`)}}function ne(e){const t=new Map,n=e.split(`
|
|
7
|
+
`),s=[];for(let r of n){const o=r.trim();if(o.startsWith("@alias")){const a=o.match(/^@alias\s+([a-zA-Z0-9_-]+)\s+(.+?)\s*;$/);if(a){const c=a[1],l=a[2];t.set(c,l);continue}}s.push(r)}return{aliases:t,css:s.join(`
|
|
8
|
+
`)}}function H(e,t){if(!t.enableMacros)return{macros:new Map,css:e};const n=new Map,s=e.split(`
|
|
9
|
+
`),r=[];let o=!1,a="",c=[],l=[],i=0;for(let u of s){const p=u.trim();if(!o&&p.startsWith("@macro")){const f=p.match(/^@macro\s+([a-zA-Z0-9_-]+)\s*\(([^)]*)\)\s*\{$/);if(f){o=!0,a=f[1],c=f[2].split(",").map(h=>h.trim()).filter(h=>h).map(h=>{const m=h.split(":").map(y=>y.trim());return{name:m[0].slice(1),defaultValue:m[1]||null}}),l=[],i=1;continue}}if(o){for(const f of u)f==="{"&&i++,f==="}"&&i--;i===0?(n.set(a,{params:c,body:l.join(`
|
|
10
|
+
`)}),o=!1,a="",c=[],l=[]):l.push(u);continue}r.push(u)}return{macros:n,css:r.join(`
|
|
11
|
+
`)}}function re(e,t,n){if(!n.enableMacros||t.size===0)return e;const s=Array.from(t.keys()).map(a=>a.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")).join("|"),r=new RegExp(`@(${s})\\s*\\(([^)]*)\\)\\s*;?`,"g");return(a=>{let c=a,l=!1;do l=!1,c=c.replace(r,(i,u,p)=>{l=!0;const f=t.get(u);if(!f)return console.warn(`\u672A\u5B9A\u4E49\u7684\u5B8F: ${u}`),i;const h=U(p,f.params);return O(f.body,h,n)});while(l);return c})(e)}function U(e,t){const n=new Map,s=se(e);for(let r=0;r<t.length;r++){const o=t[r];let a;r<s.length?a=s[r]:o.defaultValue!==null?a=o.defaultValue:(console.warn(`\u5B8F\u8C03\u7528\u7F3A\u5C11\u5FC5\u9700\u53C2\u6570: ${o.name}`),a=""),n.set(o.name,a)}return s.length>t.length&&console.warn("\u5B8F\u8C03\u7528\u4F20\u9012\u4E86\u8FC7\u591A\u53C2\u6570\uFF0C\u591A\u4F59\u7684\u53C2\u6570\u5C06\u88AB\u5FFD\u7565"),n}function se(e){const t=[];let n="",s=!1,r="",o=0,a=0;for(let c=0;c<e.length;c++){const l=e[c];(l==='"'||l==="'")&&!s?(s=!0,r=l):l===r&&s&&(s=!1,r=""),s||(l==="("&&o++,l===")"&&o--,l==="["&&a++,l==="]"&&a--),l===","&&!s&&o===0&&a===0?(t.push(n.trim()),n=""):n+=l}return n.trim()&&t.push(n.trim()),t}function O(e,t,n){let s=e;for(const[r,o]of t){const a=new RegExp(`\\$${r}`,"g");s=s.replace(a,o)}if(n.enableMacros){const r=Array.from(v.keys()).map(o=>o.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")).join("|");if(r){const o=new RegExp(`@(${r})\\s*\\(([^)]*)\\)\\s*;?`,"g");let a;do a=!1,s=s.replace(o,(c,l,i)=>{a=!0;const u=v.get(l);if(!u)return c;const p=U(i,u.params);return O(u.body,p,n)});while(a)}}return s=R(s,n),s}function oe(e){return e.replace(/-([a-z])/g,function(t,n){return n.toUpperCase()})}function _(){if(x!==null)return x;if(typeof window>"u"||!window.CSS)return x=!1,!1;try{x=CSS.supports("color","color(display-p3 1 0 0)")}catch(e){console.warn("P3\u652F\u6301\u68C0\u6D4B\u5931\u8D25:",e),x=!1}return x}function G(e,t=[]){let n=e.trim();if(n.endsWith(";")&&(n=n.slice(0,-1)),!n)return[];const s=[];let r="",o=0,a=!1,c="";for(let i=0;i<n.length;i++){const u=n[i];(u==='"'||u==="'")&&!a?(a=!0,c=u):u===c&&a&&(a=!1,c=""),a||(u==="("?o++:u===")"&&o--),u===";"&&!a&&o===0?r.trim()&&(s.push(r.trim()),r=""):r+=u}r.trim()&&s.push(r.trim());const l=[];for(const i of s){if(!i.trim())continue;const u=ae(i);if(u===-1){console.warn(`\u65E0\u6548\u7684CSS\u58F0\u660E: "${i}"`);continue}let p=i.substring(0,u).trim(),f=i.substring(u+1).trim();f.endsWith(";")&&(f=f.slice(0,-1).trim()),t.get(p)&&(p=t.get(p)),l.push({[p]:f})}return l}function ae(e){let t=!1,n="";for(let s=0;s<e.length;s++){const r=e[s];if((r==='"'||r==="'")&&!t?(t=!0,n=r):r===n&&t&&(t=!1,n=""),r===":"&&!t)return s}return-1}function k(e,t){if(!t.enableMath)return`math(${e})`;try{let n=e.replace(/\s+/g,"");const s=X(n,t);return ce(n)?le(n,s):D(s,t.mathPrecision)}catch(n){return console.warn("math()\u8868\u8FBE\u5F0F\u89E3\u6790\u5931\u8D25:",e,n),`math(${e})`}}function X(e,t){for(;e.includes("(")&&e.includes(")");){const n=e.lastIndexOf("("),s=e.indexOf(")",n);if(s===-1)break;const r=e.substring(n+1,s),o=X(r,t);e=e.substring(0,n)+o+e.substring(s+1)}return q(e,t)}function q(e,t){const n=[{regex:/([\d.]+(?:[a-zA-Z%]+)?)\*([\d.]+(?:[a-zA-Z%]+)?)/,handler:ie},{regex:/([\d.]+(?:[a-zA-Z%]+)?)\/([\d.]+(?:[a-zA-Z%]+)?)/,handler:ue},{regex:/([\d.]+(?:[a-zA-Z%]+)?)\+([\d.]+(?:[a-zA-Z%]+)?)/,handler:fe},{regex:/([\d.]+(?:[a-zA-Z%]+)?)-([\d.]+(?:[a-zA-Z%]+)?)/,handler:pe}];let s=e;for(const o of n){let a;for(;(a=e.match(o.regex))!==null;){const c=z(a[1]),l=z(a[2]),i=o.handler(c,l);e=e.substring(0,a.index)+i.value+e.substring(a.index+a[0].length)}}return e!==s?q(e,t):z(e).value}function z(e){const t=e.match(/^([\d.]+)([a-zA-Z%]*)$/);if(!t)throw new Error(`\u65E0\u6CD5\u89E3\u6790\u503C: ${e}`);const n=parseFloat(t[1]),s=t[2]||"";return{value:n,unit:s}}function ce(e){return/[a-zA-Z%]/.test(e)}function le(e,t){const n=e.match(/([a-zA-Z%]+)(?!.*[a-zA-Z%])/);return n?t+n[1]:e.includes("%")?t+"%":t+"px"}function ie(e,t){return e.unit===t.unit||!e.unit&&!t.unit?{value:e.value*t.value,unit:e.unit||t.unit}:{value:`${e.value}${e.unit}*${t.value}${t.unit}`,unit:""}}function ue(e,t){if(t.value===0)throw new Error("\u9664\u4EE5\u96F6");return e.unit&&!t.unit?{value:e.value/t.value,unit:e.unit}:e.unit===t.unit?{value:e.value/t.value,unit:""}:{value:`${e.value}${e.unit}/${t.value}${t.unit}`,unit:""}}function fe(e,t){return e.unit===t.unit?{value:e.value+t.value,unit:e.unit}:{value:`${e.value}${e.unit}+${t.value}${t.unit}`,unit:""}}function pe(e,t){return e.unit===t.unit?{value:e.value-t.value,unit:e.unit}:{value:`${e.value}${e.unit}-${t.value}${t.unit}`,unit:""}}function D(e,t=6){const n=Math.pow(10,t),r=(Math.round(e*n)/n).toString();return r.includes(".")?r.replace(/(\.\d*?)0+$/,"$1").replace(/\.$/,""):r}function he(e,t){if(!t.enableMath)return e;let n=e;const s=/math\(([^)]+)\)/gi,r=a=>a.replace(s,(c,l)=>k(l,t));let o;do o=n,n=r(n);while(n!==o&&n.includes("math("));return n}function de(e,t){if(!t.convertLabToRGB&&!t.convertLchToRGB)return e;let n=e;n=me(n,t);const s=/(lab|lch)\([^)]+\)/gi,r=new Map,o=c=>c.replace(s,l=>{if(r.has(l))return r.get(l);let i=l;return l.toLowerCase().startsWith("lab(")?t.convertLabToRGB&&(i=ge(l,t)):l.toLowerCase().startsWith("lch(")&&t.convertLchToRGB&&(i=be(l,t)),r.set(l,i),i});let a;do a=n,n=o(n);while(n!==a);return n}function me(e,t){let n=e;const s=/lab#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/gi,r=/lch#([0-9a-f]{2})([0-9a-f]{2})(\d{1,3})/gi,o=new Map;return n=n.replace(s,(a,c,l,i)=>{if(o.has(a))return o.get(a);try{const u=parseInt(c,16)/255*100,p=(parseInt(l,16)-128)*1.5,f=(parseInt(i,16)-128)*1.5,h=L(u,p,f,t);return o.set(a,h),h}catch(u){return console.warn(`\u65E0\u6CD5\u89E3\u6790lab#\u5341\u516D\u8FDB\u5236\u989C\u8272: ${a}`,u),a}}),n=n.replace(r,(a,c,l,i)=>{if(o.has(a))return o.get(a);try{const u=parseInt(c,16)/255*100,p=parseInt(l,16)/255*150,f=parseInt(i)/100*360,h=A(u,p,f),m=L(h.L,h.a,h.b,t);return o.set(a,m),m}catch(u){return console.warn(`\u65E0\u6CD5\u89E3\u6790lch#\u5341\u516D\u8FDB\u5236\u989C\u8272: ${a}`,u),a}}),n}function ge(e,t){const n=/lab\(\s*([\d.]+)(%?)\s+([\d.-]+)\s+([\d.-]+)(?:\s*\/\s*([\d.%]+))?\s*\)/i,s=e.match(n);if(!s)return e;try{let r=parseFloat(s[1]);s[2]==="%"?r=r:(r<0&&(r=0),r>100&&(r=100));const o=parseFloat(s[3]),a=parseFloat(s[4]),c=s[5]!==void 0?s[5].includes("%")?parseFloat(s[5])/100:parseFloat(s[5]):null;return L(r,o,a,t,c)}catch(r){return console.warn(`\u65E0\u6CD5\u8F6C\u6362LAB\u989C\u8272: ${e}`,r),e}}function be(e,t){const n=/lch\(\s*([\d.]+)(%?)\s+([\d.]+)\s+([\d.]+)(deg)?(?:\s*\/\s*([\d.%]+))?\s*\)/i,s=e.match(n);if(!s)return e;try{let r=parseFloat(s[1]);s[2]==="%"?r=r:(r<0&&(r=0),r>100&&(r=100));const o=parseFloat(s[3]);let a=parseFloat(s[4]);o<0&&console.warn(`LCH\u4E2D\u7684C\u503C\u4E0D\u80FD\u4E3A\u8D1F: ${o}`),a=(a%360+360)%360;const c=A(r,o,a),l=s[6]!==void 0?s[6].includes("%")?parseFloat(s[6])/100:parseFloat(s[6]):null;return L(c.L,c.a,c.b,t,l)}catch(r){return console.warn(`\u65E0\u6CD5\u8F6C\u6362LCH\u989C\u8272: ${e}`,r),e}}function A(e,t,n){const s=n*Math.PI/180,r=t*Math.cos(s),o=t*Math.sin(s);return{L:e,a:r,b:o}}function C(e,t,n){const s=(g,b,$)=>{const W=(g+16)/116,j=b/500+W,N=W-$/200,Re=j**3>.008856?j**3:(116*j-16)/903.3,xe=g>903.3*.008856?((g+16)/116)**3:g/903.3,Pe=N**3>.008856?N**3:(116*N-16)/903.3;return[Re*.95047,xe*1,Pe*1.08883]},r=(g,b,$)=>{const w=[[3.2404542,-1.5371385,-.4985314],[-.969266,1.8760108,.041556],[.0556434,-.2040259,1.0572252]],M=w[0][0]*g+w[0][1]*b+w[0][2]*$,S=w[1][0]*g+w[1][1]*b+w[1][2]*$,F=w[2][0]*g+w[2][1]*b+w[2][2]*$;return[M,S,F]},o=g=>{const b=g<0?-1:1,$=Math.abs(g);return $<=.0031308?b*12.92*$:b*(1.055*Math.pow($,.4166666666666667)-.055)},a=g=>Math.max(0,Math.min(255,Math.round(g*255))),[c,l,i]=s(e,t,n),[u,p,f]=r(c,l,i),h=o(u),m=o(p),y=o(f);return{r:a(h),g:a(m),b:a(y)}}function E(e,t,n){const s=(c,l,i)=>{const y=(c+16)/116,g=l/500+y,b=y-i/200,$=g**3>.008856?g**3:(116*g-16)/903.3,w=c>903.3*.008856?((c+16)/116)**3:c/903.3,M=b**3>.008856?b**3:(116*b-16)/903.3;return[$*.95047,w*1,M*1.08883]},r=(c,l,i)=>{const u=[[2.493496911941425,-.9313836179191239,-.40271078445071684],[-.8294889695615747,1.7626640603183463,.023624685841943577],[.03584583024378447,-.07617238926804182,.9568845240076872]],p=u[0][0]*c+u[0][1]*l+u[0][2]*i,f=u[1][0]*c+u[1][1]*l+u[1][2]*i,h=u[2][0]*c+u[2][1]*l+u[2][2]*i;return[p,f,h]},o=c=>{const l=c<0?-1:1,i=Math.abs(c);return i<=.0031308?l*12.92*i:l*(1.055*Math.pow(i,.4166666666666667)-.055)},a=c=>Math.max(0,Math.min(255,Math.round(c*255)));try{const[c,l,i]=s(e,t,n),[u,p,f]=r(c,l,i),h=o(u),m=o(p),y=o(f);return{r:Math.max(0,Math.min(1,h)),g:Math.max(0,Math.min(1,m)),b:Math.max(0,Math.min(1,y))}}catch(c){console.warn("P3\u8F6C\u6362\u5931\u8D25:",c);const l=C(e,t,n);return{r:l.r/255,g:l.g/255,b:l.b/255}}}function L(e,t,n,s,r=null){if(!s.enableP3||!_()){const o=C(e,t,n);return r!==null?`rgba(${o.r}, ${o.g}, ${o.b}, ${r})`:`rgb(${o.r}, ${o.g}, ${o.b})`}else{const o=E(e,t,n);return r!==null?`color(display-p3 ${o.r.toFixed(4)} ${o.g.toFixed(4)} ${o.b.toFixed(4)} / ${r})`:`color(display-p3 ${o.r.toFixed(4)} ${o.g.toFixed(4)} ${o.b.toFixed(4)})`}}function R(e,t){let n=e;return t.enableMath&&(n=he(n,t)),(t.convertLabToRGB||t.convertLchToRGB)&&(n=de(n,t)),n}function ye(e,t){const n=e.split(`
|
|
12
|
+
`),s={},r=new Map;let o="",a=null,c=!1,l=0;for(let i of n){const u=i.trim(),p=u.match(/^\$([a-zA-Z0-9_-]+)\s*:\s*(.+?);?$/);if(p){const[,f,h]=p,m=R(Q(h.trim(),{...s,...a?r.get(a)||{}:{}}),t);a?(r.has(a)||r.set(a,{}),r.get(a)[f]=m):s[f]=m;continue}if(u.endsWith("{")){a=u.slice(0,-1).trim(),c=!0,o+=i+`
|
|
13
|
+
`;continue}if(u==="}"){if(c&&a&&r.has(a)){const f=r.get(a),h=" ".repeat(l);for(const[m,y]of Object.entries(f))o+=`${h} --${m}: ${y};
|
|
14
|
+
`}c=!1,a=null}o+=i+`
|
|
15
|
+
`,i.includes("{")&&(l+=t.indentSize),i.includes("}")&&(l=Math.max(0,l-t.indentSize))}return{globalVariables:s,selectorVariables:r,cssWithoutVars:o.trim()}}function $e(e,t,n){const s=e.split(`
|
|
16
|
+
`),r=[],o=[];let a=0;for(let c=0;c<s.length;c++){const l=s[c],i=l.trim();if(!i)continue;const u=l.match(/^(\s*)/)[0].length;if(i==="}"){if(r.length>0){const p=r.pop();if(r.length>0){const f=r[r.length-1];f.children||(f.children=[]),f.children.push(p)}else o.push(p)}continue}if(i.endsWith("{")){const f={selector:i.slice(0,-1).trim(),properties:[],children:[]};r.push(f);continue}if(!i.includes("{")&&!i.includes("}")&&i.includes(":")){if(r.length>0){const p=r[r.length-1];G(i,n).forEach(h=>{const m=Object.keys(h)[0],y=R(h[m],t);p.properties.push(`${m}: ${y}`)})}continue}}for(;r.length>0;){const c=r.pop();if(r.length===0)o.push(c);else{const l=r[r.length-1];l.children||(l.children=[]),l.children.push(c)}}return Y(o,t,"",n)}function Y(e,t,n="",s=[]){let r="";const o=n.startsWith("@");for(const a of e){const c=a.selector.startsWith("@");let l=(o?" ".repeat(t.indentSize):"")+a.selector;if(c&&(l=a.selector+` {
|
|
17
|
+
`),n&&(l.includes("&")?l=l.replace(/&/g,n):l.trim().startsWith(":")?l=n+l:l=n+(o?"":" ")+l),a.properties.length>0){r+=(c?"":l)+` {
|
|
18
|
+
`;for(const i of a.properties)G(i,s).forEach(p=>{const f=Object.keys(p)[0],h=R(p[f],t);r+=(o?" ".repeat(t.indentSize):"")+" ".repeat(t.indentSize)+`${f}: ${h};
|
|
19
|
+
`});r+=o?" ".repeat(t.indentSize)+`}
|
|
17
20
|
`:`}
|
|
18
21
|
|
|
19
|
-
`}
|
|
22
|
+
`}a.children&&a.children.length>0&&(r+=Y(a.children,t,l,s)),o&&(r+=`}
|
|
20
23
|
|
|
21
24
|
`)}return r.trim()+`
|
|
22
25
|
|
|
23
|
-
`}function
|
|
24
|
-
`);return`${
|
|
26
|
+
`}function Q(e,t){return e.replace(/\$([a-zA-Z0-9_-]+)/g,(n,s)=>t[s]?`var(--${s})`:n)}function we(e,t,n,s){let r=e;return r=r.replace(/(?:\$\$|\$)([a-zA-Z0-9_-]+)/g,(o,a)=>o.startsWith("$$")?`attr(${a})`:`var(--${a})`),r=R(r,s),r}function ve(e,t){if(Object.keys(e).length===0)return"";const n=Object.entries(e).map(([s,r])=>{const o=R(Q(r,e),t);return" ".repeat(t.indentSize)+`--${s}: ${o};`}).join(`
|
|
27
|
+
`);return`${t.rootSelector} {
|
|
25
28
|
${n}
|
|
26
29
|
}
|
|
27
30
|
|
|
28
|
-
`}function
|
|
29
|
-
`);let o=null;for(let
|
|
30
|
-
`}o=null}s+=
|
|
31
|
-
`}return s.trim()}async function
|
|
31
|
+
`}function Me(e,t,n){let s="";const r=e.split(`
|
|
32
|
+
`);let o=null;for(let a of r){const c=a.trim();if(c.endsWith("{")&&(o=c.slice(0,-1).trim()),c==="}"&&o){if(t.has(o)){const l=t.get(o),i=a.match(/^(\s*)/)[0];for(const[u,p]of Object.entries(l))s+=" --"+u+": "+p+`;
|
|
33
|
+
`}o=null}s+=R(a,n)+`
|
|
34
|
+
`}return s.trim()}async function K(e,t){const n=/@import\s+([^;]+?)\s*;/g;return(async r=>{const o=[],a=r.replace(n,(c,l)=>{const i=Ce(l,t).then(u=>K(u,t)).catch(u=>(console.warn(`\u65E0\u6CD5\u5BFC\u5165CSS\u6587\u4EF6: ${l}`,u),""));return o.push(i),`__IMPORT_PLACEHOLDER_${o.length-1}__`});if(o.length>0){const c=await Promise.all(o);let l=a;for(let i=0;i<c.length;i++)l=l.replace(`__IMPORT_PLACEHOLDER_${i}__`,c[i]);return l}return a})(e)}async function Ce(e,t){if(t.importCache&&B.has(e))return B.get(e);const n=t.importBaseUrl?new URL(e,t.importBaseUrl).href:e;let s;if(typeof process<"u"&&process.versions&&process.versions.node&&typeof require<"u")try{const o=require("fs"),a=require("path");let c=n;n.startsWith(".")&&(c=a.join(process.cwd(),n)),s=o.readFileSync(c,"utf-8")}catch(o){throw new Error(`Node.js\u6587\u4EF6\u8BFB\u53D6\u5931\u8D25: ${n} - ${o.message}`)}else try{const o=new AbortController,a=setTimeout(()=>o.abort(),t.importTimeout),c=await fetch(n,{signal:o.signal,headers:{Accept:"text/css"}});if(clearTimeout(a),!c.ok)throw new Error(`HTTP\u9519\u8BEF: ${c.status} ${c.statusText}`);s=await c.text()}catch(o){throw o.name==="AbortError"?new Error(`\u5BFC\u5165\u8D85\u65F6: ${n}`):new Error(`\u65E0\u6CD5\u83B7\u53D6CSS: ${n} - ${o.message}`)}return t.importCache&&B.set(e,s),s}function P(e,t={}){const n=(o,a)=>{const{config:c,css:l}=te(o),i={...d,...t,...c};let u=l,p=new Map;if(i.enableAlias){const{aliases:S,css:F}=ne(u);p=S,u=F}let f=u,h=new Map;if(i.enableMacros){const{macros:S,css:F}=H(f,i);h=S,f=F;for(const[ee,W]of h)v.set(ee,W)}let m=f;i.enableMacros&&h.size>0&&(m=re(m,h,i));const{globalVariables:y,selectorVariables:g,cssWithoutVars:b}=ye(m,i);let $=b.trim();if(i.enableNesting&&b.includes("{"))try{$=$e(b,i,p)}catch(S){console.warn("\u5D4C\u5957\u89E3\u6790\u5931\u8D25\uFF0C\u4F7F\u7528\u539F\u59CBCSS:",S)}const w=ve(y,i);let M=$;return g.size>0&&(M=Me($,g,i)),M=we(M,y,g,i),w+M},s=e&&/@import\s+([^;]+?)\s*;/g.test(e),r={...d,...t};return s?(async()=>{try{const o=await K(e,r);return n(o,r)}catch(o){return console.error("@import\u5904\u7406\u5931\u8D25:",o),n(e,r)}})():n(e,r)}function J(e={}){const t={...d,...e},n=document.querySelectorAll(`style[${t.styleTagAttribute||"e"}]`);return n.forEach(s=>{let r=s.textContent;(async()=>{try{s.getAttribute("src")&&(r="@import "+s.getAttribute("src")+";");const a=await P(r,t),c=document.createElement("style");c.textContent=a,s.parentNode.insertBefore(c,s.nextSibling),t.preserveOriginal?s.style.display="none":s.remove()}catch(a){console.error("\u5904\u7406style\u6807\u7B7E\u5931\u8D25:",a)}})()}),n.length}function Se(e={}){d={...d,...e}}function Z(e,t={}){const n={...d,...t};if(e){if(/@import\s+([^;]+?)\s*;/g.test(e))return(async()=>{try{const r=await P(e,n),o=document.createElement("style");return o.textContent=r,document.head.appendChild(o),o}catch(r){return console.error("\u5E94\u7528CSS\u5931\u8D25:",r),null}})();{const r=P(e,n),o=document.createElement("style");return o.textContent=r,document.head.appendChild(o),o}}else document.readyState==="loading"?document.addEventListener("DOMContentLoaded",()=>{J(n)}):J(n)}const Ae={convert:P,apply:Z,config:Se,supportsP3:_(),detectP3Support:_,imports:{clearCache:function(){B.clear()},setBaseUrl:function(e){d.importBaseUrl=e},setCacheEnabled:function(e){d.importCache=e},setTimeout:function(e){d.importTimeout=e}},aliases:{add:function(e,t){I.set(e,t)},remove:function(e){I.delete(e)},getAll:function(){return Array.from(I.entries())},clear:function(){I.clear()}},macros:{define:function(e,t,n=[]){if(typeof t=="function"){const r=t.toString().match(/{([\s\S]*)}/);r&&v.set(e,{params:n.map(o=>typeof o=="string"?{name:o}:o),body:r[1].trim()})}else v.set(e,{params:n.map(s=>typeof s=="string"?{name:s}:s),body:t})},call:function(e,...t){const n=v.get(e);if(!n)throw new Error(`\u672A\u5B9A\u4E49\u7684\u5B8F: ${e}`);const s=new Map;for(let r=0;r<n.params.length;r++){const o=n.params[r],a=r<t.length?t[r]:o.defaultValue;if(a===void 0&&o.defaultValue===null)throw new Error(`\u7F3A\u5C11\u5FC5\u9700\u53C2\u6570: ${o.name}`);s.set(o.name,a!==void 0?a:"")}return O(n.body,s,d)},getAll:function(){return Array.from(v.entries())},remove:function(e){v.delete(e)},clear:function(){v.clear()},parse:function(e){const{macros:t}=H(e,d);for(const[n,s]of t)v.set(n,s)}},math:{evaluate:function(e){return k(e,d)},parseUnit:z,round:D,test:function(e,t={}){const n={...d,...t};try{const s=k(e,n);return{success:!0,expression:e,result:s,parsed:z(s.replace(/^calc\(|\)$/g,""))}}catch(s){return{success:!1,expression:e,error:s.message}}}},colorUtils:{labToRGB:C,lchToLab:A,lchToRGB:function(e,t,n){const s=A(e,t,n);return C(s.L,s.a,s.b)},labToP3:E,lchToP3:function(e,t,n){const s=A(e,t,n);return E(s.L,s.a,s.b)},parseHexLab:function(e){const t=e.match(/lab#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/i);if(!t)return null;const n=t[1],s=t[2],r=t[3],o=parseInt(n,16)/255*100,a=(parseInt(s,16)-128)*1.5,c=(parseInt(r,16)-128)*1.5;return C(o,a,c)},parseHexLch:function(e){const t=e.match(/lch#([0-9a-f]{2})([0-9a-f]{2})(\d{1,3})/i);if(!t)return null;const n=t[1],s=t[2],r=t[3],o=parseInt(n,16)/255*100,a=parseInt(s,16)/255*150,c=parseInt(r)/100*360,l=A(o,a,c);return C(l.L,l.a,l.b)},generateColor:function(e,t,n,s=null,r=!0){return L(e,t,n,{enableP3:r},s)},parseColor:function(e){try{const t=e.match(/lab\(\s*([\d.]+)(%?)\s+([\d.-]+)\s+([\d.-]+)(?:\s*\/\s*([\d.%]+))?\s*\)/i);if(t){let s=parseFloat(t[1]);const r=parseFloat(t[3]),o=parseFloat(t[4]),a=t[5]?t[5].includes("%")?parseFloat(t[5])/100:parseFloat(t[5]):null,c=L(s,r,o,d,a);return{L:s,A:r,B:o,alpha:a,rgb:C(s,r,o),p3:E(s,r,o),colorString:c}}const n=e.match(/lch\(\s*([\d.]+)(%?)\s+([\d.]+)\s+([\d.]+)(deg)?(?:\s*\/\s*([\d.%]+))?\s*\)/i);if(n){let s=parseFloat(n[1]);const r=parseFloat(n[3]);let o=parseFloat(n[4]);const a=n[6]?n[6].includes("%")?parseFloat(n[6])/100:parseFloat(n[6]):null,c=A(s,r,o),l=L(c.L,c.a,c.b,d,a);return{L:s,C:r,H:o,alpha:a,lab:c,rgb:C(c.L,c.a,c.b),p3:E(c.L,c.a,c.b),colorString:l}}return null}catch(t){return console.warn("\u65E0\u6CD5\u89E3\u6790\u989C\u8272:",e,t),null}}}},T=function(...e){if(e.length>1||e[0]&&e[0].raw)return Le(...e);const t=e[0];if(typeof t=="string"){const n=P(t,{...d,...e[1]});return n&&typeof n.then=="function",n}return typeof t=="object"&&t!==null?(d={...d,...t},T):e.length===0?Z():T};function Le(e,...t){let n=e[0];for(let r=0;r<t.length;r++){const o=t[r];let a="";typeof o=="function"?a=o():Array.isArray(o)?a=o.join(" "):a=String(o??""),n+=a+e[r+1]}const s=P(n,d);return s&&typeof s.then=="function",s}return Object.assign(T,Ae),Object.setPrototypeOf(T,Function.prototype),typeof window<"u"&&(Z(),Object.defineProperty(window.HTMLElement.prototype,"cssVar",{get(){const e=this;return new Proxy(()=>{},{get(t,n){const s=n.startsWith("--")?n:`--${n}`;return e.style.getPropertyValue(s)},set(t,n,s){const r=n.startsWith("--")?n:`--${n}`;return e.style.setProperty(r,s),!0},apply(t,n,s){const r=s[0],o=s[1],a=r.startsWith("--")?r:`--${r}`;if(o===void 0)return e.style.getPropertyValue(a);e.style.setProperty(a,o)}})}})),T})();if(typeof define=="function"&&define.amd)define([],()=>V);else if(typeof module=="object"&&module.exports)module.exports=V;else{const d=globalThis??(typeof self<"u"&&self)??(typeof window<"u"&&window)??global??{};d.styimat=V}export default V;export const{convert,apply,config,supportsP3,detectP3Support,imports,aliases,macros,math,colorUtils}=V;
|