styimat 7.0.0 → 8.0.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 +57 -7
- package/dist/styimat.js +15 -35
- package/dist/styimat.min.js +18 -18
- package/dist/styimat.min.mjs +18 -18
- package/dist/styimat.mjs +15 -35
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -227,6 +227,9 @@ styimat input.css output.css
|
|
|
227
227
|
## Styimat语法
|
|
228
228
|
|
|
229
229
|
### 1. 变量定义
|
|
230
|
+
|
|
231
|
+
变量定义要用`$name: value;`的格式,使用直接`$name`
|
|
232
|
+
|
|
230
233
|
```css
|
|
231
234
|
/* 全局变量 */
|
|
232
235
|
$primary-color: lab(54.7 77.9 80.1);
|
|
@@ -245,6 +248,9 @@ $font-family: 'Inter', sans-serif;
|
|
|
245
248
|
```
|
|
246
249
|
|
|
247
250
|
### 2. 配置头语法
|
|
251
|
+
|
|
252
|
+
变量定义要用`#configname value`的格式,写在css顶部
|
|
253
|
+
|
|
248
254
|
```css
|
|
249
255
|
#indent-size 4
|
|
250
256
|
#auto-process-style-tags true
|
|
@@ -271,6 +277,9 @@ body {
|
|
|
271
277
|
**注意**:配置头既支持驼峰命名也支持连字符命名,例如 `indent-size` 和 `indentSize` 效果相同。
|
|
272
278
|
|
|
273
279
|
### 3. 导入语法
|
|
280
|
+
|
|
281
|
+
导入文件要用`@import url;`的格式
|
|
282
|
+
|
|
274
283
|
```css
|
|
275
284
|
@import yourcss.css;
|
|
276
285
|
|
|
@@ -289,6 +298,9 @@ body {
|
|
|
289
298
|
```
|
|
290
299
|
|
|
291
300
|
### 4. 别名语法
|
|
301
|
+
|
|
302
|
+
别名定义要用`@alias alias prop`的格式,可以使用定义的别名来代替原生的属性名
|
|
303
|
+
|
|
292
304
|
```css
|
|
293
305
|
@alias bg background-color;
|
|
294
306
|
|
|
@@ -301,6 +313,17 @@ body {
|
|
|
301
313
|
```
|
|
302
314
|
|
|
303
315
|
### 5. 宏语法
|
|
316
|
+
|
|
317
|
+
更强大的替换,要用以下格式:
|
|
318
|
+
|
|
319
|
+
```css
|
|
320
|
+
@macro name($param1, $param2, $param3: defaultvalue...) {
|
|
321
|
+
/* macro body */
|
|
322
|
+
}
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
示例:
|
|
326
|
+
|
|
304
327
|
```css
|
|
305
328
|
@macro grid($columns, $gap: 10px) {
|
|
306
329
|
display: grid;
|
|
@@ -319,10 +342,10 @@ body {
|
|
|
319
342
|
```css
|
|
320
343
|
/* math() 函数 */
|
|
321
344
|
.element {
|
|
322
|
-
width: math(100
|
|
323
|
-
height: math(200px
|
|
324
|
-
padding: math(2rem
|
|
325
|
-
margin: math(
|
|
345
|
+
width: math(100%/3);
|
|
346
|
+
height: math(200px+30px);
|
|
347
|
+
padding: math(2rem*1.5);
|
|
348
|
+
margin: math((100vh-200px)/2);
|
|
326
349
|
}
|
|
327
350
|
|
|
328
351
|
/* 支持单位运算 */
|
|
@@ -361,8 +384,11 @@ body {
|
|
|
361
384
|
```
|
|
362
385
|
|
|
363
386
|
### 9. 嵌套规则
|
|
387
|
+
|
|
388
|
+
可以让写代码更简单,而且让代码可读性更高
|
|
389
|
+
|
|
364
390
|
```css
|
|
365
|
-
/*
|
|
391
|
+
/* Styimat 风格的嵌套 */
|
|
366
392
|
.container {
|
|
367
393
|
padding: 1rem;
|
|
368
394
|
|
|
@@ -454,7 +480,7 @@ styimat.imports.setTimeout(10000); // 设置导入超时时间
|
|
|
454
480
|
|
|
455
481
|
// 定义插件
|
|
456
482
|
class compressPlugin {
|
|
457
|
-
|
|
483
|
+
name = "compress";
|
|
458
484
|
constructor() {
|
|
459
485
|
// 构造函数
|
|
460
486
|
}
|
|
@@ -462,7 +488,7 @@ class compressPlugin {
|
|
|
462
488
|
// 删除时调用
|
|
463
489
|
}
|
|
464
490
|
convert(css, config) {
|
|
465
|
-
return css.replace(/[\n\
|
|
491
|
+
return css.replace(/[\n\s]/g, "");
|
|
466
492
|
}
|
|
467
493
|
}
|
|
468
494
|
|
|
@@ -472,6 +498,30 @@ styimat.plugins.use(compressPlugin);
|
|
|
472
498
|
styimat.plugins.remove("compress"); // 删除插件
|
|
473
499
|
```
|
|
474
500
|
|
|
501
|
+
### 插件的定义标准
|
|
502
|
+
|
|
503
|
+
```javascript
|
|
504
|
+
export defult class {
|
|
505
|
+
name = "compress";
|
|
506
|
+
constructor() {
|
|
507
|
+
// 构造函数
|
|
508
|
+
// 在这里做初始化工作
|
|
509
|
+
}
|
|
510
|
+
destroy() {
|
|
511
|
+
// 删除时调用
|
|
512
|
+
// 记得清理
|
|
513
|
+
}
|
|
514
|
+
convert(css, config) {
|
|
515
|
+
// 转换时调用
|
|
516
|
+
return css.replace(/[\n\s]/g, "");
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// 导入时
|
|
521
|
+
import compressPlugin from "./plugins/compress.js";
|
|
522
|
+
styimat.use(compressPlugin);
|
|
523
|
+
```
|
|
524
|
+
|
|
475
525
|
### 配置选项
|
|
476
526
|
|
|
477
527
|
| 选项 | 类型 | 默认值 | 描述 |
|
package/dist/styimat.js
CHANGED
|
@@ -105,7 +105,7 @@ const styimat = (function() {
|
|
|
105
105
|
const trimmed = line.trim();
|
|
106
106
|
|
|
107
107
|
if (trimmed.startsWith('@alias')) {
|
|
108
|
-
const aliasMatch = trimmed.match(/^@alias\s+([
|
|
108
|
+
const aliasMatch = trimmed.match(/^@alias\s+([a-zA-Z_][a-zA-Z0-9_]*)\s+(.+?)\s*;$/);
|
|
109
109
|
if (aliasMatch) {
|
|
110
110
|
const aliasName = aliasMatch[1];
|
|
111
111
|
const originalProperty = aliasMatch[2];
|
|
@@ -145,7 +145,7 @@ const styimat = (function() {
|
|
|
145
145
|
const trimmed = line.trim();
|
|
146
146
|
|
|
147
147
|
if (!inMacroDefinition && trimmed.startsWith('@macro')) {
|
|
148
|
-
const macroMatch = trimmed.match(/^@macro\s+([
|
|
148
|
+
const macroMatch = trimmed.match(/^@macro\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*\(([^)]*)\)\s*\{$/);
|
|
149
149
|
if (macroMatch) {
|
|
150
150
|
inMacroDefinition = true;
|
|
151
151
|
currentMacroName = macroMatch[1];
|
|
@@ -944,17 +944,11 @@ const styimat = (function() {
|
|
|
944
944
|
for (let line of lines) {
|
|
945
945
|
const trimmed = line.trim();
|
|
946
946
|
|
|
947
|
-
const varMatch = trimmed.match(/^\$([
|
|
947
|
+
const varMatch = trimmed.match(/^\$([a-zA-Z_][a-zA-Z0-9_]*)\s*:\s*(.+?);?$/);
|
|
948
948
|
|
|
949
949
|
if (varMatch) {
|
|
950
950
|
const [, varName, varValue] = varMatch;
|
|
951
|
-
const processedValue = processCSSValue(
|
|
952
|
-
replaceVariableUsesInValue(varValue.trim(), {
|
|
953
|
-
...globalVariables,
|
|
954
|
-
...(currentSelector ? selectorVariables.get(currentSelector) || {} : {})
|
|
955
|
-
}),
|
|
956
|
-
config
|
|
957
|
-
);
|
|
951
|
+
const processedValue = processCSSValue(varValue.trim(), config);
|
|
958
952
|
|
|
959
953
|
if (currentSelector) {
|
|
960
954
|
if (!selectorVariables.has(currentSelector)) {
|
|
@@ -1129,27 +1123,16 @@ const styimat = (function() {
|
|
|
1129
1123
|
return result.trim() + "\n\n";
|
|
1130
1124
|
}
|
|
1131
1125
|
|
|
1132
|
-
/**
|
|
1133
|
-
* 替换变量值中的变量引用
|
|
1134
|
-
*/
|
|
1135
|
-
function replaceVariableUsesInValue(value, variables) {
|
|
1136
|
-
return value.replace(/\$([\w]+)(\W?)/g, (match, varName, char) => {
|
|
1137
|
-
if (variables[varName]) {
|
|
1138
|
-
return `var(--${varName})${char}`;
|
|
1139
|
-
}
|
|
1140
|
-
return match;
|
|
1141
|
-
});
|
|
1142
|
-
}
|
|
1143
|
-
|
|
1144
1126
|
/**
|
|
1145
1127
|
* 替换变量使用
|
|
1146
1128
|
*/
|
|
1147
|
-
function replaceVariableUses(cssText,
|
|
1129
|
+
function replaceVariableUses(cssText, variables, config) {
|
|
1148
1130
|
let result = cssText;
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1131
|
+
for (const variable in variables) {
|
|
1132
|
+
result = result.replace(new RegExp(`(?:\\$\\$|\\$)(${variable})(?=[^a-zA-Z0-9_])`, "g"), (match, varName) => {
|
|
1133
|
+
return match.startsWith('$$') ? `attr(${varName})` : `var(--${varName})`;
|
|
1134
|
+
});
|
|
1135
|
+
}
|
|
1153
1136
|
|
|
1154
1137
|
result = processCSSValue(result, config);
|
|
1155
1138
|
|
|
@@ -1166,10 +1149,7 @@ const styimat = (function() {
|
|
|
1166
1149
|
|
|
1167
1150
|
const declarations = Object.entries(variables)
|
|
1168
1151
|
.map(([name, value]) => {
|
|
1169
|
-
const processedValue = processCSSValue(
|
|
1170
|
-
replaceVariableUsesInValue(value, variables),
|
|
1171
|
-
config
|
|
1172
|
-
);
|
|
1152
|
+
const processedValue = processCSSValue(value, config);
|
|
1173
1153
|
return " ".repeat(config.indentSize) + `--${name}: ${processedValue};`;
|
|
1174
1154
|
})
|
|
1175
1155
|
.join('\n');
|
|
@@ -1361,7 +1341,7 @@ const styimat = (function() {
|
|
|
1361
1341
|
finalCSS = injectSelectorVariables(processedCSS, selectorVariables, finalConfig);
|
|
1362
1342
|
}
|
|
1363
1343
|
|
|
1364
|
-
finalCSS = replaceVariableUses(finalCSS, globalVariables, selectorVariables, finalConfig);
|
|
1344
|
+
finalCSS = replaceVariableUses(finalCSS, { ...globalVariables, ...selectorVariables }, finalConfig);
|
|
1365
1345
|
|
|
1366
1346
|
let fullCSS = rootRule + finalCSS;
|
|
1367
1347
|
|
|
@@ -1476,12 +1456,12 @@ const styimat = (function() {
|
|
|
1476
1456
|
plugins: {
|
|
1477
1457
|
use(plugin) {
|
|
1478
1458
|
const pluginObj = new plugin();
|
|
1479
|
-
pluginMap.set(plugin.name, pluginObj);
|
|
1459
|
+
pluginMap.set(pluginObj.name ?? plugin.name, pluginObj);
|
|
1480
1460
|
return true;
|
|
1481
1461
|
},
|
|
1482
1462
|
remove(name) {
|
|
1483
|
-
if (pluginMap.get(name)
|
|
1484
|
-
pluginMap.get(name).destroy()
|
|
1463
|
+
if (pluginMap.get(name)?.destroy) {
|
|
1464
|
+
pluginMap.get(name).destroy();
|
|
1485
1465
|
}
|
|
1486
1466
|
pluginMap.delete(name);
|
|
1487
1467
|
return true;
|
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
|
-
`),n=[];for(let
|
|
6
|
-
`)}}function
|
|
7
|
-
`),n=[];for(let
|
|
8
|
-
`)}}function
|
|
9
|
-
`),
|
|
10
|
-
`)}),t=!1,a="",c=[],l=[]):l.push(u);continue}
|
|
11
|
-
`)}}function
|
|
12
|
-
`),n={},
|
|
13
|
-
`;continue}if(u==="}"){if(c&&a&&
|
|
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},L=null;const z=new Map,B=new Map,$=new Map,S=new Map;function Y(s){const e={...g},r=s.split(`
|
|
5
|
+
`),n=[];for(let o of r){const t=o.trim();if(t.startsWith("#")){const a=t.substring(1).trim(),c=a.indexOf(" ");if(c!==-1){const l=a.substring(0,c).trim(),i=a.substring(c+1).trim(),u=ee(l);i==="true"||i==="false"?e[u]=i==="true":!isNaN(i)&&i.trim()!==""?e[u]=Number(i):e[u]=i}else console.warn(`\u65E0\u6548\u7684\u914D\u7F6E\u884C: ${t}`)}else n.push(o)}return{config:e,css:n.join(`
|
|
6
|
+
`)}}function Q(s){const e=new Map,r=s.split(`
|
|
7
|
+
`),n=[];for(let o of r){const t=o.trim();if(t.startsWith("@alias")){const a=t.match(/^@alias\s+([a-zA-Z_][a-zA-Z0-9_]*)\s+(.+?)\s*;$/);if(a){const c=a[1],l=a[2];e.set(c,l);continue}}n.push(o)}return{aliases:e,css:n.join(`
|
|
8
|
+
`)}}function O(s,e){if(!e.enableMacros)return{macros:new Map,css:s};const r=new Map,n=s.split(`
|
|
9
|
+
`),o=[];let t=!1,a="",c=[],l=[],i=0;for(let u of n){const h=u.trim();if(!t&&h.startsWith("@macro")){const f=h.match(/^@macro\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*\(([^)]*)\)\s*\{$/);if(f){t=!0,a=f[1],c=f[2].split(",").map(p=>p.trim()).filter(p=>p).map(p=>{const d=p.split(":").map(b=>b.trim());return{name:d[0].slice(1),defaultValue:d[1]||null}}),l=[],i=1;continue}}if(t){for(const f of u)f==="{"&&i++,f==="}"&&i--;i===0?(r.set(a,{params:c,body:l.join(`
|
|
10
|
+
`)}),t=!1,a="",c=[],l=[]):l.push(u);continue}o.push(u)}return{macros:r,css:o.join(`
|
|
11
|
+
`)}}function K(s,e,r){if(!r.enableMacros||e.size===0)return s;const n=Array.from(e.keys()).map(a=>a.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")).join("|"),o=new RegExp(`@(${n})\\s*\\(([^)]*)\\)\\s*;?`,"g");return(a=>{let c=a,l=!1;do l=!1,c=c.replace(o,(i,u,h)=>{l=!0;const f=e.get(u);if(!f)return console.warn(`\u672A\u5B9A\u4E49\u7684\u5B8F: ${u}`),i;const p=_(h,f.params);return E(f.body,p,r)});while(l);return c})(s)}function _(s,e){const r=new Map,n=J(s);for(let o=0;o<e.length;o++){const t=e[o];let a;o<n.length?a=n[o]:t.defaultValue!==null?a=t.defaultValue:(console.warn(`\u5B8F\u8C03\u7528\u7F3A\u5C11\u5FC5\u9700\u53C2\u6570: ${t.name}`),a=""),r.set(t.name,a)}return n.length>e.length&&console.warn("\u5B8F\u8C03\u7528\u4F20\u9012\u4E86\u8FC7\u591A\u53C2\u6570\uFF0C\u591A\u4F59\u7684\u53C2\u6570\u5C06\u88AB\u5FFD\u7565"),r}function J(s){const e=[];let r="",n=!1,o="",t=0,a=0;for(let c=0;c<s.length;c++){const l=s[c];(l==='"'||l==="'")&&!n?(n=!0,o=l):l===o&&n&&(n=!1,o=""),n||(l==="("&&t++,l===")"&&t--,l==="["&&a++,l==="]"&&a--),l===","&&!n&&t===0&&a===0?(e.push(r.trim()),r=""):r+=l}return r.trim()&&e.push(r.trim()),e}function E(s,e,r){let n=s;for(const[o,t]of e){const a=new RegExp(`\\$${o}`,"g");n=n.replace(a,t)}if(r.enableMacros){const o=Array.from($.keys()).map(t=>t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")).join("|");if(o){const t=new RegExp(`@(${o})\\s*\\(([^)]*)\\)\\s*;?`,"g");let a;do a=!1,n=n.replace(t,(c,l,i)=>{a=!0;const u=$.get(l);if(!u)return c;const h=_(i,u.params);return E(u.body,h,r)});while(a)}}return n=v(n,r),n}function ee(s){return s.replace(/-([a-z])/g,function(e,r){return r.toUpperCase()})}function V(){if(L!==null)return L;if(typeof window>"u"||!window.CSS)return L=!1,!1;try{L=CSS.supports("color","color(display-p3 1 0 0)")}catch(s){console.warn("P3\u652F\u6301\u68C0\u6D4B\u5931\u8D25:",s),L=!1}return L}function I(s,e,r){let n=s.trim();if(n.endsWith(";")&&(n=n.slice(0,-1)),!n)return[];const o=[];let t="",a=0,c=!1,l="";for(let u=0;u<n.length;u++){const h=n[u];(h==='"'||h==="'")&&!c?(c=!0,l=h):h===l&&c&&(c=!1,l=""),c||(h==="("?a++:h===")"&&a--),h===";"&&!c&&a===0?t.trim()&&(o.push(t.trim()),t=""):t+=h}t.trim()&&o.push(t.trim());const i=[];for(let u of o){if(u=u.replace(/\/\*[\s\S]*?\*\//g,""),!u.trim())continue;const h=te(u);if(h===-1){console.warn(`\u65E0\u6548\u7684CSS\u58F0\u660E: "${u}"`);continue}let f=u.substring(0,h).trim(),p=u.substring(h+1).trim();if(p.endsWith(";")&&(p=p.slice(0,-1).trim()),e.get(f)&&(f=e.get(f)),f.startsWith("@")&&r.get(f.slice(1))){const d=r.get(f.slice(1)),b=_(p.split(" ").join(","),d.params);f=E(d.body,b,U),p=""}i.push({[f]:p})}return i}function te(s){let e=!1,r="";for(let n=0;n<s.length;n++){const o=s[n];if((o==='"'||o==="'")&&!e?(e=!0,r=o):o===r&&e&&(e=!1,r=""),o===":"&&!e)return n}return-1}function N(s,e){if(!e.enableMath)return`math(${s})`;try{let r=s.replace(/\s+/g,"");return ne(r,e.enableMath)}catch(r){return console.warn("math()\u8868\u8FBE\u5F0F\u89E3\u6790\u5931\u8D25:",s,r),`calc(${s})`}}function ne(s,e){return`calc(${s.replace(/([\da-z])([+-])(-?[\da-z])/g,"$1 $2 $3").replace(/([\da-z])([+-])(-?[\da-z])/g,"$1 $2 $3")})`}function re(s,e){if(!e.enableMath)return s;let r=s;const n=/math\(([^)]+)\)/gi,o=a=>a.replace(n,(c,l)=>N(l,e));let t;do t=r,r=o(r);while(r!==t&&r.includes("math("));return r}function se(s,e){if(!e.convertLabToRGB&&!e.convertLchToRGB)return s;let r=s;r=oe(r,e);const n=/(lab|lch)\([^)]+\)/gi,o=new Map,t=c=>c.replace(n,l=>{if(o.has(l))return o.get(l);let i=l;return l.toLowerCase().startsWith("lab(")?e.convertLabToRGB&&(i=ae(l,e)):l.toLowerCase().startsWith("lch(")&&e.convertLchToRGB&&(i=ce(l,e)),o.set(l,i),i});let a;do a=r,r=t(r);while(r!==a);return r}function oe(s,e){let r=s;const n=/lab#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/gi,o=/lch#([0-9a-f]{2})([0-9a-f]{2})(\d{1,3})/gi,t=new Map;return r=r.replace(n,(a,c,l,i)=>{if(t.has(a))return t.get(a);try{const u=parseInt(c,16)/255*100,h=(parseInt(l,16)-128)*1.5,f=(parseInt(i,16)-128)*1.5,p=A(u,h,f,e);return t.set(a,p),p}catch(u){return console.warn(`\u65E0\u6CD5\u89E3\u6790lab#\u5341\u516D\u8FDB\u5236\u989C\u8272: ${a}`,u),a}}),r=r.replace(o,(a,c,l,i)=>{if(t.has(a))return t.get(a);try{const u=parseInt(c,16)/255*100,h=parseInt(l,16)/255*150,f=parseInt(i)/100*360,p=x(u,h,f),d=A(p.L,p.a,p.b,e);return t.set(a,d),d}catch(u){return console.warn(`\u65E0\u6CD5\u89E3\u6790lch#\u5341\u516D\u8FDB\u5236\u989C\u8272: ${a}`,u),a}}),r}function ae(s,e){const r=/lab\(\s*([\d.]+)(%?)\s+([\d.-]+)\s+([\d.-]+)(?:\s*\/\s*([\d.%]+))?\s*\)/i,n=s.match(r);if(!n)return s;try{let o=parseFloat(n[1]);n[2]==="%"?o=o:(o<0&&(o=0),o>100&&(o=100));const t=parseFloat(n[3]),a=parseFloat(n[4]),c=n[5]!==void 0?n[5].includes("%")?parseFloat(n[5])/100:parseFloat(n[5]):null;return A(o,t,a,e,c)}catch(o){return console.warn(`\u65E0\u6CD5\u8F6C\u6362LAB\u989C\u8272: ${s}`,o),s}}function ce(s,e){const r=/lch\(\s*([\d.]+)(%?)\s+([\d.]+)\s+([\d.]+)(deg)?(?:\s*\/\s*([\d.%]+))?\s*\)/i,n=s.match(r);if(!n)return s;try{let o=parseFloat(n[1]);n[2]==="%"?o=o:(o<0&&(o=0),o>100&&(o=100));const t=parseFloat(n[3]);let a=parseFloat(n[4]);t<0&&console.warn(`LCH\u4E2D\u7684C\u503C\u4E0D\u80FD\u4E3A\u8D1F: ${t}`),a=(a%360+360)%360;const c=x(o,t,a),l=n[6]!==void 0?n[6].includes("%")?parseFloat(n[6])/100:parseFloat(n[6]):null;return A(c.L,c.a,c.b,e,l)}catch(o){return console.warn(`\u65E0\u6CD5\u8F6C\u6362LCH\u989C\u8272: ${s}`,o),s}}function x(s,e,r){const n=r*Math.PI/180,o=e*Math.cos(n),t=e*Math.sin(n);return{L:s,a:o,b:t}}function C(s,e,r){const n=(m,y,w)=>{const D=(m+16)/116,j=y/500+D,k=D-w/200,ge=j**3>.008856?j**3:(116*j-16)/903.3,be=m>903.3*.008856?((m+16)/116)**3:m/903.3,ye=k**3>.008856?k**3:(116*k-16)/903.3;return[ge*.95047,be*1,ye*1.08883]},o=(m,y,w)=>{const M=[[3.2404542,-1.5371385,-.4985314],[-.969266,1.8760108,.041556],[.0556434,-.2040259,1.0572252]],R=M[0][0]*m+M[0][1]*y+M[0][2]*w,X=M[1][0]*m+M[1][1]*y+M[1][2]*w,q=M[2][0]*m+M[2][1]*y+M[2][2]*w;return[R,X,q]},t=m=>{const y=m<0?-1:1,w=Math.abs(m);return w<=.0031308?y*12.92*w:y*(1.055*Math.pow(w,.4166666666666667)-.055)},a=m=>Math.max(0,Math.min(255,Math.round(m*255))),[c,l,i]=n(s,e,r),[u,h,f]=o(c,l,i),p=t(u),d=t(h),b=t(f);return{r:a(p),g:a(d),b:a(b)}}function T(s,e,r){const n=(c,l,i)=>{const b=(c+16)/116,m=l/500+b,y=b-i/200,w=m**3>.008856?m**3:(116*m-16)/903.3,M=c>903.3*.008856?((c+16)/116)**3:c/903.3,R=y**3>.008856?y**3:(116*y-16)/903.3;return[w*.95047,M*1,R*1.08883]},o=(c,l,i)=>{const u=[[2.493496911941425,-.9313836179191239,-.40271078445071684],[-.8294889695615747,1.7626640603183463,.023624685841943577],[.03584583024378447,-.07617238926804182,.9568845240076872]],h=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,p=u[2][0]*c+u[2][1]*l+u[2][2]*i;return[h,f,p]},t=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]=n(s,e,r),[u,h,f]=o(c,l,i),p=t(u),d=t(h),b=t(f);return{r:Math.max(0,Math.min(1,p)),g:Math.max(0,Math.min(1,d)),b:Math.max(0,Math.min(1,b))}}catch(c){console.warn("P3\u8F6C\u6362\u5931\u8D25:",c);const l=C(s,e,r);return{r:l.r/255,g:l.g/255,b:l.b/255}}}function A(s,e,r,n,o=null){if(!n.enableP3||!V()){const t=C(s,e,r);return o!==null?`rgba(${t.r}, ${t.g}, ${t.b}, ${o})`:`rgb(${t.r}, ${t.g}, ${t.b})`}else{const t=T(s,e,r);return o!==null?`color(display-p3 ${t.r.toFixed(4)} ${t.g.toFixed(4)} ${t.b.toFixed(4)} / ${o})`:`color(display-p3 ${t.r.toFixed(4)} ${t.g.toFixed(4)} ${t.b.toFixed(4)})`}}function v(s,e){let r=s;return e.enableMath&&(r=re(r,e)),(e.convertLabToRGB||e.convertLchToRGB)&&(r=se(r,e)),r}function le(s,e){const r=s.split(`
|
|
12
|
+
`),n={},o=new Map;let t="",a=null,c=!1,l=0;for(let i of r){const u=i.trim(),h=u.match(/^\$([a-zA-Z_][a-zA-Z0-9_]*)\s*:\s*(.+?);?$/);if(h){const[,f,p]=h,d=v(p.trim(),e);a?(o.has(a)||o.set(a,{}),o.get(a)[f]=d):n[f]=d;continue}if(u.endsWith("{")){a=u.slice(0,-1).trim(),c=!0,t+=i+`
|
|
13
|
+
`;continue}if(u==="}"){if(c&&a&&o.has(a)){const f=o.get(a),p=" ".repeat(l);for(const[d,b]of Object.entries(f))t+=`${p} --${d}: ${b};
|
|
14
14
|
`}c=!1,a=null}t+=i+`
|
|
15
|
-
`,i.includes("{")&&(l+=e.indentSize),i.includes("}")&&(l=Math.max(0,l-e.indentSize))}return{globalVariables:n,selectorVariables:
|
|
16
|
-
`),t=[],a=[];let c=0;for(let l=0;l<
|
|
15
|
+
`,i.includes("{")&&(l+=e.indentSize),i.includes("}")&&(l=Math.max(0,l-e.indentSize))}return{globalVariables:n,selectorVariables:o,cssWithoutVars:t.trim()}}function ie(s,e,r,n){const o=s.split(`
|
|
16
|
+
`),t=[],a=[];let c=0;for(let l=0;l<o.length;l++){const i=o[l],u=i.trim();if(!u)continue;const h=i.match(/^(\s*)/)[0].length;if(u==="}"){if(t.length>0){const f=t.pop();if(t.length>0){const p=t[t.length-1];p.children||(p.children=[]),p.children.push(f)}else a.push(f)}continue}if(u.endsWith("{")){const p={selector:u.slice(0,-1).trim(),properties:[],children:[]};t.push(p);continue}if(!u.includes("{")&&!u.includes("}")&&u.includes(":")){if(t.length>0){const f=t[t.length-1];I(u,r,n).forEach(d=>{const b=Object.keys(d)[0],m=v(d[b],e);f.properties.push(m===""?b:`${b}: ${m}`)})}continue}}for(;t.length>0;){const l=t.pop();if(t.length===0)a.push(l);else{const i=t[t.length-1];i.children||(i.children=[]),i.children.push(l)}}return H(a,e,"",r,n)}function H(s,e,r="",n,o){let t="";const a=r.startsWith("@");for(const c of s){const l=c.selector.startsWith("@");let i=(a?" ".repeat(e.indentSize):"")+c.selector;if(l&&(i=c.selector+` {
|
|
17
17
|
`),r&&(i.includes("&")?i=i.replace(/&/g,r):i.trim().startsWith(":")?i=r+i:i=r+(a?"":" ")+i),c.properties.length>0){t+=(l?"":i)+` {
|
|
18
|
-
`;for(const u of c.properties)
|
|
18
|
+
`;for(const u of c.properties)I(u,n,o).forEach(f=>{const p=Object.keys(f)[0],d=v(f[p],e);t+=(a?" ".repeat(e.indentSize):"")+" ".repeat(e.indentSize)+(d===""?p:`${p}: ${d};
|
|
19
19
|
`)});t+=a?" ".repeat(e.indentSize)+`}
|
|
20
20
|
`:`}
|
|
21
21
|
|
|
22
|
-
`}c.children&&c.children.length>0&&(t+=
|
|
22
|
+
`}c.children&&c.children.length>0&&(t+=H(c.children,e,i,n,o)),a&&(t+=`}
|
|
23
23
|
|
|
24
24
|
`)}return t.trim()+`
|
|
25
25
|
|
|
26
|
-
`}function
|
|
26
|
+
`}function ue(s,e,r){let n=s;for(const o in e)n=n.replace(new RegExp(`(?:\\$\\$|\\$)(${o})(?=[^a-zA-Z0-9_])`,"g"),(t,a)=>t.startsWith("$$")?`attr(${a})`:`var(--${a})`);return n=v(n,r),n}function fe(s,e){if(Object.keys(s).length===0)return"";const r=Object.entries(s).map(([n,o])=>{const t=v(o,e);return" ".repeat(e.indentSize)+`--${n}: ${t};`}).join(`
|
|
27
27
|
`);return`${e.rootSelector} {
|
|
28
28
|
${r}
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
`}function
|
|
32
|
-
`);let t=null;for(let a of
|
|
33
|
-
`}return n.trim()}async function
|
|
31
|
+
`}function pe(s,e,r){let n="";const o=s.split(`
|
|
32
|
+
`);let t=null;for(let a of o){const c=a.trim();c.endsWith("{")&&(t=c.slice(0,-1).trim()),c==="}"&&t&&(t=null),n+=v(a,r)+`
|
|
33
|
+
`}return n.trim()}async function G(s,e){const r=/@import\s+([^;]+?)\s*;/g;return(async o=>{const t=[],a=o.replace(r,(c,l)=>{const i=he(l,e).then(u=>G(u,e)).catch(u=>(console.warn(`\u65E0\u6CD5\u5BFC\u5165CSS\u6587\u4EF6: ${l}`,u),""));return t.push(i),`__IMPORT_PLACEHOLDER_${t.length-1}__`});if(t.length>0){const c=await Promise.all(t);let l=a;for(let i=0;i<c.length;i++)l=l.replace(`__IMPORT_PLACEHOLDER_${i}__`,c[i]);return l}return a})(s)}async function he(s,e){if(e.importCache&&z.has(s))return z.get(s);const r=e.importBaseUrl?new URL(s,e.importBaseUrl).href:s;let n;if(typeof process<"u"&&process.versions&&process.versions.node&&typeof require<"u")try{const t=require("fs"),a=require("path");let c=r;r.startsWith(".")&&(c=a.join(process.cwd(),r)),n=t.readFileSync(c,"utf-8")}catch(t){throw new Error(`Node.js\u6587\u4EF6\u8BFB\u53D6\u5931\u8D25: ${r} - ${t.message}`)}else try{const t=new AbortController,a=setTimeout(()=>t.abort(),e.importTimeout),c=await fetch(r,{signal:t.signal,headers:{Accept:"text/css"}});if(clearTimeout(a),!c.ok)throw new Error(`HTTP\u9519\u8BEF: ${c.status} ${c.statusText}`);n=await c.text()}catch(t){throw t.name==="AbortError"?new Error(`\u5BFC\u5165\u8D85\u65F6: ${r}`):new Error(`\u65E0\u6CD5\u83B7\u53D6CSS: ${r} - ${t.message}`)}return e.importCache&&z.set(s,n),n}async function P(s,e={}){let{config:r,css:n}=Y(s);const o={...g,...e,...r};n=await G(n,o);let t=n,a=new Map;if(o.enableAlias){const{aliases:y,css:w}=Q(t);a=y,t=w}let c=t,l=new Map;if(o.enableMacros){const{macros:y,css:w}=O(c,o);l=y,c=w;for(const[M,R]of l)$.set(M,R)}let i=c;o.enableMacros&&l.size>0&&(i=K(i,l,o));const{globalVariables:u,selectorVariables:h,cssWithoutVars:f}=le(i,o);let p=f.trim();if(o.enableNesting&&f.includes("{"))try{p=ie(f,o,a,l)}catch(y){console.warn("\u5D4C\u5957\u89E3\u6790\u5931\u8D25\uFF0C\u4F7F\u7528\u539F\u59CBCSS:",y)}const d=fe(u,o);let b=p;h.size>0&&(b=pe(p,h,o)),b=ue(b,{...u,...h},o);let m=d+b;for(const[y,w]of S)try{m=w.convert(m,o)}catch(M){console.error("\u63D2\u4EF6\u5904\u7406\u5931\u8D25:",M)}return m}function Z(s={}){const e={...g,...s},r=document.querySelectorAll(`style[${e.styleTagAttribute||"e"}]`);return r.forEach(n=>{let o=n.textContent;(async()=>{try{n.getAttribute("src")&&(o="@import "+n.getAttribute("src")+";");const a=await P(o,e),c=document.createElement("style");c.textContent=a,n.parentNode.insertBefore(c,n.nextSibling),e.preserveOriginal?n.style.display="none":n.remove()}catch(a){console.error("\u5904\u7406style\u6807\u7B7E\u5931\u8D25:",a)}})()}),r.length}function U(s={}){g={...g,...s}}function W(s,e={}){const r={...g,...e};if(s)return(async()=>{try{const n=await P(s,r),o=document.createElement("style");return o.textContent=n,document.head.appendChild(o),o}catch(n){return console.error("\u5E94\u7528CSS\u5931\u8D25:",n),null}})();document.readyState==="loading"?document.addEventListener("DOMContentLoaded",()=>{Z(r)}):Z(r)}const de={convert:P,apply:W,config:U,supportsP3:V(),detectP3Support:V,imports:{clearCache:function(){z.clear()},setBaseUrl:function(s){g.importBaseUrl=s},setCacheEnabled:function(s){g.importCache=s},setTimeout:function(s){g.importTimeout=s}},plugins:{use(s){const e=new s;return S.set(e.name??s.name,e),!0},remove(s){return S.get(s)?.destroy&&S.get(s).destroy(),S.delete(s),!0},getAll:function(){return Array.from(S.entries())},clear:function(){S.clear()}},aliases:{add:function(s,e){B.set(s,e)},remove:function(s){B.delete(s)},getAll:function(){return Array.from(B.entries())},clear:function(){B.clear()}},macros:{define:function(s,e,r=[]){if(typeof e=="function"){const o=e.toString().match(/{([\s\S]*)}/);o&&$.set(s,{params:r.map(t=>typeof t=="string"?{name:t}:t),body:o[1].trim()})}else $.set(s,{params:r.map(n=>typeof n=="string"?{name:n}:n),body:e})},call:function(s,...e){const r=$.get(s);if(!r)throw new Error(`\u672A\u5B9A\u4E49\u7684\u5B8F: ${s}`);const n=new Map;for(let o=0;o<r.params.length;o++){const t=r.params[o],a=o<e.length?e[o]:t.defaultValue;if(a===void 0&&t.defaultValue===null)throw new Error(`\u7F3A\u5C11\u5FC5\u9700\u53C2\u6570: ${t.name}`);n.set(t.name,a!==void 0?a:"")}return E(r.body,n,g)},getAll:function(){return Array.from($.entries())},remove:function(s){$.delete(s)},clear:function(){$.clear()},parse:function(s){const{macros:e}=O(s,g);for(const[r,n]of e)$.set(r,n)}},math:{evaluate:function(s){return N(s,g)}},colorUtils:{labToRGB:C,lchToLab:x,lchToRGB:function(s,e,r){const n=x(s,e,r);return C(n.L,n.a,n.b)},labToP3:T,lchToP3:function(s,e,r){const n=x(s,e,r);return T(n.L,n.a,n.b)},parseHexLab:function(s){const e=s.match(/lab#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/i);if(!e)return null;const r=e[1],n=e[2],o=e[3],t=parseInt(r,16)/255*100,a=(parseInt(n,16)-128)*1.5,c=(parseInt(o,16)-128)*1.5;return C(t,a,c)},parseHexLch:function(s){const e=s.match(/lch#([0-9a-f]{2})([0-9a-f]{2})(\d{1,3})/i);if(!e)return null;const r=e[1],n=e[2],o=e[3],t=parseInt(r,16)/255*100,a=parseInt(n,16)/255*150,c=parseInt(o)/100*360,l=x(t,a,c);return C(l.L,l.a,l.b)},generateColor:function(s,e,r,n=null,o=!0){return A(s,e,r,{enableP3:o},n)},parseColor:function(s){try{const e=s.match(/lab\(\s*([\d.]+)(%?)\s+([\d.-]+)\s+([\d.-]+)(?:\s*\/\s*([\d.%]+))?\s*\)/i);if(e){let n=parseFloat(e[1]);const o=parseFloat(e[3]),t=parseFloat(e[4]),a=e[5]?e[5].includes("%")?parseFloat(e[5])/100:parseFloat(e[5]):null,c=A(n,o,t,g,a);return{L:n,A:o,B:t,alpha:a,rgb:C(n,o,t),p3:T(n,o,t),colorString:c}}const r=s.match(/lch\(\s*([\d.]+)(%?)\s+([\d.]+)\s+([\d.]+)(deg)?(?:\s*\/\s*([\d.%]+))?\s*\)/i);if(r){let n=parseFloat(r[1]);const o=parseFloat(r[3]);let t=parseFloat(r[4]);const a=r[6]?r[6].includes("%")?parseFloat(r[6])/100:parseFloat(r[6]):null,c=x(n,o,t),l=A(c.L,c.a,c.b,g,a);return{L:n,C:o,H:t,alpha:a,lab:c,rgb:C(c.L,c.a,c.b),p3:T(c.L,c.a,c.b),colorString:l}}return null}catch(e){return console.warn("\u65E0\u6CD5\u89E3\u6790\u989C\u8272:",s,e),null}}}},F=async function(...s){if(s.length>1||s[0]&&s[0].raw)return await me(...s);const e=s[0];if(typeof e=="string"){const r=await P(e,{...g,...s[1]});return r&&typeof r.then=="function",r}return typeof e=="object"&&e!==null?(g={...g,...e},F):s.length===0?W():F};async function me(s,...e){let r=s[0];for(let o=0;o<e.length;o++){const t=e[o];let a="";typeof t=="function"?a=t():Array.isArray(t)?a=t.join(" "):a=String(t??""),r+=a+s[o+1]}const n=await P(r,g);return n&&typeof n.then=="function",n}return Object.assign(F,de),Object.setPrototypeOf(F,Function.prototype),typeof window<"u"&&(W(),Object.defineProperty(window.HTMLElement.prototype,"cssVar",{get(){const s=this;return new Proxy(()=>{},{get(e,r){const n=r.startsWith("--")?r:`--${r}`;return s.style.getPropertyValue(n)},set(e,r,n){const o=r.startsWith("--")?r:`--${r}`;return s.style.setProperty(o,n),!0},apply(e,r,n){const o=n[0],t=n[1],a=o.startsWith("--")?o:`--${o}`;if(t===void 0)return s.style.getPropertyValue(a);s.style.setProperty(a,t)}})}})),F})();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
|
-
`),n=[];for(let
|
|
6
|
-
`)}}function
|
|
7
|
-
`),n=[];for(let
|
|
8
|
-
`)}}function
|
|
9
|
-
`),
|
|
10
|
-
`)}),t=!1,a="",c=[],l=[]):l.push(u);continue}
|
|
11
|
-
`)}}function
|
|
12
|
-
`),n={},
|
|
13
|
-
`;continue}if(u==="}"){if(c&&a&&
|
|
4
|
+
*/const z=(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},L=null;const B=new Map,E=new Map,$=new Map,S=new Map;function Q(s){const e={...g},r=s.split(`
|
|
5
|
+
`),n=[];for(let o of r){const t=o.trim();if(t.startsWith("#")){const a=t.substring(1).trim(),c=a.indexOf(" ");if(c!==-1){const l=a.substring(0,c).trim(),i=a.substring(c+1).trim(),u=te(l);i==="true"||i==="false"?e[u]=i==="true":!isNaN(i)&&i.trim()!==""?e[u]=Number(i):e[u]=i}else console.warn(`\u65E0\u6548\u7684\u914D\u7F6E\u884C: ${t}`)}else n.push(o)}return{config:e,css:n.join(`
|
|
6
|
+
`)}}function K(s){const e=new Map,r=s.split(`
|
|
7
|
+
`),n=[];for(let o of r){const t=o.trim();if(t.startsWith("@alias")){const a=t.match(/^@alias\s+([a-zA-Z_][a-zA-Z0-9_]*)\s+(.+?)\s*;$/);if(a){const c=a[1],l=a[2];e.set(c,l);continue}}n.push(o)}return{aliases:e,css:n.join(`
|
|
8
|
+
`)}}function I(s,e){if(!e.enableMacros)return{macros:new Map,css:s};const r=new Map,n=s.split(`
|
|
9
|
+
`),o=[];let t=!1,a="",c=[],l=[],i=0;for(let u of n){const h=u.trim();if(!t&&h.startsWith("@macro")){const f=h.match(/^@macro\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*\(([^)]*)\)\s*\{$/);if(f){t=!0,a=f[1],c=f[2].split(",").map(p=>p.trim()).filter(p=>p).map(p=>{const d=p.split(":").map(b=>b.trim());return{name:d[0].slice(1),defaultValue:d[1]||null}}),l=[],i=1;continue}}if(t){for(const f of u)f==="{"&&i++,f==="}"&&i--;i===0?(r.set(a,{params:c,body:l.join(`
|
|
10
|
+
`)}),t=!1,a="",c=[],l=[]):l.push(u);continue}o.push(u)}return{macros:r,css:o.join(`
|
|
11
|
+
`)}}function J(s,e,r){if(!r.enableMacros||e.size===0)return s;const n=Array.from(e.keys()).map(a=>a.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")).join("|"),o=new RegExp(`@(${n})\\s*\\(([^)]*)\\)\\s*;?`,"g");return(a=>{let c=a,l=!1;do l=!1,c=c.replace(o,(i,u,h)=>{l=!0;const f=e.get(u);if(!f)return console.warn(`\u672A\u5B9A\u4E49\u7684\u5B8F: ${u}`),i;const p=V(h,f.params);return _(f.body,p,r)});while(l);return c})(s)}function V(s,e){const r=new Map,n=ee(s);for(let o=0;o<e.length;o++){const t=e[o];let a;o<n.length?a=n[o]:t.defaultValue!==null?a=t.defaultValue:(console.warn(`\u5B8F\u8C03\u7528\u7F3A\u5C11\u5FC5\u9700\u53C2\u6570: ${t.name}`),a=""),r.set(t.name,a)}return n.length>e.length&&console.warn("\u5B8F\u8C03\u7528\u4F20\u9012\u4E86\u8FC7\u591A\u53C2\u6570\uFF0C\u591A\u4F59\u7684\u53C2\u6570\u5C06\u88AB\u5FFD\u7565"),r}function ee(s){const e=[];let r="",n=!1,o="",t=0,a=0;for(let c=0;c<s.length;c++){const l=s[c];(l==='"'||l==="'")&&!n?(n=!0,o=l):l===o&&n&&(n=!1,o=""),n||(l==="("&&t++,l===")"&&t--,l==="["&&a++,l==="]"&&a--),l===","&&!n&&t===0&&a===0?(e.push(r.trim()),r=""):r+=l}return r.trim()&&e.push(r.trim()),e}function _(s,e,r){let n=s;for(const[o,t]of e){const a=new RegExp(`\\$${o}`,"g");n=n.replace(a,t)}if(r.enableMacros){const o=Array.from($.keys()).map(t=>t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")).join("|");if(o){const t=new RegExp(`@(${o})\\s*\\(([^)]*)\\)\\s*;?`,"g");let a;do a=!1,n=n.replace(t,(c,l,i)=>{a=!0;const u=$.get(l);if(!u)return c;const h=V(i,u.params);return _(u.body,h,r)});while(a)}}return n=A(n,r),n}function te(s){return s.replace(/-([a-z])/g,function(e,r){return r.toUpperCase()})}function W(){if(L!==null)return L;if(typeof window>"u"||!window.CSS)return L=!1,!1;try{L=CSS.supports("color","color(display-p3 1 0 0)")}catch(s){console.warn("P3\u652F\u6301\u68C0\u6D4B\u5931\u8D25:",s),L=!1}return L}function N(s,e,r){let n=s.trim();if(n.endsWith(";")&&(n=n.slice(0,-1)),!n)return[];const o=[];let t="",a=0,c=!1,l="";for(let u=0;u<n.length;u++){const h=n[u];(h==='"'||h==="'")&&!c?(c=!0,l=h):h===l&&c&&(c=!1,l=""),c||(h==="("?a++:h===")"&&a--),h===";"&&!c&&a===0?t.trim()&&(o.push(t.trim()),t=""):t+=h}t.trim()&&o.push(t.trim());const i=[];for(let u of o){if(u=u.replace(/\/\*[\s\S]*?\*\//g,""),!u.trim())continue;const h=ne(u);if(h===-1){console.warn(`\u65E0\u6548\u7684CSS\u58F0\u660E: "${u}"`);continue}let f=u.substring(0,h).trim(),p=u.substring(h+1).trim();if(p.endsWith(";")&&(p=p.slice(0,-1).trim()),e.get(f)&&(f=e.get(f)),f.startsWith("@")&&r.get(f.slice(1))){const d=r.get(f.slice(1)),b=V(p.split(" ").join(","),d.params);f=_(d.body,b,X),p=""}i.push({[f]:p})}return i}function ne(s){let e=!1,r="";for(let n=0;n<s.length;n++){const o=s[n];if((o==='"'||o==="'")&&!e?(e=!0,r=o):o===r&&e&&(e=!1,r=""),o===":"&&!e)return n}return-1}function H(s,e){if(!e.enableMath)return`math(${s})`;try{let r=s.replace(/\s+/g,"");return re(r,e.enableMath)}catch(r){return console.warn("math()\u8868\u8FBE\u5F0F\u89E3\u6790\u5931\u8D25:",s,r),`calc(${s})`}}function re(s,e){return`calc(${s.replace(/([\da-z])([+-])(-?[\da-z])/g,"$1 $2 $3").replace(/([\da-z])([+-])(-?[\da-z])/g,"$1 $2 $3")})`}function se(s,e){if(!e.enableMath)return s;let r=s;const n=/math\(([^)]+)\)/gi,o=a=>a.replace(n,(c,l)=>H(l,e));let t;do t=r,r=o(r);while(r!==t&&r.includes("math("));return r}function oe(s,e){if(!e.convertLabToRGB&&!e.convertLchToRGB)return s;let r=s;r=ae(r,e);const n=/(lab|lch)\([^)]+\)/gi,o=new Map,t=c=>c.replace(n,l=>{if(o.has(l))return o.get(l);let i=l;return l.toLowerCase().startsWith("lab(")?e.convertLabToRGB&&(i=ce(l,e)):l.toLowerCase().startsWith("lch(")&&e.convertLchToRGB&&(i=le(l,e)),o.set(l,i),i});let a;do a=r,r=t(r);while(r!==a);return r}function ae(s,e){let r=s;const n=/lab#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/gi,o=/lch#([0-9a-f]{2})([0-9a-f]{2})(\d{1,3})/gi,t=new Map;return r=r.replace(n,(a,c,l,i)=>{if(t.has(a))return t.get(a);try{const u=parseInt(c,16)/255*100,h=(parseInt(l,16)-128)*1.5,f=(parseInt(i,16)-128)*1.5,p=v(u,h,f,e);return t.set(a,p),p}catch(u){return console.warn(`\u65E0\u6CD5\u89E3\u6790lab#\u5341\u516D\u8FDB\u5236\u989C\u8272: ${a}`,u),a}}),r=r.replace(o,(a,c,l,i)=>{if(t.has(a))return t.get(a);try{const u=parseInt(c,16)/255*100,h=parseInt(l,16)/255*150,f=parseInt(i)/100*360,p=x(u,h,f),d=v(p.L,p.a,p.b,e);return t.set(a,d),d}catch(u){return console.warn(`\u65E0\u6CD5\u89E3\u6790lch#\u5341\u516D\u8FDB\u5236\u989C\u8272: ${a}`,u),a}}),r}function ce(s,e){const r=/lab\(\s*([\d.]+)(%?)\s+([\d.-]+)\s+([\d.-]+)(?:\s*\/\s*([\d.%]+))?\s*\)/i,n=s.match(r);if(!n)return s;try{let o=parseFloat(n[1]);n[2]==="%"?o=o:(o<0&&(o=0),o>100&&(o=100));const t=parseFloat(n[3]),a=parseFloat(n[4]),c=n[5]!==void 0?n[5].includes("%")?parseFloat(n[5])/100:parseFloat(n[5]):null;return v(o,t,a,e,c)}catch(o){return console.warn(`\u65E0\u6CD5\u8F6C\u6362LAB\u989C\u8272: ${s}`,o),s}}function le(s,e){const r=/lch\(\s*([\d.]+)(%?)\s+([\d.]+)\s+([\d.]+)(deg)?(?:\s*\/\s*([\d.%]+))?\s*\)/i,n=s.match(r);if(!n)return s;try{let o=parseFloat(n[1]);n[2]==="%"?o=o:(o<0&&(o=0),o>100&&(o=100));const t=parseFloat(n[3]);let a=parseFloat(n[4]);t<0&&console.warn(`LCH\u4E2D\u7684C\u503C\u4E0D\u80FD\u4E3A\u8D1F: ${t}`),a=(a%360+360)%360;const c=x(o,t,a),l=n[6]!==void 0?n[6].includes("%")?parseFloat(n[6])/100:parseFloat(n[6]):null;return v(c.L,c.a,c.b,e,l)}catch(o){return console.warn(`\u65E0\u6CD5\u8F6C\u6362LCH\u989C\u8272: ${s}`,o),s}}function x(s,e,r){const n=r*Math.PI/180,o=e*Math.cos(n),t=e*Math.sin(n);return{L:s,a:o,b:t}}function C(s,e,r){const n=(m,y,w)=>{const Y=(m+16)/116,k=y/500+Y,O=Y-w/200,be=k**3>.008856?k**3:(116*k-16)/903.3,ye=m>903.3*.008856?((m+16)/116)**3:m/903.3,we=O**3>.008856?O**3:(116*O-16)/903.3;return[be*.95047,ye*1,we*1.08883]},o=(m,y,w)=>{const M=[[3.2404542,-1.5371385,-.4985314],[-.969266,1.8760108,.041556],[.0556434,-.2040259,1.0572252]],R=M[0][0]*m+M[0][1]*y+M[0][2]*w,q=M[1][0]*m+M[1][1]*y+M[1][2]*w,D=M[2][0]*m+M[2][1]*y+M[2][2]*w;return[R,q,D]},t=m=>{const y=m<0?-1:1,w=Math.abs(m);return w<=.0031308?y*12.92*w:y*(1.055*Math.pow(w,.4166666666666667)-.055)},a=m=>Math.max(0,Math.min(255,Math.round(m*255))),[c,l,i]=n(s,e,r),[u,h,f]=o(c,l,i),p=t(u),d=t(h),b=t(f);return{r:a(p),g:a(d),b:a(b)}}function P(s,e,r){const n=(c,l,i)=>{const b=(c+16)/116,m=l/500+b,y=b-i/200,w=m**3>.008856?m**3:(116*m-16)/903.3,M=c>903.3*.008856?((c+16)/116)**3:c/903.3,R=y**3>.008856?y**3:(116*y-16)/903.3;return[w*.95047,M*1,R*1.08883]},o=(c,l,i)=>{const u=[[2.493496911941425,-.9313836179191239,-.40271078445071684],[-.8294889695615747,1.7626640603183463,.023624685841943577],[.03584583024378447,-.07617238926804182,.9568845240076872]],h=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,p=u[2][0]*c+u[2][1]*l+u[2][2]*i;return[h,f,p]},t=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]=n(s,e,r),[u,h,f]=o(c,l,i),p=t(u),d=t(h),b=t(f);return{r:Math.max(0,Math.min(1,p)),g:Math.max(0,Math.min(1,d)),b:Math.max(0,Math.min(1,b))}}catch(c){console.warn("P3\u8F6C\u6362\u5931\u8D25:",c);const l=C(s,e,r);return{r:l.r/255,g:l.g/255,b:l.b/255}}}function v(s,e,r,n,o=null){if(!n.enableP3||!W()){const t=C(s,e,r);return o!==null?`rgba(${t.r}, ${t.g}, ${t.b}, ${o})`:`rgb(${t.r}, ${t.g}, ${t.b})`}else{const t=P(s,e,r);return o!==null?`color(display-p3 ${t.r.toFixed(4)} ${t.g.toFixed(4)} ${t.b.toFixed(4)} / ${o})`:`color(display-p3 ${t.r.toFixed(4)} ${t.g.toFixed(4)} ${t.b.toFixed(4)})`}}function A(s,e){let r=s;return e.enableMath&&(r=se(r,e)),(e.convertLabToRGB||e.convertLchToRGB)&&(r=oe(r,e)),r}function ie(s,e){const r=s.split(`
|
|
12
|
+
`),n={},o=new Map;let t="",a=null,c=!1,l=0;for(let i of r){const u=i.trim(),h=u.match(/^\$([a-zA-Z_][a-zA-Z0-9_]*)\s*:\s*(.+?);?$/);if(h){const[,f,p]=h,d=A(p.trim(),e);a?(o.has(a)||o.set(a,{}),o.get(a)[f]=d):n[f]=d;continue}if(u.endsWith("{")){a=u.slice(0,-1).trim(),c=!0,t+=i+`
|
|
13
|
+
`;continue}if(u==="}"){if(c&&a&&o.has(a)){const f=o.get(a),p=" ".repeat(l);for(const[d,b]of Object.entries(f))t+=`${p} --${d}: ${b};
|
|
14
14
|
`}c=!1,a=null}t+=i+`
|
|
15
|
-
`,i.includes("{")&&(l+=e.indentSize),i.includes("}")&&(l=Math.max(0,l-e.indentSize))}return{globalVariables:n,selectorVariables:
|
|
16
|
-
`),t=[],a=[];let c=0;for(let l=0;l<
|
|
15
|
+
`,i.includes("{")&&(l+=e.indentSize),i.includes("}")&&(l=Math.max(0,l-e.indentSize))}return{globalVariables:n,selectorVariables:o,cssWithoutVars:t.trim()}}function ue(s,e,r,n){const o=s.split(`
|
|
16
|
+
`),t=[],a=[];let c=0;for(let l=0;l<o.length;l++){const i=o[l],u=i.trim();if(!u)continue;const h=i.match(/^(\s*)/)[0].length;if(u==="}"){if(t.length>0){const f=t.pop();if(t.length>0){const p=t[t.length-1];p.children||(p.children=[]),p.children.push(f)}else a.push(f)}continue}if(u.endsWith("{")){const p={selector:u.slice(0,-1).trim(),properties:[],children:[]};t.push(p);continue}if(!u.includes("{")&&!u.includes("}")&&u.includes(":")){if(t.length>0){const f=t[t.length-1];N(u,r,n).forEach(d=>{const b=Object.keys(d)[0],m=A(d[b],e);f.properties.push(m===""?b:`${b}: ${m}`)})}continue}}for(;t.length>0;){const l=t.pop();if(t.length===0)a.push(l);else{const i=t[t.length-1];i.children||(i.children=[]),i.children.push(l)}}return G(a,e,"",r,n)}function G(s,e,r="",n,o){let t="";const a=r.startsWith("@");for(const c of s){const l=c.selector.startsWith("@");let i=(a?" ".repeat(e.indentSize):"")+c.selector;if(l&&(i=c.selector+` {
|
|
17
17
|
`),r&&(i.includes("&")?i=i.replace(/&/g,r):i.trim().startsWith(":")?i=r+i:i=r+(a?"":" ")+i),c.properties.length>0){t+=(l?"":i)+` {
|
|
18
|
-
`;for(const u of c.properties)
|
|
18
|
+
`;for(const u of c.properties)N(u,n,o).forEach(f=>{const p=Object.keys(f)[0],d=A(f[p],e);t+=(a?" ".repeat(e.indentSize):"")+" ".repeat(e.indentSize)+(d===""?p:`${p}: ${d};
|
|
19
19
|
`)});t+=a?" ".repeat(e.indentSize)+`}
|
|
20
20
|
`:`}
|
|
21
21
|
|
|
22
|
-
`}c.children&&c.children.length>0&&(t+=G(c.children,e,i,n,
|
|
22
|
+
`}c.children&&c.children.length>0&&(t+=G(c.children,e,i,n,o)),a&&(t+=`}
|
|
23
23
|
|
|
24
24
|
`)}return t.trim()+`
|
|
25
25
|
|
|
26
|
-
`}function
|
|
26
|
+
`}function fe(s,e,r){let n=s;for(const o in e)n=n.replace(new RegExp(`(?:\\$\\$|\\$)(${o})(?=[^a-zA-Z0-9_])`,"g"),(t,a)=>t.startsWith("$$")?`attr(${a})`:`var(--${a})`);return n=A(n,r),n}function pe(s,e){if(Object.keys(s).length===0)return"";const r=Object.entries(s).map(([n,o])=>{const t=A(o,e);return" ".repeat(e.indentSize)+`--${n}: ${t};`}).join(`
|
|
27
27
|
`);return`${e.rootSelector} {
|
|
28
28
|
${r}
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
`}function
|
|
32
|
-
`);let t=null;for(let a of
|
|
33
|
-
`}return n.trim()}async function
|
|
31
|
+
`}function he(s,e,r){let n="";const o=s.split(`
|
|
32
|
+
`);let t=null;for(let a of o){const c=a.trim();c.endsWith("{")&&(t=c.slice(0,-1).trim()),c==="}"&&t&&(t=null),n+=A(a,r)+`
|
|
33
|
+
`}return n.trim()}async function Z(s,e){const r=/@import\s+([^;]+?)\s*;/g;return(async o=>{const t=[],a=o.replace(r,(c,l)=>{const i=de(l,e).then(u=>Z(u,e)).catch(u=>(console.warn(`\u65E0\u6CD5\u5BFC\u5165CSS\u6587\u4EF6: ${l}`,u),""));return t.push(i),`__IMPORT_PLACEHOLDER_${t.length-1}__`});if(t.length>0){const c=await Promise.all(t);let l=a;for(let i=0;i<c.length;i++)l=l.replace(`__IMPORT_PLACEHOLDER_${i}__`,c[i]);return l}return a})(s)}async function de(s,e){if(e.importCache&&B.has(s))return B.get(s);const r=e.importBaseUrl?new URL(s,e.importBaseUrl).href:s;let n;if(typeof process<"u"&&process.versions&&process.versions.node&&typeof require<"u")try{const t=require("fs"),a=require("path");let c=r;r.startsWith(".")&&(c=a.join(process.cwd(),r)),n=t.readFileSync(c,"utf-8")}catch(t){throw new Error(`Node.js\u6587\u4EF6\u8BFB\u53D6\u5931\u8D25: ${r} - ${t.message}`)}else try{const t=new AbortController,a=setTimeout(()=>t.abort(),e.importTimeout),c=await fetch(r,{signal:t.signal,headers:{Accept:"text/css"}});if(clearTimeout(a),!c.ok)throw new Error(`HTTP\u9519\u8BEF: ${c.status} ${c.statusText}`);n=await c.text()}catch(t){throw t.name==="AbortError"?new Error(`\u5BFC\u5165\u8D85\u65F6: ${r}`):new Error(`\u65E0\u6CD5\u83B7\u53D6CSS: ${r} - ${t.message}`)}return e.importCache&&B.set(s,n),n}async function T(s,e={}){let{config:r,css:n}=Q(s);const o={...g,...e,...r};n=await Z(n,o);let t=n,a=new Map;if(o.enableAlias){const{aliases:y,css:w}=K(t);a=y,t=w}let c=t,l=new Map;if(o.enableMacros){const{macros:y,css:w}=I(c,o);l=y,c=w;for(const[M,R]of l)$.set(M,R)}let i=c;o.enableMacros&&l.size>0&&(i=J(i,l,o));const{globalVariables:u,selectorVariables:h,cssWithoutVars:f}=ie(i,o);let p=f.trim();if(o.enableNesting&&f.includes("{"))try{p=ue(f,o,a,l)}catch(y){console.warn("\u5D4C\u5957\u89E3\u6790\u5931\u8D25\uFF0C\u4F7F\u7528\u539F\u59CBCSS:",y)}const d=pe(u,o);let b=p;h.size>0&&(b=he(p,h,o)),b=fe(b,{...u,...h},o);let m=d+b;for(const[y,w]of S)try{m=w.convert(m,o)}catch(M){console.error("\u63D2\u4EF6\u5904\u7406\u5931\u8D25:",M)}return m}function U(s={}){const e={...g,...s},r=document.querySelectorAll(`style[${e.styleTagAttribute||"e"}]`);return r.forEach(n=>{let o=n.textContent;(async()=>{try{n.getAttribute("src")&&(o="@import "+n.getAttribute("src")+";");const a=await T(o,e),c=document.createElement("style");c.textContent=a,n.parentNode.insertBefore(c,n.nextSibling),e.preserveOriginal?n.style.display="none":n.remove()}catch(a){console.error("\u5904\u7406style\u6807\u7B7E\u5931\u8D25:",a)}})()}),r.length}function X(s={}){g={...g,...s}}function j(s,e={}){const r={...g,...e};if(s)return(async()=>{try{const n=await T(s,r),o=document.createElement("style");return o.textContent=n,document.head.appendChild(o),o}catch(n){return console.error("\u5E94\u7528CSS\u5931\u8D25:",n),null}})();document.readyState==="loading"?document.addEventListener("DOMContentLoaded",()=>{U(r)}):U(r)}const me={convert:T,apply:j,config:X,supportsP3:W(),detectP3Support:W,imports:{clearCache:function(){B.clear()},setBaseUrl:function(s){g.importBaseUrl=s},setCacheEnabled:function(s){g.importCache=s},setTimeout:function(s){g.importTimeout=s}},plugins:{use(s){const e=new s;return S.set(e.name??s.name,e),!0},remove(s){return S.get(s)?.destroy&&S.get(s).destroy(),S.delete(s),!0},getAll:function(){return Array.from(S.entries())},clear:function(){S.clear()}},aliases:{add:function(s,e){E.set(s,e)},remove:function(s){E.delete(s)},getAll:function(){return Array.from(E.entries())},clear:function(){E.clear()}},macros:{define:function(s,e,r=[]){if(typeof e=="function"){const o=e.toString().match(/{([\s\S]*)}/);o&&$.set(s,{params:r.map(t=>typeof t=="string"?{name:t}:t),body:o[1].trim()})}else $.set(s,{params:r.map(n=>typeof n=="string"?{name:n}:n),body:e})},call:function(s,...e){const r=$.get(s);if(!r)throw new Error(`\u672A\u5B9A\u4E49\u7684\u5B8F: ${s}`);const n=new Map;for(let o=0;o<r.params.length;o++){const t=r.params[o],a=o<e.length?e[o]:t.defaultValue;if(a===void 0&&t.defaultValue===null)throw new Error(`\u7F3A\u5C11\u5FC5\u9700\u53C2\u6570: ${t.name}`);n.set(t.name,a!==void 0?a:"")}return _(r.body,n,g)},getAll:function(){return Array.from($.entries())},remove:function(s){$.delete(s)},clear:function(){$.clear()},parse:function(s){const{macros:e}=I(s,g);for(const[r,n]of e)$.set(r,n)}},math:{evaluate:function(s){return H(s,g)}},colorUtils:{labToRGB:C,lchToLab:x,lchToRGB:function(s,e,r){const n=x(s,e,r);return C(n.L,n.a,n.b)},labToP3:P,lchToP3:function(s,e,r){const n=x(s,e,r);return P(n.L,n.a,n.b)},parseHexLab:function(s){const e=s.match(/lab#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/i);if(!e)return null;const r=e[1],n=e[2],o=e[3],t=parseInt(r,16)/255*100,a=(parseInt(n,16)-128)*1.5,c=(parseInt(o,16)-128)*1.5;return C(t,a,c)},parseHexLch:function(s){const e=s.match(/lch#([0-9a-f]{2})([0-9a-f]{2})(\d{1,3})/i);if(!e)return null;const r=e[1],n=e[2],o=e[3],t=parseInt(r,16)/255*100,a=parseInt(n,16)/255*150,c=parseInt(o)/100*360,l=x(t,a,c);return C(l.L,l.a,l.b)},generateColor:function(s,e,r,n=null,o=!0){return v(s,e,r,{enableP3:o},n)},parseColor:function(s){try{const e=s.match(/lab\(\s*([\d.]+)(%?)\s+([\d.-]+)\s+([\d.-]+)(?:\s*\/\s*([\d.%]+))?\s*\)/i);if(e){let n=parseFloat(e[1]);const o=parseFloat(e[3]),t=parseFloat(e[4]),a=e[5]?e[5].includes("%")?parseFloat(e[5])/100:parseFloat(e[5]):null,c=v(n,o,t,g,a);return{L:n,A:o,B:t,alpha:a,rgb:C(n,o,t),p3:P(n,o,t),colorString:c}}const r=s.match(/lch\(\s*([\d.]+)(%?)\s+([\d.]+)\s+([\d.]+)(deg)?(?:\s*\/\s*([\d.%]+))?\s*\)/i);if(r){let n=parseFloat(r[1]);const o=parseFloat(r[3]);let t=parseFloat(r[4]);const a=r[6]?r[6].includes("%")?parseFloat(r[6])/100:parseFloat(r[6]):null,c=x(n,o,t),l=v(c.L,c.a,c.b,g,a);return{L:n,C:o,H:t,alpha:a,lab:c,rgb:C(c.L,c.a,c.b),p3:P(c.L,c.a,c.b),colorString:l}}return null}catch(e){return console.warn("\u65E0\u6CD5\u89E3\u6790\u989C\u8272:",s,e),null}}}},F=async function(...s){if(s.length>1||s[0]&&s[0].raw)return await ge(...s);const e=s[0];if(typeof e=="string"){const r=await T(e,{...g,...s[1]});return r&&typeof r.then=="function",r}return typeof e=="object"&&e!==null?(g={...g,...e},F):s.length===0?j():F};async function ge(s,...e){let r=s[0];for(let o=0;o<e.length;o++){const t=e[o];let a="";typeof t=="function"?a=t():Array.isArray(t)?a=t.join(" "):a=String(t??""),r+=a+s[o+1]}const n=await T(r,g);return n&&typeof n.then=="function",n}return Object.assign(F,me),Object.setPrototypeOf(F,Function.prototype),typeof window<"u"&&(j(),Object.defineProperty(window.HTMLElement.prototype,"cssVar",{get(){const s=this;return new Proxy(()=>{},{get(e,r){const n=r.startsWith("--")?r:`--${r}`;return s.style.getPropertyValue(n)},set(e,r,n){const o=r.startsWith("--")?r:`--${r}`;return s.style.setProperty(o,n),!0},apply(e,r,n){const o=n[0],t=n[1],a=o.startsWith("--")?o:`--${o}`;if(t===void 0)return s.style.getPropertyValue(a);s.style.setProperty(a,t)}})}})),F})();if(typeof define=="function"&&define.amd)define([],g=>z);else if(typeof module=="object"&&module.exports)module.exports=z;else{const g=globalThis??(typeof self<"u"&&self)??(typeof window<"u"&&window)??global??{};g.styimat=z}export default z;export const{convert,apply,config,supportsP3,detectP3Support,imports,plugins,aliases,macros,math,colorUtils}=z;
|
package/dist/styimat.mjs
CHANGED
|
@@ -105,7 +105,7 @@ const styimat = (function() {
|
|
|
105
105
|
const trimmed = line.trim();
|
|
106
106
|
|
|
107
107
|
if (trimmed.startsWith('@alias')) {
|
|
108
|
-
const aliasMatch = trimmed.match(/^@alias\s+([
|
|
108
|
+
const aliasMatch = trimmed.match(/^@alias\s+([a-zA-Z_][a-zA-Z0-9_]*)\s+(.+?)\s*;$/);
|
|
109
109
|
if (aliasMatch) {
|
|
110
110
|
const aliasName = aliasMatch[1];
|
|
111
111
|
const originalProperty = aliasMatch[2];
|
|
@@ -145,7 +145,7 @@ const styimat = (function() {
|
|
|
145
145
|
const trimmed = line.trim();
|
|
146
146
|
|
|
147
147
|
if (!inMacroDefinition && trimmed.startsWith('@macro')) {
|
|
148
|
-
const macroMatch = trimmed.match(/^@macro\s+([
|
|
148
|
+
const macroMatch = trimmed.match(/^@macro\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*\(([^)]*)\)\s*\{$/);
|
|
149
149
|
if (macroMatch) {
|
|
150
150
|
inMacroDefinition = true;
|
|
151
151
|
currentMacroName = macroMatch[1];
|
|
@@ -944,17 +944,11 @@ const styimat = (function() {
|
|
|
944
944
|
for (let line of lines) {
|
|
945
945
|
const trimmed = line.trim();
|
|
946
946
|
|
|
947
|
-
const varMatch = trimmed.match(/^\$([
|
|
947
|
+
const varMatch = trimmed.match(/^\$([a-zA-Z_][a-zA-Z0-9_]*)\s*:\s*(.+?);?$/);
|
|
948
948
|
|
|
949
949
|
if (varMatch) {
|
|
950
950
|
const [, varName, varValue] = varMatch;
|
|
951
|
-
const processedValue = processCSSValue(
|
|
952
|
-
replaceVariableUsesInValue(varValue.trim(), {
|
|
953
|
-
...globalVariables,
|
|
954
|
-
...(currentSelector ? selectorVariables.get(currentSelector) || {} : {})
|
|
955
|
-
}),
|
|
956
|
-
config
|
|
957
|
-
);
|
|
951
|
+
const processedValue = processCSSValue(varValue.trim(), config);
|
|
958
952
|
|
|
959
953
|
if (currentSelector) {
|
|
960
954
|
if (!selectorVariables.has(currentSelector)) {
|
|
@@ -1129,27 +1123,16 @@ const styimat = (function() {
|
|
|
1129
1123
|
return result.trim() + "\n\n";
|
|
1130
1124
|
}
|
|
1131
1125
|
|
|
1132
|
-
/**
|
|
1133
|
-
* 替换变量值中的变量引用
|
|
1134
|
-
*/
|
|
1135
|
-
function replaceVariableUsesInValue(value, variables) {
|
|
1136
|
-
return value.replace(/\$([\w]+)(\W?)/g, (match, varName, char) => {
|
|
1137
|
-
if (variables[varName]) {
|
|
1138
|
-
return `var(--${varName})${char}`;
|
|
1139
|
-
}
|
|
1140
|
-
return match;
|
|
1141
|
-
});
|
|
1142
|
-
}
|
|
1143
|
-
|
|
1144
1126
|
/**
|
|
1145
1127
|
* 替换变量使用
|
|
1146
1128
|
*/
|
|
1147
|
-
function replaceVariableUses(cssText,
|
|
1129
|
+
function replaceVariableUses(cssText, variables, config) {
|
|
1148
1130
|
let result = cssText;
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1131
|
+
for (const variable in variables) {
|
|
1132
|
+
result = result.replace(new RegExp(`(?:\\$\\$|\\$)(${variable})(?=[^a-zA-Z0-9_])`, "g"), (match, varName) => {
|
|
1133
|
+
return match.startsWith('$$') ? `attr(${varName})` : `var(--${varName})`;
|
|
1134
|
+
});
|
|
1135
|
+
}
|
|
1153
1136
|
|
|
1154
1137
|
result = processCSSValue(result, config);
|
|
1155
1138
|
|
|
@@ -1166,10 +1149,7 @@ const styimat = (function() {
|
|
|
1166
1149
|
|
|
1167
1150
|
const declarations = Object.entries(variables)
|
|
1168
1151
|
.map(([name, value]) => {
|
|
1169
|
-
const processedValue = processCSSValue(
|
|
1170
|
-
replaceVariableUsesInValue(value, variables),
|
|
1171
|
-
config
|
|
1172
|
-
);
|
|
1152
|
+
const processedValue = processCSSValue(value, config);
|
|
1173
1153
|
return " ".repeat(config.indentSize) + `--${name}: ${processedValue};`;
|
|
1174
1154
|
})
|
|
1175
1155
|
.join('\n');
|
|
@@ -1361,7 +1341,7 @@ const styimat = (function() {
|
|
|
1361
1341
|
finalCSS = injectSelectorVariables(processedCSS, selectorVariables, finalConfig);
|
|
1362
1342
|
}
|
|
1363
1343
|
|
|
1364
|
-
finalCSS = replaceVariableUses(finalCSS, globalVariables, selectorVariables, finalConfig);
|
|
1344
|
+
finalCSS = replaceVariableUses(finalCSS, { ...globalVariables, ...selectorVariables }, finalConfig);
|
|
1365
1345
|
|
|
1366
1346
|
let fullCSS = rootRule + finalCSS;
|
|
1367
1347
|
|
|
@@ -1476,12 +1456,12 @@ const styimat = (function() {
|
|
|
1476
1456
|
plugins: {
|
|
1477
1457
|
use(plugin) {
|
|
1478
1458
|
const pluginObj = new plugin();
|
|
1479
|
-
pluginMap.set(plugin.name, pluginObj);
|
|
1459
|
+
pluginMap.set(pluginObj.name ?? plugin.name, pluginObj);
|
|
1480
1460
|
return true;
|
|
1481
1461
|
},
|
|
1482
1462
|
remove(name) {
|
|
1483
|
-
if (pluginMap.get(name)
|
|
1484
|
-
pluginMap.get(name).destroy()
|
|
1463
|
+
if (pluginMap.get(name)?.destroy) {
|
|
1464
|
+
pluginMap.get(name).destroy();
|
|
1485
1465
|
}
|
|
1486
1466
|
pluginMap.delete(name);
|
|
1487
1467
|
return true;
|