styimat 8.5.0 → 8.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/styimat.js +47 -70
- package/dist/styimat.min.js +17 -20
- package/dist/styimat.min.mjs +17 -20
- package/dist/styimat.mjs +47 -70
- package/package.json +1 -1
package/dist/styimat.js
CHANGED
|
@@ -50,7 +50,8 @@ const styimat = (function() {
|
|
|
50
50
|
HEXLCH: /lch#([0-9a-f]{2})([0-9a-f]{2})(\d{1,3})/gi,
|
|
51
51
|
SPACE: /\s+/g,
|
|
52
52
|
AND: /&/g,
|
|
53
|
-
HYPHEN: /-([a-z])/g
|
|
53
|
+
HYPHEN: /-([a-z])/g,
|
|
54
|
+
VARIABLE: /(?:\$\$|\$)([a-zA-Z_][a-zA-Z0-9_]*)(?=[^a-zA-Z0-9_])/g
|
|
54
55
|
}
|
|
55
56
|
|
|
56
57
|
// 全局P3支持检测结果
|
|
@@ -72,10 +73,10 @@ const styimat = (function() {
|
|
|
72
73
|
const splitCharCode = splitChar.charCodeAt(0);
|
|
73
74
|
let start = 0;
|
|
74
75
|
|
|
75
|
-
for (let i = 0; i <
|
|
76
|
-
if (str.charCodeAt(i) === splitCharCode) {
|
|
76
|
+
for (let i = 0, length = str.length; i < length; i++) {
|
|
77
|
+
if (str.charCodeAt(i) === splitCharCode) {
|
|
77
78
|
if (i > start || str.charAt(start) !== '\r') {
|
|
78
|
-
lines.push(str.
|
|
79
|
+
lines.push(str.slice(start, i));
|
|
79
80
|
}
|
|
80
81
|
start = i + 1;
|
|
81
82
|
}
|
|
@@ -284,7 +285,7 @@ const styimat = (function() {
|
|
|
284
285
|
const argsMap = new Map();
|
|
285
286
|
const argValues = parseArgumentList(argsStr);
|
|
286
287
|
|
|
287
|
-
for (let i = 0; i <
|
|
288
|
+
for (let i = 0, length = paramDefs.length; i < length; i++) {
|
|
288
289
|
const param = paramDefs[i];
|
|
289
290
|
let value;
|
|
290
291
|
|
|
@@ -313,7 +314,7 @@ const styimat = (function() {
|
|
|
313
314
|
let parenDepth = 0;
|
|
314
315
|
let bracketDepth = 0;
|
|
315
316
|
|
|
316
|
-
for (let i = 0; i <
|
|
317
|
+
for (let i = 0, length = argsStr.length; i < length; i++) {
|
|
317
318
|
const char = argsStr[i];
|
|
318
319
|
|
|
319
320
|
if ((char === '"' || char === "'") && !inQuotes) {
|
|
@@ -351,11 +352,13 @@ const styimat = (function() {
|
|
|
351
352
|
*/
|
|
352
353
|
function applyMacro(macroBody, args, config) {
|
|
353
354
|
let result = macroBody;
|
|
355
|
+
const paramRegex = /\$([a-zA-Z_][a-zA-Z0-9_]*)/g;
|
|
354
356
|
|
|
355
|
-
|
|
356
|
-
const
|
|
357
|
-
|
|
358
|
-
|
|
357
|
+
result = result.replace(paramRegex, (match, paramName) => {
|
|
358
|
+
const paramValue = args.get(paramName);
|
|
359
|
+
if (paramValue) return args.get(paramName);
|
|
360
|
+
return match;
|
|
361
|
+
});
|
|
359
362
|
|
|
360
363
|
if (config.enableMacros) {
|
|
361
364
|
const macroNames = Array.from(macroRegistry.keys()).map(name =>
|
|
@@ -435,7 +438,7 @@ const styimat = (function() {
|
|
|
435
438
|
let inQuotes = false;
|
|
436
439
|
let quoteChar = '';
|
|
437
440
|
|
|
438
|
-
for (let i = 0; i <
|
|
441
|
+
for (let i = 0, length = str.length; i < length; i++) {
|
|
439
442
|
const char = str[i];
|
|
440
443
|
|
|
441
444
|
if ((char === '"' || char === "'") && !inQuotes) {
|
|
@@ -488,6 +491,10 @@ const styimat = (function() {
|
|
|
488
491
|
value = value.slice(0, -1).trim();
|
|
489
492
|
}
|
|
490
493
|
|
|
494
|
+
if (property.startsWith('$')) {
|
|
495
|
+
property = "--" + property.slice(1);
|
|
496
|
+
}
|
|
497
|
+
|
|
491
498
|
property = aliases.get(property) ?? property;
|
|
492
499
|
|
|
493
500
|
if (property.startsWith("@") && macros.get(property.slice(1))) {
|
|
@@ -510,7 +517,7 @@ const styimat = (function() {
|
|
|
510
517
|
let inQuotes = false;
|
|
511
518
|
let quoteChar = '';
|
|
512
519
|
|
|
513
|
-
for (let i = 0; i <
|
|
520
|
+
for (let i = 0, length = str.length; i < length; i++) {
|
|
514
521
|
const char = str[i];
|
|
515
522
|
|
|
516
523
|
if ((char === '"' || char === "'") && !inQuotes) {
|
|
@@ -953,9 +960,9 @@ const styimat = (function() {
|
|
|
953
960
|
const globalVariables = {};
|
|
954
961
|
const selectorVariables = new Map();
|
|
955
962
|
let cssWithoutVars = '';
|
|
956
|
-
let currentSelector = null;
|
|
957
963
|
let inSelectorBlock = false;
|
|
958
964
|
let currentIndent = 0;
|
|
965
|
+
let currentSelector = null;
|
|
959
966
|
|
|
960
967
|
for (let line of lines) {
|
|
961
968
|
const trimmed = line.trim();
|
|
@@ -966,12 +973,7 @@ const styimat = (function() {
|
|
|
966
973
|
const [, varName, varValue] = varMatch;
|
|
967
974
|
const processedValue = processCSSValue(varValue.trim(), config);
|
|
968
975
|
|
|
969
|
-
if (currentSelector) {
|
|
970
|
-
if (!selectorVariables.has(currentSelector)) {
|
|
971
|
-
selectorVariables.set(currentSelector, {});
|
|
972
|
-
}
|
|
973
|
-
selectorVariables.get(currentSelector)[varName] = processedValue;
|
|
974
|
-
} else {
|
|
976
|
+
if (!currentSelector) {
|
|
975
977
|
globalVariables[varName] = processedValue;
|
|
976
978
|
}
|
|
977
979
|
continue;
|
|
@@ -983,7 +985,7 @@ const styimat = (function() {
|
|
|
983
985
|
cssWithoutVars += line + '\n';
|
|
984
986
|
continue;
|
|
985
987
|
}
|
|
986
|
-
|
|
988
|
+
|
|
987
989
|
if (trimmed === '}') {
|
|
988
990
|
if (inSelectorBlock) {
|
|
989
991
|
if (currentSelector && selectorVariables.has(currentSelector)) {
|
|
@@ -997,9 +999,9 @@ const styimat = (function() {
|
|
|
997
999
|
inSelectorBlock = false;
|
|
998
1000
|
currentSelector = null;
|
|
999
1001
|
}
|
|
1000
|
-
|
|
1002
|
+
|
|
1001
1003
|
cssWithoutVars += line + '\n';
|
|
1002
|
-
|
|
1004
|
+
|
|
1003
1005
|
if (line.includes('{')) {
|
|
1004
1006
|
currentIndent += config.indentSize;
|
|
1005
1007
|
}
|
|
@@ -1008,6 +1010,8 @@ const styimat = (function() {
|
|
|
1008
1010
|
}
|
|
1009
1011
|
}
|
|
1010
1012
|
|
|
1013
|
+
cssWithoutVars = cssText;
|
|
1014
|
+
|
|
1011
1015
|
return {
|
|
1012
1016
|
globalVariables,
|
|
1013
1017
|
selectorVariables,
|
|
@@ -1024,7 +1028,7 @@ const styimat = (function() {
|
|
|
1024
1028
|
const rootRules = [];
|
|
1025
1029
|
let currentIndent = 0;
|
|
1026
1030
|
|
|
1027
|
-
for (let i = 0; i <
|
|
1031
|
+
for (let i = 0, length = lines.length; i < length; i++) {
|
|
1028
1032
|
const line = lines[i];
|
|
1029
1033
|
const trimmed = line.trim();
|
|
1030
1034
|
|
|
@@ -1092,61 +1096,62 @@ const styimat = (function() {
|
|
|
1092
1096
|
/**
|
|
1093
1097
|
* 将规则树转换为CSS字符串
|
|
1094
1098
|
*/
|
|
1095
|
-
function convertRulesToCSS(rules, config, parentSelector = '', aliases, macros) {
|
|
1099
|
+
function convertRulesToCSS(rules, config, parentSelector = '', aliases, macros, isRootAt = false) {
|
|
1096
1100
|
let result = '';
|
|
1097
|
-
const isParentAt = parentSelector.startsWith("@");
|
|
1098
1101
|
|
|
1099
1102
|
for (const rule of rules) {
|
|
1100
1103
|
const isAt = rule.selector.startsWith("@");
|
|
1101
|
-
let fullSelector = (isParentAt ? " ".repeat(config.indentSize) : '') + rule.selector;
|
|
1102
1104
|
|
|
1103
1105
|
if (isAt) {
|
|
1104
|
-
|
|
1106
|
+
result += rule.selector + " {\n";
|
|
1105
1107
|
}
|
|
1106
1108
|
|
|
1109
|
+
let fullSelector = rule.selector;
|
|
1110
|
+
|
|
1107
1111
|
if (parentSelector) {
|
|
1108
1112
|
if (fullSelector.includes('&')) {
|
|
1109
1113
|
fullSelector = fullSelector.replace(REGEX.AND, parentSelector);
|
|
1110
1114
|
} else if (fullSelector.trim().startsWith(':')) {
|
|
1111
1115
|
fullSelector = parentSelector + fullSelector;
|
|
1112
1116
|
} else {
|
|
1113
|
-
fullSelector = parentSelector +
|
|
1117
|
+
fullSelector = parentSelector + " " + fullSelector;
|
|
1114
1118
|
}
|
|
1115
1119
|
}
|
|
1116
1120
|
|
|
1121
|
+
fullSelector = fullSelector;
|
|
1122
|
+
|
|
1117
1123
|
if (rule.properties.length > 0) {
|
|
1118
|
-
result += (
|
|
1124
|
+
result += (isRootAt ? " ".repeat(config.indentSize) : '') + fullSelector + ' {\n';
|
|
1119
1125
|
for (const prop of rule.properties) {
|
|
1120
1126
|
const parsed = parseSingleLineCSS(prop, aliases, macros);
|
|
1121
1127
|
parsed.forEach(obj => {
|
|
1122
1128
|
const key = Object.keys(obj)[0];
|
|
1123
1129
|
const value = processCSSValue(obj[key], config);
|
|
1124
|
-
result += (
|
|
1130
|
+
result += (isRootAt ? " ".repeat(config.indentSize) : '') + " ".repeat(config.indentSize) + (value === "" ? key : `${key}: ${value};\n`);
|
|
1125
1131
|
});
|
|
1126
1132
|
}
|
|
1127
|
-
result +=
|
|
1133
|
+
result += isRootAt ? " ".repeat(config.indentSize) + '}\n' : '}\n\n';
|
|
1128
1134
|
}
|
|
1129
1135
|
|
|
1130
1136
|
if (rule.children && rule.children.length > 0) {
|
|
1131
|
-
result += convertRulesToCSS(rule.children, config, fullSelector, aliases, macros);
|
|
1137
|
+
result += convertRulesToCSS(rule.children, config, (isAt ? "" : fullSelector), aliases, macros, isRootAt || isAt);
|
|
1132
1138
|
}
|
|
1133
1139
|
|
|
1134
|
-
if (
|
|
1140
|
+
if (isAt) {
|
|
1135
1141
|
result += "}\n\n";
|
|
1136
1142
|
}
|
|
1137
1143
|
}
|
|
1138
1144
|
|
|
1139
|
-
return result
|
|
1145
|
+
return result;
|
|
1140
1146
|
}
|
|
1141
1147
|
|
|
1142
1148
|
/**
|
|
1143
1149
|
* 替换变量使用
|
|
1144
1150
|
*/
|
|
1145
|
-
function replaceVariableUses(cssText,
|
|
1151
|
+
function replaceVariableUses(cssText, config) {
|
|
1146
1152
|
let result = cssText;
|
|
1147
|
-
result = result.replace(
|
|
1148
|
-
|
|
1149
|
-
return match;
|
|
1153
|
+
result = result.replace(REGEX.VARIABLE, (match, varName) => {
|
|
1154
|
+
return match.startsWith('$$') ? `attr(${varName})` : `var(--${varName})`;
|
|
1150
1155
|
});
|
|
1151
1156
|
|
|
1152
1157
|
result = processCSSValue(result, config);
|
|
@@ -1172,31 +1177,6 @@ const styimat = (function() {
|
|
|
1172
1177
|
return `${config.rootSelector} {\n${declarations}\n}\n\n`;
|
|
1173
1178
|
}
|
|
1174
1179
|
|
|
1175
|
-
/**
|
|
1176
|
-
* 注入选择器局部变量
|
|
1177
|
-
*/
|
|
1178
|
-
function injectSelectorVariables(cssText, selectorVariables, config) {
|
|
1179
|
-
let result = '';
|
|
1180
|
-
const lines = splitLines(cssText);
|
|
1181
|
-
let currentSelector = null;
|
|
1182
|
-
|
|
1183
|
-
for (let line of lines) {
|
|
1184
|
-
const trimmed = line.trim();
|
|
1185
|
-
|
|
1186
|
-
if (trimmed.endsWith('{')) {
|
|
1187
|
-
currentSelector = trimmed.slice(0, -1).trim();
|
|
1188
|
-
}
|
|
1189
|
-
|
|
1190
|
-
if (trimmed === '}' && currentSelector) {
|
|
1191
|
-
currentSelector = null;
|
|
1192
|
-
}
|
|
1193
|
-
|
|
1194
|
-
result += processCSSValue(line, config) + '\n';
|
|
1195
|
-
}
|
|
1196
|
-
|
|
1197
|
-
return result.trim();
|
|
1198
|
-
}
|
|
1199
|
-
|
|
1200
1180
|
/**
|
|
1201
1181
|
* 处理 @import 语句
|
|
1202
1182
|
*/
|
|
@@ -1225,7 +1205,7 @@ const styimat = (function() {
|
|
|
1225
1205
|
const importedContents = await Promise.all(importPromises);
|
|
1226
1206
|
|
|
1227
1207
|
let finalText = processedText;
|
|
1228
|
-
for (let i =
|
|
1208
|
+
for (let i = importedContents.length; i--;) {
|
|
1229
1209
|
finalText = finalText.replace(`__IMPORT_PLACEHOLDER_${i}__`, importedContents[i]);
|
|
1230
1210
|
}
|
|
1231
1211
|
|
|
@@ -1352,11 +1332,8 @@ const styimat = (function() {
|
|
|
1352
1332
|
const rootRule = generateRootRule(globalVariables, finalConfig);
|
|
1353
1333
|
|
|
1354
1334
|
let finalCSS = processedCSS;
|
|
1355
|
-
if (selectorVariables.size > 0) {
|
|
1356
|
-
finalCSS = injectSelectorVariables(processedCSS, selectorVariables, finalConfig);
|
|
1357
|
-
}
|
|
1358
1335
|
|
|
1359
|
-
finalCSS = replaceVariableUses(finalCSS,
|
|
1336
|
+
finalCSS = replaceVariableUses(finalCSS, finalConfig);
|
|
1360
1337
|
|
|
1361
1338
|
let fullCSS = rootRule + finalCSS;
|
|
1362
1339
|
|
|
@@ -1530,7 +1507,7 @@ const styimat = (function() {
|
|
|
1530
1507
|
}
|
|
1531
1508
|
|
|
1532
1509
|
const argsMap = new Map();
|
|
1533
|
-
for (let i = 0
|
|
1510
|
+
for (let i = 0, length = macro.params.length; i < length; i++) {
|
|
1534
1511
|
const param = macro.params[i];
|
|
1535
1512
|
const value = i < args.length ? args[i] : param.defaultValue;
|
|
1536
1513
|
if (value === undefined && param.defaultValue === null) {
|
|
@@ -1695,7 +1672,7 @@ const styimat = (function() {
|
|
|
1695
1672
|
async function handleTemplateTag(strings, ...values) {
|
|
1696
1673
|
let cssText = strings[0];
|
|
1697
1674
|
|
|
1698
|
-
for (let i = 0; i <
|
|
1675
|
+
for (let i = 0, length = values.length; i < length; i++) {
|
|
1699
1676
|
const value = values[i];
|
|
1700
1677
|
let result = '';
|
|
1701
1678
|
|
package/dist/styimat.min.js
CHANGED
|
@@ -1,28 +1,25 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* MIT License
|
|
3
3
|
* Copyright (c) 2025 王小玗
|
|
4
|
-
*/const styimat=(function(){let
|
|
5
|
-
`){const
|
|
6
|
-
`)}}function J(
|
|
7
|
-
`)}}function
|
|
8
|
-
`)}),
|
|
9
|
-
`)}}function ee(
|
|
10
|
-
`;continue}if(u==="}"){if(
|
|
11
|
-
`}
|
|
12
|
-
`,i.includes("{")&&(
|
|
13
|
-
`)
|
|
14
|
-
`;for(const u of a.properties)j(u,
|
|
15
|
-
`)});
|
|
4
|
+
*/const styimat=(function(){let b={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 A={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,VARIABLE:/(?:\$\$|\$)([a-zA-Z_][a-zA-Z0-9_]*)(?=[^a-zA-Z0-9_])/g};let x=null;const B=new Map,_=new Map,w=new Map,L=new Map;function S(r,e=`
|
|
5
|
+
`){const n=[],t=e.charCodeAt(0);let s=0;for(let o=0,c=r.length;o<c;o++)r.charCodeAt(o)===t&&((o>s||r.charAt(s)!=="\r")&&n.push(r.slice(s,o)),s=o+1);return s<r.length?n.push(r.substring(s)):s===r.length&&r.charCodeAt(r.length-1)===10&&n.push(""),n}function K(r){const e={...b},n=S(r),t=[];for(let s of n){const o=s.trim();if(o.startsWith("#")){const c=o.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"?e[u]=i==="true":!isNaN(i)&&i.trim()!==""?e[u]=Number(i):e[u]=i}else console.warn(`\u65E0\u6548\u7684\u914D\u7F6E\u884C: ${o}`)}else t.push(s)}return{config:e,css:t.join(`
|
|
6
|
+
`)}}function J(r){const e=new Map,n=S(r),t=[];for(let s of n){const o=s.trim();if(o.startsWith("@alias")){const c=o.match(A.ALIAS);if(c){const a=c[1],l=c[2];e.set(a,l);continue}}t.push(s)}return{aliases:e,css:t.join(`
|
|
7
|
+
`)}}function N(r,e){if(!e.enableMacros)return{macros:new Map,css:r};const n=new Map,t=S(r),s=[];let o=!1,c="",a=[],l=[],i=0;for(let u of t){const p=u.trim();if(!o&&p.startsWith("@macro")){const f=p.match(A.MACRO);if(f){o=!0,c=f[1],a=S(f[2],",").map(h=>h.trim()).filter(h=>h).map(h=>{const d=S(h,":").map(y=>y.trim());return{name:d[0].slice(1),defaultValue:d[1]||null}}),l=[],i=1;continue}}if(o){for(const f of u)f==="{"&&i++,f==="}"&&i--;i===0?(n.set(c,{params:a,body:l.join(`
|
|
8
|
+
`)}),o=!1,c="",a=[],l=[]):l.push(u);continue}s.push(u)}return{macros:n,css:s.join(`
|
|
9
|
+
`)}}function ee(r,e,n){if(!n.enableMacros||e.size===0)return r;const t=Array.from(e.keys()).map(c=>c.replace(A.SPECHAR,"\\$&")).join("|"),s=new RegExp(`@(${t})\\s*\\(([^)]*)\\)\\s*;?`,"g");return(c=>{let a=c,l=!1;do l=!1,a=a.replace(s,(i,u,p)=>{l=!0;const f=e.get(u);if(!f)return console.warn(`\u672A\u5B9A\u4E49\u7684\u5B8F: ${u}`),i;const h=z(p,f.params);return O(f.body,h,n)});while(l);return a})(r)}function z(r,e){const n=new Map,t=te(r);for(let s=0,o=e.length;s<o;s++){const c=e[s];let a;s<t.length?a=t[s]:c.defaultValue!==null?a=c.defaultValue:a="none",n.set(c.name,a)}return n}function te(r){const e=[];let n="",t=!1,s="",o=0,c=0;for(let a=0,l=r.length;a<l;a++){const i=r[a];(i==='"'||i==="'")&&!t?(t=!0,s=i):i===s&&t&&(t=!1,s=""),t||(i==="("&&o++,i===")"&&o--,i==="["&&c++,i==="]"&&c--),i===","&&!t&&o===0&&c===0?(e.push(n.trim()),n=""):n+=i}return n.trim()&&e.push(n.trim()),e}function O(r,e,n){let t=r;const s=/\$([a-zA-Z_][a-zA-Z0-9_]*)/g;if(t=t.replace(s,(o,c)=>e.get(c)?e.get(c):o),n.enableMacros){const o=Array.from(w.keys()).map(c=>c.replace(A.SPECHAR,"\\$&")).join("|");if(o){const c=new RegExp(`@(${o})\\s*\\(([^)]*)\\)\\s*;?`,"g");let a;do a=!1,t=t.replace(c,(l,i,u)=>{a=!0;const p=w.get(i);if(!p)return l;const f=z(u,p.params);return O(p.body,f,n)});while(a)}}return t=T(t,n),t}function ne(r){return r.replace(A.HYPHEN,function(e,n){return n.toUpperCase()})}function I(){if(x!==null)return x;if(typeof window>"u"||!window.CSS)return x=!1,!1;try{x=CSS.supports("color","color(display-p3 1 0 0)")}catch{x=!1}return x}function j(r,e,n){let t=r.trim();if(t.endsWith(";")&&(t=t.slice(0,-1)),!t)return[];const s=[];let o="",c=0,a=!1,l="";for(let u=0,p=t.length;u<p;u++){const f=t[u];(f==='"'||f==="'")&&!a?(a=!0,l=f):f===l&&a&&(a=!1,l=""),a||(f==="("?c++:f===")"&&c--),f===";"&&!a&&c===0?o.trim()&&(s.push(o.trim()),o=""):o+=f}o.trim()&&s.push(o.trim());const i=[];for(let u of s){if(u=u.replace(A.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.startsWith("$")&&(f="--"+f.slice(1)),f=e.get(f)??f,f.startsWith("@")&&n.get(f.slice(1))){const d=n.get(f.slice(1)),y=z(S(h," ").join(","),d.params);f=O(d.body,y,Y),h=""}i.push({[f]:h})}return i}function re(r){let e=!1,n="";for(let t=0,s=r.length;t<s;t++){const o=r[t];if((o==='"'||o==="'")&&!e?(e=!0,n=o):o===n&&e&&(e=!1,n=""),o===":"&&!e)return t}return-1}function Z(r,e){if(!e.enableMath)return`math(${r})`;let n=r.replace(A.SPACE,"");return se(n,e.enableMath)}function se(r,e){return`calc(${r.replace(A.MATH_SYMBOL,"$1 $2 $3")})`}function oe(r,e){if(!e.enableMath)return r;let n=r;return n=(s=>s.replace(A.MATH,(o,c)=>Z(c,e)))(n),n}function ae(r,e){if(!e.convertLabToRGB&&!e.convertLchToRGB)return r;let n=r;n=ce(n,e);const t=new Map,s=c=>c.replace(A.COLOR,a=>{if(t.has(a))return t.get(a);let l=a;return a.toLowerCase().startsWith("lab(")?e.convertLabToRGB&&(l=le(a,e)):a.toLowerCase().startsWith("lch(")&&e.convertLchToRGB&&(l=ie(a,e)),t.set(a,l),l});let o;do o=n,n=s(n);while(n!==o);return n}function ce(r,e){let n=r;const t=new Map;return n=n.replace(A.HEXLAB,(s,o,c,a)=>{if(t.has(s))return t.get(s);try{const l=parseInt(o,16)/255*100,i=(parseInt(c,16)-128)*1.5,u=(parseInt(a,16)-128)*1.5,p=R(l,i,u,e);return t.set(s,p),p}catch(l){return console.warn(`\u65E0\u6CD5\u89E3\u6790lab#\u5341\u516D\u8FDB\u5236\u989C\u8272: ${s}`,l),s}}),n=n.replace(A.HEXLCH,(s,o,c,a)=>{if(t.has(s))return t.get(s);try{const l=parseInt(o,16)/255*100,i=parseInt(c,16)/255*150,u=parseInt(a)/100*360,p=v(l,i,u),f=R(p.L,p.a,p.b,e);return t.set(s,f),f}catch(l){return console.warn(`\u65E0\u6CD5\u89E3\u6790lch#\u5341\u516D\u8FDB\u5236\u989C\u8272: ${s}`,l),s}}),n}function le(r,e){const n=/lab\(\s*([\d.]+)(%?)\s+([\d.-]+)\s+([\d.-]+)(?:\s*\/\s*([\d.%]+))?\s*\)/i,t=r.match(n);if(!t)return r;try{let s=parseFloat(t[1]);t[2]==="%"?s=s:(s<0&&(s=0),s>100&&(s=100));const o=parseFloat(t[3]),c=parseFloat(t[4]),a=t[5]!==void 0?t[5].includes("%")?parseFloat(t[5])/100:parseFloat(t[5]):null;return R(s,o,c,e,a)}catch(s){return console.warn(`\u65E0\u6CD5\u8F6C\u6362LAB\u989C\u8272: ${r}`,s),r}}function ie(r,e){const n=/lch\(\s*([\d.]+)(%?)\s+([\d.]+)\s+([\d.]+)(deg)?(?:\s*\/\s*([\d.%]+))?\s*\)/i,t=r.match(n);if(!t)return r;try{let s=parseFloat(t[1]);t[2]==="%"?s=s:(s<0&&(s=0),s>100&&(s=100));const o=parseFloat(t[3]);let c=parseFloat(t[4]);o<0&&console.warn(`LCH\u4E2D\u7684C\u503C\u4E0D\u80FD\u4E3A\u8D1F: ${o}`),c=(c%360+360)%360;const a=v(s,o,c),l=t[6]!==void 0?t[6].includes("%")?parseFloat(t[6])/100:parseFloat(t[6]):null;return R(a.L,a.a,a.b,e,l)}catch(s){return console.warn(`\u65E0\u6CD5\u8F6C\u6362LCH\u989C\u8272: ${r}`,s),r}}function v(r,e,n){const t=n*Math.PI/180,s=e*Math.cos(t),o=e*Math.sin(t);return{L:r,a:s,b:o}}function $(r,e,n){const t=(m,g,M)=>{const Q=(m+16)/116,W=g/500+Q,k=Q-M/200,be=W**3>.008856?W**3:(116*W-16)/903.3,ye=m>903.3*.008856?((m+16)/116)**3:m/903.3,Me=k**3>.008856?k**3:(116*k-16)/903.3;return[be*.95047,ye*1,Me*1.08883]},s=(m,g,M)=>{const C=[[3.2404542,-1.5371385,-.4985314],[-.969266,1.8760108,.041556],[.0556434,-.2040259,1.0572252]],P=C[0][0]*m+C[0][1]*g+C[0][2]*M,D=C[1][0]*m+C[1][1]*g+C[1][2]*M,q=C[2][0]*m+C[2][1]*g+C[2][2]*M;return[P,D,q]},o=m=>{const g=m<0?-1:1,M=g*m;return M<=.0031308?12.92*M:g*(1.055*Math.pow(M,.4166666666666667)-.055)},c=m=>Math.max(0,Math.min(255,m*255|0)),[a,l,i]=t(r,e,n),[u,p,f]=s(a,l,i),h=o(u),d=o(p),y=o(f);return{r:c(h),g:c(d),b:c(y)}}function E(r,e,n){const t=(a,l,i)=>{const y=(a+16)/116,m=l/500+y,g=y-i/200,M=m**3>.008856?m**3:(116*m-16)/903.3,C=a>903.3*.008856?((a+16)/116)**3:a/903.3,P=g**3>.008856?g**3:(116*g-16)/903.3;return[M*.95047,C*1,P*1.08883]},s=(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]},o=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]=t(r,e,n),[u,p,f]=s(a,l,i),h=o(u),d=o(p),y=o(f);return{r:Math.max(0,Math.min(1,h)),g:Math.max(0,Math.min(1,d)),b:Math.max(0,Math.min(1,y))}}catch(a){console.warn("P3\u8F6C\u6362\u5931\u8D25:",a);const l=$(r,e,n);return{r:l.r/255,g:l.g/255,b:l.b/255}}}function R(r,e,n,t,s=null){if(!t.enableP3||!I()){const o=$(r,e,n);return s!==null?`rgba(${o.r}, ${o.g}, ${o.b}, ${s})`:`rgb(${o.r}, ${o.g}, ${o.b})`}else{const o=E(r,e,n);return s!==null?`color(display-p3 ${o.r.toFixed(4)} ${o.g.toFixed(4)} ${o.b.toFixed(4)} / ${s})`:`color(display-p3 ${o.r.toFixed(4)} ${o.g.toFixed(4)} ${o.b.toFixed(4)})`}}function T(r,e){let n=r;return e.enableMath&&(n=oe(n,e)),(e.convertLabToRGB||e.convertLchToRGB)&&(n=ae(n,e)),n}function ue(r,e){const n=S(r),t={},s=new Map;let o="",c=!1,a=0,l=null;for(let i of n){const u=i.trim(),p=u.match(/^\$([a-zA-Z_][a-zA-Z0-9_]*)\s*:\s*(.+?);?$/);if(p){const[,f,h]=p,d=T(h.trim(),e);l||(t[f]=d);continue}if(u.endsWith("{")){l=u.slice(0,-1).trim(),c=!0,o+=i+`
|
|
10
|
+
`;continue}if(u==="}"){if(c&&l&&s.has(l)){const f=s.get(l),h=" ".repeat(a);for(const[d,y]of Object.entries(f))o+=`${h} --${d}: ${y};
|
|
11
|
+
`}c=!1,l=null}o+=i+`
|
|
12
|
+
`,i.includes("{")&&(a+=e.indentSize),i.includes("}")&&(a=Math.max(0,a-e.indentSize))}return o=r,{globalVariables:t,selectorVariables:s,cssWithoutVars:o.trim()}}function fe(r,e,n,t){const s=S(r),o=[],c=[];let a=0;for(let l=0,i=s.length;l<i;l++){const u=s[l],p=u.trim();if(!p)continue;const f=u.match(/^(\s*)/)[0].length;if(p==="}"){if(o.length>0){const h=o.pop();if(o.length>0){const d=o[o.length-1];d.children||(d.children=[]),d.children.push(h)}else c.push(h)}continue}if(p.endsWith("{")){const d={selector:p.slice(0,-1).trim(),properties:[],children:[]};o.push(d);continue}if(!p.includes("{")&&!p.includes("}")&&p.includes(":")){if(o.length>0){const h=o[o.length-1];j(p,n,t).forEach(y=>{const m=Object.keys(y)[0],g=T(y[m],e);h.properties.push(g===""?m:`${m}: ${g}`)})}continue}}for(;o.length>0;){const l=o.pop();if(o.length===0)c.push(l);else{const i=o[o.length-1];i.children||(i.children=[]),i.children.push(l)}}return G(c,e,"",n,t)}function G(r,e,n="",t,s,o=!1){let c="";for(const a of r){const l=a.selector.startsWith("@");l&&(c+=a.selector+` {
|
|
13
|
+
`);let i=a.selector;if(n&&(i.includes("&")?i=i.replace(A.AND,n):i.trim().startsWith(":")?i=n+i:i=n+" "+i),i=i,a.properties.length>0){c+=(o?" ".repeat(e.indentSize):"")+i+` {
|
|
14
|
+
`;for(const u of a.properties)j(u,t,s).forEach(f=>{const h=Object.keys(f)[0],d=T(f[h],e);c+=(o?" ".repeat(e.indentSize):"")+" ".repeat(e.indentSize)+(d===""?h:`${h}: ${d};
|
|
15
|
+
`)});c+=o?" ".repeat(e.indentSize)+`}
|
|
16
16
|
`:`}
|
|
17
17
|
|
|
18
|
-
`}a.children&&a.children.length>0&&(
|
|
18
|
+
`}a.children&&a.children.length>0&&(c+=G(a.children,e,l?"":i,t,s,o||l)),l&&(c+=`}
|
|
19
19
|
|
|
20
|
-
`)}return n.
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
`);return`${r.rootSelector} {
|
|
24
|
-
${o}
|
|
20
|
+
`)}return c}function pe(r,e){let n=r;return n=n.replace(A.VARIABLE,(t,s)=>t.startsWith("$$")?`attr(${s})`:`var(--${s})`),n=T(n,e),n}function he(r,e){if(Object.keys(r).length===0)return"";const n=Object.entries(r).map(([t,s])=>{const o=T(s,e);return" ".repeat(e.indentSize)+`--${t}: ${o};`}).join(`
|
|
21
|
+
`);return`${e.rootSelector} {
|
|
22
|
+
${n}
|
|
25
23
|
}
|
|
26
24
|
|
|
27
|
-
`}function
|
|
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}
|
|
25
|
+
`}async function X(r,e){const n=A.IMPORT;return(async s=>{const o=[],c=s.replace(n,(a,l)=>{const i=de(l,e).then(u=>X(u,e)).catch(u=>(console.warn(`\u65E0\u6CD5\u5BFC\u5165CSS\u6587\u4EF6: ${l}`,u),""));return o.push(i),`__IMPORT_PLACEHOLDER_${o.length-1}__`});if(o.length>0){const a=await Promise.all(o);let l=c;for(let i=a.length;i--;)l=l.replace(`__IMPORT_PLACEHOLDER_${i}__`,a[i]);return l}return c})(r)}async function de(r,e){if(e.importCache&&B.has(r))return B.get(r);const n=e.importBaseUrl?new URL(r,e.importBaseUrl).href:r;let t;if(typeof process<"u"&&process.versions&&process.versions.node&&typeof require<"u")try{const o=require("fs"),c=require("path");let a=n;n.startsWith(".")&&(a=c.join(process.cwd(),n)),t=o.readFileSync(a,"utf-8")}catch(o){throw new Error(`Node.js\u6587\u4EF6\u8BFB\u53D6\u5931\u8D25: ${n} - ${o.message}`)}else try{const o=new AbortController,c=setTimeout(()=>o.abort(),e.importTimeout),a=await fetch(n,{signal:o.signal,headers:{Accept:"text/css"}});if(clearTimeout(c),!a.ok)throw new Error(`HTTP\u9519\u8BEF: ${a.status} ${a.statusText}`);t=await a.text()}catch(o){throw o.name==="AbortError"?new Error(`\u5BFC\u5165\u8D85\u65F6: ${n}`):new Error(`\u65E0\u6CD5\u83B7\u53D6CSS: ${n} - ${o.message}`)}return e.importCache&&B.set(r,t),t}async function H(r,e={}){let{config:n,css:t}=K(r);const s={...b,...e,...n};t=await X(t,s);let o=t,c=new Map;if(s.enableAlias){const{aliases:g,css:M}=J(o);c=g,o=M}let a=o,l=new Map;if(s.enableMacros){const{macros:g,css:M}=N(a,s);l=g,a=M;for(const[C,P]of l)w.set(C,P)}let i=a;s.enableMacros&&l.size>0&&(i=ee(i,l,s));const{globalVariables:u,selectorVariables:p,cssWithoutVars:f}=ue(i,s);let h=f.trim();if(s.enableNesting&&f.includes("{"))try{h=fe(f,s,c,l)}catch(g){console.warn("\u5D4C\u5957\u89E3\u6790\u5931\u8D25\uFF0C\u4F7F\u7528\u539F\u59CBCSS:",g)}const d=he(u,s);let y=h;y=pe(y,s);let m=d+y;for(const[g,M]of L)try{m=M.convert(m,s)}catch(C){console.error("\u63D2\u4EF6\u5904\u7406\u5931\u8D25:",C)}return m}function U(r={}){const e={...b,...r},n=document.querySelectorAll(`style[${e.styleTagAttribute||"e"}]`);return n.forEach(t=>{let s=t.textContent;(async()=>{try{t.getAttribute("src")&&(s="@import "+t.getAttribute("src")+";");const c=await H(s,e),a=document.createElement("style");a.textContent=c,t.parentNode.insertBefore(a,t.nextSibling),e.preserveOriginal?t.style.display="none":t.remove()}catch(c){console.error("\u5904\u7406style\u6807\u7B7E\u5931\u8D25:",c)}})()}),n.length}function Y(r={}){b={...b,...r}}function V(r,e={}){const n={...b,...e};if(r)return(async()=>{try{const t=await H(r,n),s=document.createElement("style");return s.textContent=t,document.head.appendChild(s),s}catch(t){return console.error("\u5E94\u7528CSS\u5931\u8D25:",t),null}})();document.readyState==="loading"?document.addEventListener("DOMContentLoaded",()=>{U(n)}):U(n)}const me={convert:H,apply:V,config:Y,supportsP3:I(),detectP3Support:I,imports:{clearCache:function(){B.clear()},setBaseUrl:function(r){b.importBaseUrl=r},setCacheEnabled:function(r){b.importCache=r},setTimeout:function(r){b.importTimeout=r}},plugins:{use(r){const e=new r;return L.set(e.name??r.name,e),!0},remove(r){return L.get(r)?.destroy&&L.get(r).destroy(),L.delete(r),!0},getAll:function(){return Array.from(L.entries())},clear:function(){L.clear()}},aliases:{add:function(r,e){_.set(r,e)},remove:function(r){_.delete(r)},getAll:function(){return Array.from(_.entries())},clear:function(){_.clear()}},macros:{define:function(r,e,n=[]){if(typeof e=="function"){const s=e.toString().match(/{([\s\S]*)}/);s&&w.set(r,{params:n.map(o=>typeof o=="string"?{name:o}:o),body:s[1].trim()})}else w.set(r,{params:n.map(t=>typeof t=="string"?{name:t}:t),body:e})},call:function(r,...e){const n=w.get(r);if(!n)throw new Error(`\u672A\u5B9A\u4E49\u7684\u5B8F: ${r}`);const t=new Map;for(let s=0,o=n.params.length;s<o;s++){const c=n.params[s],a=s<e.length?e[s]:c.defaultValue;if(a===void 0&&c.defaultValue===null)throw new Error(`\u7F3A\u5C11\u5FC5\u9700\u53C2\u6570: ${c.name}`);t.set(c.name,a!==void 0?a:"")}return O(n.body,t,b)},getAll:function(){return Array.from(w.entries())},remove:function(r){w.delete(r)},clear:function(){w.clear()},parse:function(r){const{macros:e}=N(r,b);for(const[n,t]of e)w.set(n,t)}},math:{evaluate:function(r){return Z(r,b)}},colorUtils:{labToRGB:$,lchToLab:v,lchToRGB:function(r,e,n){const t=v(r,e,n);return $(t.L,t.a,t.b)},labToP3:E,lchToP3:function(r,e,n){const t=v(r,e,n);return E(t.L,t.a,t.b)},parseHexLab:function(r){const e=r.match(/lab#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/i);if(!e)return null;const n=e[1],t=e[2],s=e[3],o=parseInt(n,16)/255*100,c=(parseInt(t,16)-128)*1.5,a=(parseInt(s,16)-128)*1.5;return $(o,c,a)},parseHexLch:function(r){const e=r.match(/lch#([0-9a-f]{2})([0-9a-f]{2})(\d{1,3})/i);if(!e)return null;const n=e[1],t=e[2],s=e[3],o=parseInt(n,16)/255*100,c=parseInt(t,16)/255*150,a=parseInt(s)/100*360,l=v(o,c,a);return $(l.L,l.a,l.b)},generateColor:function(r,e,n,t=null,s=!0){return R(r,e,n,{enableP3:s},t)},parseColor:function(r){try{const e=r.match(/lab\(\s*([\d.]+)(%?)\s+([\d.-]+)\s+([\d.-]+)(?:\s*\/\s*([\d.%]+))?\s*\)/i);if(e){let t=parseFloat(e[1]);const s=parseFloat(e[3]),o=parseFloat(e[4]),c=e[5]?e[5].includes("%")?parseFloat(e[5])/100:parseFloat(e[5]):null,a=R(t,s,o,b,c);return{L:t,A:s,B:o,alpha:c,rgb:$(t,s,o),p3:E(t,s,o),colorString:a}}const n=r.match(/lch\(\s*([\d.]+)(%?)\s+([\d.]+)\s+([\d.]+)(deg)?(?:\s*\/\s*([\d.%]+))?\s*\)/i);if(n){let t=parseFloat(n[1]);const s=parseFloat(n[3]);let o=parseFloat(n[4]);const c=n[6]?n[6].includes("%")?parseFloat(n[6])/100:parseFloat(n[6]):null,a=v(t,s,o),l=R(a.L,a.a,a.b,b,c);return{L:t,C:s,H:o,alpha:c,lab:a,rgb:$(a.L,a.a,a.b),p3:E(a.L,a.a,a.b),colorString:l}}return null}catch(e){return console.warn("\u65E0\u6CD5\u89E3\u6790\u989C\u8272:",r,e),null}}}},F=async function(...r){if(r.length>1||r[0]&&r[0].raw)return await ge(...r);const e=r[0];if(typeof e=="string"){const n=await H(e,{...b,...r[1]});return n&&typeof n.then=="function",n}return typeof e=="object"&&e!==null?(b={...b,...e},F):r.length===0?V():F};async function ge(r,...e){let n=r[0];for(let s=0,o=e.length;s<o;s++){const c=e[s];let a="";typeof c=="function"?a=c():Array.isArray(c)?a=c.join(" "):a=String(c??""),n+=a+r[s+1]}const t=await H(n,b);return t&&typeof t.then=="function",t}return Object.assign(F,me),typeof window<"u"&&(V(),Object.defineProperty(window.HTMLElement.prototype,"cssVar",{get(){const r=this;return new Proxy(()=>{},{get(e,n){const t=n.startsWith("--")?n:`--${n}`;return r.style.getPropertyValue(t)},set(e,n,t){const s=n.startsWith("--")?n:`--${n}`;return r.style.setProperty(s,t),!0},apply(e,n,t){const s=t[0],o=t[1],c=s.startsWith("--")?s:`--${s}`;if(o===void 0)return r.style.getPropertyValue(c);r.style.setProperty(c,o)}})}})),F})();if(typeof define=="function"&&define.amd)define([],b=>styimat);else if(typeof module=="object"&&module.exports)module.exports=styimat;else{const b=globalThis??(typeof self<"u"&&self)??(typeof window<"u"&&window)??global??{};b.styimat=styimat}
|
package/dist/styimat.min.mjs
CHANGED
|
@@ -1,28 +1,25 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* MIT License
|
|
3
3
|
* Copyright (c) 2025 王小玗
|
|
4
|
-
*/const
|
|
5
|
-
`){const
|
|
6
|
-
`)}}function ee(
|
|
7
|
-
`)}}function j(
|
|
8
|
-
`)}),
|
|
9
|
-
`)}}function te(
|
|
10
|
-
`;continue}if(u==="}"){if(
|
|
11
|
-
`}
|
|
12
|
-
`,i.includes("{")&&(
|
|
13
|
-
`)
|
|
14
|
-
`;for(const u of a.properties)
|
|
15
|
-
`)});
|
|
4
|
+
*/const B=(function(){let b={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 A={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,VARIABLE:/(?:\$\$|\$)([a-zA-Z_][a-zA-Z0-9_]*)(?=[^a-zA-Z0-9_])/g};let R=null;const _=new Map,O=new Map,w=new Map,L=new Map;function S(r,e=`
|
|
5
|
+
`){const n=[],t=e.charCodeAt(0);let s=0;for(let o=0,c=r.length;o<c;o++)r.charCodeAt(o)===t&&((o>s||r.charAt(s)!=="\r")&&n.push(r.slice(s,o)),s=o+1);return s<r.length?n.push(r.substring(s)):s===r.length&&r.charCodeAt(r.length-1)===10&&n.push(""),n}function J(r){const e={...b},n=S(r),t=[];for(let s of n){const o=s.trim();if(o.startsWith("#")){const c=o.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"?e[u]=i==="true":!isNaN(i)&&i.trim()!==""?e[u]=Number(i):e[u]=i}else console.warn(`\u65E0\u6548\u7684\u914D\u7F6E\u884C: ${o}`)}else t.push(s)}return{config:e,css:t.join(`
|
|
6
|
+
`)}}function ee(r){const e=new Map,n=S(r),t=[];for(let s of n){const o=s.trim();if(o.startsWith("@alias")){const c=o.match(A.ALIAS);if(c){const a=c[1],l=c[2];e.set(a,l);continue}}t.push(s)}return{aliases:e,css:t.join(`
|
|
7
|
+
`)}}function j(r,e){if(!e.enableMacros)return{macros:new Map,css:r};const n=new Map,t=S(r),s=[];let o=!1,c="",a=[],l=[],i=0;for(let u of t){const p=u.trim();if(!o&&p.startsWith("@macro")){const f=p.match(A.MACRO);if(f){o=!0,c=f[1],a=S(f[2],",").map(h=>h.trim()).filter(h=>h).map(h=>{const d=S(h,":").map(y=>y.trim());return{name:d[0].slice(1),defaultValue:d[1]||null}}),l=[],i=1;continue}}if(o){for(const f of u)f==="{"&&i++,f==="}"&&i--;i===0?(n.set(c,{params:a,body:l.join(`
|
|
8
|
+
`)}),o=!1,c="",a=[],l=[]):l.push(u);continue}s.push(u)}return{macros:n,css:s.join(`
|
|
9
|
+
`)}}function te(r,e,n){if(!n.enableMacros||e.size===0)return r;const t=Array.from(e.keys()).map(c=>c.replace(A.SPECHAR,"\\$&")).join("|"),s=new RegExp(`@(${t})\\s*\\(([^)]*)\\)\\s*;?`,"g");return(c=>{let a=c,l=!1;do l=!1,a=a.replace(s,(i,u,p)=>{l=!0;const f=e.get(u);if(!f)return console.warn(`\u672A\u5B9A\u4E49\u7684\u5B8F: ${u}`),i;const h=I(p,f.params);return F(f.body,h,n)});while(l);return a})(r)}function I(r,e){const n=new Map,t=ne(r);for(let s=0,o=e.length;s<o;s++){const c=e[s];let a;s<t.length?a=t[s]:c.defaultValue!==null?a=c.defaultValue:a="none",n.set(c.name,a)}return n}function ne(r){const e=[];let n="",t=!1,s="",o=0,c=0;for(let a=0,l=r.length;a<l;a++){const i=r[a];(i==='"'||i==="'")&&!t?(t=!0,s=i):i===s&&t&&(t=!1,s=""),t||(i==="("&&o++,i===")"&&o--,i==="["&&c++,i==="]"&&c--),i===","&&!t&&o===0&&c===0?(e.push(n.trim()),n=""):n+=i}return n.trim()&&e.push(n.trim()),e}function F(r,e,n){let t=r;const s=/\$([a-zA-Z_][a-zA-Z0-9_]*)/g;if(t=t.replace(s,(o,c)=>e.get(c)?e.get(c):o),n.enableMacros){const o=Array.from(w.keys()).map(c=>c.replace(A.SPECHAR,"\\$&")).join("|");if(o){const c=new RegExp(`@(${o})\\s*\\(([^)]*)\\)\\s*;?`,"g");let a;do a=!1,t=t.replace(c,(l,i,u)=>{a=!0;const p=w.get(i);if(!p)return l;const f=I(u,p.params);return F(p.body,f,n)});while(a)}}return t=P(t,n),t}function re(r){return r.replace(A.HYPHEN,function(e,n){return n.toUpperCase()})}function V(){if(R!==null)return R;if(typeof window>"u"||!window.CSS)return R=!1,!1;try{R=CSS.supports("color","color(display-p3 1 0 0)")}catch{R=!1}return R}function Z(r,e,n){let t=r.trim();if(t.endsWith(";")&&(t=t.slice(0,-1)),!t)return[];const s=[];let o="",c=0,a=!1,l="";for(let u=0,p=t.length;u<p;u++){const f=t[u];(f==='"'||f==="'")&&!a?(a=!0,l=f):f===l&&a&&(a=!1,l=""),a||(f==="("?c++:f===")"&&c--),f===";"&&!a&&c===0?o.trim()&&(s.push(o.trim()),o=""):o+=f}o.trim()&&s.push(o.trim());const i=[];for(let u of s){if(u=u.replace(A.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.startsWith("$")&&(f="--"+f.slice(1)),f=e.get(f)??f,f.startsWith("@")&&n.get(f.slice(1))){const d=n.get(f.slice(1)),y=I(S(h," ").join(","),d.params);f=F(d.body,y,D),h=""}i.push({[f]:h})}return i}function se(r){let e=!1,n="";for(let t=0,s=r.length;t<s;t++){const o=r[t];if((o==='"'||o==="'")&&!e?(e=!0,n=o):o===n&&e&&(e=!1,n=""),o===":"&&!e)return t}return-1}function G(r,e){if(!e.enableMath)return`math(${r})`;let n=r.replace(A.SPACE,"");return oe(n,e.enableMath)}function oe(r,e){return`calc(${r.replace(A.MATH_SYMBOL,"$1 $2 $3")})`}function ae(r,e){if(!e.enableMath)return r;let n=r;return n=(s=>s.replace(A.MATH,(o,c)=>G(c,e)))(n),n}function ce(r,e){if(!e.convertLabToRGB&&!e.convertLchToRGB)return r;let n=r;n=le(n,e);const t=new Map,s=c=>c.replace(A.COLOR,a=>{if(t.has(a))return t.get(a);let l=a;return a.toLowerCase().startsWith("lab(")?e.convertLabToRGB&&(l=ie(a,e)):a.toLowerCase().startsWith("lch(")&&e.convertLchToRGB&&(l=ue(a,e)),t.set(a,l),l});let o;do o=n,n=s(n);while(n!==o);return n}function le(r,e){let n=r;const t=new Map;return n=n.replace(A.HEXLAB,(s,o,c,a)=>{if(t.has(s))return t.get(s);try{const l=parseInt(o,16)/255*100,i=(parseInt(c,16)-128)*1.5,u=(parseInt(a,16)-128)*1.5,p=x(l,i,u,e);return t.set(s,p),p}catch(l){return console.warn(`\u65E0\u6CD5\u89E3\u6790lab#\u5341\u516D\u8FDB\u5236\u989C\u8272: ${s}`,l),s}}),n=n.replace(A.HEXLCH,(s,o,c,a)=>{if(t.has(s))return t.get(s);try{const l=parseInt(o,16)/255*100,i=parseInt(c,16)/255*150,u=parseInt(a)/100*360,p=v(l,i,u),f=x(p.L,p.a,p.b,e);return t.set(s,f),f}catch(l){return console.warn(`\u65E0\u6CD5\u89E3\u6790lch#\u5341\u516D\u8FDB\u5236\u989C\u8272: ${s}`,l),s}}),n}function ie(r,e){const n=/lab\(\s*([\d.]+)(%?)\s+([\d.-]+)\s+([\d.-]+)(?:\s*\/\s*([\d.%]+))?\s*\)/i,t=r.match(n);if(!t)return r;try{let s=parseFloat(t[1]);t[2]==="%"?s=s:(s<0&&(s=0),s>100&&(s=100));const o=parseFloat(t[3]),c=parseFloat(t[4]),a=t[5]!==void 0?t[5].includes("%")?parseFloat(t[5])/100:parseFloat(t[5]):null;return x(s,o,c,e,a)}catch(s){return console.warn(`\u65E0\u6CD5\u8F6C\u6362LAB\u989C\u8272: ${r}`,s),r}}function ue(r,e){const n=/lch\(\s*([\d.]+)(%?)\s+([\d.]+)\s+([\d.]+)(deg)?(?:\s*\/\s*([\d.%]+))?\s*\)/i,t=r.match(n);if(!t)return r;try{let s=parseFloat(t[1]);t[2]==="%"?s=s:(s<0&&(s=0),s>100&&(s=100));const o=parseFloat(t[3]);let c=parseFloat(t[4]);o<0&&console.warn(`LCH\u4E2D\u7684C\u503C\u4E0D\u80FD\u4E3A\u8D1F: ${o}`),c=(c%360+360)%360;const a=v(s,o,c),l=t[6]!==void 0?t[6].includes("%")?parseFloat(t[6])/100:parseFloat(t[6]):null;return x(a.L,a.a,a.b,e,l)}catch(s){return console.warn(`\u65E0\u6CD5\u8F6C\u6362LCH\u989C\u8272: ${r}`,s),r}}function v(r,e,n){const t=n*Math.PI/180,s=e*Math.cos(t),o=e*Math.sin(t);return{L:r,a:s,b:o}}function $(r,e,n){const t=(m,g,M)=>{const K=(m+16)/116,k=g/500+K,N=K-M/200,ye=k**3>.008856?k**3:(116*k-16)/903.3,Me=m>903.3*.008856?((m+16)/116)**3:m/903.3,Ce=N**3>.008856?N**3:(116*N-16)/903.3;return[ye*.95047,Me*1,Ce*1.08883]},s=(m,g,M)=>{const C=[[3.2404542,-1.5371385,-.4985314],[-.969266,1.8760108,.041556],[.0556434,-.2040259,1.0572252]],T=C[0][0]*m+C[0][1]*g+C[0][2]*M,q=C[1][0]*m+C[1][1]*g+C[1][2]*M,Q=C[2][0]*m+C[2][1]*g+C[2][2]*M;return[T,q,Q]},o=m=>{const g=m<0?-1:1,M=g*m;return M<=.0031308?12.92*M:g*(1.055*Math.pow(M,.4166666666666667)-.055)},c=m=>Math.max(0,Math.min(255,m*255|0)),[a,l,i]=t(r,e,n),[u,p,f]=s(a,l,i),h=o(u),d=o(p),y=o(f);return{r:c(h),g:c(d),b:c(y)}}function E(r,e,n){const t=(a,l,i)=>{const y=(a+16)/116,m=l/500+y,g=y-i/200,M=m**3>.008856?m**3:(116*m-16)/903.3,C=a>903.3*.008856?((a+16)/116)**3:a/903.3,T=g**3>.008856?g**3:(116*g-16)/903.3;return[M*.95047,C*1,T*1.08883]},s=(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]},o=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]=t(r,e,n),[u,p,f]=s(a,l,i),h=o(u),d=o(p),y=o(f);return{r:Math.max(0,Math.min(1,h)),g:Math.max(0,Math.min(1,d)),b:Math.max(0,Math.min(1,y))}}catch(a){console.warn("P3\u8F6C\u6362\u5931\u8D25:",a);const l=$(r,e,n);return{r:l.r/255,g:l.g/255,b:l.b/255}}}function x(r,e,n,t,s=null){if(!t.enableP3||!V()){const o=$(r,e,n);return s!==null?`rgba(${o.r}, ${o.g}, ${o.b}, ${s})`:`rgb(${o.r}, ${o.g}, ${o.b})`}else{const o=E(r,e,n);return s!==null?`color(display-p3 ${o.r.toFixed(4)} ${o.g.toFixed(4)} ${o.b.toFixed(4)} / ${s})`:`color(display-p3 ${o.r.toFixed(4)} ${o.g.toFixed(4)} ${o.b.toFixed(4)})`}}function P(r,e){let n=r;return e.enableMath&&(n=ae(n,e)),(e.convertLabToRGB||e.convertLchToRGB)&&(n=ce(n,e)),n}function fe(r,e){const n=S(r),t={},s=new Map;let o="",c=!1,a=0,l=null;for(let i of n){const u=i.trim(),p=u.match(/^\$([a-zA-Z_][a-zA-Z0-9_]*)\s*:\s*(.+?);?$/);if(p){const[,f,h]=p,d=P(h.trim(),e);l||(t[f]=d);continue}if(u.endsWith("{")){l=u.slice(0,-1).trim(),c=!0,o+=i+`
|
|
10
|
+
`;continue}if(u==="}"){if(c&&l&&s.has(l)){const f=s.get(l),h=" ".repeat(a);for(const[d,y]of Object.entries(f))o+=`${h} --${d}: ${y};
|
|
11
|
+
`}c=!1,l=null}o+=i+`
|
|
12
|
+
`,i.includes("{")&&(a+=e.indentSize),i.includes("}")&&(a=Math.max(0,a-e.indentSize))}return o=r,{globalVariables:t,selectorVariables:s,cssWithoutVars:o.trim()}}function pe(r,e,n,t){const s=S(r),o=[],c=[];let a=0;for(let l=0,i=s.length;l<i;l++){const u=s[l],p=u.trim();if(!p)continue;const f=u.match(/^(\s*)/)[0].length;if(p==="}"){if(o.length>0){const h=o.pop();if(o.length>0){const d=o[o.length-1];d.children||(d.children=[]),d.children.push(h)}else c.push(h)}continue}if(p.endsWith("{")){const d={selector:p.slice(0,-1).trim(),properties:[],children:[]};o.push(d);continue}if(!p.includes("{")&&!p.includes("}")&&p.includes(":")){if(o.length>0){const h=o[o.length-1];Z(p,n,t).forEach(y=>{const m=Object.keys(y)[0],g=P(y[m],e);h.properties.push(g===""?m:`${m}: ${g}`)})}continue}}for(;o.length>0;){const l=o.pop();if(o.length===0)c.push(l);else{const i=o[o.length-1];i.children||(i.children=[]),i.children.push(l)}}return X(c,e,"",n,t)}function X(r,e,n="",t,s,o=!1){let c="";for(const a of r){const l=a.selector.startsWith("@");l&&(c+=a.selector+` {
|
|
13
|
+
`);let i=a.selector;if(n&&(i.includes("&")?i=i.replace(A.AND,n):i.trim().startsWith(":")?i=n+i:i=n+" "+i),i=i,a.properties.length>0){c+=(o?" ".repeat(e.indentSize):"")+i+` {
|
|
14
|
+
`;for(const u of a.properties)Z(u,t,s).forEach(f=>{const h=Object.keys(f)[0],d=P(f[h],e);c+=(o?" ".repeat(e.indentSize):"")+" ".repeat(e.indentSize)+(d===""?h:`${h}: ${d};
|
|
15
|
+
`)});c+=o?" ".repeat(e.indentSize)+`}
|
|
16
16
|
`:`}
|
|
17
17
|
|
|
18
|
-
`}a.children&&a.children.length>0&&(
|
|
18
|
+
`}a.children&&a.children.length>0&&(c+=X(a.children,e,l?"":i,t,s,o||l)),l&&(c+=`}
|
|
19
19
|
|
|
20
|
-
`)}return n.
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
`);return`${r.rootSelector} {
|
|
24
|
-
${o}
|
|
20
|
+
`)}return c}function he(r,e){let n=r;return n=n.replace(A.VARIABLE,(t,s)=>t.startsWith("$$")?`attr(${s})`:`var(--${s})`),n=P(n,e),n}function de(r,e){if(Object.keys(r).length===0)return"";const n=Object.entries(r).map(([t,s])=>{const o=P(s,e);return" ".repeat(e.indentSize)+`--${t}: ${o};`}).join(`
|
|
21
|
+
`);return`${e.rootSelector} {
|
|
22
|
+
${n}
|
|
25
23
|
}
|
|
26
24
|
|
|
27
|
-
`}function
|
|
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;
|
|
25
|
+
`}async function U(r,e){const n=A.IMPORT;return(async s=>{const o=[],c=s.replace(n,(a,l)=>{const i=me(l,e).then(u=>U(u,e)).catch(u=>(console.warn(`\u65E0\u6CD5\u5BFC\u5165CSS\u6587\u4EF6: ${l}`,u),""));return o.push(i),`__IMPORT_PLACEHOLDER_${o.length-1}__`});if(o.length>0){const a=await Promise.all(o);let l=c;for(let i=a.length;i--;)l=l.replace(`__IMPORT_PLACEHOLDER_${i}__`,a[i]);return l}return c})(r)}async function me(r,e){if(e.importCache&&_.has(r))return _.get(r);const n=e.importBaseUrl?new URL(r,e.importBaseUrl).href:r;let t;if(typeof process<"u"&&process.versions&&process.versions.node&&typeof require<"u")try{const o=require("fs"),c=require("path");let a=n;n.startsWith(".")&&(a=c.join(process.cwd(),n)),t=o.readFileSync(a,"utf-8")}catch(o){throw new Error(`Node.js\u6587\u4EF6\u8BFB\u53D6\u5931\u8D25: ${n} - ${o.message}`)}else try{const o=new AbortController,c=setTimeout(()=>o.abort(),e.importTimeout),a=await fetch(n,{signal:o.signal,headers:{Accept:"text/css"}});if(clearTimeout(c),!a.ok)throw new Error(`HTTP\u9519\u8BEF: ${a.status} ${a.statusText}`);t=await a.text()}catch(o){throw o.name==="AbortError"?new Error(`\u5BFC\u5165\u8D85\u65F6: ${n}`):new Error(`\u65E0\u6CD5\u83B7\u53D6CSS: ${n} - ${o.message}`)}return e.importCache&&_.set(r,t),t}async function H(r,e={}){let{config:n,css:t}=J(r);const s={...b,...e,...n};t=await U(t,s);let o=t,c=new Map;if(s.enableAlias){const{aliases:g,css:M}=ee(o);c=g,o=M}let a=o,l=new Map;if(s.enableMacros){const{macros:g,css:M}=j(a,s);l=g,a=M;for(const[C,T]of l)w.set(C,T)}let i=a;s.enableMacros&&l.size>0&&(i=te(i,l,s));const{globalVariables:u,selectorVariables:p,cssWithoutVars:f}=fe(i,s);let h=f.trim();if(s.enableNesting&&f.includes("{"))try{h=pe(f,s,c,l)}catch(g){console.warn("\u5D4C\u5957\u89E3\u6790\u5931\u8D25\uFF0C\u4F7F\u7528\u539F\u59CBCSS:",g)}const d=de(u,s);let y=h;y=he(y,s);let m=d+y;for(const[g,M]of L)try{m=M.convert(m,s)}catch(C){console.error("\u63D2\u4EF6\u5904\u7406\u5931\u8D25:",C)}return m}function Y(r={}){const e={...b,...r},n=document.querySelectorAll(`style[${e.styleTagAttribute||"e"}]`);return n.forEach(t=>{let s=t.textContent;(async()=>{try{t.getAttribute("src")&&(s="@import "+t.getAttribute("src")+";");const c=await H(s,e),a=document.createElement("style");a.textContent=c,t.parentNode.insertBefore(a,t.nextSibling),e.preserveOriginal?t.style.display="none":t.remove()}catch(c){console.error("\u5904\u7406style\u6807\u7B7E\u5931\u8D25:",c)}})()}),n.length}function D(r={}){b={...b,...r}}function W(r,e={}){const n={...b,...e};if(r)return(async()=>{try{const t=await H(r,n),s=document.createElement("style");return s.textContent=t,document.head.appendChild(s),s}catch(t){return console.error("\u5E94\u7528CSS\u5931\u8D25:",t),null}})();document.readyState==="loading"?document.addEventListener("DOMContentLoaded",()=>{Y(n)}):Y(n)}const ge={convert:H,apply:W,config:D,supportsP3:V(),detectP3Support:V,imports:{clearCache:function(){_.clear()},setBaseUrl:function(r){b.importBaseUrl=r},setCacheEnabled:function(r){b.importCache=r},setTimeout:function(r){b.importTimeout=r}},plugins:{use(r){const e=new r;return L.set(e.name??r.name,e),!0},remove(r){return L.get(r)?.destroy&&L.get(r).destroy(),L.delete(r),!0},getAll:function(){return Array.from(L.entries())},clear:function(){L.clear()}},aliases:{add:function(r,e){O.set(r,e)},remove:function(r){O.delete(r)},getAll:function(){return Array.from(O.entries())},clear:function(){O.clear()}},macros:{define:function(r,e,n=[]){if(typeof e=="function"){const s=e.toString().match(/{([\s\S]*)}/);s&&w.set(r,{params:n.map(o=>typeof o=="string"?{name:o}:o),body:s[1].trim()})}else w.set(r,{params:n.map(t=>typeof t=="string"?{name:t}:t),body:e})},call:function(r,...e){const n=w.get(r);if(!n)throw new Error(`\u672A\u5B9A\u4E49\u7684\u5B8F: ${r}`);const t=new Map;for(let s=0,o=n.params.length;s<o;s++){const c=n.params[s],a=s<e.length?e[s]:c.defaultValue;if(a===void 0&&c.defaultValue===null)throw new Error(`\u7F3A\u5C11\u5FC5\u9700\u53C2\u6570: ${c.name}`);t.set(c.name,a!==void 0?a:"")}return F(n.body,t,b)},getAll:function(){return Array.from(w.entries())},remove:function(r){w.delete(r)},clear:function(){w.clear()},parse:function(r){const{macros:e}=j(r,b);for(const[n,t]of e)w.set(n,t)}},math:{evaluate:function(r){return G(r,b)}},colorUtils:{labToRGB:$,lchToLab:v,lchToRGB:function(r,e,n){const t=v(r,e,n);return $(t.L,t.a,t.b)},labToP3:E,lchToP3:function(r,e,n){const t=v(r,e,n);return E(t.L,t.a,t.b)},parseHexLab:function(r){const e=r.match(/lab#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/i);if(!e)return null;const n=e[1],t=e[2],s=e[3],o=parseInt(n,16)/255*100,c=(parseInt(t,16)-128)*1.5,a=(parseInt(s,16)-128)*1.5;return $(o,c,a)},parseHexLch:function(r){const e=r.match(/lch#([0-9a-f]{2})([0-9a-f]{2})(\d{1,3})/i);if(!e)return null;const n=e[1],t=e[2],s=e[3],o=parseInt(n,16)/255*100,c=parseInt(t,16)/255*150,a=parseInt(s)/100*360,l=v(o,c,a);return $(l.L,l.a,l.b)},generateColor:function(r,e,n,t=null,s=!0){return x(r,e,n,{enableP3:s},t)},parseColor:function(r){try{const e=r.match(/lab\(\s*([\d.]+)(%?)\s+([\d.-]+)\s+([\d.-]+)(?:\s*\/\s*([\d.%]+))?\s*\)/i);if(e){let t=parseFloat(e[1]);const s=parseFloat(e[3]),o=parseFloat(e[4]),c=e[5]?e[5].includes("%")?parseFloat(e[5])/100:parseFloat(e[5]):null,a=x(t,s,o,b,c);return{L:t,A:s,B:o,alpha:c,rgb:$(t,s,o),p3:E(t,s,o),colorString:a}}const n=r.match(/lch\(\s*([\d.]+)(%?)\s+([\d.]+)\s+([\d.]+)(deg)?(?:\s*\/\s*([\d.%]+))?\s*\)/i);if(n){let t=parseFloat(n[1]);const s=parseFloat(n[3]);let o=parseFloat(n[4]);const c=n[6]?n[6].includes("%")?parseFloat(n[6])/100:parseFloat(n[6]):null,a=v(t,s,o),l=x(a.L,a.a,a.b,b,c);return{L:t,C:s,H:o,alpha:c,lab:a,rgb:$(a.L,a.a,a.b),p3:E(a.L,a.a,a.b),colorString:l}}return null}catch(e){return console.warn("\u65E0\u6CD5\u89E3\u6790\u989C\u8272:",r,e),null}}}},z=async function(...r){if(r.length>1||r[0]&&r[0].raw)return await be(...r);const e=r[0];if(typeof e=="string"){const n=await H(e,{...b,...r[1]});return n&&typeof n.then=="function",n}return typeof e=="object"&&e!==null?(b={...b,...e},z):r.length===0?W():z};async function be(r,...e){let n=r[0];for(let s=0,o=e.length;s<o;s++){const c=e[s];let a="";typeof c=="function"?a=c():Array.isArray(c)?a=c.join(" "):a=String(c??""),n+=a+r[s+1]}const t=await H(n,b);return t&&typeof t.then=="function",t}return Object.assign(z,ge),typeof window<"u"&&(W(),Object.defineProperty(window.HTMLElement.prototype,"cssVar",{get(){const r=this;return new Proxy(()=>{},{get(e,n){const t=n.startsWith("--")?n:`--${n}`;return r.style.getPropertyValue(t)},set(e,n,t){const s=n.startsWith("--")?n:`--${n}`;return r.style.setProperty(s,t),!0},apply(e,n,t){const s=t[0],o=t[1],c=s.startsWith("--")?s:`--${s}`;if(o===void 0)return r.style.getPropertyValue(c);r.style.setProperty(c,o)}})}})),z})();if(typeof define=="function"&&define.amd)define([],b=>B);else if(typeof module=="object"&&module.exports)module.exports=B;else{const b=globalThis??(typeof self<"u"&&self)??(typeof window<"u"&&window)??global??{};b.styimat=B}export default B;export const{convert,apply,config,supportsP3,detectP3Support,imports,plugins,aliases,macros,math,colorUtils}=B;
|
package/dist/styimat.mjs
CHANGED
|
@@ -50,7 +50,8 @@ const styimat = (function() {
|
|
|
50
50
|
HEXLCH: /lch#([0-9a-f]{2})([0-9a-f]{2})(\d{1,3})/gi,
|
|
51
51
|
SPACE: /\s+/g,
|
|
52
52
|
AND: /&/g,
|
|
53
|
-
HYPHEN: /-([a-z])/g
|
|
53
|
+
HYPHEN: /-([a-z])/g,
|
|
54
|
+
VARIABLE: /(?:\$\$|\$)([a-zA-Z_][a-zA-Z0-9_]*)(?=[^a-zA-Z0-9_])/g
|
|
54
55
|
}
|
|
55
56
|
|
|
56
57
|
// 全局P3支持检测结果
|
|
@@ -72,10 +73,10 @@ const styimat = (function() {
|
|
|
72
73
|
const splitCharCode = splitChar.charCodeAt(0);
|
|
73
74
|
let start = 0;
|
|
74
75
|
|
|
75
|
-
for (let i = 0; i <
|
|
76
|
-
if (str.charCodeAt(i) === splitCharCode) {
|
|
76
|
+
for (let i = 0, length = str.length; i < length; i++) {
|
|
77
|
+
if (str.charCodeAt(i) === splitCharCode) {
|
|
77
78
|
if (i > start || str.charAt(start) !== '\r') {
|
|
78
|
-
lines.push(str.
|
|
79
|
+
lines.push(str.slice(start, i));
|
|
79
80
|
}
|
|
80
81
|
start = i + 1;
|
|
81
82
|
}
|
|
@@ -284,7 +285,7 @@ const styimat = (function() {
|
|
|
284
285
|
const argsMap = new Map();
|
|
285
286
|
const argValues = parseArgumentList(argsStr);
|
|
286
287
|
|
|
287
|
-
for (let i = 0; i <
|
|
288
|
+
for (let i = 0, length = paramDefs.length; i < length; i++) {
|
|
288
289
|
const param = paramDefs[i];
|
|
289
290
|
let value;
|
|
290
291
|
|
|
@@ -313,7 +314,7 @@ const styimat = (function() {
|
|
|
313
314
|
let parenDepth = 0;
|
|
314
315
|
let bracketDepth = 0;
|
|
315
316
|
|
|
316
|
-
for (let i = 0; i <
|
|
317
|
+
for (let i = 0, length = argsStr.length; i < length; i++) {
|
|
317
318
|
const char = argsStr[i];
|
|
318
319
|
|
|
319
320
|
if ((char === '"' || char === "'") && !inQuotes) {
|
|
@@ -351,11 +352,13 @@ const styimat = (function() {
|
|
|
351
352
|
*/
|
|
352
353
|
function applyMacro(macroBody, args, config) {
|
|
353
354
|
let result = macroBody;
|
|
355
|
+
const paramRegex = /\$([a-zA-Z_][a-zA-Z0-9_]*)/g;
|
|
354
356
|
|
|
355
|
-
|
|
356
|
-
const
|
|
357
|
-
|
|
358
|
-
|
|
357
|
+
result = result.replace(paramRegex, (match, paramName) => {
|
|
358
|
+
const paramValue = args.get(paramName);
|
|
359
|
+
if (paramValue) return args.get(paramName);
|
|
360
|
+
return match;
|
|
361
|
+
});
|
|
359
362
|
|
|
360
363
|
if (config.enableMacros) {
|
|
361
364
|
const macroNames = Array.from(macroRegistry.keys()).map(name =>
|
|
@@ -435,7 +438,7 @@ const styimat = (function() {
|
|
|
435
438
|
let inQuotes = false;
|
|
436
439
|
let quoteChar = '';
|
|
437
440
|
|
|
438
|
-
for (let i = 0; i <
|
|
441
|
+
for (let i = 0, length = str.length; i < length; i++) {
|
|
439
442
|
const char = str[i];
|
|
440
443
|
|
|
441
444
|
if ((char === '"' || char === "'") && !inQuotes) {
|
|
@@ -488,6 +491,10 @@ const styimat = (function() {
|
|
|
488
491
|
value = value.slice(0, -1).trim();
|
|
489
492
|
}
|
|
490
493
|
|
|
494
|
+
if (property.startsWith('$')) {
|
|
495
|
+
property = "--" + property.slice(1);
|
|
496
|
+
}
|
|
497
|
+
|
|
491
498
|
property = aliases.get(property) ?? property;
|
|
492
499
|
|
|
493
500
|
if (property.startsWith("@") && macros.get(property.slice(1))) {
|
|
@@ -510,7 +517,7 @@ const styimat = (function() {
|
|
|
510
517
|
let inQuotes = false;
|
|
511
518
|
let quoteChar = '';
|
|
512
519
|
|
|
513
|
-
for (let i = 0; i <
|
|
520
|
+
for (let i = 0, length = str.length; i < length; i++) {
|
|
514
521
|
const char = str[i];
|
|
515
522
|
|
|
516
523
|
if ((char === '"' || char === "'") && !inQuotes) {
|
|
@@ -953,9 +960,9 @@ const styimat = (function() {
|
|
|
953
960
|
const globalVariables = {};
|
|
954
961
|
const selectorVariables = new Map();
|
|
955
962
|
let cssWithoutVars = '';
|
|
956
|
-
let currentSelector = null;
|
|
957
963
|
let inSelectorBlock = false;
|
|
958
964
|
let currentIndent = 0;
|
|
965
|
+
let currentSelector = null;
|
|
959
966
|
|
|
960
967
|
for (let line of lines) {
|
|
961
968
|
const trimmed = line.trim();
|
|
@@ -966,12 +973,7 @@ const styimat = (function() {
|
|
|
966
973
|
const [, varName, varValue] = varMatch;
|
|
967
974
|
const processedValue = processCSSValue(varValue.trim(), config);
|
|
968
975
|
|
|
969
|
-
if (currentSelector) {
|
|
970
|
-
if (!selectorVariables.has(currentSelector)) {
|
|
971
|
-
selectorVariables.set(currentSelector, {});
|
|
972
|
-
}
|
|
973
|
-
selectorVariables.get(currentSelector)[varName] = processedValue;
|
|
974
|
-
} else {
|
|
976
|
+
if (!currentSelector) {
|
|
975
977
|
globalVariables[varName] = processedValue;
|
|
976
978
|
}
|
|
977
979
|
continue;
|
|
@@ -983,7 +985,7 @@ const styimat = (function() {
|
|
|
983
985
|
cssWithoutVars += line + '\n';
|
|
984
986
|
continue;
|
|
985
987
|
}
|
|
986
|
-
|
|
988
|
+
|
|
987
989
|
if (trimmed === '}') {
|
|
988
990
|
if (inSelectorBlock) {
|
|
989
991
|
if (currentSelector && selectorVariables.has(currentSelector)) {
|
|
@@ -997,9 +999,9 @@ const styimat = (function() {
|
|
|
997
999
|
inSelectorBlock = false;
|
|
998
1000
|
currentSelector = null;
|
|
999
1001
|
}
|
|
1000
|
-
|
|
1002
|
+
|
|
1001
1003
|
cssWithoutVars += line + '\n';
|
|
1002
|
-
|
|
1004
|
+
|
|
1003
1005
|
if (line.includes('{')) {
|
|
1004
1006
|
currentIndent += config.indentSize;
|
|
1005
1007
|
}
|
|
@@ -1008,6 +1010,8 @@ const styimat = (function() {
|
|
|
1008
1010
|
}
|
|
1009
1011
|
}
|
|
1010
1012
|
|
|
1013
|
+
cssWithoutVars = cssText;
|
|
1014
|
+
|
|
1011
1015
|
return {
|
|
1012
1016
|
globalVariables,
|
|
1013
1017
|
selectorVariables,
|
|
@@ -1024,7 +1028,7 @@ const styimat = (function() {
|
|
|
1024
1028
|
const rootRules = [];
|
|
1025
1029
|
let currentIndent = 0;
|
|
1026
1030
|
|
|
1027
|
-
for (let i = 0; i <
|
|
1031
|
+
for (let i = 0, length = lines.length; i < length; i++) {
|
|
1028
1032
|
const line = lines[i];
|
|
1029
1033
|
const trimmed = line.trim();
|
|
1030
1034
|
|
|
@@ -1092,61 +1096,62 @@ const styimat = (function() {
|
|
|
1092
1096
|
/**
|
|
1093
1097
|
* 将规则树转换为CSS字符串
|
|
1094
1098
|
*/
|
|
1095
|
-
function convertRulesToCSS(rules, config, parentSelector = '', aliases, macros) {
|
|
1099
|
+
function convertRulesToCSS(rules, config, parentSelector = '', aliases, macros, isRootAt = false) {
|
|
1096
1100
|
let result = '';
|
|
1097
|
-
const isParentAt = parentSelector.startsWith("@");
|
|
1098
1101
|
|
|
1099
1102
|
for (const rule of rules) {
|
|
1100
1103
|
const isAt = rule.selector.startsWith("@");
|
|
1101
|
-
let fullSelector = (isParentAt ? " ".repeat(config.indentSize) : '') + rule.selector;
|
|
1102
1104
|
|
|
1103
1105
|
if (isAt) {
|
|
1104
|
-
|
|
1106
|
+
result += rule.selector + " {\n";
|
|
1105
1107
|
}
|
|
1106
1108
|
|
|
1109
|
+
let fullSelector = rule.selector;
|
|
1110
|
+
|
|
1107
1111
|
if (parentSelector) {
|
|
1108
1112
|
if (fullSelector.includes('&')) {
|
|
1109
1113
|
fullSelector = fullSelector.replace(REGEX.AND, parentSelector);
|
|
1110
1114
|
} else if (fullSelector.trim().startsWith(':')) {
|
|
1111
1115
|
fullSelector = parentSelector + fullSelector;
|
|
1112
1116
|
} else {
|
|
1113
|
-
fullSelector = parentSelector +
|
|
1117
|
+
fullSelector = parentSelector + " " + fullSelector;
|
|
1114
1118
|
}
|
|
1115
1119
|
}
|
|
1116
1120
|
|
|
1121
|
+
fullSelector = fullSelector;
|
|
1122
|
+
|
|
1117
1123
|
if (rule.properties.length > 0) {
|
|
1118
|
-
result += (
|
|
1124
|
+
result += (isRootAt ? " ".repeat(config.indentSize) : '') + fullSelector + ' {\n';
|
|
1119
1125
|
for (const prop of rule.properties) {
|
|
1120
1126
|
const parsed = parseSingleLineCSS(prop, aliases, macros);
|
|
1121
1127
|
parsed.forEach(obj => {
|
|
1122
1128
|
const key = Object.keys(obj)[0];
|
|
1123
1129
|
const value = processCSSValue(obj[key], config);
|
|
1124
|
-
result += (
|
|
1130
|
+
result += (isRootAt ? " ".repeat(config.indentSize) : '') + " ".repeat(config.indentSize) + (value === "" ? key : `${key}: ${value};\n`);
|
|
1125
1131
|
});
|
|
1126
1132
|
}
|
|
1127
|
-
result +=
|
|
1133
|
+
result += isRootAt ? " ".repeat(config.indentSize) + '}\n' : '}\n\n';
|
|
1128
1134
|
}
|
|
1129
1135
|
|
|
1130
1136
|
if (rule.children && rule.children.length > 0) {
|
|
1131
|
-
result += convertRulesToCSS(rule.children, config, fullSelector, aliases, macros);
|
|
1137
|
+
result += convertRulesToCSS(rule.children, config, (isAt ? "" : fullSelector), aliases, macros, isRootAt || isAt);
|
|
1132
1138
|
}
|
|
1133
1139
|
|
|
1134
|
-
if (
|
|
1140
|
+
if (isAt) {
|
|
1135
1141
|
result += "}\n\n";
|
|
1136
1142
|
}
|
|
1137
1143
|
}
|
|
1138
1144
|
|
|
1139
|
-
return result
|
|
1145
|
+
return result;
|
|
1140
1146
|
}
|
|
1141
1147
|
|
|
1142
1148
|
/**
|
|
1143
1149
|
* 替换变量使用
|
|
1144
1150
|
*/
|
|
1145
|
-
function replaceVariableUses(cssText,
|
|
1151
|
+
function replaceVariableUses(cssText, config) {
|
|
1146
1152
|
let result = cssText;
|
|
1147
|
-
result = result.replace(
|
|
1148
|
-
|
|
1149
|
-
return match;
|
|
1153
|
+
result = result.replace(REGEX.VARIABLE, (match, varName) => {
|
|
1154
|
+
return match.startsWith('$$') ? `attr(${varName})` : `var(--${varName})`;
|
|
1150
1155
|
});
|
|
1151
1156
|
|
|
1152
1157
|
result = processCSSValue(result, config);
|
|
@@ -1172,31 +1177,6 @@ const styimat = (function() {
|
|
|
1172
1177
|
return `${config.rootSelector} {\n${declarations}\n}\n\n`;
|
|
1173
1178
|
}
|
|
1174
1179
|
|
|
1175
|
-
/**
|
|
1176
|
-
* 注入选择器局部变量
|
|
1177
|
-
*/
|
|
1178
|
-
function injectSelectorVariables(cssText, selectorVariables, config) {
|
|
1179
|
-
let result = '';
|
|
1180
|
-
const lines = splitLines(cssText);
|
|
1181
|
-
let currentSelector = null;
|
|
1182
|
-
|
|
1183
|
-
for (let line of lines) {
|
|
1184
|
-
const trimmed = line.trim();
|
|
1185
|
-
|
|
1186
|
-
if (trimmed.endsWith('{')) {
|
|
1187
|
-
currentSelector = trimmed.slice(0, -1).trim();
|
|
1188
|
-
}
|
|
1189
|
-
|
|
1190
|
-
if (trimmed === '}' && currentSelector) {
|
|
1191
|
-
currentSelector = null;
|
|
1192
|
-
}
|
|
1193
|
-
|
|
1194
|
-
result += processCSSValue(line, config) + '\n';
|
|
1195
|
-
}
|
|
1196
|
-
|
|
1197
|
-
return result.trim();
|
|
1198
|
-
}
|
|
1199
|
-
|
|
1200
1180
|
/**
|
|
1201
1181
|
* 处理 @import 语句
|
|
1202
1182
|
*/
|
|
@@ -1225,7 +1205,7 @@ const styimat = (function() {
|
|
|
1225
1205
|
const importedContents = await Promise.all(importPromises);
|
|
1226
1206
|
|
|
1227
1207
|
let finalText = processedText;
|
|
1228
|
-
for (let i =
|
|
1208
|
+
for (let i = importedContents.length; i--;) {
|
|
1229
1209
|
finalText = finalText.replace(`__IMPORT_PLACEHOLDER_${i}__`, importedContents[i]);
|
|
1230
1210
|
}
|
|
1231
1211
|
|
|
@@ -1352,11 +1332,8 @@ const styimat = (function() {
|
|
|
1352
1332
|
const rootRule = generateRootRule(globalVariables, finalConfig);
|
|
1353
1333
|
|
|
1354
1334
|
let finalCSS = processedCSS;
|
|
1355
|
-
if (selectorVariables.size > 0) {
|
|
1356
|
-
finalCSS = injectSelectorVariables(processedCSS, selectorVariables, finalConfig);
|
|
1357
|
-
}
|
|
1358
1335
|
|
|
1359
|
-
finalCSS = replaceVariableUses(finalCSS,
|
|
1336
|
+
finalCSS = replaceVariableUses(finalCSS, finalConfig);
|
|
1360
1337
|
|
|
1361
1338
|
let fullCSS = rootRule + finalCSS;
|
|
1362
1339
|
|
|
@@ -1530,7 +1507,7 @@ const styimat = (function() {
|
|
|
1530
1507
|
}
|
|
1531
1508
|
|
|
1532
1509
|
const argsMap = new Map();
|
|
1533
|
-
for (let i = 0
|
|
1510
|
+
for (let i = 0, length = macro.params.length; i < length; i++) {
|
|
1534
1511
|
const param = macro.params[i];
|
|
1535
1512
|
const value = i < args.length ? args[i] : param.defaultValue;
|
|
1536
1513
|
if (value === undefined && param.defaultValue === null) {
|
|
@@ -1695,7 +1672,7 @@ const styimat = (function() {
|
|
|
1695
1672
|
async function handleTemplateTag(strings, ...values) {
|
|
1696
1673
|
let cssText = strings[0];
|
|
1697
1674
|
|
|
1698
|
-
for (let i = 0; i <
|
|
1675
|
+
for (let i = 0, length = values.length; i < length; i++) {
|
|
1699
1676
|
const value = values[i];
|
|
1700
1677
|
let result = '';
|
|
1701
1678
|
|