styimat 8.3.0 → 8.5.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/bin/cli.js +8 -9
- package/bin/cli.min.js +20 -0
- package/dist/styimat.js +43 -28
- package/dist/styimat.min.js +19 -24
- package/dist/styimat.min.mjs +19 -24
- package/dist/styimat.mjs +43 -28
- package/package.json +5 -4
package/bin/cli.js
CHANGED
|
@@ -134,25 +134,24 @@ async function convertFile(inputFilePath, outputFilePath) {
|
|
|
134
134
|
|
|
135
135
|
async function interactiveMode() {
|
|
136
136
|
console.log('styimat 交互模式');
|
|
137
|
-
console.log('请输入 CSS 代码,按 Ctrl+D
|
|
138
|
-
console.log('
|
|
139
|
-
console.log('='.repeat(50));
|
|
137
|
+
console.log('请输入 CSS 代码,按 Ctrl+D 提交转换');
|
|
138
|
+
console.log('输入 .exit 或空行退出交互模式');
|
|
140
139
|
|
|
141
140
|
let sessionCount = 0;
|
|
142
141
|
|
|
143
142
|
while (true) {
|
|
144
143
|
sessionCount++;
|
|
144
|
+
process.stderr.write('styimat>');
|
|
145
|
+
|
|
145
146
|
try {
|
|
146
147
|
// 使用 fs.createReadStream 读取输入
|
|
147
148
|
const input = await readFromStdin();
|
|
148
149
|
|
|
149
|
-
if (input === null) {
|
|
150
|
-
console.log('检测到退出信号');
|
|
150
|
+
if (input === null || input === ".exit") {
|
|
151
151
|
break;
|
|
152
152
|
}
|
|
153
153
|
|
|
154
154
|
if (input.trim() === '') {
|
|
155
|
-
console.log('输入为空,跳过');
|
|
156
155
|
continue;
|
|
157
156
|
}
|
|
158
157
|
|
|
@@ -182,11 +181,10 @@ function readFromStdin() {
|
|
|
182
181
|
highWaterMark: 1024 // 每次读取 1KB
|
|
183
182
|
});
|
|
184
183
|
|
|
185
|
-
|
|
186
184
|
stream.on('data', (chunk) => {
|
|
187
185
|
chunks.push(chunk);
|
|
188
186
|
totalBytes += chunk.length;
|
|
189
|
-
process.stderr.write('
|
|
187
|
+
process.stderr.write('........'); // 显示正在读取的指示
|
|
190
188
|
});
|
|
191
189
|
|
|
192
190
|
stream.on('end', () => {
|
|
@@ -237,7 +235,8 @@ async function convertStyimat(inputContent) {
|
|
|
237
235
|
const possiblePaths = [
|
|
238
236
|
path.join(path.dirname(__dirname), 'dist', 'styimat.js'),
|
|
239
237
|
path.join(process.cwd(), 'dist', 'styimat.js'),
|
|
240
|
-
path.join(process.cwd(), 'node_modules', 'styimat', 'dist', 'styimat.js')
|
|
238
|
+
path.join(process.cwd(), 'node_modules', 'styimat', 'dist', 'styimat.js'),
|
|
239
|
+
"styimat"
|
|
241
240
|
];
|
|
242
241
|
|
|
243
242
|
let styimatModule = null;
|
package/bin/cli.min.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const fs=require("fs"),path=require("path"),chokidar=require("chokidar");async function main(){const s=process.argv.slice(2);let e="",t=null,i=!1,o=null;for(let r=0;r<s.length;r++){const n=s[r];if(n==="--watch"||n==="-w")i=!0;else if(n==="--interactive"||n==="-i"){interactiveMode();return}else n==="--help"||n==="-h"?(c(),process.exit(0)):n.startsWith("-")?(console.error(`\u9519\u8BEF: \u672A\u77E5\u9009\u9879 ${n}`),c(),process.exit(1)):o?t?(console.error("\u9519\u8BEF: \u53C2\u6570\u8FC7\u591A"),c(),process.exit(1)):t=n:o=n}function c(){console.log(`
|
|
3
|
+
\u7528\u6CD5: styimat [\u9009\u9879] [\u8F93\u5165\u6587\u4EF6] [\u8F93\u51FA\u6587\u4EF6]
|
|
4
|
+
|
|
5
|
+
\u9009\u9879:
|
|
6
|
+
-w, --watch \u76D1\u63A7\u8F93\u5165\u6587\u4EF6\u53D8\u5316\u5E76\u81EA\u52A8\u8F6C\u6362
|
|
7
|
+
-h, --help \u663E\u793A\u5E2E\u52A9\u4FE1\u606F
|
|
8
|
+
-i, --interactive \u8FDB\u5165\u4EA4\u4E92\u6A21\u5F0F
|
|
9
|
+
|
|
10
|
+
\u793A\u4F8B:
|
|
11
|
+
styimat # \u4ECE stdin \u8BFB\u53D6\uFF0C\u8F93\u51FA\u5230 stdout
|
|
12
|
+
styimat input.css # \u4ECE\u6587\u4EF6\u8BFB\u53D6\uFF0C\u8F93\u51FA\u5230 stdout
|
|
13
|
+
styimat input.css output.css # \u4ECE\u6587\u4EF6\u8BFB\u53D6\uFF0C\u8F93\u51FA\u5230\u6587\u4EF6
|
|
14
|
+
styimat -w input.css output.css # \u76D1\u63A7\u5E76\u81EA\u52A8\u8F6C\u6362
|
|
15
|
+
styimat -i # \u8FDB\u5165\u4EA4\u4E92\u6A21\u5F0F
|
|
16
|
+
`)}if(!o){i&&(console.error("\u9519\u8BEF: \u76D1\u63A7\u6A21\u5F0F\u9700\u8981\u6307\u5B9A\u8F93\u5165\u6587\u4EF6"),process.exit(1)),e=fs.readFileSync(0,"utf8"),await convertAndOutput(e,null);return}if(fs.existsSync(o)||(console.error(`\u9519\u8BEF: \u8F93\u5165\u6587\u4EF6 "${o}" \u4E0D\u5B58\u5728`),process.exit(1)),i){t||(console.error("\u9519\u8BEF: \u76D1\u63A7\u6A21\u5F0F\u9700\u8981\u6307\u5B9A\u8F93\u51FA\u6587\u4EF6"),process.exit(1)),console.log(`\u76D1\u63A7\u6A21\u5F0F\u5DF2\u542F\u52A8: ${o} -> ${t}`),console.log("\u6309 Ctrl+C \u9000\u51FA"),await convertFile(o,t);const r=chokidar.watch(o,{persistent:!0,ignoreInitial:!0,awaitWriteFinish:{stabilityThreshold:200,pollInterval:100}});r.on("change",async n=>{console.log(`${new Date().toLocaleTimeString()} - \u68C0\u6D4B\u5230\u53D8\u5316: ${n}`),await convertFile(n,t)}).on("error",n=>{console.error(`\u76D1\u63A7\u9519\u8BEF: ${n.message}`)}),process.on("SIGINT",()=>{console.log("\u505C\u6B62\u76D1\u63A7..."),r.close(),process.exit(0)})}else e=fs.readFileSync(o,"utf8"),await convertAndOutput(e,t)}async function convertFile(s,e){try{const t=fs.readFileSync(s,"utf8"),i=await convertStyimat(t);fs.writeFileSync(e,i,"utf8"),console.log(`${new Date().toLocaleTimeString()} - \u8F6C\u6362\u5B8C\u6210: ${s} -> ${e}`)}catch(t){console.error(`${new Date().toLocaleTimeString()} - \u8F6C\u6362\u9519\u8BEF: ${t.message}`)}}async function interactiveMode(){console.log("styimat \u4EA4\u4E92\u6A21\u5F0F"),console.log("\u8BF7\u8F93\u5165 CSS \u4EE3\u7801\uFF0C\u6309 Ctrl+D \u63D0\u4EA4\u8F6C\u6362"),console.log("\u8F93\u5165 .exit \u6216\u7A7A\u884C\u9000\u51FA\u4EA4\u4E92\u6A21\u5F0F");let s=0;for(;;){s++,process.stderr.write("styimat>");try{const e=await readFromStdin();if(e===null||e===".exit")break;if(e.trim()==="")continue;const t=await convertStyimat(e);console.log(`
|
|
17
|
+
\u8F6C\u6362\u7ED3\u679C:`),console.log(t)}catch(e){console.error("\u8F6C\u6362\u9519\u8BEF:",e.message),console.log("\u8BF7\u91CD\u8BD5...")}}console.log(`
|
|
18
|
+
\u518D\u89C1\uFF01`)}function readFromStdin(){return new Promise((s,e)=>{const t=[];let i=0;const o=fs.createReadStream("/dev/stdin",{encoding:"utf8",highWaterMark:1024});o.on("data",r=>{t.push(r),i+=r.length,process.stderr.write("........")}),o.on("end",()=>{if(process.stderr.write(`
|
|
19
|
+
`),t.length===0)s(null);else{const r=t.join("");s(r)}}),o.on("error",r=>{e(r)});const c=setTimeout(()=>{o.destroy(),console.log(`
|
|
20
|
+
\u8BFB\u53D6\u8D85\u65F6`),s(null)},3e4);o.on("end",()=>{clearTimeout(c)})})}async function convertAndOutput(s,e){try{const t=await convertStyimat(s);e?fs.writeFileSync(e,t,"utf8"):process.stdout.write(t)}catch(t){console.error("\u8F6C\u6362\u9519\u8BEF:",t.message),process.exit(1)}}async function convertStyimat(s){const e=[path.join(path.dirname(__dirname),"dist","styimat.js"),path.join(process.cwd(),"dist","styimat.js"),path.join(process.cwd(),"node_modules","styimat","dist","styimat.js"),"styimat"];let t=null;for(const o of e)try{if(fs.existsSync(o)){t=require(o);break}}catch{}if(!t)throw new Error("\u627E\u4E0D\u5230 styimat.js \u6A21\u5757");const i=t.styimat||t;if(typeof i.convert!="function")throw new Error("styimat.convert \u4E0D\u662F\u51FD\u6570");return await i.convert(s)}main().catch(s=>{console.error("\u7A0B\u5E8F\u9519\u8BEF:",s.message),process.exit(1)});
|
package/dist/styimat.js
CHANGED
|
@@ -67,12 +67,36 @@ const styimat = (function() {
|
|
|
67
67
|
|
|
68
68
|
const pluginMap = new Map();
|
|
69
69
|
|
|
70
|
+
function splitLines(str, splitChar = "\n") {
|
|
71
|
+
const lines = [];
|
|
72
|
+
const splitCharCode = splitChar.charCodeAt(0);
|
|
73
|
+
let start = 0;
|
|
74
|
+
|
|
75
|
+
for (let i = 0; i < str.length; i++) {
|
|
76
|
+
if (str.charCodeAt(i) === splitCharCode) { // '\n' 的 charCode
|
|
77
|
+
if (i > start || str.charAt(start) !== '\r') {
|
|
78
|
+
lines.push(str.substring(start, i));
|
|
79
|
+
}
|
|
80
|
+
start = i + 1;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// 处理最后一行
|
|
85
|
+
if (start < str.length) {
|
|
86
|
+
lines.push(str.substring(start));
|
|
87
|
+
} else if (start === str.length && str.charCodeAt(str.length - 1) === 10) {
|
|
88
|
+
lines.push('');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return lines;
|
|
92
|
+
}
|
|
93
|
+
|
|
70
94
|
/**
|
|
71
95
|
* 解析配置头
|
|
72
96
|
*/
|
|
73
97
|
function parseConfigHeader(cssText) {
|
|
74
98
|
const config = { ...defaultConfig };
|
|
75
|
-
const lines = cssText
|
|
99
|
+
const lines = splitLines(cssText);
|
|
76
100
|
const cleanLines = [];
|
|
77
101
|
|
|
78
102
|
for (let line of lines) {
|
|
@@ -82,7 +106,7 @@ const styimat = (function() {
|
|
|
82
106
|
const configLine = trimmed.substring(1).trim();
|
|
83
107
|
const firstSpace = configLine.indexOf(' ');
|
|
84
108
|
|
|
85
|
-
if (firstSpace
|
|
109
|
+
if (~firstSpace) {
|
|
86
110
|
const key = configLine.substring(0, firstSpace).trim();
|
|
87
111
|
const value = configLine.substring(firstSpace + 1).trim();
|
|
88
112
|
|
|
@@ -115,7 +139,7 @@ const styimat = (function() {
|
|
|
115
139
|
*/
|
|
116
140
|
function parseAliasStatements(cssText) {
|
|
117
141
|
const aliases = new Map();
|
|
118
|
-
const lines = cssText
|
|
142
|
+
const lines = splitLines(cssText);
|
|
119
143
|
const cleanLines = [];
|
|
120
144
|
|
|
121
145
|
for (let line of lines) {
|
|
@@ -150,7 +174,7 @@ const styimat = (function() {
|
|
|
150
174
|
}
|
|
151
175
|
|
|
152
176
|
const macros = new Map();
|
|
153
|
-
const lines = cssText
|
|
177
|
+
const lines = splitLines(cssText);
|
|
154
178
|
const cleanLines = [];
|
|
155
179
|
let inMacroDefinition = false;
|
|
156
180
|
let currentMacroName = '';
|
|
@@ -166,12 +190,11 @@ const styimat = (function() {
|
|
|
166
190
|
if (macroMatch) {
|
|
167
191
|
inMacroDefinition = true;
|
|
168
192
|
currentMacroName = macroMatch[1];
|
|
169
|
-
currentMacroParams = macroMatch[2]
|
|
170
|
-
.split(',')
|
|
193
|
+
currentMacroParams = splitLines(macroMatch[2], ',')
|
|
171
194
|
.map(param => param.trim())
|
|
172
195
|
.filter(param => param)
|
|
173
196
|
.map(param => {
|
|
174
|
-
const paramParts = param
|
|
197
|
+
const paramParts = splitLines(param, ':').map(p => p.trim());
|
|
175
198
|
return {
|
|
176
199
|
name: paramParts[0].slice(1),
|
|
177
200
|
defaultValue: paramParts[1] || null
|
|
@@ -453,7 +476,7 @@ const styimat = (function() {
|
|
|
453
476
|
|
|
454
477
|
const colonIndex = findFirstColonOutsideQuotes(declaration);
|
|
455
478
|
|
|
456
|
-
if (colonIndex
|
|
479
|
+
if (!~colonIndex) {
|
|
457
480
|
console.warn(`无效的CSS声明: "${declaration}"`);
|
|
458
481
|
continue;
|
|
459
482
|
}
|
|
@@ -465,13 +488,11 @@ const styimat = (function() {
|
|
|
465
488
|
value = value.slice(0, -1).trim();
|
|
466
489
|
}
|
|
467
490
|
|
|
468
|
-
|
|
469
|
-
property = aliases.get(property);
|
|
470
|
-
}
|
|
491
|
+
property = aliases.get(property) ?? property;
|
|
471
492
|
|
|
472
493
|
if (property.startsWith("@") && macros.get(property.slice(1))) {
|
|
473
494
|
const macro = macros.get(property.slice(1));
|
|
474
|
-
const args = parseMacroArguments(value
|
|
495
|
+
const args = parseMacroArguments(splitLines(value, " ").join(","), macro.params);
|
|
475
496
|
property = applyMacro(macro.body, args, config);
|
|
476
497
|
value = "";
|
|
477
498
|
}
|
|
@@ -545,12 +566,8 @@ const styimat = (function() {
|
|
|
545
566
|
});
|
|
546
567
|
};
|
|
547
568
|
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
lastResult = result;
|
|
551
|
-
result = processMath(result);
|
|
552
|
-
} while (result !== lastResult && result.includes('math('));
|
|
553
|
-
|
|
569
|
+
result = processMath(result);
|
|
570
|
+
|
|
554
571
|
return result;
|
|
555
572
|
}
|
|
556
573
|
|
|
@@ -932,7 +949,7 @@ const styimat = (function() {
|
|
|
932
949
|
* 提取变量定义并移除
|
|
933
950
|
*/
|
|
934
951
|
function extractVariablesAndCSS(cssText, config) {
|
|
935
|
-
const lines = cssText
|
|
952
|
+
const lines = splitLines(cssText);
|
|
936
953
|
const globalVariables = {};
|
|
937
954
|
const selectorVariables = new Map();
|
|
938
955
|
let cssWithoutVars = '';
|
|
@@ -1002,7 +1019,7 @@ const styimat = (function() {
|
|
|
1002
1019
|
* 解析嵌套规则
|
|
1003
1020
|
*/
|
|
1004
1021
|
function parseNestedRules(cssText, config, aliases, macros) {
|
|
1005
|
-
const lines = cssText
|
|
1022
|
+
const lines = splitLines(cssText);
|
|
1006
1023
|
const stack = [];
|
|
1007
1024
|
const rootRules = [];
|
|
1008
1025
|
let currentIndent = 0;
|
|
@@ -1127,11 +1144,10 @@ const styimat = (function() {
|
|
|
1127
1144
|
*/
|
|
1128
1145
|
function replaceVariableUses(cssText, variables, config) {
|
|
1129
1146
|
let result = cssText;
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
}
|
|
1147
|
+
result = result.replace(new RegExp(`(?:\\$\\$|\\$)([a-zA-Z_][a-zA-Z0-9_]*)(?=[^a-zA-Z0-9_])`, "g"), (match, varName) => {
|
|
1148
|
+
if (variables[varName]) return match.startsWith('$$') ? `attr(${varName})` : `var(--${varName})`;
|
|
1149
|
+
return match;
|
|
1150
|
+
});
|
|
1135
1151
|
|
|
1136
1152
|
result = processCSSValue(result, config);
|
|
1137
1153
|
|
|
@@ -1161,7 +1177,7 @@ const styimat = (function() {
|
|
|
1161
1177
|
*/
|
|
1162
1178
|
function injectSelectorVariables(cssText, selectorVariables, config) {
|
|
1163
1179
|
let result = '';
|
|
1164
|
-
const lines = cssText
|
|
1180
|
+
const lines = splitLines(cssText);
|
|
1165
1181
|
let currentSelector = null;
|
|
1166
1182
|
|
|
1167
1183
|
for (let line of lines) {
|
|
@@ -1703,8 +1719,7 @@ const styimat = (function() {
|
|
|
1703
1719
|
}
|
|
1704
1720
|
|
|
1705
1721
|
Object.assign(styimat, api);
|
|
1706
|
-
|
|
1707
|
-
|
|
1722
|
+
|
|
1708
1723
|
// 自动初始化
|
|
1709
1724
|
if (typeof window !== 'undefined') {
|
|
1710
1725
|
apply();
|
package/dist/styimat.min.js
CHANGED
|
@@ -1,33 +1,28 @@
|
|
|
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};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
|
|
5
|
-
`),e=
|
|
6
|
-
`)}}function
|
|
7
|
-
`),e=[];for(let
|
|
8
|
-
`)}
|
|
9
|
-
`),n=[];let r=!1,
|
|
10
|
-
|
|
11
|
-
`
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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)+`}
|
|
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 T=null;const O=new Map,B=new Map,A=new Map,L=new Map;function S(s,r=`
|
|
5
|
+
`){const o=[],e=r.charCodeAt(0);let t=0;for(let n=0;n<s.length;n++)s.charCodeAt(n)===e&&((n>t||s.charAt(t)!=="\r")&&o.push(s.substring(t,n)),t=n+1);return t<s.length?o.push(s.substring(t)):t===s.length&&s.charCodeAt(s.length-1)===10&&o.push(""),o}function K(s){const r={...g},o=S(s),e=[];for(let t of o){const n=t.trim();if(n.startsWith("#")){const c=n.substring(1).trim(),a=c.indexOf(" ");if(~a){const l=c.substring(0,a).trim(),i=c.substring(a+1).trim(),u=ne(l);i==="true"||i==="false"?r[u]=i==="true":!isNaN(i)&&i.trim()!==""?r[u]=Number(i):r[u]=i}else console.warn(`\u65E0\u6548\u7684\u914D\u7F6E\u884C: ${n}`)}else e.push(t)}return{config:r,css:e.join(`
|
|
6
|
+
`)}}function J(s){const r=new Map,o=S(s),e=[];for(let t of o){const n=t.trim();if(n.startsWith("@alias")){const c=n.match(w.ALIAS);if(c){const a=c[1],l=c[2];r.set(a,l);continue}}e.push(t)}return{aliases:r,css:e.join(`
|
|
7
|
+
`)}}function k(s,r){if(!r.enableMacros)return{macros:new Map,css:s};const o=new Map,e=S(s),t=[];let n=!1,c="",a=[],l=[],i=0;for(let u of e){const p=u.trim();if(!n&&p.startsWith("@macro")){const f=p.match(w.MACRO);if(f){n=!0,c=f[1],a=S(f[2],",").map(h=>h.trim()).filter(h=>h).map(h=>{const m=S(h,":").map(b=>b.trim());return{name:m[0].slice(1),defaultValue:m[1]||null}}),l=[],i=1;continue}}if(n){for(const f of u)f==="{"&&i++,f==="}"&&i--;i===0?(o.set(c,{params:a,body:l.join(`
|
|
8
|
+
`)}),n=!1,c="",a=[],l=[]):l.push(u);continue}t.push(u)}return{macros:o,css:t.join(`
|
|
9
|
+
`)}}function ee(s,r,o){if(!o.enableMacros||r.size===0)return s;const e=Array.from(r.keys()).map(c=>c.replace(w.SPECHAR,"\\$&")).join("|"),t=new RegExp(`@(${e})\\s*\\(([^)]*)\\)\\s*;?`,"g");return(c=>{let a=c,l=!1;do l=!1,a=a.replace(t,(i,u,p)=>{l=!0;const f=r.get(u);if(!f)return console.warn(`\u672A\u5B9A\u4E49\u7684\u5B8F: ${u}`),i;const h=z(p,f.params);return F(f.body,h,o)});while(l);return a})(s)}function z(s,r){const o=new Map,e=te(s);for(let t=0;t<r.length;t++){const n=r[t];let c;t<e.length?c=e[t]:n.defaultValue!==null?c=n.defaultValue:c="none",o.set(n.name,c)}return o}function te(s){const r=[];let o="",e=!1,t="",n=0,c=0;for(let a=0;a<s.length;a++){const l=s[a];(l==='"'||l==="'")&&!e?(e=!0,t=l):l===t&&e&&(e=!1,t=""),e||(l==="("&&n++,l===")"&&n--,l==="["&&c++,l==="]"&&c--),l===","&&!e&&n===0&&c===0?(r.push(o.trim()),o=""):o+=l}return o.trim()&&r.push(o.trim()),r}function F(s,r,o){let e=s;for(const[t,n]of r){const c=new RegExp(`\\$${t}`,"g");e=e.replace(c,n)}if(o.enableMacros){const t=Array.from(A.keys()).map(n=>n.replace(w.SPECHAR,"\\$&")).join("|");if(t){const n=new RegExp(`@(${t})\\s*\\(([^)]*)\\)\\s*;?`,"g");let c;do c=!1,e=e.replace(n,(a,l,i)=>{c=!0;const u=A.get(l);if(!u)return a;const p=z(i,u.params);return F(u.body,p,o)});while(c)}}return e=v(e,o),e}function ne(s){return s.replace(w.HYPHEN,function(r,o){return o.toUpperCase()})}function V(){if(T!==null)return T;if(typeof window>"u"||!window.CSS)return T=!1,!1;try{T=CSS.supports("color","color(display-p3 1 0 0)")}catch{T=!1}return T}function j(s,r,o){let e=s.trim();if(e.endsWith(";")&&(e=e.slice(0,-1)),!e)return[];const t=[];let n="",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?n.trim()&&(t.push(n.trim()),n=""):n+=p}n.trim()&&t.push(n.trim());const i=[];for(let u of t){if(u=u.replace(w.COMMENT,""),!u.trim())continue;const p=re(u);if(!~p){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()),f=r.get(f)??f,f.startsWith("@")&&o.get(f.slice(1))){const m=o.get(f.slice(1)),b=z(S(h," ").join(","),m.params);f=F(m.body,b,Y),h=""}i.push({[f]:h})}return i}function re(s){let r=!1,o="";for(let e=0;e<s.length;e++){const t=s[e];if((t==='"'||t==="'")&&!r?(r=!0,o=t):t===o&&r&&(r=!1,o=""),t===":"&&!r)return e}return-1}function G(s,r){if(!r.enableMath)return`math(${s})`;let o=s.replace(w.SPACE,"");return se(o,r.enableMath)}function se(s,r){return`calc(${s.replace(w.MATH_SYMBOL,"$1 $2 $3")})`}function oe(s,r){if(!r.enableMath)return s;let o=s;return o=(t=>t.replace(w.MATH,(n,c)=>G(c,r)))(o),o}function ae(s,r){if(!r.convertLabToRGB&&!r.convertLchToRGB)return s;let o=s;o=ce(o,r);const e=new Map,t=c=>c.replace(w.COLOR,a=>{if(e.has(a))return e.get(a);let l=a;return a.toLowerCase().startsWith("lab(")?r.convertLabToRGB&&(l=le(a,r)):a.toLowerCase().startsWith("lch(")&&r.convertLchToRGB&&(l=ie(a,r)),e.set(a,l),l});let n;do n=o,o=t(o);while(o!==n);return o}function ce(s,r){let o=s;const e=new Map;return o=o.replace(w.HEXLAB,(t,n,c,a)=>{if(e.has(t))return e.get(t);try{const l=parseInt(n,16)/255*100,i=(parseInt(c,16)-128)*1.5,u=(parseInt(a,16)-128)*1.5,p=x(l,i,u,r);return e.set(t,p),p}catch(l){return console.warn(`\u65E0\u6CD5\u89E3\u6790lab#\u5341\u516D\u8FDB\u5236\u989C\u8272: ${t}`,l),t}}),o=o.replace(w.HEXLCH,(t,n,c,a)=>{if(e.has(t))return e.get(t);try{const l=parseInt(n,16)/255*100,i=parseInt(c,16)/255*150,u=parseInt(a)/100*360,p=R(l,i,u),f=x(p.L,p.a,p.b,r);return e.set(t,f),f}catch(l){return console.warn(`\u65E0\u6CD5\u89E3\u6790lch#\u5341\u516D\u8FDB\u5236\u989C\u8272: ${t}`,l),t}}),o}function le(s,r){const o=/lab\(\s*([\d.]+)(%?)\s+([\d.-]+)\s+([\d.-]+)(?:\s*\/\s*([\d.%]+))?\s*\)/i,e=s.match(o);if(!e)return s;try{let t=parseFloat(e[1]);e[2]==="%"?t=t:(t<0&&(t=0),t>100&&(t=100));const n=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(t,n,c,r,a)}catch(t){return console.warn(`\u65E0\u6CD5\u8F6C\u6362LAB\u989C\u8272: ${s}`,t),s}}function ie(s,r){const o=/lch\(\s*([\d.]+)(%?)\s+([\d.]+)\s+([\d.]+)(deg)?(?:\s*\/\s*([\d.%]+))?\s*\)/i,e=s.match(o);if(!e)return s;try{let t=parseFloat(e[1]);e[2]==="%"?t=t:(t<0&&(t=0),t>100&&(t=100));const n=parseFloat(e[3]);let c=parseFloat(e[4]);n<0&&console.warn(`LCH\u4E2D\u7684C\u503C\u4E0D\u80FD\u4E3A\u8D1F: ${n}`),c=(c%360+360)%360;const a=R(t,n,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,r,l)}catch(t){return console.warn(`\u65E0\u6CD5\u8F6C\u6362LCH\u989C\u8272: ${s}`,t),s}}function R(s,r,o){const e=o*Math.PI/180,t=r*Math.cos(e),n=r*Math.sin(e);return{L:s,a:t,b:n}}function $(s,r,o){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]},t=(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]},n=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(s,r,o),[u,p,f]=t(a,l,i),h=n(u),m=n(p),b=n(f);return{r:c(h),g:c(m),b:c(b)}}function E(s,r,o){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]},t=(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]},n=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(s,r,o),[u,p,f]=t(a,l,i),h=n(u),m=n(p),b=n(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=$(s,r,o);return{r:l.r/255,g:l.g/255,b:l.b/255}}}function x(s,r,o,e,t=null){if(!e.enableP3||!V()){const n=$(s,r,o);return t!==null?`rgba(${n.r}, ${n.g}, ${n.b}, ${t})`:`rgb(${n.r}, ${n.g}, ${n.b})`}else{const n=E(s,r,o);return t!==null?`color(display-p3 ${n.r.toFixed(4)} ${n.g.toFixed(4)} ${n.b.toFixed(4)} / ${t})`:`color(display-p3 ${n.r.toFixed(4)} ${n.g.toFixed(4)} ${n.b.toFixed(4)})`}}function v(s,r){let o=s;return r.enableMath&&(o=oe(o,r)),(r.convertLabToRGB||r.convertLchToRGB)&&(o=ae(o,r)),o}function ue(s,r){const o=S(s),e={},t=new Map;let n="",c=null,a=!1,l=0;for(let i of o){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(),r);c?(t.has(c)||t.set(c,{}),t.get(c)[f]=m):e[f]=m;continue}if(u.endsWith("{")){c=u.slice(0,-1).trim(),a=!0,n+=i+`
|
|
10
|
+
`;continue}if(u==="}"){if(a&&c&&t.has(c)){const f=t.get(c),h=" ".repeat(l);for(const[m,b]of Object.entries(f))n+=`${h} --${m}: ${b};
|
|
11
|
+
`}a=!1,c=null}n+=i+`
|
|
12
|
+
`,i.includes("{")&&(l+=r.indentSize),i.includes("}")&&(l=Math.max(0,l-r.indentSize))}return{globalVariables:e,selectorVariables:t,cssWithoutVars:n.trim()}}function fe(s,r,o,e){const t=S(s),n=[],c=[];let a=0;for(let l=0;l<t.length;l++){const i=t[l],u=i.trim();if(!u)continue;const p=i.match(/^(\s*)/)[0].length;if(u==="}"){if(n.length>0){const f=n.pop();if(n.length>0){const h=n[n.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:[]};n.push(h);continue}if(!u.includes("{")&&!u.includes("}")&&u.includes(":")){if(n.length>0){const f=n[n.length-1];j(u,o,e).forEach(m=>{const b=Object.keys(m)[0],d=v(m[b],r);f.properties.push(d===""?b:`${b}: ${d}`)})}continue}}for(;n.length>0;){const l=n.pop();if(n.length===0)c.push(l);else{const i=n[n.length-1];i.children||(i.children=[]),i.children.push(l)}}return Z(c,r,"",o,e)}function Z(s,r,o="",e,t){let n="";const c=o.startsWith("@");for(const a of s){const l=a.selector.startsWith("@");let i=(c?" ".repeat(r.indentSize):"")+a.selector;if(l&&(i=a.selector+` {
|
|
13
|
+
`),o&&(i.includes("&")?i=i.replace(w.AND,o):i.trim().startsWith(":")?i=o+i:i=o+(c?"":" ")+i),a.properties.length>0){n+=(l?"":i)+` {
|
|
14
|
+
`;for(const u of a.properties)j(u,e,t).forEach(f=>{const h=Object.keys(f)[0],m=v(f[h],r);n+=(c?" ".repeat(r.indentSize):"")+" ".repeat(r.indentSize)+(m===""?h:`${h}: ${m};
|
|
15
|
+
`)});n+=c?" ".repeat(r.indentSize)+`}
|
|
20
16
|
`:`}
|
|
21
17
|
|
|
22
|
-
`}a.children&&a.children.length>0&&(
|
|
18
|
+
`}a.children&&a.children.length>0&&(n+=Z(a.children,r,i,e,t)),c&&(n+=`}
|
|
23
19
|
|
|
24
|
-
`)}return
|
|
20
|
+
`)}return n.trim()+`
|
|
25
21
|
|
|
26
|
-
`}function
|
|
27
|
-
`);return`${
|
|
28
|
-
${
|
|
22
|
+
`}function pe(s,r,o){let e=s;return e=e.replace(new RegExp("(?:\\$\\$|\\$)([a-zA-Z_][a-zA-Z0-9_]*)(?=[^a-zA-Z0-9_])","g"),(t,n)=>r[n]?t.startsWith("$$")?`attr(${n})`:`var(--${n})`:t),e=v(e,o),e}function he(s,r){if(Object.keys(s).length===0)return"";const o=Object.entries(s).map(([e,t])=>{const n=v(t,r);return" ".repeat(r.indentSize)+`--${e}: ${n};`}).join(`
|
|
23
|
+
`);return`${r.rootSelector} {
|
|
24
|
+
${o}
|
|
29
25
|
}
|
|
30
26
|
|
|
31
|
-
`}function
|
|
32
|
-
`);
|
|
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}
|
|
27
|
+
`}function de(s,r,o){let e="";const t=S(s);let n=null;for(let c of t){const a=c.trim();a.endsWith("{")&&(n=a.slice(0,-1).trim()),a==="}"&&n&&(n=null),e+=v(c,o)+`
|
|
28
|
+
`}return e.trim()}async function X(s,r){const o=w.IMPORT;return(async t=>{const n=[],c=t.replace(o,(a,l)=>{const i=me(l,r).then(u=>X(u,r)).catch(u=>(console.warn(`\u65E0\u6CD5\u5BFC\u5165CSS\u6587\u4EF6: ${l}`,u),""));return n.push(i),`__IMPORT_PLACEHOLDER_${n.length-1}__`});if(n.length>0){const a=await Promise.all(n);let l=c;for(let i=0;i<a.length;i++)l=l.replace(`__IMPORT_PLACEHOLDER_${i}__`,a[i]);return l}return c})(s)}async function me(s,r){if(r.importCache&&O.has(s))return O.get(s);const o=r.importBaseUrl?new URL(s,r.importBaseUrl).href:s;let e;if(typeof process<"u"&&process.versions&&process.versions.node&&typeof require<"u")try{const n=require("fs"),c=require("path");let a=o;o.startsWith(".")&&(a=c.join(process.cwd(),o)),e=n.readFileSync(a,"utf-8")}catch(n){throw new Error(`Node.js\u6587\u4EF6\u8BFB\u53D6\u5931\u8D25: ${o} - ${n.message}`)}else try{const n=new AbortController,c=setTimeout(()=>n.abort(),r.importTimeout),a=await fetch(o,{signal:n.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(n){throw n.name==="AbortError"?new Error(`\u5BFC\u5165\u8D85\u65F6: ${o}`):new Error(`\u65E0\u6CD5\u83B7\u53D6CSS: ${o} - ${n.message}`)}return r.importCache&&O.set(s,e),e}async function H(s,r={}){let{config:o,css:e}=K(s);const t={...g,...r,...o};e=await X(e,t);let n=e,c=new Map;if(t.enableAlias){const{aliases:y,css:M}=J(n);c=y,n=M}let a=n,l=new Map;if(t.enableMacros){const{macros:y,css:M}=k(a,t);l=y,a=M;for(const[C,P]of l)A.set(C,P)}let i=a;t.enableMacros&&l.size>0&&(i=ee(i,l,t));const{globalVariables:u,selectorVariables:p,cssWithoutVars:f}=ue(i,t);let h=f.trim();if(t.enableNesting&&f.includes("{"))try{h=fe(f,t,c,l)}catch(y){console.warn("\u5D4C\u5957\u89E3\u6790\u5931\u8D25\uFF0C\u4F7F\u7528\u539F\u59CBCSS:",y)}const m=he(u,t);let b=h;p.size>0&&(b=de(h,p,t)),b=pe(b,{...u,...p},t);let d=m+b;for(const[y,M]of L)try{d=M.convert(d,t)}catch(C){console.error("\u63D2\u4EF6\u5904\u7406\u5931\u8D25:",C)}return d}function U(s={}){const r={...g,...s},o=document.querySelectorAll(`style[${r.styleTagAttribute||"e"}]`);return o.forEach(e=>{let t=e.textContent;(async()=>{try{e.getAttribute("src")&&(t="@import "+e.getAttribute("src")+";");const c=await H(t,r),a=document.createElement("style");a.textContent=c,e.parentNode.insertBefore(a,e.nextSibling),r.preserveOriginal?e.style.display="none":e.remove()}catch(c){console.error("\u5904\u7406style\u6807\u7B7E\u5931\u8D25:",c)}})()}),o.length}function Y(s={}){g={...g,...s}}function W(s,r={}){const o={...g,...r};if(s)return(async()=>{try{const e=await H(s,o),t=document.createElement("style");return t.textContent=e,document.head.appendChild(t),t}catch(e){return console.error("\u5E94\u7528CSS\u5931\u8D25:",e),null}})();document.readyState==="loading"?document.addEventListener("DOMContentLoaded",()=>{U(o)}):U(o)}const ge={convert:H,apply:W,config:Y,supportsP3:V(),detectP3Support:V,imports:{clearCache:function(){O.clear()},setBaseUrl:function(s){g.importBaseUrl=s},setCacheEnabled:function(s){g.importCache=s},setTimeout:function(s){g.importTimeout=s}},plugins:{use(s){const r=new s;return L.set(r.name??s.name,r),!0},remove(s){return L.get(s)?.destroy&&L.get(s).destroy(),L.delete(s),!0},getAll:function(){return Array.from(L.entries())},clear:function(){L.clear()}},aliases:{add:function(s,r){B.set(s,r)},remove:function(s){B.delete(s)},getAll:function(){return Array.from(B.entries())},clear:function(){B.clear()}},macros:{define:function(s,r,o=[]){if(typeof r=="function"){const t=r.toString().match(/{([\s\S]*)}/);t&&A.set(s,{params:o.map(n=>typeof n=="string"?{name:n}:n),body:t[1].trim()})}else A.set(s,{params:o.map(e=>typeof e=="string"?{name:e}:e),body:r})},call:function(s,...r){const o=A.get(s);if(!o)throw new Error(`\u672A\u5B9A\u4E49\u7684\u5B8F: ${s}`);const e=new Map;for(let t=0;t<o.params.length;t++){const n=o.params[t],c=t<r.length?r[t]:n.defaultValue;if(c===void 0&&n.defaultValue===null)throw new Error(`\u7F3A\u5C11\u5FC5\u9700\u53C2\u6570: ${n.name}`);e.set(n.name,c!==void 0?c:"")}return F(o.body,e,g)},getAll:function(){return Array.from(A.entries())},remove:function(s){A.delete(s)},clear:function(){A.clear()},parse:function(s){const{macros:r}=k(s,g);for(const[o,e]of r)A.set(o,e)}},math:{evaluate:function(s){return G(s,g)}},colorUtils:{labToRGB:$,lchToLab:R,lchToRGB:function(s,r,o){const e=R(s,r,o);return $(e.L,e.a,e.b)},labToP3:E,lchToP3:function(s,r,o){const e=R(s,r,o);return E(e.L,e.a,e.b)},parseHexLab:function(s){const r=s.match(/lab#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/i);if(!r)return null;const o=r[1],e=r[2],t=r[3],n=parseInt(o,16)/255*100,c=(parseInt(e,16)-128)*1.5,a=(parseInt(t,16)-128)*1.5;return $(n,c,a)},parseHexLch:function(s){const r=s.match(/lch#([0-9a-f]{2})([0-9a-f]{2})(\d{1,3})/i);if(!r)return null;const o=r[1],e=r[2],t=r[3],n=parseInt(o,16)/255*100,c=parseInt(e,16)/255*150,a=parseInt(t)/100*360,l=R(n,c,a);return $(l.L,l.a,l.b)},generateColor:function(s,r,o,e=null,t=!0){return x(s,r,o,{enableP3:t},e)},parseColor:function(s){try{const r=s.match(/lab\(\s*([\d.]+)(%?)\s+([\d.-]+)\s+([\d.-]+)(?:\s*\/\s*([\d.%]+))?\s*\)/i);if(r){let e=parseFloat(r[1]);const t=parseFloat(r[3]),n=parseFloat(r[4]),c=r[5]?r[5].includes("%")?parseFloat(r[5])/100:parseFloat(r[5]):null,a=x(e,t,n,g,c);return{L:e,A:t,B:n,alpha:c,rgb:$(e,t,n),p3:E(e,t,n),colorString:a}}const o=s.match(/lch\(\s*([\d.]+)(%?)\s+([\d.]+)\s+([\d.]+)(deg)?(?:\s*\/\s*([\d.%]+))?\s*\)/i);if(o){let e=parseFloat(o[1]);const t=parseFloat(o[3]);let n=parseFloat(o[4]);const c=o[6]?o[6].includes("%")?parseFloat(o[6])/100:parseFloat(o[6]):null,a=R(e,t,n),l=x(a.L,a.a,a.b,g,c);return{L:e,C:t,H:n,alpha:c,lab:a,rgb:$(a.L,a.a,a.b),p3:E(a.L,a.a,a.b),colorString:l}}return null}catch(r){return console.warn("\u65E0\u6CD5\u89E3\u6790\u989C\u8272:",s,r),null}}}},_=async function(...s){if(s.length>1||s[0]&&s[0].raw)return await be(...s);const r=s[0];if(typeof r=="string"){const o=await H(r,{...g,...s[1]});return o&&typeof o.then=="function",o}return typeof r=="object"&&r!==null?(g={...g,...r},_):s.length===0?W():_};async function be(s,...r){let o=s[0];for(let t=0;t<r.length;t++){const n=r[t];let c="";typeof n=="function"?c=n():Array.isArray(n)?c=n.join(" "):c=String(n??""),o+=c+s[t+1]}const e=await H(o,g);return e&&typeof e.then=="function",e}return Object.assign(_,ge),typeof window<"u"&&(W(),Object.defineProperty(window.HTMLElement.prototype,"cssVar",{get(){const s=this;return new Proxy(()=>{},{get(r,o){const e=o.startsWith("--")?o:`--${o}`;return s.style.getPropertyValue(e)},set(r,o,e){const t=o.startsWith("--")?o:`--${o}`;return s.style.setProperty(t,e),!0},apply(r,o,e){const t=e[0],n=e[1],c=t.startsWith("--")?t:`--${t}`;if(n===void 0)return s.style.getPropertyValue(c);s.style.setProperty(c,n)}})}})),_})();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,28 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* MIT License
|
|
3
3
|
* Copyright (c) 2025 王小玗
|
|
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
|
|
5
|
-
`),e=
|
|
6
|
-
`)}}function
|
|
7
|
-
`),e=[];for(let
|
|
8
|
-
`)}
|
|
9
|
-
`),n=[];let r=!1,
|
|
10
|
-
|
|
11
|
-
`
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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)+`}
|
|
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 P=null;const B=new Map,F=new Map,A=new Map,L=new Map;function S(s,r=`
|
|
5
|
+
`){const o=[],e=r.charCodeAt(0);let t=0;for(let n=0;n<s.length;n++)s.charCodeAt(n)===e&&((n>t||s.charAt(t)!=="\r")&&o.push(s.substring(t,n)),t=n+1);return t<s.length?o.push(s.substring(t)):t===s.length&&s.charCodeAt(s.length-1)===10&&o.push(""),o}function J(s){const r={...g},o=S(s),e=[];for(let t of o){const n=t.trim();if(n.startsWith("#")){const c=n.substring(1).trim(),a=c.indexOf(" ");if(~a){const l=c.substring(0,a).trim(),i=c.substring(a+1).trim(),u=re(l);i==="true"||i==="false"?r[u]=i==="true":!isNaN(i)&&i.trim()!==""?r[u]=Number(i):r[u]=i}else console.warn(`\u65E0\u6548\u7684\u914D\u7F6E\u884C: ${n}`)}else e.push(t)}return{config:r,css:e.join(`
|
|
6
|
+
`)}}function ee(s){const r=new Map,o=S(s),e=[];for(let t of o){const n=t.trim();if(n.startsWith("@alias")){const c=n.match(w.ALIAS);if(c){const a=c[1],l=c[2];r.set(a,l);continue}}e.push(t)}return{aliases:r,css:e.join(`
|
|
7
|
+
`)}}function j(s,r){if(!r.enableMacros)return{macros:new Map,css:s};const o=new Map,e=S(s),t=[];let n=!1,c="",a=[],l=[],i=0;for(let u of e){const p=u.trim();if(!n&&p.startsWith("@macro")){const f=p.match(w.MACRO);if(f){n=!0,c=f[1],a=S(f[2],",").map(h=>h.trim()).filter(h=>h).map(h=>{const m=S(h,":").map(b=>b.trim());return{name:m[0].slice(1),defaultValue:m[1]||null}}),l=[],i=1;continue}}if(n){for(const f of u)f==="{"&&i++,f==="}"&&i--;i===0?(o.set(c,{params:a,body:l.join(`
|
|
8
|
+
`)}),n=!1,c="",a=[],l=[]):l.push(u);continue}t.push(u)}return{macros:o,css:t.join(`
|
|
9
|
+
`)}}function te(s,r,o){if(!o.enableMacros||r.size===0)return s;const e=Array.from(r.keys()).map(c=>c.replace(w.SPECHAR,"\\$&")).join("|"),t=new RegExp(`@(${e})\\s*\\(([^)]*)\\)\\s*;?`,"g");return(c=>{let a=c,l=!1;do l=!1,a=a.replace(t,(i,u,p)=>{l=!0;const f=r.get(u);if(!f)return console.warn(`\u672A\u5B9A\u4E49\u7684\u5B8F: ${u}`),i;const h=V(p,f.params);return _(f.body,h,o)});while(l);return a})(s)}function V(s,r){const o=new Map,e=ne(s);for(let t=0;t<r.length;t++){const n=r[t];let c;t<e.length?c=e[t]:n.defaultValue!==null?c=n.defaultValue:c="none",o.set(n.name,c)}return o}function ne(s){const r=[];let o="",e=!1,t="",n=0,c=0;for(let a=0;a<s.length;a++){const l=s[a];(l==='"'||l==="'")&&!e?(e=!0,t=l):l===t&&e&&(e=!1,t=""),e||(l==="("&&n++,l===")"&&n--,l==="["&&c++,l==="]"&&c--),l===","&&!e&&n===0&&c===0?(r.push(o.trim()),o=""):o+=l}return o.trim()&&r.push(o.trim()),r}function _(s,r,o){let e=s;for(const[t,n]of r){const c=new RegExp(`\\$${t}`,"g");e=e.replace(c,n)}if(o.enableMacros){const t=Array.from(A.keys()).map(n=>n.replace(w.SPECHAR,"\\$&")).join("|");if(t){const n=new RegExp(`@(${t})\\s*\\(([^)]*)\\)\\s*;?`,"g");let c;do c=!1,e=e.replace(n,(a,l,i)=>{c=!0;const u=A.get(l);if(!u)return a;const p=V(i,u.params);return _(u.body,p,o)});while(c)}}return e=v(e,o),e}function re(s){return s.replace(w.HYPHEN,function(r,o){return o.toUpperCase()})}function W(){if(P!==null)return P;if(typeof window>"u"||!window.CSS)return P=!1,!1;try{P=CSS.supports("color","color(display-p3 1 0 0)")}catch{P=!1}return P}function G(s,r,o){let e=s.trim();if(e.endsWith(";")&&(e=e.slice(0,-1)),!e)return[];const t=[];let n="",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?n.trim()&&(t.push(n.trim()),n=""):n+=p}n.trim()&&t.push(n.trim());const i=[];for(let u of t){if(u=u.replace(w.COMMENT,""),!u.trim())continue;const p=se(u);if(!~p){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()),f=r.get(f)??f,f.startsWith("@")&&o.get(f.slice(1))){const m=o.get(f.slice(1)),b=V(S(h," ").join(","),m.params);f=_(m.body,b,D),h=""}i.push({[f]:h})}return i}function se(s){let r=!1,o="";for(let e=0;e<s.length;e++){const t=s[e];if((t==='"'||t==="'")&&!r?(r=!0,o=t):t===o&&r&&(r=!1,o=""),t===":"&&!r)return e}return-1}function Z(s,r){if(!r.enableMath)return`math(${s})`;let o=s.replace(w.SPACE,"");return oe(o,r.enableMath)}function oe(s,r){return`calc(${s.replace(w.MATH_SYMBOL,"$1 $2 $3")})`}function ae(s,r){if(!r.enableMath)return s;let o=s;return o=(t=>t.replace(w.MATH,(n,c)=>Z(c,r)))(o),o}function ce(s,r){if(!r.convertLabToRGB&&!r.convertLchToRGB)return s;let o=s;o=le(o,r);const e=new Map,t=c=>c.replace(w.COLOR,a=>{if(e.has(a))return e.get(a);let l=a;return a.toLowerCase().startsWith("lab(")?r.convertLabToRGB&&(l=ie(a,r)):a.toLowerCase().startsWith("lch(")&&r.convertLchToRGB&&(l=ue(a,r)),e.set(a,l),l});let n;do n=o,o=t(o);while(o!==n);return o}function le(s,r){let o=s;const e=new Map;return o=o.replace(w.HEXLAB,(t,n,c,a)=>{if(e.has(t))return e.get(t);try{const l=parseInt(n,16)/255*100,i=(parseInt(c,16)-128)*1.5,u=(parseInt(a,16)-128)*1.5,p=R(l,i,u,r);return e.set(t,p),p}catch(l){return console.warn(`\u65E0\u6CD5\u89E3\u6790lab#\u5341\u516D\u8FDB\u5236\u989C\u8272: ${t}`,l),t}}),o=o.replace(w.HEXLCH,(t,n,c,a)=>{if(e.has(t))return e.get(t);try{const l=parseInt(n,16)/255*100,i=parseInt(c,16)/255*150,u=parseInt(a)/100*360,p=x(l,i,u),f=R(p.L,p.a,p.b,r);return e.set(t,f),f}catch(l){return console.warn(`\u65E0\u6CD5\u89E3\u6790lch#\u5341\u516D\u8FDB\u5236\u989C\u8272: ${t}`,l),t}}),o}function ie(s,r){const o=/lab\(\s*([\d.]+)(%?)\s+([\d.-]+)\s+([\d.-]+)(?:\s*\/\s*([\d.%]+))?\s*\)/i,e=s.match(o);if(!e)return s;try{let t=parseFloat(e[1]);e[2]==="%"?t=t:(t<0&&(t=0),t>100&&(t=100));const n=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(t,n,c,r,a)}catch(t){return console.warn(`\u65E0\u6CD5\u8F6C\u6362LAB\u989C\u8272: ${s}`,t),s}}function ue(s,r){const o=/lch\(\s*([\d.]+)(%?)\s+([\d.]+)\s+([\d.]+)(deg)?(?:\s*\/\s*([\d.%]+))?\s*\)/i,e=s.match(o);if(!e)return s;try{let t=parseFloat(e[1]);e[2]==="%"?t=t:(t<0&&(t=0),t>100&&(t=100));const n=parseFloat(e[3]);let c=parseFloat(e[4]);n<0&&console.warn(`LCH\u4E2D\u7684C\u503C\u4E0D\u80FD\u4E3A\u8D1F: ${n}`),c=(c%360+360)%360;const a=x(t,n,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,r,l)}catch(t){return console.warn(`\u65E0\u6CD5\u8F6C\u6362LCH\u989C\u8272: ${s}`,t),s}}function x(s,r,o){const e=o*Math.PI/180,t=r*Math.cos(e),n=r*Math.sin(e);return{L:s,a:t,b:n}}function $(s,r,o){const e=(d,y,M)=>{const K=(d+16)/116,N=y/500+K,k=K-M/200,Me=N**3>.008856?N**3:(116*N-16)/903.3,Ce=d>903.3*.008856?((d+16)/116)**3:d/903.3,we=k**3>.008856?k**3:(116*k-16)/903.3;return[Me*.95047,Ce*1,we*1.08883]},t=(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,q=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[T,q,Q]},n=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(s,r,o),[u,p,f]=t(a,l,i),h=n(u),m=n(p),b=n(f);return{r:c(h),g:c(m),b:c(b)}}function E(s,r,o){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]},t=(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]},n=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(s,r,o),[u,p,f]=t(a,l,i),h=n(u),m=n(p),b=n(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=$(s,r,o);return{r:l.r/255,g:l.g/255,b:l.b/255}}}function R(s,r,o,e,t=null){if(!e.enableP3||!W()){const n=$(s,r,o);return t!==null?`rgba(${n.r}, ${n.g}, ${n.b}, ${t})`:`rgb(${n.r}, ${n.g}, ${n.b})`}else{const n=E(s,r,o);return t!==null?`color(display-p3 ${n.r.toFixed(4)} ${n.g.toFixed(4)} ${n.b.toFixed(4)} / ${t})`:`color(display-p3 ${n.r.toFixed(4)} ${n.g.toFixed(4)} ${n.b.toFixed(4)})`}}function v(s,r){let o=s;return r.enableMath&&(o=ae(o,r)),(r.convertLabToRGB||r.convertLchToRGB)&&(o=ce(o,r)),o}function fe(s,r){const o=S(s),e={},t=new Map;let n="",c=null,a=!1,l=0;for(let i of o){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(),r);c?(t.has(c)||t.set(c,{}),t.get(c)[f]=m):e[f]=m;continue}if(u.endsWith("{")){c=u.slice(0,-1).trim(),a=!0,n+=i+`
|
|
10
|
+
`;continue}if(u==="}"){if(a&&c&&t.has(c)){const f=t.get(c),h=" ".repeat(l);for(const[m,b]of Object.entries(f))n+=`${h} --${m}: ${b};
|
|
11
|
+
`}a=!1,c=null}n+=i+`
|
|
12
|
+
`,i.includes("{")&&(l+=r.indentSize),i.includes("}")&&(l=Math.max(0,l-r.indentSize))}return{globalVariables:e,selectorVariables:t,cssWithoutVars:n.trim()}}function pe(s,r,o,e){const t=S(s),n=[],c=[];let a=0;for(let l=0;l<t.length;l++){const i=t[l],u=i.trim();if(!u)continue;const p=i.match(/^(\s*)/)[0].length;if(u==="}"){if(n.length>0){const f=n.pop();if(n.length>0){const h=n[n.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:[]};n.push(h);continue}if(!u.includes("{")&&!u.includes("}")&&u.includes(":")){if(n.length>0){const f=n[n.length-1];G(u,o,e).forEach(m=>{const b=Object.keys(m)[0],d=v(m[b],r);f.properties.push(d===""?b:`${b}: ${d}`)})}continue}}for(;n.length>0;){const l=n.pop();if(n.length===0)c.push(l);else{const i=n[n.length-1];i.children||(i.children=[]),i.children.push(l)}}return X(c,r,"",o,e)}function X(s,r,o="",e,t){let n="";const c=o.startsWith("@");for(const a of s){const l=a.selector.startsWith("@");let i=(c?" ".repeat(r.indentSize):"")+a.selector;if(l&&(i=a.selector+` {
|
|
13
|
+
`),o&&(i.includes("&")?i=i.replace(w.AND,o):i.trim().startsWith(":")?i=o+i:i=o+(c?"":" ")+i),a.properties.length>0){n+=(l?"":i)+` {
|
|
14
|
+
`;for(const u of a.properties)G(u,e,t).forEach(f=>{const h=Object.keys(f)[0],m=v(f[h],r);n+=(c?" ".repeat(r.indentSize):"")+" ".repeat(r.indentSize)+(m===""?h:`${h}: ${m};
|
|
15
|
+
`)});n+=c?" ".repeat(r.indentSize)+`}
|
|
20
16
|
`:`}
|
|
21
17
|
|
|
22
|
-
`}a.children&&a.children.length>0&&(
|
|
18
|
+
`}a.children&&a.children.length>0&&(n+=X(a.children,r,i,e,t)),c&&(n+=`}
|
|
23
19
|
|
|
24
|
-
`)}return
|
|
20
|
+
`)}return n.trim()+`
|
|
25
21
|
|
|
26
|
-
`}function
|
|
27
|
-
`);return`${
|
|
28
|
-
${
|
|
22
|
+
`}function he(s,r,o){let e=s;return e=e.replace(new RegExp("(?:\\$\\$|\\$)([a-zA-Z_][a-zA-Z0-9_]*)(?=[^a-zA-Z0-9_])","g"),(t,n)=>r[n]?t.startsWith("$$")?`attr(${n})`:`var(--${n})`:t),e=v(e,o),e}function de(s,r){if(Object.keys(s).length===0)return"";const o=Object.entries(s).map(([e,t])=>{const n=v(t,r);return" ".repeat(r.indentSize)+`--${e}: ${n};`}).join(`
|
|
23
|
+
`);return`${r.rootSelector} {
|
|
24
|
+
${o}
|
|
29
25
|
}
|
|
30
26
|
|
|
31
|
-
`}function
|
|
32
|
-
`);
|
|
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;
|
|
27
|
+
`}function me(s,r,o){let e="";const t=S(s);let n=null;for(let c of t){const a=c.trim();a.endsWith("{")&&(n=a.slice(0,-1).trim()),a==="}"&&n&&(n=null),e+=v(c,o)+`
|
|
28
|
+
`}return e.trim()}async function U(s,r){const o=w.IMPORT;return(async t=>{const n=[],c=t.replace(o,(a,l)=>{const i=ge(l,r).then(u=>U(u,r)).catch(u=>(console.warn(`\u65E0\u6CD5\u5BFC\u5165CSS\u6587\u4EF6: ${l}`,u),""));return n.push(i),`__IMPORT_PLACEHOLDER_${n.length-1}__`});if(n.length>0){const a=await Promise.all(n);let l=c;for(let i=0;i<a.length;i++)l=l.replace(`__IMPORT_PLACEHOLDER_${i}__`,a[i]);return l}return c})(s)}async function ge(s,r){if(r.importCache&&B.has(s))return B.get(s);const o=r.importBaseUrl?new URL(s,r.importBaseUrl).href:s;let e;if(typeof process<"u"&&process.versions&&process.versions.node&&typeof require<"u")try{const n=require("fs"),c=require("path");let a=o;o.startsWith(".")&&(a=c.join(process.cwd(),o)),e=n.readFileSync(a,"utf-8")}catch(n){throw new Error(`Node.js\u6587\u4EF6\u8BFB\u53D6\u5931\u8D25: ${o} - ${n.message}`)}else try{const n=new AbortController,c=setTimeout(()=>n.abort(),r.importTimeout),a=await fetch(o,{signal:n.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(n){throw n.name==="AbortError"?new Error(`\u5BFC\u5165\u8D85\u65F6: ${o}`):new Error(`\u65E0\u6CD5\u83B7\u53D6CSS: ${o} - ${n.message}`)}return r.importCache&&B.set(s,e),e}async function H(s,r={}){let{config:o,css:e}=J(s);const t={...g,...r,...o};e=await U(e,t);let n=e,c=new Map;if(t.enableAlias){const{aliases:y,css:M}=ee(n);c=y,n=M}let a=n,l=new Map;if(t.enableMacros){const{macros:y,css:M}=j(a,t);l=y,a=M;for(const[C,T]of l)A.set(C,T)}let i=a;t.enableMacros&&l.size>0&&(i=te(i,l,t));const{globalVariables:u,selectorVariables:p,cssWithoutVars:f}=fe(i,t);let h=f.trim();if(t.enableNesting&&f.includes("{"))try{h=pe(f,t,c,l)}catch(y){console.warn("\u5D4C\u5957\u89E3\u6790\u5931\u8D25\uFF0C\u4F7F\u7528\u539F\u59CBCSS:",y)}const m=de(u,t);let b=h;p.size>0&&(b=me(h,p,t)),b=he(b,{...u,...p},t);let d=m+b;for(const[y,M]of L)try{d=M.convert(d,t)}catch(C){console.error("\u63D2\u4EF6\u5904\u7406\u5931\u8D25:",C)}return d}function Y(s={}){const r={...g,...s},o=document.querySelectorAll(`style[${r.styleTagAttribute||"e"}]`);return o.forEach(e=>{let t=e.textContent;(async()=>{try{e.getAttribute("src")&&(t="@import "+e.getAttribute("src")+";");const c=await H(t,r),a=document.createElement("style");a.textContent=c,e.parentNode.insertBefore(a,e.nextSibling),r.preserveOriginal?e.style.display="none":e.remove()}catch(c){console.error("\u5904\u7406style\u6807\u7B7E\u5931\u8D25:",c)}})()}),o.length}function D(s={}){g={...g,...s}}function I(s,r={}){const o={...g,...r};if(s)return(async()=>{try{const e=await H(s,o),t=document.createElement("style");return t.textContent=e,document.head.appendChild(t),t}catch(e){return console.error("\u5E94\u7528CSS\u5931\u8D25:",e),null}})();document.readyState==="loading"?document.addEventListener("DOMContentLoaded",()=>{Y(o)}):Y(o)}const be={convert:H,apply:I,config:D,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 r=new s;return L.set(r.name??s.name,r),!0},remove(s){return L.get(s)?.destroy&&L.get(s).destroy(),L.delete(s),!0},getAll:function(){return Array.from(L.entries())},clear:function(){L.clear()}},aliases:{add:function(s,r){F.set(s,r)},remove:function(s){F.delete(s)},getAll:function(){return Array.from(F.entries())},clear:function(){F.clear()}},macros:{define:function(s,r,o=[]){if(typeof r=="function"){const t=r.toString().match(/{([\s\S]*)}/);t&&A.set(s,{params:o.map(n=>typeof n=="string"?{name:n}:n),body:t[1].trim()})}else A.set(s,{params:o.map(e=>typeof e=="string"?{name:e}:e),body:r})},call:function(s,...r){const o=A.get(s);if(!o)throw new Error(`\u672A\u5B9A\u4E49\u7684\u5B8F: ${s}`);const e=new Map;for(let t=0;t<o.params.length;t++){const n=o.params[t],c=t<r.length?r[t]:n.defaultValue;if(c===void 0&&n.defaultValue===null)throw new Error(`\u7F3A\u5C11\u5FC5\u9700\u53C2\u6570: ${n.name}`);e.set(n.name,c!==void 0?c:"")}return _(o.body,e,g)},getAll:function(){return Array.from(A.entries())},remove:function(s){A.delete(s)},clear:function(){A.clear()},parse:function(s){const{macros:r}=j(s,g);for(const[o,e]of r)A.set(o,e)}},math:{evaluate:function(s){return Z(s,g)}},colorUtils:{labToRGB:$,lchToLab:x,lchToRGB:function(s,r,o){const e=x(s,r,o);return $(e.L,e.a,e.b)},labToP3:E,lchToP3:function(s,r,o){const e=x(s,r,o);return E(e.L,e.a,e.b)},parseHexLab:function(s){const r=s.match(/lab#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/i);if(!r)return null;const o=r[1],e=r[2],t=r[3],n=parseInt(o,16)/255*100,c=(parseInt(e,16)-128)*1.5,a=(parseInt(t,16)-128)*1.5;return $(n,c,a)},parseHexLch:function(s){const r=s.match(/lch#([0-9a-f]{2})([0-9a-f]{2})(\d{1,3})/i);if(!r)return null;const o=r[1],e=r[2],t=r[3],n=parseInt(o,16)/255*100,c=parseInt(e,16)/255*150,a=parseInt(t)/100*360,l=x(n,c,a);return $(l.L,l.a,l.b)},generateColor:function(s,r,o,e=null,t=!0){return R(s,r,o,{enableP3:t},e)},parseColor:function(s){try{const r=s.match(/lab\(\s*([\d.]+)(%?)\s+([\d.-]+)\s+([\d.-]+)(?:\s*\/\s*([\d.%]+))?\s*\)/i);if(r){let e=parseFloat(r[1]);const t=parseFloat(r[3]),n=parseFloat(r[4]),c=r[5]?r[5].includes("%")?parseFloat(r[5])/100:parseFloat(r[5]):null,a=R(e,t,n,g,c);return{L:e,A:t,B:n,alpha:c,rgb:$(e,t,n),p3:E(e,t,n),colorString:a}}const o=s.match(/lch\(\s*([\d.]+)(%?)\s+([\d.]+)\s+([\d.]+)(deg)?(?:\s*\/\s*([\d.%]+))?\s*\)/i);if(o){let e=parseFloat(o[1]);const t=parseFloat(o[3]);let n=parseFloat(o[4]);const c=o[6]?o[6].includes("%")?parseFloat(o[6])/100:parseFloat(o[6]):null,a=x(e,t,n),l=R(a.L,a.a,a.b,g,c);return{L:e,C:t,H:n,alpha:c,lab:a,rgb:$(a.L,a.a,a.b),p3:E(a.L,a.a,a.b),colorString:l}}return null}catch(r){return console.warn("\u65E0\u6CD5\u89E3\u6790\u989C\u8272:",s,r),null}}}},z=async function(...s){if(s.length>1||s[0]&&s[0].raw)return await ye(...s);const r=s[0];if(typeof r=="string"){const o=await H(r,{...g,...s[1]});return o&&typeof o.then=="function",o}return typeof r=="object"&&r!==null?(g={...g,...r},z):s.length===0?I():z};async function ye(s,...r){let o=s[0];for(let t=0;t<r.length;t++){const n=r[t];let c="";typeof n=="function"?c=n():Array.isArray(n)?c=n.join(" "):c=String(n??""),o+=c+s[t+1]}const e=await H(o,g);return e&&typeof e.then=="function",e}return Object.assign(z,be),typeof window<"u"&&(I(),Object.defineProperty(window.HTMLElement.prototype,"cssVar",{get(){const s=this;return new Proxy(()=>{},{get(r,o){const e=o.startsWith("--")?o:`--${o}`;return s.style.getPropertyValue(e)},set(r,o,e){const t=o.startsWith("--")?o:`--${o}`;return s.style.setProperty(t,e),!0},apply(r,o,e){const t=e[0],n=e[1],c=t.startsWith("--")?t:`--${t}`;if(n===void 0)return s.style.getPropertyValue(c);s.style.setProperty(c,n)}})}})),z})();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
|
@@ -67,12 +67,36 @@ const styimat = (function() {
|
|
|
67
67
|
|
|
68
68
|
const pluginMap = new Map();
|
|
69
69
|
|
|
70
|
+
function splitLines(str, splitChar = "\n") {
|
|
71
|
+
const lines = [];
|
|
72
|
+
const splitCharCode = splitChar.charCodeAt(0);
|
|
73
|
+
let start = 0;
|
|
74
|
+
|
|
75
|
+
for (let i = 0; i < str.length; i++) {
|
|
76
|
+
if (str.charCodeAt(i) === splitCharCode) { // '\n' 的 charCode
|
|
77
|
+
if (i > start || str.charAt(start) !== '\r') {
|
|
78
|
+
lines.push(str.substring(start, i));
|
|
79
|
+
}
|
|
80
|
+
start = i + 1;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// 处理最后一行
|
|
85
|
+
if (start < str.length) {
|
|
86
|
+
lines.push(str.substring(start));
|
|
87
|
+
} else if (start === str.length && str.charCodeAt(str.length - 1) === 10) {
|
|
88
|
+
lines.push('');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return lines;
|
|
92
|
+
}
|
|
93
|
+
|
|
70
94
|
/**
|
|
71
95
|
* 解析配置头
|
|
72
96
|
*/
|
|
73
97
|
function parseConfigHeader(cssText) {
|
|
74
98
|
const config = { ...defaultConfig };
|
|
75
|
-
const lines = cssText
|
|
99
|
+
const lines = splitLines(cssText);
|
|
76
100
|
const cleanLines = [];
|
|
77
101
|
|
|
78
102
|
for (let line of lines) {
|
|
@@ -82,7 +106,7 @@ const styimat = (function() {
|
|
|
82
106
|
const configLine = trimmed.substring(1).trim();
|
|
83
107
|
const firstSpace = configLine.indexOf(' ');
|
|
84
108
|
|
|
85
|
-
if (firstSpace
|
|
109
|
+
if (~firstSpace) {
|
|
86
110
|
const key = configLine.substring(0, firstSpace).trim();
|
|
87
111
|
const value = configLine.substring(firstSpace + 1).trim();
|
|
88
112
|
|
|
@@ -115,7 +139,7 @@ const styimat = (function() {
|
|
|
115
139
|
*/
|
|
116
140
|
function parseAliasStatements(cssText) {
|
|
117
141
|
const aliases = new Map();
|
|
118
|
-
const lines = cssText
|
|
142
|
+
const lines = splitLines(cssText);
|
|
119
143
|
const cleanLines = [];
|
|
120
144
|
|
|
121
145
|
for (let line of lines) {
|
|
@@ -150,7 +174,7 @@ const styimat = (function() {
|
|
|
150
174
|
}
|
|
151
175
|
|
|
152
176
|
const macros = new Map();
|
|
153
|
-
const lines = cssText
|
|
177
|
+
const lines = splitLines(cssText);
|
|
154
178
|
const cleanLines = [];
|
|
155
179
|
let inMacroDefinition = false;
|
|
156
180
|
let currentMacroName = '';
|
|
@@ -166,12 +190,11 @@ const styimat = (function() {
|
|
|
166
190
|
if (macroMatch) {
|
|
167
191
|
inMacroDefinition = true;
|
|
168
192
|
currentMacroName = macroMatch[1];
|
|
169
|
-
currentMacroParams = macroMatch[2]
|
|
170
|
-
.split(',')
|
|
193
|
+
currentMacroParams = splitLines(macroMatch[2], ',')
|
|
171
194
|
.map(param => param.trim())
|
|
172
195
|
.filter(param => param)
|
|
173
196
|
.map(param => {
|
|
174
|
-
const paramParts = param
|
|
197
|
+
const paramParts = splitLines(param, ':').map(p => p.trim());
|
|
175
198
|
return {
|
|
176
199
|
name: paramParts[0].slice(1),
|
|
177
200
|
defaultValue: paramParts[1] || null
|
|
@@ -453,7 +476,7 @@ const styimat = (function() {
|
|
|
453
476
|
|
|
454
477
|
const colonIndex = findFirstColonOutsideQuotes(declaration);
|
|
455
478
|
|
|
456
|
-
if (colonIndex
|
|
479
|
+
if (!~colonIndex) {
|
|
457
480
|
console.warn(`无效的CSS声明: "${declaration}"`);
|
|
458
481
|
continue;
|
|
459
482
|
}
|
|
@@ -465,13 +488,11 @@ const styimat = (function() {
|
|
|
465
488
|
value = value.slice(0, -1).trim();
|
|
466
489
|
}
|
|
467
490
|
|
|
468
|
-
|
|
469
|
-
property = aliases.get(property);
|
|
470
|
-
}
|
|
491
|
+
property = aliases.get(property) ?? property;
|
|
471
492
|
|
|
472
493
|
if (property.startsWith("@") && macros.get(property.slice(1))) {
|
|
473
494
|
const macro = macros.get(property.slice(1));
|
|
474
|
-
const args = parseMacroArguments(value
|
|
495
|
+
const args = parseMacroArguments(splitLines(value, " ").join(","), macro.params);
|
|
475
496
|
property = applyMacro(macro.body, args, config);
|
|
476
497
|
value = "";
|
|
477
498
|
}
|
|
@@ -545,12 +566,8 @@ const styimat = (function() {
|
|
|
545
566
|
});
|
|
546
567
|
};
|
|
547
568
|
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
lastResult = result;
|
|
551
|
-
result = processMath(result);
|
|
552
|
-
} while (result !== lastResult && result.includes('math('));
|
|
553
|
-
|
|
569
|
+
result = processMath(result);
|
|
570
|
+
|
|
554
571
|
return result;
|
|
555
572
|
}
|
|
556
573
|
|
|
@@ -932,7 +949,7 @@ const styimat = (function() {
|
|
|
932
949
|
* 提取变量定义并移除
|
|
933
950
|
*/
|
|
934
951
|
function extractVariablesAndCSS(cssText, config) {
|
|
935
|
-
const lines = cssText
|
|
952
|
+
const lines = splitLines(cssText);
|
|
936
953
|
const globalVariables = {};
|
|
937
954
|
const selectorVariables = new Map();
|
|
938
955
|
let cssWithoutVars = '';
|
|
@@ -1002,7 +1019,7 @@ const styimat = (function() {
|
|
|
1002
1019
|
* 解析嵌套规则
|
|
1003
1020
|
*/
|
|
1004
1021
|
function parseNestedRules(cssText, config, aliases, macros) {
|
|
1005
|
-
const lines = cssText
|
|
1022
|
+
const lines = splitLines(cssText);
|
|
1006
1023
|
const stack = [];
|
|
1007
1024
|
const rootRules = [];
|
|
1008
1025
|
let currentIndent = 0;
|
|
@@ -1127,11 +1144,10 @@ const styimat = (function() {
|
|
|
1127
1144
|
*/
|
|
1128
1145
|
function replaceVariableUses(cssText, variables, config) {
|
|
1129
1146
|
let result = cssText;
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
}
|
|
1147
|
+
result = result.replace(new RegExp(`(?:\\$\\$|\\$)([a-zA-Z_][a-zA-Z0-9_]*)(?=[^a-zA-Z0-9_])`, "g"), (match, varName) => {
|
|
1148
|
+
if (variables[varName]) return match.startsWith('$$') ? `attr(${varName})` : `var(--${varName})`;
|
|
1149
|
+
return match;
|
|
1150
|
+
});
|
|
1135
1151
|
|
|
1136
1152
|
result = processCSSValue(result, config);
|
|
1137
1153
|
|
|
@@ -1161,7 +1177,7 @@ const styimat = (function() {
|
|
|
1161
1177
|
*/
|
|
1162
1178
|
function injectSelectorVariables(cssText, selectorVariables, config) {
|
|
1163
1179
|
let result = '';
|
|
1164
|
-
const lines = cssText
|
|
1180
|
+
const lines = splitLines(cssText);
|
|
1165
1181
|
let currentSelector = null;
|
|
1166
1182
|
|
|
1167
1183
|
for (let line of lines) {
|
|
@@ -1703,8 +1719,7 @@ const styimat = (function() {
|
|
|
1703
1719
|
}
|
|
1704
1720
|
|
|
1705
1721
|
Object.assign(styimat, api);
|
|
1706
|
-
|
|
1707
|
-
|
|
1722
|
+
|
|
1708
1723
|
// 自动初始化
|
|
1709
1724
|
if (typeof window !== 'undefined') {
|
|
1710
1725
|
apply();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "styimat",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.5.0",
|
|
4
4
|
"description": "一个高效的CSS变量预处理库,支持Lab/LCH颜色空间自动转换、嵌套选择器和Display P3广色域,让现代CSS开发更简洁强大。",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"css",
|
|
@@ -43,10 +43,11 @@
|
|
|
43
43
|
"dist/styimat.min.mjs",
|
|
44
44
|
"LICENSE",
|
|
45
45
|
"README.md",
|
|
46
|
-
"bin/cli.js"
|
|
46
|
+
"bin/cli.js",
|
|
47
|
+
"bin/cli.min.js"
|
|
47
48
|
],
|
|
48
49
|
"bin": {
|
|
49
|
-
"styimat": "bin/cli.js"
|
|
50
|
+
"styimat": "bin/cli.min.js"
|
|
50
51
|
},
|
|
51
52
|
"repository": {
|
|
52
53
|
"type": "git",
|
|
@@ -58,7 +59,7 @@
|
|
|
58
59
|
"homepage": "https://gitee.com/wxy6987/styimat#readme",
|
|
59
60
|
"scripts": {
|
|
60
61
|
"test": "echo \"No tests yet\" && exit 0",
|
|
61
|
-
"build:min": "npx esbuild dist/styimat.js --minify --outfile=dist/styimat.min.js && node build.js && npx esbuild dist/styimat.mjs --minify --outfile=dist/styimat.min.mjs",
|
|
62
|
+
"build:min": "npx esbuild dist/styimat.js --minify --outfile=dist/styimat.min.js && node build.js && npx esbuild dist/styimat.mjs --minify --outfile=dist/styimat.min.mjs && npx esbuild bin/cli.js --minify --outfile=bin/cli.min.js",
|
|
62
63
|
"prepare": "npm run build:min",
|
|
63
64
|
"prepublishOnly": "npm run build:min",
|
|
64
65
|
"postpublish": "npm run git:auto-commit && npm run git:auto-tag",
|