styimat 8.0.1 → 8.2.0
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/dist/styimat.js +38 -39
- package/dist/styimat.min.js +24 -24
- package/dist/styimat.min.mjs +24 -24
- package/dist/styimat.mjs +38 -39
- package/package.json +1 -1
package/dist/styimat.js
CHANGED
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
* 支持 @macro 宏定义系统
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
|
+
|
|
18
19
|
const styimat = (function() {
|
|
19
20
|
// 默认配置
|
|
20
21
|
let defaultConfig = {
|
|
@@ -35,6 +36,22 @@ const styimat = (function() {
|
|
|
35
36
|
enableAlias: true,
|
|
36
37
|
enableMacros: true
|
|
37
38
|
};
|
|
39
|
+
|
|
40
|
+
const REGEX = {
|
|
41
|
+
IMPORT: /@import\s+([^;]+?)\s*;/g,
|
|
42
|
+
ALIAS: /^@alias\s+([a-zA-Z_][a-zA-Z0-9_]*)\s+(.+?)\s*;$/,
|
|
43
|
+
MACRO: /^@macro\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*\(([^)]*)\)\s*\{$/,
|
|
44
|
+
SPECHAR: /[.*+?^${}()|[\]\\]/g,
|
|
45
|
+
COMMENT: /\/\*[\s\S]*?\*\//g,
|
|
46
|
+
MATH_SYMBOL: /([\da-z])([+-])(-?[\da-z])/g,
|
|
47
|
+
MATH: /math\(([^)]+)\)/gi,
|
|
48
|
+
COLOR: /(lab|lch)\([^)]+\)/gi,
|
|
49
|
+
HEXLAB: /lab#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/gi,
|
|
50
|
+
HEXLCH: /lch#([0-9a-f]{2})([0-9a-f]{2})(\d{1,3})/gi,
|
|
51
|
+
SPACE: /\s+/g,
|
|
52
|
+
AND: /&/g,
|
|
53
|
+
HYPHEN: /-([a-z])/g
|
|
54
|
+
}
|
|
38
55
|
|
|
39
56
|
// 全局P3支持检测结果
|
|
40
57
|
let p3Supported = null;
|
|
@@ -105,7 +122,7 @@ const styimat = (function() {
|
|
|
105
122
|
const trimmed = line.trim();
|
|
106
123
|
|
|
107
124
|
if (trimmed.startsWith('@alias')) {
|
|
108
|
-
const aliasMatch = trimmed.match(
|
|
125
|
+
const aliasMatch = trimmed.match(REGEX.ALIAS);
|
|
109
126
|
if (aliasMatch) {
|
|
110
127
|
const aliasName = aliasMatch[1];
|
|
111
128
|
const originalProperty = aliasMatch[2];
|
|
@@ -145,7 +162,7 @@ const styimat = (function() {
|
|
|
145
162
|
const trimmed = line.trim();
|
|
146
163
|
|
|
147
164
|
if (!inMacroDefinition && trimmed.startsWith('@macro')) {
|
|
148
|
-
const macroMatch = trimmed.match(
|
|
165
|
+
const macroMatch = trimmed.match(REGEX.MACRO);
|
|
149
166
|
if (macroMatch) {
|
|
150
167
|
inMacroDefinition = true;
|
|
151
168
|
currentMacroName = macroMatch[1];
|
|
@@ -206,7 +223,7 @@ const styimat = (function() {
|
|
|
206
223
|
}
|
|
207
224
|
|
|
208
225
|
const macroNames = Array.from(macros.keys()).map(name =>
|
|
209
|
-
name.replace(
|
|
226
|
+
name.replace(REGEX.SPECHAR, '\\$&')
|
|
210
227
|
).join('|');
|
|
211
228
|
|
|
212
229
|
const macroCallRegex = new RegExp(`@(${macroNames})\\s*\\(([^)]*)\\)\\s*;?`, 'g');
|
|
@@ -253,17 +270,12 @@ const styimat = (function() {
|
|
|
253
270
|
} else if (param.defaultValue !== null) {
|
|
254
271
|
value = param.defaultValue;
|
|
255
272
|
} else {
|
|
256
|
-
|
|
257
|
-
value = '';
|
|
273
|
+
value = 'none';
|
|
258
274
|
}
|
|
259
275
|
|
|
260
276
|
argsMap.set(param.name, value);
|
|
261
277
|
}
|
|
262
278
|
|
|
263
|
-
if (argValues.length > paramDefs.length) {
|
|
264
|
-
console.warn(`宏调用传递了过多参数,多余的参数将被忽略`);
|
|
265
|
-
}
|
|
266
|
-
|
|
267
279
|
return argsMap;
|
|
268
280
|
}
|
|
269
281
|
|
|
@@ -324,7 +336,7 @@ const styimat = (function() {
|
|
|
324
336
|
|
|
325
337
|
if (config.enableMacros) {
|
|
326
338
|
const macroNames = Array.from(macroRegistry.keys()).map(name =>
|
|
327
|
-
name.replace(
|
|
339
|
+
name.replace(REGEX.SPECHAR, '\\$&')
|
|
328
340
|
).join('|');
|
|
329
341
|
|
|
330
342
|
if (macroNames) {
|
|
@@ -355,7 +367,7 @@ const styimat = (function() {
|
|
|
355
367
|
* 将连字符分隔的字符串转换为驼峰命名
|
|
356
368
|
*/
|
|
357
369
|
function camelCase(str) {
|
|
358
|
-
return str.replace(
|
|
370
|
+
return str.replace(REGEX.HYPHEN, function(match, letter) {
|
|
359
371
|
return letter.toUpperCase();
|
|
360
372
|
});
|
|
361
373
|
}
|
|
@@ -374,7 +386,6 @@ const styimat = (function() {
|
|
|
374
386
|
try {
|
|
375
387
|
p3Supported = CSS.supports('color', 'color(display-p3 1 0 0)');
|
|
376
388
|
} catch (error) {
|
|
377
|
-
console.warn('P3支持检测失败:', error);
|
|
378
389
|
p3Supported = false;
|
|
379
390
|
}
|
|
380
391
|
|
|
@@ -437,7 +448,7 @@ const styimat = (function() {
|
|
|
437
448
|
const result = [];
|
|
438
449
|
|
|
439
450
|
for (let declaration of declarations) {
|
|
440
|
-
declaration = declaration.replace(
|
|
451
|
+
declaration = declaration.replace(REGEX.COMMENT, '');
|
|
441
452
|
if (!declaration.trim()) continue;
|
|
442
453
|
|
|
443
454
|
const colonIndex = findFirstColonOutsideQuotes(declaration);
|
|
@@ -505,14 +516,9 @@ const styimat = (function() {
|
|
|
505
516
|
return `math(${expression})`;
|
|
506
517
|
}
|
|
507
518
|
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
return parseMathExpression(cleanExpr, config.enableMath);
|
|
512
|
-
} catch (error) {
|
|
513
|
-
console.warn('math()表达式解析失败:', expression, error);
|
|
514
|
-
return `calc(${expression})`;
|
|
515
|
-
}
|
|
519
|
+
let cleanExpr = expression.replace(REGEX.SPACE, '');
|
|
520
|
+
|
|
521
|
+
return parseMathExpression(cleanExpr, config.enableMath);
|
|
516
522
|
}
|
|
517
523
|
|
|
518
524
|
/**
|
|
@@ -520,8 +526,7 @@ const styimat = (function() {
|
|
|
520
526
|
*/
|
|
521
527
|
function parseMathExpression(expr, enableMath) {
|
|
522
528
|
return `calc(${expr
|
|
523
|
-
.replace(
|
|
524
|
-
.replace(/([\da-z])([+-])(-?[\da-z])/g, '$1 $2 $3')})`;
|
|
529
|
+
.replace(REGEX.MATH_SYMBOL, '$1 $2 $3')})`;
|
|
525
530
|
}
|
|
526
531
|
|
|
527
532
|
/**
|
|
@@ -533,11 +538,9 @@ const styimat = (function() {
|
|
|
533
538
|
}
|
|
534
539
|
|
|
535
540
|
let result = cssValue;
|
|
536
|
-
const mathRegex = /math\(([^)]+)\)/gi;
|
|
537
541
|
|
|
538
542
|
const processMath = (str) => {
|
|
539
|
-
return str.replace(
|
|
540
|
-
|
|
543
|
+
return str.replace(REGEX.MATH, (match, expression) => {
|
|
541
544
|
return evaluateMathExpression(expression, config);
|
|
542
545
|
});
|
|
543
546
|
};
|
|
@@ -562,11 +565,10 @@ const styimat = (function() {
|
|
|
562
565
|
let result = cssValue;
|
|
563
566
|
result = parseHexColorFormats(result, config);
|
|
564
567
|
|
|
565
|
-
const colorFunctionRegex = /(lab|lch)\([^)]+\)/gi;
|
|
566
568
|
const processedColors = new Map();
|
|
567
569
|
|
|
568
570
|
const processColorFunctions = (str) => {
|
|
569
|
-
return str.replace(
|
|
571
|
+
return str.replace(REGEX.COLOR, (match) => {
|
|
570
572
|
if (processedColors.has(match)) {
|
|
571
573
|
return processedColors.get(match);
|
|
572
574
|
}
|
|
@@ -603,12 +605,9 @@ const styimat = (function() {
|
|
|
603
605
|
function parseHexColorFormats(value, config) {
|
|
604
606
|
let result = value;
|
|
605
607
|
|
|
606
|
-
const hexLabRegex = /lab#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/gi;
|
|
607
|
-
const hexLchRegex = /lch#([0-9a-f]{2})([0-9a-f]{2})(\d{1,3})/gi;
|
|
608
|
-
|
|
609
608
|
const processedHexColors = new Map();
|
|
610
609
|
|
|
611
|
-
result = result.replace(
|
|
610
|
+
result = result.replace(REGEX.HEXLAB, (match, L_hex, A_hex, B_hex) => {
|
|
612
611
|
if (processedHexColors.has(match)) {
|
|
613
612
|
return processedHexColors.get(match);
|
|
614
613
|
}
|
|
@@ -627,7 +626,7 @@ const styimat = (function() {
|
|
|
627
626
|
}
|
|
628
627
|
});
|
|
629
628
|
|
|
630
|
-
result = result.replace(
|
|
629
|
+
result = result.replace(REGEX.HEXLCH, (match, L_hex, C_hex, H_dec) => {
|
|
631
630
|
if (processedHexColors.has(match)) {
|
|
632
631
|
return processedHexColors.get(match);
|
|
633
632
|
}
|
|
@@ -786,17 +785,17 @@ const styimat = (function() {
|
|
|
786
785
|
|
|
787
786
|
const applyGamma = (c) => {
|
|
788
787
|
const sign = c < 0 ? -1 : 1;
|
|
789
|
-
const absC =
|
|
788
|
+
const absC = sign * c;
|
|
790
789
|
|
|
791
790
|
if (absC <= 0.0031308) {
|
|
792
|
-
return
|
|
791
|
+
return 12.92 * absC;
|
|
793
792
|
} else {
|
|
794
793
|
return sign * (1.055 * Math.pow(absC, 1/2.4) - 0.055);
|
|
795
794
|
}
|
|
796
795
|
};
|
|
797
796
|
|
|
798
797
|
const clamp = (value) => {
|
|
799
|
-
return Math.max(0, Math.min(255,
|
|
798
|
+
return Math.max(0, Math.min(255, value * 255 |0));
|
|
800
799
|
};
|
|
801
800
|
|
|
802
801
|
const [X, Y, Z] = labToXyz(L, a, b);
|
|
@@ -866,7 +865,7 @@ const styimat = (function() {
|
|
|
866
865
|
};
|
|
867
866
|
|
|
868
867
|
const clamp = (value) => {
|
|
869
|
-
return Math.max(0, Math.min(255,
|
|
868
|
+
return Math.max(0, Math.min(255, value * 255 |0));
|
|
870
869
|
};
|
|
871
870
|
|
|
872
871
|
try {
|
|
@@ -1090,7 +1089,7 @@ const styimat = (function() {
|
|
|
1090
1089
|
|
|
1091
1090
|
if (parentSelector) {
|
|
1092
1091
|
if (fullSelector.includes('&')) {
|
|
1093
|
-
fullSelector = fullSelector.replace(
|
|
1092
|
+
fullSelector = fullSelector.replace(REGEX.AND, parentSelector);
|
|
1094
1093
|
} else if (fullSelector.trim().startsWith(':')) {
|
|
1095
1094
|
fullSelector = parentSelector + fullSelector;
|
|
1096
1095
|
} else {
|
|
@@ -1186,7 +1185,7 @@ const styimat = (function() {
|
|
|
1186
1185
|
* 处理 @import 语句
|
|
1187
1186
|
*/
|
|
1188
1187
|
async function processImports(cssText, config) {
|
|
1189
|
-
const importRegex =
|
|
1188
|
+
const importRegex = REGEX.IMPORT;
|
|
1190
1189
|
|
|
1191
1190
|
const processRecursive = async (text) => {
|
|
1192
1191
|
const importPromises = [];
|
package/dist/styimat.min.js
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* MIT License
|
|
3
3
|
* Copyright (c) 2025 王小玗
|
|
4
|
-
*/const styimat=(function(){let g={rootSelector:":root",variablePrefix:"--",preserveOriginal:!1,indentSize:4,enableNesting:!0,autoProcessStyleTags:!0,styleTagAttribute:"e",convertLabToRGB:!0,convertLchToRGB:!0,enableP3:!0,enableMath:!0,importBaseUrl:"",importCache:!0,importTimeout:5e3,enableAlias:!0,enableMacros:!0},
|
|
5
|
-
`),
|
|
6
|
-
`)}}function
|
|
7
|
-
`),
|
|
8
|
-
`)}}function
|
|
9
|
-
`),
|
|
10
|
-
`)}),
|
|
11
|
-
`)}}function
|
|
12
|
-
`),
|
|
13
|
-
`;continue}if(u==="}"){if(c&&
|
|
14
|
-
`}
|
|
15
|
-
`,i.includes("{")&&(l+=
|
|
16
|
-
`),
|
|
17
|
-
`),
|
|
18
|
-
`;for(const u of
|
|
19
|
-
`)});
|
|
4
|
+
*/const styimat=(function(){let g={rootSelector:":root",variablePrefix:"--",preserveOriginal:!1,indentSize:4,enableNesting:!0,autoProcessStyleTags:!0,styleTagAttribute:"e",convertLabToRGB:!0,convertLchToRGB:!0,enableP3:!0,enableMath:!0,importBaseUrl:"",importCache:!0,importTimeout:5e3,enableAlias:!0,enableMacros:!0};const w={IMPORT:/@import\s+([^;]+?)\s*;/g,ALIAS:/^@alias\s+([a-zA-Z_][a-zA-Z0-9_]*)\s+(.+?)\s*;$/,MACRO:/^@macro\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*\(([^)]*)\)\s*\{$/,SPECHAR:/[.*+?^${}()|[\]\\]/g,COMMENT:/\/\*[\s\S]*?\*\//g,MATH_SYMBOL:/([\da-z])([+-])(-?[\da-z])/g,MATH:/math\(([^)]+)\)/gi,COLOR:/(lab|lch)\([^)]+\)/gi,HEXLAB:/lab#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/gi,HEXLCH:/lch#([0-9a-f]{2})([0-9a-f]{2})(\d{1,3})/gi,SPACE:/\s+/g,AND:/&/g,HYPHEN:/-([a-z])/g};let x=null;const O=new Map,F=new Map,S=new Map,A=new Map;function Q(o){const t={...g},s=o.split(`
|
|
5
|
+
`),e=[];for(let n of s){const r=n.trim();if(r.startsWith("#")){const c=r.substring(1).trim(),a=c.indexOf(" ");if(a!==-1){const l=c.substring(0,a).trim(),i=c.substring(a+1).trim(),u=te(l);i==="true"||i==="false"?t[u]=i==="true":!isNaN(i)&&i.trim()!==""?t[u]=Number(i):t[u]=i}else console.warn(`\u65E0\u6548\u7684\u914D\u7F6E\u884C: ${r}`)}else e.push(n)}return{config:t,css:e.join(`
|
|
6
|
+
`)}}function K(o){const t=new Map,s=o.split(`
|
|
7
|
+
`),e=[];for(let n of s){const r=n.trim();if(r.startsWith("@alias")){const c=r.match(w.ALIAS);if(c){const a=c[1],l=c[2];t.set(a,l);continue}}e.push(n)}return{aliases:t,css:e.join(`
|
|
8
|
+
`)}}function N(o,t){if(!t.enableMacros)return{macros:new Map,css:o};const s=new Map,e=o.split(`
|
|
9
|
+
`),n=[];let r=!1,c="",a=[],l=[],i=0;for(let u of e){const p=u.trim();if(!r&&p.startsWith("@macro")){const f=p.match(w.MACRO);if(f){r=!0,c=f[1],a=f[2].split(",").map(h=>h.trim()).filter(h=>h).map(h=>{const m=h.split(":").map(b=>b.trim());return{name:m[0].slice(1),defaultValue:m[1]||null}}),l=[],i=1;continue}}if(r){for(const f of u)f==="{"&&i++,f==="}"&&i--;i===0?(s.set(c,{params:a,body:l.join(`
|
|
10
|
+
`)}),r=!1,c="",a=[],l=[]):l.push(u);continue}n.push(u)}return{macros:s,css:n.join(`
|
|
11
|
+
`)}}function J(o,t,s){if(!s.enableMacros||t.size===0)return o;const e=Array.from(t.keys()).map(c=>c.replace(w.SPECHAR,"\\$&")).join("|"),n=new RegExp(`@(${e})\\s*\\(([^)]*)\\)\\s*;?`,"g");return(c=>{let a=c,l=!1;do l=!1,a=a.replace(n,(i,u,p)=>{l=!0;const f=t.get(u);if(!f)return console.warn(`\u672A\u5B9A\u4E49\u7684\u5B8F: ${u}`),i;const h=_(p,f.params);return B(f.body,h,s)});while(l);return a})(o)}function _(o,t){const s=new Map,e=ee(o);for(let n=0;n<t.length;n++){const r=t[n];let c;n<e.length?c=e[n]:r.defaultValue!==null?c=r.defaultValue:c="none",s.set(r.name,c)}return s}function ee(o){const t=[];let s="",e=!1,n="",r=0,c=0;for(let a=0;a<o.length;a++){const l=o[a];(l==='"'||l==="'")&&!e?(e=!0,n=l):l===n&&e&&(e=!1,n=""),e||(l==="("&&r++,l===")"&&r--,l==="["&&c++,l==="]"&&c--),l===","&&!e&&r===0&&c===0?(t.push(s.trim()),s=""):s+=l}return s.trim()&&t.push(s.trim()),t}function B(o,t,s){let e=o;for(const[n,r]of t){const c=new RegExp(`\\$${n}`,"g");e=e.replace(c,r)}if(s.enableMacros){const n=Array.from(S.keys()).map(r=>r.replace(w.SPECHAR,"\\$&")).join("|");if(n){const r=new RegExp(`@(${n})\\s*\\(([^)]*)\\)\\s*;?`,"g");let c;do c=!1,e=e.replace(r,(a,l,i)=>{c=!0;const u=S.get(l);if(!u)return a;const p=_(i,u.params);return B(u.body,p,s)});while(c)}}return e=v(e,s),e}function te(o){return o.replace(w.HYPHEN,function(t,s){return s.toUpperCase()})}function V(){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{x=!1}return x}function j(o,t,s){let e=o.trim();if(e.endsWith(";")&&(e=e.slice(0,-1)),!e)return[];const n=[];let r="",c=0,a=!1,l="";for(let u=0;u<e.length;u++){const p=e[u];(p==='"'||p==="'")&&!a?(a=!0,l=p):p===l&&a&&(a=!1,l=""),a||(p==="("?c++:p===")"&&c--),p===";"&&!a&&c===0?r.trim()&&(n.push(r.trim()),r=""):r+=p}r.trim()&&n.push(r.trim());const i=[];for(let u of n){if(u=u.replace(w.COMMENT,""),!u.trim())continue;const p=ne(u);if(p===-1){console.warn(`\u65E0\u6548\u7684CSS\u58F0\u660E: "${u}"`);continue}let f=u.substring(0,p).trim(),h=u.substring(p+1).trim();if(h.endsWith(";")&&(h=h.slice(0,-1).trim()),t.get(f)&&(f=t.get(f)),f.startsWith("@")&&s.get(f.slice(1))){const m=s.get(f.slice(1)),b=_(h.split(" ").join(","),m.params);f=B(m.body,b,U),h=""}i.push({[f]:h})}return i}function ne(o){let t=!1,s="";for(let e=0;e<o.length;e++){const n=o[e];if((n==='"'||n==="'")&&!t?(t=!0,s=n):n===s&&t&&(t=!1,s=""),n===":"&&!t)return e}return-1}function k(o,t){if(!t.enableMath)return`math(${o})`;let s=o.replace(w.SPACE,"");return re(s,t.enableMath)}function re(o,t){return`calc(${o.replace(w.MATH_SYMBOL,"$1 $2 $3")})`}function se(o,t){if(!t.enableMath)return o;let s=o;const e=r=>r.replace(w.MATH,(c,a)=>k(a,t));let n;do n=s,s=e(s);while(s!==n&&s.includes("math("));return s}function oe(o,t){if(!t.convertLabToRGB&&!t.convertLchToRGB)return o;let s=o;s=ae(s,t);const e=new Map,n=c=>c.replace(w.COLOR,a=>{if(e.has(a))return e.get(a);let l=a;return a.toLowerCase().startsWith("lab(")?t.convertLabToRGB&&(l=ce(a,t)):a.toLowerCase().startsWith("lch(")&&t.convertLchToRGB&&(l=le(a,t)),e.set(a,l),l});let r;do r=s,s=n(s);while(s!==r);return s}function ae(o,t){let s=o;const e=new Map;return s=s.replace(w.HEXLAB,(n,r,c,a)=>{if(e.has(n))return e.get(n);try{const l=parseInt(r,16)/255*100,i=(parseInt(c,16)-128)*1.5,u=(parseInt(a,16)-128)*1.5,p=R(l,i,u,t);return e.set(n,p),p}catch(l){return console.warn(`\u65E0\u6CD5\u89E3\u6790lab#\u5341\u516D\u8FDB\u5236\u989C\u8272: ${n}`,l),n}}),s=s.replace(w.HEXLCH,(n,r,c,a)=>{if(e.has(n))return e.get(n);try{const l=parseInt(r,16)/255*100,i=parseInt(c,16)/255*150,u=parseInt(a)/100*360,p=L(l,i,u),f=R(p.L,p.a,p.b,t);return e.set(n,f),f}catch(l){return console.warn(`\u65E0\u6CD5\u89E3\u6790lch#\u5341\u516D\u8FDB\u5236\u989C\u8272: ${n}`,l),n}}),s}function ce(o,t){const s=/lab\(\s*([\d.]+)(%?)\s+([\d.-]+)\s+([\d.-]+)(?:\s*\/\s*([\d.%]+))?\s*\)/i,e=o.match(s);if(!e)return o;try{let n=parseFloat(e[1]);e[2]==="%"?n=n:(n<0&&(n=0),n>100&&(n=100));const r=parseFloat(e[3]),c=parseFloat(e[4]),a=e[5]!==void 0?e[5].includes("%")?parseFloat(e[5])/100:parseFloat(e[5]):null;return R(n,r,c,t,a)}catch(n){return console.warn(`\u65E0\u6CD5\u8F6C\u6362LAB\u989C\u8272: ${o}`,n),o}}function le(o,t){const s=/lch\(\s*([\d.]+)(%?)\s+([\d.]+)\s+([\d.]+)(deg)?(?:\s*\/\s*([\d.%]+))?\s*\)/i,e=o.match(s);if(!e)return o;try{let n=parseFloat(e[1]);e[2]==="%"?n=n:(n<0&&(n=0),n>100&&(n=100));const r=parseFloat(e[3]);let c=parseFloat(e[4]);r<0&&console.warn(`LCH\u4E2D\u7684C\u503C\u4E0D\u80FD\u4E3A\u8D1F: ${r}`),c=(c%360+360)%360;const a=L(n,r,c),l=e[6]!==void 0?e[6].includes("%")?parseFloat(e[6])/100:parseFloat(e[6]):null;return R(a.L,a.a,a.b,t,l)}catch(n){return console.warn(`\u65E0\u6CD5\u8F6C\u6362LCH\u989C\u8272: ${o}`,n),o}}function L(o,t,s){const e=s*Math.PI/180,n=t*Math.cos(e),r=t*Math.sin(e);return{L:o,a:n,b:r}}function $(o,t,s){const e=(d,y,M)=>{const q=(d+16)/116,z=y/500+q,I=q-M/200,be=z**3>.008856?z**3:(116*z-16)/903.3,ye=d>903.3*.008856?((d+16)/116)**3:d/903.3,Me=I**3>.008856?I**3:(116*I-16)/903.3;return[be*.95047,ye*1,Me*1.08883]},n=(d,y,M)=>{const C=[[3.2404542,-1.5371385,-.4985314],[-.969266,1.8760108,.041556],[.0556434,-.2040259,1.0572252]],T=C[0][0]*d+C[0][1]*y+C[0][2]*M,Y=C[1][0]*d+C[1][1]*y+C[1][2]*M,D=C[2][0]*d+C[2][1]*y+C[2][2]*M;return[T,Y,D]},r=d=>{const y=d<0?-1:1,M=y*d;return M<=.0031308?12.92*M:y*(1.055*Math.pow(M,.4166666666666667)-.055)},c=d=>Math.max(0,Math.min(255,d*255|0)),[a,l,i]=e(o,t,s),[u,p,f]=n(a,l,i),h=r(u),m=r(p),b=r(f);return{r:c(h),g:c(m),b:c(b)}}function P(o,t,s){const e=(a,l,i)=>{const b=(a+16)/116,d=l/500+b,y=b-i/200,M=d**3>.008856?d**3:(116*d-16)/903.3,C=a>903.3*.008856?((a+16)/116)**3:a/903.3,T=y**3>.008856?y**3:(116*y-16)/903.3;return[M*.95047,C*1,T*1.08883]},n=(a,l,i)=>{const u=[[2.493496911941425,-.9313836179191239,-.40271078445071684],[-.8294889695615747,1.7626640603183463,.023624685841943577],[.03584583024378447,-.07617238926804182,.9568845240076872]],p=u[0][0]*a+u[0][1]*l+u[0][2]*i,f=u[1][0]*a+u[1][1]*l+u[1][2]*i,h=u[2][0]*a+u[2][1]*l+u[2][2]*i;return[p,f,h]},r=a=>{const l=a<0?-1:1,i=Math.abs(a);return i<=.0031308?l*12.92*i:l*(1.055*Math.pow(i,.4166666666666667)-.055)},c=a=>Math.max(0,Math.min(255,a*255|0));try{const[a,l,i]=e(o,t,s),[u,p,f]=n(a,l,i),h=r(u),m=r(p),b=r(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,b))}}catch(a){console.warn("P3\u8F6C\u6362\u5931\u8D25:",a);const l=$(o,t,s);return{r:l.r/255,g:l.g/255,b:l.b/255}}}function R(o,t,s,e,n=null){if(!e.enableP3||!V()){const r=$(o,t,s);return n!==null?`rgba(${r.r}, ${r.g}, ${r.b}, ${n})`:`rgb(${r.r}, ${r.g}, ${r.b})`}else{const r=P(o,t,s);return n!==null?`color(display-p3 ${r.r.toFixed(4)} ${r.g.toFixed(4)} ${r.b.toFixed(4)} / ${n})`:`color(display-p3 ${r.r.toFixed(4)} ${r.g.toFixed(4)} ${r.b.toFixed(4)})`}}function v(o,t){let s=o;return t.enableMath&&(s=se(s,t)),(t.convertLabToRGB||t.convertLchToRGB)&&(s=oe(s,t)),s}function ie(o,t){const s=o.split(`
|
|
12
|
+
`),e={},n=new Map;let r="",c=null,a=!1,l=0;for(let i of s){const u=i.trim(),p=u.match(/^\$([a-zA-Z_][a-zA-Z0-9_]*)\s*:\s*(.+?);?$/);if(p){const[,f,h]=p,m=v(h.trim(),t);c?(n.has(c)||n.set(c,{}),n.get(c)[f]=m):e[f]=m;continue}if(u.endsWith("{")){c=u.slice(0,-1).trim(),a=!0,r+=i+`
|
|
13
|
+
`;continue}if(u==="}"){if(a&&c&&n.has(c)){const f=n.get(c),h=" ".repeat(l);for(const[m,b]of Object.entries(f))r+=`${h} --${m}: ${b};
|
|
14
|
+
`}a=!1,c=null}r+=i+`
|
|
15
|
+
`,i.includes("{")&&(l+=t.indentSize),i.includes("}")&&(l=Math.max(0,l-t.indentSize))}return{globalVariables:e,selectorVariables:n,cssWithoutVars:r.trim()}}function ue(o,t,s,e){const n=o.split(`
|
|
16
|
+
`),r=[],c=[];let a=0;for(let l=0;l<n.length;l++){const i=n[l],u=i.trim();if(!u)continue;const p=i.match(/^(\s*)/)[0].length;if(u==="}"){if(r.length>0){const f=r.pop();if(r.length>0){const h=r[r.length-1];h.children||(h.children=[]),h.children.push(f)}else c.push(f)}continue}if(u.endsWith("{")){const h={selector:u.slice(0,-1).trim(),properties:[],children:[]};r.push(h);continue}if(!u.includes("{")&&!u.includes("}")&&u.includes(":")){if(r.length>0){const f=r[r.length-1];j(u,s,e).forEach(m=>{const b=Object.keys(m)[0],d=v(m[b],t);f.properties.push(d===""?b:`${b}: ${d}`)})}continue}}for(;r.length>0;){const l=r.pop();if(r.length===0)c.push(l);else{const i=r[r.length-1];i.children||(i.children=[]),i.children.push(l)}}return G(c,t,"",s,e)}function G(o,t,s="",e,n){let r="";const c=s.startsWith("@");for(const a of o){const l=a.selector.startsWith("@");let i=(c?" ".repeat(t.indentSize):"")+a.selector;if(l&&(i=a.selector+` {
|
|
17
|
+
`),s&&(i.includes("&")?i=i.replace(w.AND,s):i.trim().startsWith(":")?i=s+i:i=s+(c?"":" ")+i),a.properties.length>0){r+=(l?"":i)+` {
|
|
18
|
+
`;for(const u of a.properties)j(u,e,n).forEach(f=>{const h=Object.keys(f)[0],m=v(f[h],t);r+=(c?" ".repeat(t.indentSize):"")+" ".repeat(t.indentSize)+(m===""?h:`${h}: ${m};
|
|
19
|
+
`)});r+=c?" ".repeat(t.indentSize)+`}
|
|
20
20
|
`:`}
|
|
21
21
|
|
|
22
|
-
`}
|
|
22
|
+
`}a.children&&a.children.length>0&&(r+=G(a.children,t,i,e,n)),c&&(r+=`}
|
|
23
23
|
|
|
24
|
-
`)}return
|
|
24
|
+
`)}return r.trim()+`
|
|
25
25
|
|
|
26
|
-
`}function
|
|
27
|
-
`);return`${
|
|
28
|
-
${
|
|
26
|
+
`}function fe(o,t,s){let e=o;for(const n in t)e=e.replace(new RegExp(`(?:\\$\\$|\\$)(${n})(?=[^a-zA-Z0-9_])`,"g"),(r,c)=>r.startsWith("$$")?`attr(${c})`:`var(--${c})`);return e=v(e,s),e}function pe(o,t){if(Object.keys(o).length===0)return"";const s=Object.entries(o).map(([e,n])=>{const r=v(n,t);return" ".repeat(t.indentSize)+`--${e}: ${r};`}).join(`
|
|
27
|
+
`);return`${t.rootSelector} {
|
|
28
|
+
${s}
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
`}function
|
|
32
|
-
`);let
|
|
33
|
-
`}return
|
|
31
|
+
`}function he(o,t,s){let e="";const n=o.split(`
|
|
32
|
+
`);let r=null;for(let c of n){const a=c.trim();a.endsWith("{")&&(r=a.slice(0,-1).trim()),a==="}"&&r&&(r=null),e+=v(c,s)+`
|
|
33
|
+
`}return e.trim()}async function X(o,t){const s=w.IMPORT;return(async n=>{const r=[],c=n.replace(s,(a,l)=>{const i=de(l,t).then(u=>X(u,t)).catch(u=>(console.warn(`\u65E0\u6CD5\u5BFC\u5165CSS\u6587\u4EF6: ${l}`,u),""));return r.push(i),`__IMPORT_PLACEHOLDER_${r.length-1}__`});if(r.length>0){const a=await Promise.all(r);let l=c;for(let i=0;i<a.length;i++)l=l.replace(`__IMPORT_PLACEHOLDER_${i}__`,a[i]);return l}return c})(o)}async function de(o,t){if(t.importCache&&O.has(o))return O.get(o);const s=t.importBaseUrl?new URL(o,t.importBaseUrl).href:o;let e;if(typeof process<"u"&&process.versions&&process.versions.node&&typeof require<"u")try{const r=require("fs"),c=require("path");let a=s;s.startsWith(".")&&(a=c.join(process.cwd(),s)),e=r.readFileSync(a,"utf-8")}catch(r){throw new Error(`Node.js\u6587\u4EF6\u8BFB\u53D6\u5931\u8D25: ${s} - ${r.message}`)}else try{const r=new AbortController,c=setTimeout(()=>r.abort(),t.importTimeout),a=await fetch(s,{signal:r.signal,headers:{Accept:"text/css"}});if(clearTimeout(c),!a.ok)throw new Error(`HTTP\u9519\u8BEF: ${a.status} ${a.statusText}`);e=await a.text()}catch(r){throw r.name==="AbortError"?new Error(`\u5BFC\u5165\u8D85\u65F6: ${s}`):new Error(`\u65E0\u6CD5\u83B7\u53D6CSS: ${s} - ${r.message}`)}return t.importCache&&O.set(o,e),e}async function E(o,t={}){let{config:s,css:e}=Q(o);const n={...g,...t,...s};e=await X(e,n);let r=e,c=new Map;if(n.enableAlias){const{aliases:y,css:M}=K(r);c=y,r=M}let a=r,l=new Map;if(n.enableMacros){const{macros:y,css:M}=N(a,n);l=y,a=M;for(const[C,T]of l)S.set(C,T)}let i=a;n.enableMacros&&l.size>0&&(i=J(i,l,n));const{globalVariables:u,selectorVariables:p,cssWithoutVars:f}=ie(i,n);let h=f.trim();if(n.enableNesting&&f.includes("{"))try{h=ue(f,n,c,l)}catch(y){console.warn("\u5D4C\u5957\u89E3\u6790\u5931\u8D25\uFF0C\u4F7F\u7528\u539F\u59CBCSS:",y)}const m=pe(u,n);let b=h;p.size>0&&(b=he(h,p,n)),b=fe(b,{...u,...p},n);let d=m+b;for(const[y,M]of A)try{d=M.convert(d,n)}catch(C){console.error("\u63D2\u4EF6\u5904\u7406\u5931\u8D25:",C)}return d}function Z(o={}){const t={...g,...o},s=document.querySelectorAll(`style[${t.styleTagAttribute||"e"}]`);return s.forEach(e=>{let n=e.textContent;(async()=>{try{e.getAttribute("src")&&(n="@import "+e.getAttribute("src")+";");const c=await E(n,t),a=document.createElement("style");a.textContent=c,e.parentNode.insertBefore(a,e.nextSibling),t.preserveOriginal?e.style.display="none":e.remove()}catch(c){console.error("\u5904\u7406style\u6807\u7B7E\u5931\u8D25:",c)}})()}),s.length}function U(o={}){g={...g,...o}}function W(o,t={}){const s={...g,...t};if(o)return(async()=>{try{const e=await E(o,s),n=document.createElement("style");return n.textContent=e,document.head.appendChild(n),n}catch(e){return console.error("\u5E94\u7528CSS\u5931\u8D25:",e),null}})();document.readyState==="loading"?document.addEventListener("DOMContentLoaded",()=>{Z(s)}):Z(s)}const me={convert:E,apply:W,config:U,supportsP3:V(),detectP3Support:V,imports:{clearCache:function(){O.clear()},setBaseUrl:function(o){g.importBaseUrl=o},setCacheEnabled:function(o){g.importCache=o},setTimeout:function(o){g.importTimeout=o}},plugins:{use(o){const t=new o;return A.set(t.name??o.name,t),!0},remove(o){return A.get(o)?.destroy&&A.get(o).destroy(),A.delete(o),!0},getAll:function(){return Array.from(A.entries())},clear:function(){A.clear()}},aliases:{add:function(o,t){F.set(o,t)},remove:function(o){F.delete(o)},getAll:function(){return Array.from(F.entries())},clear:function(){F.clear()}},macros:{define:function(o,t,s=[]){if(typeof t=="function"){const n=t.toString().match(/{([\s\S]*)}/);n&&S.set(o,{params:s.map(r=>typeof r=="string"?{name:r}:r),body:n[1].trim()})}else S.set(o,{params:s.map(e=>typeof e=="string"?{name:e}:e),body:t})},call:function(o,...t){const s=S.get(o);if(!s)throw new Error(`\u672A\u5B9A\u4E49\u7684\u5B8F: ${o}`);const e=new Map;for(let n=0;n<s.params.length;n++){const r=s.params[n],c=n<t.length?t[n]:r.defaultValue;if(c===void 0&&r.defaultValue===null)throw new Error(`\u7F3A\u5C11\u5FC5\u9700\u53C2\u6570: ${r.name}`);e.set(r.name,c!==void 0?c:"")}return B(s.body,e,g)},getAll:function(){return Array.from(S.entries())},remove:function(o){S.delete(o)},clear:function(){S.clear()},parse:function(o){const{macros:t}=N(o,g);for(const[s,e]of t)S.set(s,e)}},math:{evaluate:function(o){return k(o,g)}},colorUtils:{labToRGB:$,lchToLab:L,lchToRGB:function(o,t,s){const e=L(o,t,s);return $(e.L,e.a,e.b)},labToP3:P,lchToP3:function(o,t,s){const e=L(o,t,s);return P(e.L,e.a,e.b)},parseHexLab:function(o){const t=o.match(/lab#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/i);if(!t)return null;const s=t[1],e=t[2],n=t[3],r=parseInt(s,16)/255*100,c=(parseInt(e,16)-128)*1.5,a=(parseInt(n,16)-128)*1.5;return $(r,c,a)},parseHexLch:function(o){const t=o.match(/lch#([0-9a-f]{2})([0-9a-f]{2})(\d{1,3})/i);if(!t)return null;const s=t[1],e=t[2],n=t[3],r=parseInt(s,16)/255*100,c=parseInt(e,16)/255*150,a=parseInt(n)/100*360,l=L(r,c,a);return $(l.L,l.a,l.b)},generateColor:function(o,t,s,e=null,n=!0){return R(o,t,s,{enableP3:n},e)},parseColor:function(o){try{const t=o.match(/lab\(\s*([\d.]+)(%?)\s+([\d.-]+)\s+([\d.-]+)(?:\s*\/\s*([\d.%]+))?\s*\)/i);if(t){let e=parseFloat(t[1]);const n=parseFloat(t[3]),r=parseFloat(t[4]),c=t[5]?t[5].includes("%")?parseFloat(t[5])/100:parseFloat(t[5]):null,a=R(e,n,r,g,c);return{L:e,A:n,B:r,alpha:c,rgb:$(e,n,r),p3:P(e,n,r),colorString:a}}const s=o.match(/lch\(\s*([\d.]+)(%?)\s+([\d.]+)\s+([\d.]+)(deg)?(?:\s*\/\s*([\d.%]+))?\s*\)/i);if(s){let e=parseFloat(s[1]);const n=parseFloat(s[3]);let r=parseFloat(s[4]);const c=s[6]?s[6].includes("%")?parseFloat(s[6])/100:parseFloat(s[6]):null,a=L(e,n,r),l=R(a.L,a.a,a.b,g,c);return{L:e,C:n,H:r,alpha:c,lab:a,rgb:$(a.L,a.a,a.b),p3:P(a.L,a.a,a.b),colorString:l}}return null}catch(t){return console.warn("\u65E0\u6CD5\u89E3\u6790\u989C\u8272:",o,t),null}}}},H=async function(...o){if(o.length>1||o[0]&&o[0].raw)return await ge(...o);const t=o[0];if(typeof t=="string"){const s=await E(t,{...g,...o[1]});return s&&typeof s.then=="function",s}return typeof t=="object"&&t!==null?(g={...g,...t},H):o.length===0?W():H};async function ge(o,...t){let s=o[0];for(let n=0;n<t.length;n++){const r=t[n];let c="";typeof r=="function"?c=r():Array.isArray(r)?c=r.join(" "):c=String(r??""),s+=c+o[n+1]}const e=await E(s,g);return e&&typeof e.then=="function",e}return Object.assign(H,me),Object.setPrototypeOf(H,Function.prototype),typeof window<"u"&&(W(),Object.defineProperty(window.HTMLElement.prototype,"cssVar",{get(){const o=this;return new Proxy(()=>{},{get(t,s){const e=s.startsWith("--")?s:`--${s}`;return o.style.getPropertyValue(e)},set(t,s,e){const n=s.startsWith("--")?s:`--${s}`;return o.style.setProperty(n,e),!0},apply(t,s,e){const n=e[0],r=e[1],c=n.startsWith("--")?n:`--${n}`;if(r===void 0)return o.style.getPropertyValue(c);o.style.setProperty(c,r)}})}})),H})();if(typeof define=="function"&&define.amd)define([],g=>styimat);else if(typeof module=="object"&&module.exports)module.exports=styimat;else{const g=globalThis??(typeof self<"u"&&self)??(typeof window<"u"&&window)??global??{};g.styimat=styimat}
|
package/dist/styimat.min.mjs
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* MIT License
|
|
3
3
|
* Copyright (c) 2025 王小玗
|
|
4
|
-
*/const
|
|
5
|
-
`),
|
|
6
|
-
`)}}function
|
|
7
|
-
`),
|
|
8
|
-
`)}}function
|
|
9
|
-
`),
|
|
10
|
-
`)}),
|
|
11
|
-
`)}}function
|
|
12
|
-
`),
|
|
13
|
-
`;continue}if(u==="}"){if(c&&
|
|
14
|
-
`}
|
|
15
|
-
`,i.includes("{")&&(l+=
|
|
16
|
-
`),
|
|
17
|
-
`),
|
|
18
|
-
`;for(const u of
|
|
19
|
-
`)});
|
|
4
|
+
*/const O=(function(){let g={rootSelector:":root",variablePrefix:"--",preserveOriginal:!1,indentSize:4,enableNesting:!0,autoProcessStyleTags:!0,styleTagAttribute:"e",convertLabToRGB:!0,convertLchToRGB:!0,enableP3:!0,enableMath:!0,importBaseUrl:"",importCache:!0,importTimeout:5e3,enableAlias:!0,enableMacros:!0};const w={IMPORT:/@import\s+([^;]+?)\s*;/g,ALIAS:/^@alias\s+([a-zA-Z_][a-zA-Z0-9_]*)\s+(.+?)\s*;$/,MACRO:/^@macro\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*\(([^)]*)\)\s*\{$/,SPECHAR:/[.*+?^${}()|[\]\\]/g,COMMENT:/\/\*[\s\S]*?\*\//g,MATH_SYMBOL:/([\da-z])([+-])(-?[\da-z])/g,MATH:/math\(([^)]+)\)/gi,COLOR:/(lab|lch)\([^)]+\)/gi,HEXLAB:/lab#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/gi,HEXLCH:/lch#([0-9a-f]{2})([0-9a-f]{2})(\d{1,3})/gi,SPACE:/\s+/g,AND:/&/g,HYPHEN:/-([a-z])/g};let v=null;const F=new Map,B=new Map,S=new Map,A=new Map;function K(o){const t={...g},s=o.split(`
|
|
5
|
+
`),e=[];for(let n of s){const r=n.trim();if(r.startsWith("#")){const c=r.substring(1).trim(),a=c.indexOf(" ");if(a!==-1){const l=c.substring(0,a).trim(),i=c.substring(a+1).trim(),u=ne(l);i==="true"||i==="false"?t[u]=i==="true":!isNaN(i)&&i.trim()!==""?t[u]=Number(i):t[u]=i}else console.warn(`\u65E0\u6548\u7684\u914D\u7F6E\u884C: ${r}`)}else e.push(n)}return{config:t,css:e.join(`
|
|
6
|
+
`)}}function J(o){const t=new Map,s=o.split(`
|
|
7
|
+
`),e=[];for(let n of s){const r=n.trim();if(r.startsWith("@alias")){const c=r.match(w.ALIAS);if(c){const a=c[1],l=c[2];t.set(a,l);continue}}e.push(n)}return{aliases:t,css:e.join(`
|
|
8
|
+
`)}}function j(o,t){if(!t.enableMacros)return{macros:new Map,css:o};const s=new Map,e=o.split(`
|
|
9
|
+
`),n=[];let r=!1,c="",a=[],l=[],i=0;for(let u of e){const p=u.trim();if(!r&&p.startsWith("@macro")){const f=p.match(w.MACRO);if(f){r=!0,c=f[1],a=f[2].split(",").map(h=>h.trim()).filter(h=>h).map(h=>{const m=h.split(":").map(b=>b.trim());return{name:m[0].slice(1),defaultValue:m[1]||null}}),l=[],i=1;continue}}if(r){for(const f of u)f==="{"&&i++,f==="}"&&i--;i===0?(s.set(c,{params:a,body:l.join(`
|
|
10
|
+
`)}),r=!1,c="",a=[],l=[]):l.push(u);continue}n.push(u)}return{macros:s,css:n.join(`
|
|
11
|
+
`)}}function ee(o,t,s){if(!s.enableMacros||t.size===0)return o;const e=Array.from(t.keys()).map(c=>c.replace(w.SPECHAR,"\\$&")).join("|"),n=new RegExp(`@(${e})\\s*\\(([^)]*)\\)\\s*;?`,"g");return(c=>{let a=c,l=!1;do l=!1,a=a.replace(n,(i,u,p)=>{l=!0;const f=t.get(u);if(!f)return console.warn(`\u672A\u5B9A\u4E49\u7684\u5B8F: ${u}`),i;const h=V(p,f.params);return _(f.body,h,s)});while(l);return a})(o)}function V(o,t){const s=new Map,e=te(o);for(let n=0;n<t.length;n++){const r=t[n];let c;n<e.length?c=e[n]:r.defaultValue!==null?c=r.defaultValue:c="none",s.set(r.name,c)}return s}function te(o){const t=[];let s="",e=!1,n="",r=0,c=0;for(let a=0;a<o.length;a++){const l=o[a];(l==='"'||l==="'")&&!e?(e=!0,n=l):l===n&&e&&(e=!1,n=""),e||(l==="("&&r++,l===")"&&r--,l==="["&&c++,l==="]"&&c--),l===","&&!e&&r===0&&c===0?(t.push(s.trim()),s=""):s+=l}return s.trim()&&t.push(s.trim()),t}function _(o,t,s){let e=o;for(const[n,r]of t){const c=new RegExp(`\\$${n}`,"g");e=e.replace(c,r)}if(s.enableMacros){const n=Array.from(S.keys()).map(r=>r.replace(w.SPECHAR,"\\$&")).join("|");if(n){const r=new RegExp(`@(${n})\\s*\\(([^)]*)\\)\\s*;?`,"g");let c;do c=!1,e=e.replace(r,(a,l,i)=>{c=!0;const u=S.get(l);if(!u)return a;const p=V(i,u.params);return _(u.body,p,s)});while(c)}}return e=R(e,s),e}function ne(o){return o.replace(w.HYPHEN,function(t,s){return s.toUpperCase()})}function W(){if(v!==null)return v;if(typeof window>"u"||!window.CSS)return v=!1,!1;try{v=CSS.supports("color","color(display-p3 1 0 0)")}catch{v=!1}return v}function k(o,t,s){let e=o.trim();if(e.endsWith(";")&&(e=e.slice(0,-1)),!e)return[];const n=[];let r="",c=0,a=!1,l="";for(let u=0;u<e.length;u++){const p=e[u];(p==='"'||p==="'")&&!a?(a=!0,l=p):p===l&&a&&(a=!1,l=""),a||(p==="("?c++:p===")"&&c--),p===";"&&!a&&c===0?r.trim()&&(n.push(r.trim()),r=""):r+=p}r.trim()&&n.push(r.trim());const i=[];for(let u of n){if(u=u.replace(w.COMMENT,""),!u.trim())continue;const p=re(u);if(p===-1){console.warn(`\u65E0\u6548\u7684CSS\u58F0\u660E: "${u}"`);continue}let f=u.substring(0,p).trim(),h=u.substring(p+1).trim();if(h.endsWith(";")&&(h=h.slice(0,-1).trim()),t.get(f)&&(f=t.get(f)),f.startsWith("@")&&s.get(f.slice(1))){const m=s.get(f.slice(1)),b=V(h.split(" ").join(","),m.params);f=_(m.body,b,Y),h=""}i.push({[f]:h})}return i}function re(o){let t=!1,s="";for(let e=0;e<o.length;e++){const n=o[e];if((n==='"'||n==="'")&&!t?(t=!0,s=n):n===s&&t&&(t=!1,s=""),n===":"&&!t)return e}return-1}function G(o,t){if(!t.enableMath)return`math(${o})`;let s=o.replace(w.SPACE,"");return se(s,t.enableMath)}function se(o,t){return`calc(${o.replace(w.MATH_SYMBOL,"$1 $2 $3")})`}function oe(o,t){if(!t.enableMath)return o;let s=o;const e=r=>r.replace(w.MATH,(c,a)=>G(a,t));let n;do n=s,s=e(s);while(s!==n&&s.includes("math("));return s}function ae(o,t){if(!t.convertLabToRGB&&!t.convertLchToRGB)return o;let s=o;s=ce(s,t);const e=new Map,n=c=>c.replace(w.COLOR,a=>{if(e.has(a))return e.get(a);let l=a;return a.toLowerCase().startsWith("lab(")?t.convertLabToRGB&&(l=le(a,t)):a.toLowerCase().startsWith("lch(")&&t.convertLchToRGB&&(l=ie(a,t)),e.set(a,l),l});let r;do r=s,s=n(s);while(s!==r);return s}function ce(o,t){let s=o;const e=new Map;return s=s.replace(w.HEXLAB,(n,r,c,a)=>{if(e.has(n))return e.get(n);try{const l=parseInt(r,16)/255*100,i=(parseInt(c,16)-128)*1.5,u=(parseInt(a,16)-128)*1.5,p=x(l,i,u,t);return e.set(n,p),p}catch(l){return console.warn(`\u65E0\u6CD5\u89E3\u6790lab#\u5341\u516D\u8FDB\u5236\u989C\u8272: ${n}`,l),n}}),s=s.replace(w.HEXLCH,(n,r,c,a)=>{if(e.has(n))return e.get(n);try{const l=parseInt(r,16)/255*100,i=parseInt(c,16)/255*150,u=parseInt(a)/100*360,p=L(l,i,u),f=x(p.L,p.a,p.b,t);return e.set(n,f),f}catch(l){return console.warn(`\u65E0\u6CD5\u89E3\u6790lch#\u5341\u516D\u8FDB\u5236\u989C\u8272: ${n}`,l),n}}),s}function le(o,t){const s=/lab\(\s*([\d.]+)(%?)\s+([\d.-]+)\s+([\d.-]+)(?:\s*\/\s*([\d.%]+))?\s*\)/i,e=o.match(s);if(!e)return o;try{let n=parseFloat(e[1]);e[2]==="%"?n=n:(n<0&&(n=0),n>100&&(n=100));const r=parseFloat(e[3]),c=parseFloat(e[4]),a=e[5]!==void 0?e[5].includes("%")?parseFloat(e[5])/100:parseFloat(e[5]):null;return x(n,r,c,t,a)}catch(n){return console.warn(`\u65E0\u6CD5\u8F6C\u6362LAB\u989C\u8272: ${o}`,n),o}}function ie(o,t){const s=/lch\(\s*([\d.]+)(%?)\s+([\d.]+)\s+([\d.]+)(deg)?(?:\s*\/\s*([\d.%]+))?\s*\)/i,e=o.match(s);if(!e)return o;try{let n=parseFloat(e[1]);e[2]==="%"?n=n:(n<0&&(n=0),n>100&&(n=100));const r=parseFloat(e[3]);let c=parseFloat(e[4]);r<0&&console.warn(`LCH\u4E2D\u7684C\u503C\u4E0D\u80FD\u4E3A\u8D1F: ${r}`),c=(c%360+360)%360;const a=L(n,r,c),l=e[6]!==void 0?e[6].includes("%")?parseFloat(e[6])/100:parseFloat(e[6]):null;return x(a.L,a.a,a.b,t,l)}catch(n){return console.warn(`\u65E0\u6CD5\u8F6C\u6362LCH\u989C\u8272: ${o}`,n),o}}function L(o,t,s){const e=s*Math.PI/180,n=t*Math.cos(e),r=t*Math.sin(e);return{L:o,a:n,b:r}}function $(o,t,s){const e=(d,y,M)=>{const Q=(d+16)/116,I=y/500+Q,N=Q-M/200,ye=I**3>.008856?I**3:(116*I-16)/903.3,Me=d>903.3*.008856?((d+16)/116)**3:d/903.3,Ce=N**3>.008856?N**3:(116*N-16)/903.3;return[ye*.95047,Me*1,Ce*1.08883]},n=(d,y,M)=>{const C=[[3.2404542,-1.5371385,-.4985314],[-.969266,1.8760108,.041556],[.0556434,-.2040259,1.0572252]],P=C[0][0]*d+C[0][1]*y+C[0][2]*M,D=C[1][0]*d+C[1][1]*y+C[1][2]*M,q=C[2][0]*d+C[2][1]*y+C[2][2]*M;return[P,D,q]},r=d=>{const y=d<0?-1:1,M=y*d;return M<=.0031308?12.92*M:y*(1.055*Math.pow(M,.4166666666666667)-.055)},c=d=>Math.max(0,Math.min(255,d*255|0)),[a,l,i]=e(o,t,s),[u,p,f]=n(a,l,i),h=r(u),m=r(p),b=r(f);return{r:c(h),g:c(m),b:c(b)}}function T(o,t,s){const e=(a,l,i)=>{const b=(a+16)/116,d=l/500+b,y=b-i/200,M=d**3>.008856?d**3:(116*d-16)/903.3,C=a>903.3*.008856?((a+16)/116)**3:a/903.3,P=y**3>.008856?y**3:(116*y-16)/903.3;return[M*.95047,C*1,P*1.08883]},n=(a,l,i)=>{const u=[[2.493496911941425,-.9313836179191239,-.40271078445071684],[-.8294889695615747,1.7626640603183463,.023624685841943577],[.03584583024378447,-.07617238926804182,.9568845240076872]],p=u[0][0]*a+u[0][1]*l+u[0][2]*i,f=u[1][0]*a+u[1][1]*l+u[1][2]*i,h=u[2][0]*a+u[2][1]*l+u[2][2]*i;return[p,f,h]},r=a=>{const l=a<0?-1:1,i=Math.abs(a);return i<=.0031308?l*12.92*i:l*(1.055*Math.pow(i,.4166666666666667)-.055)},c=a=>Math.max(0,Math.min(255,a*255|0));try{const[a,l,i]=e(o,t,s),[u,p,f]=n(a,l,i),h=r(u),m=r(p),b=r(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,b))}}catch(a){console.warn("P3\u8F6C\u6362\u5931\u8D25:",a);const l=$(o,t,s);return{r:l.r/255,g:l.g/255,b:l.b/255}}}function x(o,t,s,e,n=null){if(!e.enableP3||!W()){const r=$(o,t,s);return n!==null?`rgba(${r.r}, ${r.g}, ${r.b}, ${n})`:`rgb(${r.r}, ${r.g}, ${r.b})`}else{const r=T(o,t,s);return n!==null?`color(display-p3 ${r.r.toFixed(4)} ${r.g.toFixed(4)} ${r.b.toFixed(4)} / ${n})`:`color(display-p3 ${r.r.toFixed(4)} ${r.g.toFixed(4)} ${r.b.toFixed(4)})`}}function R(o,t){let s=o;return t.enableMath&&(s=oe(s,t)),(t.convertLabToRGB||t.convertLchToRGB)&&(s=ae(s,t)),s}function ue(o,t){const s=o.split(`
|
|
12
|
+
`),e={},n=new Map;let r="",c=null,a=!1,l=0;for(let i of s){const u=i.trim(),p=u.match(/^\$([a-zA-Z_][a-zA-Z0-9_]*)\s*:\s*(.+?);?$/);if(p){const[,f,h]=p,m=R(h.trim(),t);c?(n.has(c)||n.set(c,{}),n.get(c)[f]=m):e[f]=m;continue}if(u.endsWith("{")){c=u.slice(0,-1).trim(),a=!0,r+=i+`
|
|
13
|
+
`;continue}if(u==="}"){if(a&&c&&n.has(c)){const f=n.get(c),h=" ".repeat(l);for(const[m,b]of Object.entries(f))r+=`${h} --${m}: ${b};
|
|
14
|
+
`}a=!1,c=null}r+=i+`
|
|
15
|
+
`,i.includes("{")&&(l+=t.indentSize),i.includes("}")&&(l=Math.max(0,l-t.indentSize))}return{globalVariables:e,selectorVariables:n,cssWithoutVars:r.trim()}}function fe(o,t,s,e){const n=o.split(`
|
|
16
|
+
`),r=[],c=[];let a=0;for(let l=0;l<n.length;l++){const i=n[l],u=i.trim();if(!u)continue;const p=i.match(/^(\s*)/)[0].length;if(u==="}"){if(r.length>0){const f=r.pop();if(r.length>0){const h=r[r.length-1];h.children||(h.children=[]),h.children.push(f)}else c.push(f)}continue}if(u.endsWith("{")){const h={selector:u.slice(0,-1).trim(),properties:[],children:[]};r.push(h);continue}if(!u.includes("{")&&!u.includes("}")&&u.includes(":")){if(r.length>0){const f=r[r.length-1];k(u,s,e).forEach(m=>{const b=Object.keys(m)[0],d=R(m[b],t);f.properties.push(d===""?b:`${b}: ${d}`)})}continue}}for(;r.length>0;){const l=r.pop();if(r.length===0)c.push(l);else{const i=r[r.length-1];i.children||(i.children=[]),i.children.push(l)}}return X(c,t,"",s,e)}function X(o,t,s="",e,n){let r="";const c=s.startsWith("@");for(const a of o){const l=a.selector.startsWith("@");let i=(c?" ".repeat(t.indentSize):"")+a.selector;if(l&&(i=a.selector+` {
|
|
17
|
+
`),s&&(i.includes("&")?i=i.replace(w.AND,s):i.trim().startsWith(":")?i=s+i:i=s+(c?"":" ")+i),a.properties.length>0){r+=(l?"":i)+` {
|
|
18
|
+
`;for(const u of a.properties)k(u,e,n).forEach(f=>{const h=Object.keys(f)[0],m=R(f[h],t);r+=(c?" ".repeat(t.indentSize):"")+" ".repeat(t.indentSize)+(m===""?h:`${h}: ${m};
|
|
19
|
+
`)});r+=c?" ".repeat(t.indentSize)+`}
|
|
20
20
|
`:`}
|
|
21
21
|
|
|
22
|
-
`}
|
|
22
|
+
`}a.children&&a.children.length>0&&(r+=X(a.children,t,i,e,n)),c&&(r+=`}
|
|
23
23
|
|
|
24
|
-
`)}return
|
|
24
|
+
`)}return r.trim()+`
|
|
25
25
|
|
|
26
|
-
`}function
|
|
27
|
-
`);return`${
|
|
28
|
-
${
|
|
26
|
+
`}function pe(o,t,s){let e=o;for(const n in t)e=e.replace(new RegExp(`(?:\\$\\$|\\$)(${n})(?=[^a-zA-Z0-9_])`,"g"),(r,c)=>r.startsWith("$$")?`attr(${c})`:`var(--${c})`);return e=R(e,s),e}function he(o,t){if(Object.keys(o).length===0)return"";const s=Object.entries(o).map(([e,n])=>{const r=R(n,t);return" ".repeat(t.indentSize)+`--${e}: ${r};`}).join(`
|
|
27
|
+
`);return`${t.rootSelector} {
|
|
28
|
+
${s}
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
`}function
|
|
32
|
-
`);let
|
|
33
|
-
`}return
|
|
31
|
+
`}function de(o,t,s){let e="";const n=o.split(`
|
|
32
|
+
`);let r=null;for(let c of n){const a=c.trim();a.endsWith("{")&&(r=a.slice(0,-1).trim()),a==="}"&&r&&(r=null),e+=R(c,s)+`
|
|
33
|
+
`}return e.trim()}async function Z(o,t){const s=w.IMPORT;return(async n=>{const r=[],c=n.replace(s,(a,l)=>{const i=me(l,t).then(u=>Z(u,t)).catch(u=>(console.warn(`\u65E0\u6CD5\u5BFC\u5165CSS\u6587\u4EF6: ${l}`,u),""));return r.push(i),`__IMPORT_PLACEHOLDER_${r.length-1}__`});if(r.length>0){const a=await Promise.all(r);let l=c;for(let i=0;i<a.length;i++)l=l.replace(`__IMPORT_PLACEHOLDER_${i}__`,a[i]);return l}return c})(o)}async function me(o,t){if(t.importCache&&F.has(o))return F.get(o);const s=t.importBaseUrl?new URL(o,t.importBaseUrl).href:o;let e;if(typeof process<"u"&&process.versions&&process.versions.node&&typeof require<"u")try{const r=require("fs"),c=require("path");let a=s;s.startsWith(".")&&(a=c.join(process.cwd(),s)),e=r.readFileSync(a,"utf-8")}catch(r){throw new Error(`Node.js\u6587\u4EF6\u8BFB\u53D6\u5931\u8D25: ${s} - ${r.message}`)}else try{const r=new AbortController,c=setTimeout(()=>r.abort(),t.importTimeout),a=await fetch(s,{signal:r.signal,headers:{Accept:"text/css"}});if(clearTimeout(c),!a.ok)throw new Error(`HTTP\u9519\u8BEF: ${a.status} ${a.statusText}`);e=await a.text()}catch(r){throw r.name==="AbortError"?new Error(`\u5BFC\u5165\u8D85\u65F6: ${s}`):new Error(`\u65E0\u6CD5\u83B7\u53D6CSS: ${s} - ${r.message}`)}return t.importCache&&F.set(o,e),e}async function E(o,t={}){let{config:s,css:e}=K(o);const n={...g,...t,...s};e=await Z(e,n);let r=e,c=new Map;if(n.enableAlias){const{aliases:y,css:M}=J(r);c=y,r=M}let a=r,l=new Map;if(n.enableMacros){const{macros:y,css:M}=j(a,n);l=y,a=M;for(const[C,P]of l)S.set(C,P)}let i=a;n.enableMacros&&l.size>0&&(i=ee(i,l,n));const{globalVariables:u,selectorVariables:p,cssWithoutVars:f}=ue(i,n);let h=f.trim();if(n.enableNesting&&f.includes("{"))try{h=fe(f,n,c,l)}catch(y){console.warn("\u5D4C\u5957\u89E3\u6790\u5931\u8D25\uFF0C\u4F7F\u7528\u539F\u59CBCSS:",y)}const m=he(u,n);let b=h;p.size>0&&(b=de(h,p,n)),b=pe(b,{...u,...p},n);let d=m+b;for(const[y,M]of A)try{d=M.convert(d,n)}catch(C){console.error("\u63D2\u4EF6\u5904\u7406\u5931\u8D25:",C)}return d}function U(o={}){const t={...g,...o},s=document.querySelectorAll(`style[${t.styleTagAttribute||"e"}]`);return s.forEach(e=>{let n=e.textContent;(async()=>{try{e.getAttribute("src")&&(n="@import "+e.getAttribute("src")+";");const c=await E(n,t),a=document.createElement("style");a.textContent=c,e.parentNode.insertBefore(a,e.nextSibling),t.preserveOriginal?e.style.display="none":e.remove()}catch(c){console.error("\u5904\u7406style\u6807\u7B7E\u5931\u8D25:",c)}})()}),s.length}function Y(o={}){g={...g,...o}}function z(o,t={}){const s={...g,...t};if(o)return(async()=>{try{const e=await E(o,s),n=document.createElement("style");return n.textContent=e,document.head.appendChild(n),n}catch(e){return console.error("\u5E94\u7528CSS\u5931\u8D25:",e),null}})();document.readyState==="loading"?document.addEventListener("DOMContentLoaded",()=>{U(s)}):U(s)}const ge={convert:E,apply:z,config:Y,supportsP3:W(),detectP3Support:W,imports:{clearCache:function(){F.clear()},setBaseUrl:function(o){g.importBaseUrl=o},setCacheEnabled:function(o){g.importCache=o},setTimeout:function(o){g.importTimeout=o}},plugins:{use(o){const t=new o;return A.set(t.name??o.name,t),!0},remove(o){return A.get(o)?.destroy&&A.get(o).destroy(),A.delete(o),!0},getAll:function(){return Array.from(A.entries())},clear:function(){A.clear()}},aliases:{add:function(o,t){B.set(o,t)},remove:function(o){B.delete(o)},getAll:function(){return Array.from(B.entries())},clear:function(){B.clear()}},macros:{define:function(o,t,s=[]){if(typeof t=="function"){const n=t.toString().match(/{([\s\S]*)}/);n&&S.set(o,{params:s.map(r=>typeof r=="string"?{name:r}:r),body:n[1].trim()})}else S.set(o,{params:s.map(e=>typeof e=="string"?{name:e}:e),body:t})},call:function(o,...t){const s=S.get(o);if(!s)throw new Error(`\u672A\u5B9A\u4E49\u7684\u5B8F: ${o}`);const e=new Map;for(let n=0;n<s.params.length;n++){const r=s.params[n],c=n<t.length?t[n]:r.defaultValue;if(c===void 0&&r.defaultValue===null)throw new Error(`\u7F3A\u5C11\u5FC5\u9700\u53C2\u6570: ${r.name}`);e.set(r.name,c!==void 0?c:"")}return _(s.body,e,g)},getAll:function(){return Array.from(S.entries())},remove:function(o){S.delete(o)},clear:function(){S.clear()},parse:function(o){const{macros:t}=j(o,g);for(const[s,e]of t)S.set(s,e)}},math:{evaluate:function(o){return G(o,g)}},colorUtils:{labToRGB:$,lchToLab:L,lchToRGB:function(o,t,s){const e=L(o,t,s);return $(e.L,e.a,e.b)},labToP3:T,lchToP3:function(o,t,s){const e=L(o,t,s);return T(e.L,e.a,e.b)},parseHexLab:function(o){const t=o.match(/lab#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/i);if(!t)return null;const s=t[1],e=t[2],n=t[3],r=parseInt(s,16)/255*100,c=(parseInt(e,16)-128)*1.5,a=(parseInt(n,16)-128)*1.5;return $(r,c,a)},parseHexLch:function(o){const t=o.match(/lch#([0-9a-f]{2})([0-9a-f]{2})(\d{1,3})/i);if(!t)return null;const s=t[1],e=t[2],n=t[3],r=parseInt(s,16)/255*100,c=parseInt(e,16)/255*150,a=parseInt(n)/100*360,l=L(r,c,a);return $(l.L,l.a,l.b)},generateColor:function(o,t,s,e=null,n=!0){return x(o,t,s,{enableP3:n},e)},parseColor:function(o){try{const t=o.match(/lab\(\s*([\d.]+)(%?)\s+([\d.-]+)\s+([\d.-]+)(?:\s*\/\s*([\d.%]+))?\s*\)/i);if(t){let e=parseFloat(t[1]);const n=parseFloat(t[3]),r=parseFloat(t[4]),c=t[5]?t[5].includes("%")?parseFloat(t[5])/100:parseFloat(t[5]):null,a=x(e,n,r,g,c);return{L:e,A:n,B:r,alpha:c,rgb:$(e,n,r),p3:T(e,n,r),colorString:a}}const s=o.match(/lch\(\s*([\d.]+)(%?)\s+([\d.]+)\s+([\d.]+)(deg)?(?:\s*\/\s*([\d.%]+))?\s*\)/i);if(s){let e=parseFloat(s[1]);const n=parseFloat(s[3]);let r=parseFloat(s[4]);const c=s[6]?s[6].includes("%")?parseFloat(s[6])/100:parseFloat(s[6]):null,a=L(e,n,r),l=x(a.L,a.a,a.b,g,c);return{L:e,C:n,H:r,alpha:c,lab:a,rgb:$(a.L,a.a,a.b),p3:T(a.L,a.a,a.b),colorString:l}}return null}catch(t){return console.warn("\u65E0\u6CD5\u89E3\u6790\u989C\u8272:",o,t),null}}}},H=async function(...o){if(o.length>1||o[0]&&o[0].raw)return await be(...o);const t=o[0];if(typeof t=="string"){const s=await E(t,{...g,...o[1]});return s&&typeof s.then=="function",s}return typeof t=="object"&&t!==null?(g={...g,...t},H):o.length===0?z():H};async function be(o,...t){let s=o[0];for(let n=0;n<t.length;n++){const r=t[n];let c="";typeof r=="function"?c=r():Array.isArray(r)?c=r.join(" "):c=String(r??""),s+=c+o[n+1]}const e=await E(s,g);return e&&typeof e.then=="function",e}return Object.assign(H,ge),Object.setPrototypeOf(H,Function.prototype),typeof window<"u"&&(z(),Object.defineProperty(window.HTMLElement.prototype,"cssVar",{get(){const o=this;return new Proxy(()=>{},{get(t,s){const e=s.startsWith("--")?s:`--${s}`;return o.style.getPropertyValue(e)},set(t,s,e){const n=s.startsWith("--")?s:`--${s}`;return o.style.setProperty(n,e),!0},apply(t,s,e){const n=e[0],r=e[1],c=n.startsWith("--")?n:`--${n}`;if(r===void 0)return o.style.getPropertyValue(c);o.style.setProperty(c,r)}})}})),H})();if(typeof define=="function"&&define.amd)define([],g=>O);else if(typeof module=="object"&&module.exports)module.exports=O;else{const g=globalThis??(typeof self<"u"&&self)??(typeof window<"u"&&window)??global??{};g.styimat=O}export default O;export const{convert,apply,config,supportsP3,detectP3Support,imports,plugins,aliases,macros,math,colorUtils}=O;
|
package/dist/styimat.mjs
CHANGED
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
* 支持 @macro 宏定义系统
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
|
+
|
|
18
19
|
const styimat = (function() {
|
|
19
20
|
// 默认配置
|
|
20
21
|
let defaultConfig = {
|
|
@@ -35,6 +36,22 @@ const styimat = (function() {
|
|
|
35
36
|
enableAlias: true,
|
|
36
37
|
enableMacros: true
|
|
37
38
|
};
|
|
39
|
+
|
|
40
|
+
const REGEX = {
|
|
41
|
+
IMPORT: /@import\s+([^;]+?)\s*;/g,
|
|
42
|
+
ALIAS: /^@alias\s+([a-zA-Z_][a-zA-Z0-9_]*)\s+(.+?)\s*;$/,
|
|
43
|
+
MACRO: /^@macro\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*\(([^)]*)\)\s*\{$/,
|
|
44
|
+
SPECHAR: /[.*+?^${}()|[\]\\]/g,
|
|
45
|
+
COMMENT: /\/\*[\s\S]*?\*\//g,
|
|
46
|
+
MATH_SYMBOL: /([\da-z])([+-])(-?[\da-z])/g,
|
|
47
|
+
MATH: /math\(([^)]+)\)/gi,
|
|
48
|
+
COLOR: /(lab|lch)\([^)]+\)/gi,
|
|
49
|
+
HEXLAB: /lab#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/gi,
|
|
50
|
+
HEXLCH: /lch#([0-9a-f]{2})([0-9a-f]{2})(\d{1,3})/gi,
|
|
51
|
+
SPACE: /\s+/g,
|
|
52
|
+
AND: /&/g,
|
|
53
|
+
HYPHEN: /-([a-z])/g
|
|
54
|
+
}
|
|
38
55
|
|
|
39
56
|
// 全局P3支持检测结果
|
|
40
57
|
let p3Supported = null;
|
|
@@ -105,7 +122,7 @@ const styimat = (function() {
|
|
|
105
122
|
const trimmed = line.trim();
|
|
106
123
|
|
|
107
124
|
if (trimmed.startsWith('@alias')) {
|
|
108
|
-
const aliasMatch = trimmed.match(
|
|
125
|
+
const aliasMatch = trimmed.match(REGEX.ALIAS);
|
|
109
126
|
if (aliasMatch) {
|
|
110
127
|
const aliasName = aliasMatch[1];
|
|
111
128
|
const originalProperty = aliasMatch[2];
|
|
@@ -145,7 +162,7 @@ const styimat = (function() {
|
|
|
145
162
|
const trimmed = line.trim();
|
|
146
163
|
|
|
147
164
|
if (!inMacroDefinition && trimmed.startsWith('@macro')) {
|
|
148
|
-
const macroMatch = trimmed.match(
|
|
165
|
+
const macroMatch = trimmed.match(REGEX.MACRO);
|
|
149
166
|
if (macroMatch) {
|
|
150
167
|
inMacroDefinition = true;
|
|
151
168
|
currentMacroName = macroMatch[1];
|
|
@@ -206,7 +223,7 @@ const styimat = (function() {
|
|
|
206
223
|
}
|
|
207
224
|
|
|
208
225
|
const macroNames = Array.from(macros.keys()).map(name =>
|
|
209
|
-
name.replace(
|
|
226
|
+
name.replace(REGEX.SPECHAR, '\\$&')
|
|
210
227
|
).join('|');
|
|
211
228
|
|
|
212
229
|
const macroCallRegex = new RegExp(`@(${macroNames})\\s*\\(([^)]*)\\)\\s*;?`, 'g');
|
|
@@ -253,17 +270,12 @@ const styimat = (function() {
|
|
|
253
270
|
} else if (param.defaultValue !== null) {
|
|
254
271
|
value = param.defaultValue;
|
|
255
272
|
} else {
|
|
256
|
-
|
|
257
|
-
value = '';
|
|
273
|
+
value = 'none';
|
|
258
274
|
}
|
|
259
275
|
|
|
260
276
|
argsMap.set(param.name, value);
|
|
261
277
|
}
|
|
262
278
|
|
|
263
|
-
if (argValues.length > paramDefs.length) {
|
|
264
|
-
console.warn(`宏调用传递了过多参数,多余的参数将被忽略`);
|
|
265
|
-
}
|
|
266
|
-
|
|
267
279
|
return argsMap;
|
|
268
280
|
}
|
|
269
281
|
|
|
@@ -324,7 +336,7 @@ const styimat = (function() {
|
|
|
324
336
|
|
|
325
337
|
if (config.enableMacros) {
|
|
326
338
|
const macroNames = Array.from(macroRegistry.keys()).map(name =>
|
|
327
|
-
name.replace(
|
|
339
|
+
name.replace(REGEX.SPECHAR, '\\$&')
|
|
328
340
|
).join('|');
|
|
329
341
|
|
|
330
342
|
if (macroNames) {
|
|
@@ -355,7 +367,7 @@ const styimat = (function() {
|
|
|
355
367
|
* 将连字符分隔的字符串转换为驼峰命名
|
|
356
368
|
*/
|
|
357
369
|
function camelCase(str) {
|
|
358
|
-
return str.replace(
|
|
370
|
+
return str.replace(REGEX.HYPHEN, function(match, letter) {
|
|
359
371
|
return letter.toUpperCase();
|
|
360
372
|
});
|
|
361
373
|
}
|
|
@@ -374,7 +386,6 @@ const styimat = (function() {
|
|
|
374
386
|
try {
|
|
375
387
|
p3Supported = CSS.supports('color', 'color(display-p3 1 0 0)');
|
|
376
388
|
} catch (error) {
|
|
377
|
-
console.warn('P3支持检测失败:', error);
|
|
378
389
|
p3Supported = false;
|
|
379
390
|
}
|
|
380
391
|
|
|
@@ -437,7 +448,7 @@ const styimat = (function() {
|
|
|
437
448
|
const result = [];
|
|
438
449
|
|
|
439
450
|
for (let declaration of declarations) {
|
|
440
|
-
declaration = declaration.replace(
|
|
451
|
+
declaration = declaration.replace(REGEX.COMMENT, '');
|
|
441
452
|
if (!declaration.trim()) continue;
|
|
442
453
|
|
|
443
454
|
const colonIndex = findFirstColonOutsideQuotes(declaration);
|
|
@@ -505,14 +516,9 @@ const styimat = (function() {
|
|
|
505
516
|
return `math(${expression})`;
|
|
506
517
|
}
|
|
507
518
|
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
return parseMathExpression(cleanExpr, config.enableMath);
|
|
512
|
-
} catch (error) {
|
|
513
|
-
console.warn('math()表达式解析失败:', expression, error);
|
|
514
|
-
return `calc(${expression})`;
|
|
515
|
-
}
|
|
519
|
+
let cleanExpr = expression.replace(REGEX.SPACE, '');
|
|
520
|
+
|
|
521
|
+
return parseMathExpression(cleanExpr, config.enableMath);
|
|
516
522
|
}
|
|
517
523
|
|
|
518
524
|
/**
|
|
@@ -520,8 +526,7 @@ const styimat = (function() {
|
|
|
520
526
|
*/
|
|
521
527
|
function parseMathExpression(expr, enableMath) {
|
|
522
528
|
return `calc(${expr
|
|
523
|
-
.replace(
|
|
524
|
-
.replace(/([\da-z])([+-])(-?[\da-z])/g, '$1 $2 $3')})`;
|
|
529
|
+
.replace(REGEX.MATH_SYMBOL, '$1 $2 $3')})`;
|
|
525
530
|
}
|
|
526
531
|
|
|
527
532
|
/**
|
|
@@ -533,11 +538,9 @@ const styimat = (function() {
|
|
|
533
538
|
}
|
|
534
539
|
|
|
535
540
|
let result = cssValue;
|
|
536
|
-
const mathRegex = /math\(([^)]+)\)/gi;
|
|
537
541
|
|
|
538
542
|
const processMath = (str) => {
|
|
539
|
-
return str.replace(
|
|
540
|
-
|
|
543
|
+
return str.replace(REGEX.MATH, (match, expression) => {
|
|
541
544
|
return evaluateMathExpression(expression, config);
|
|
542
545
|
});
|
|
543
546
|
};
|
|
@@ -562,11 +565,10 @@ const styimat = (function() {
|
|
|
562
565
|
let result = cssValue;
|
|
563
566
|
result = parseHexColorFormats(result, config);
|
|
564
567
|
|
|
565
|
-
const colorFunctionRegex = /(lab|lch)\([^)]+\)/gi;
|
|
566
568
|
const processedColors = new Map();
|
|
567
569
|
|
|
568
570
|
const processColorFunctions = (str) => {
|
|
569
|
-
return str.replace(
|
|
571
|
+
return str.replace(REGEX.COLOR, (match) => {
|
|
570
572
|
if (processedColors.has(match)) {
|
|
571
573
|
return processedColors.get(match);
|
|
572
574
|
}
|
|
@@ -603,12 +605,9 @@ const styimat = (function() {
|
|
|
603
605
|
function parseHexColorFormats(value, config) {
|
|
604
606
|
let result = value;
|
|
605
607
|
|
|
606
|
-
const hexLabRegex = /lab#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/gi;
|
|
607
|
-
const hexLchRegex = /lch#([0-9a-f]{2})([0-9a-f]{2})(\d{1,3})/gi;
|
|
608
|
-
|
|
609
608
|
const processedHexColors = new Map();
|
|
610
609
|
|
|
611
|
-
result = result.replace(
|
|
610
|
+
result = result.replace(REGEX.HEXLAB, (match, L_hex, A_hex, B_hex) => {
|
|
612
611
|
if (processedHexColors.has(match)) {
|
|
613
612
|
return processedHexColors.get(match);
|
|
614
613
|
}
|
|
@@ -627,7 +626,7 @@ const styimat = (function() {
|
|
|
627
626
|
}
|
|
628
627
|
});
|
|
629
628
|
|
|
630
|
-
result = result.replace(
|
|
629
|
+
result = result.replace(REGEX.HEXLCH, (match, L_hex, C_hex, H_dec) => {
|
|
631
630
|
if (processedHexColors.has(match)) {
|
|
632
631
|
return processedHexColors.get(match);
|
|
633
632
|
}
|
|
@@ -786,17 +785,17 @@ const styimat = (function() {
|
|
|
786
785
|
|
|
787
786
|
const applyGamma = (c) => {
|
|
788
787
|
const sign = c < 0 ? -1 : 1;
|
|
789
|
-
const absC =
|
|
788
|
+
const absC = sign * c;
|
|
790
789
|
|
|
791
790
|
if (absC <= 0.0031308) {
|
|
792
|
-
return
|
|
791
|
+
return 12.92 * absC;
|
|
793
792
|
} else {
|
|
794
793
|
return sign * (1.055 * Math.pow(absC, 1/2.4) - 0.055);
|
|
795
794
|
}
|
|
796
795
|
};
|
|
797
796
|
|
|
798
797
|
const clamp = (value) => {
|
|
799
|
-
return Math.max(0, Math.min(255,
|
|
798
|
+
return Math.max(0, Math.min(255, value * 255 |0));
|
|
800
799
|
};
|
|
801
800
|
|
|
802
801
|
const [X, Y, Z] = labToXyz(L, a, b);
|
|
@@ -866,7 +865,7 @@ const styimat = (function() {
|
|
|
866
865
|
};
|
|
867
866
|
|
|
868
867
|
const clamp = (value) => {
|
|
869
|
-
return Math.max(0, Math.min(255,
|
|
868
|
+
return Math.max(0, Math.min(255, value * 255 |0));
|
|
870
869
|
};
|
|
871
870
|
|
|
872
871
|
try {
|
|
@@ -1090,7 +1089,7 @@ const styimat = (function() {
|
|
|
1090
1089
|
|
|
1091
1090
|
if (parentSelector) {
|
|
1092
1091
|
if (fullSelector.includes('&')) {
|
|
1093
|
-
fullSelector = fullSelector.replace(
|
|
1092
|
+
fullSelector = fullSelector.replace(REGEX.AND, parentSelector);
|
|
1094
1093
|
} else if (fullSelector.trim().startsWith(':')) {
|
|
1095
1094
|
fullSelector = parentSelector + fullSelector;
|
|
1096
1095
|
} else {
|
|
@@ -1186,7 +1185,7 @@ const styimat = (function() {
|
|
|
1186
1185
|
* 处理 @import 语句
|
|
1187
1186
|
*/
|
|
1188
1187
|
async function processImports(cssText, config) {
|
|
1189
|
-
const importRegex =
|
|
1188
|
+
const importRegex = REGEX.IMPORT;
|
|
1190
1189
|
|
|
1191
1190
|
const processRecursive = async (text) => {
|
|
1192
1191
|
const importPromises = [];
|