styimat 4.3.0 → 5.0.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/README.md +10 -14
- package/dist/styimat.js +78 -178
- package/dist/styimat.min.js +23 -23
- package/dist/styimat.min.mjs +23 -23
- package/dist/styimat.mjs +78 -178
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -62,7 +62,7 @@ const css = `
|
|
|
62
62
|
.button { background-color: $primary; }
|
|
63
63
|
`;
|
|
64
64
|
|
|
65
|
-
const result = styimat.convert(css);
|
|
65
|
+
const result = await styimat.convert(css);
|
|
66
66
|
console.log(result);
|
|
67
67
|
```
|
|
68
68
|
|
|
@@ -110,8 +110,7 @@ const css = await styimat(`
|
|
|
110
110
|
// 方式2:传入配置对象(返回函数本身)
|
|
111
111
|
styimat({
|
|
112
112
|
enableP3: true,
|
|
113
|
-
enableMath: true
|
|
114
|
-
mathPrecision: 8
|
|
113
|
+
enableMath: true
|
|
115
114
|
});
|
|
116
115
|
|
|
117
116
|
// 方式3:无参数调用(自动处理页面中所有标记的<style>标签)
|
|
@@ -162,7 +161,7 @@ styimat.config({
|
|
|
162
161
|
});
|
|
163
162
|
|
|
164
163
|
// 4. 使用工具方法
|
|
165
|
-
const mathResult = styimat.math.evaluate('100px
|
|
164
|
+
const mathResult = styimat.math.evaluate('100px/2'); // "calc(100px / 2)"
|
|
166
165
|
const rgbColor = styimat.colorUtils.labToRGB(54.7, 77.9, 80.1);
|
|
167
166
|
```
|
|
168
167
|
|
|
@@ -254,18 +253,17 @@ $font-family: 'Inter', sans-serif;
|
|
|
254
253
|
#root-selector :root
|
|
255
254
|
#enable-nesting true
|
|
256
255
|
#enable-math true
|
|
257
|
-
#math-precision 8
|
|
258
256
|
|
|
259
257
|
/* 然后写您的CSS代码 */
|
|
260
258
|
$primary-color: lab#32a852;
|
|
261
|
-
$spacing: math(16px
|
|
259
|
+
$spacing: math(16px*2+4px);
|
|
262
260
|
|
|
263
261
|
body {
|
|
264
262
|
color: $primary-color;
|
|
265
263
|
padding: $spacing;
|
|
266
264
|
|
|
267
265
|
.container {
|
|
268
|
-
margin: math($spacing
|
|
266
|
+
margin: math($spacing/2);
|
|
269
267
|
}
|
|
270
268
|
}
|
|
271
269
|
```
|
|
@@ -278,14 +276,14 @@ body {
|
|
|
278
276
|
|
|
279
277
|
/* 然后写您的CSS代码 */
|
|
280
278
|
$primary-color: lab#32a852;
|
|
281
|
-
$spacing: math(16px
|
|
279
|
+
$spacing: math(16px*2+4px);
|
|
282
280
|
|
|
283
281
|
body {
|
|
284
282
|
color: $primary-color;
|
|
285
283
|
padding: $spacing;
|
|
286
284
|
|
|
287
285
|
.container {
|
|
288
|
-
margin: math($spacing
|
|
286
|
+
margin: math($spacing/2);
|
|
289
287
|
}
|
|
290
288
|
}
|
|
291
289
|
```
|
|
@@ -413,7 +411,7 @@ $padding-large: math(2rem * 1.5);
|
|
|
413
411
|
const styimat = require('styimat');
|
|
414
412
|
|
|
415
413
|
// 转换 CSS
|
|
416
|
-
const
|
|
414
|
+
const cssPromise = styimat.convert(cssString, config);
|
|
417
415
|
|
|
418
416
|
// 应用到页面
|
|
419
417
|
styimat.apply(cssString, config);
|
|
@@ -426,7 +424,6 @@ styimat.config({
|
|
|
426
424
|
convertLchToRGB: true,
|
|
427
425
|
enableP3: true,
|
|
428
426
|
enableMath: true,
|
|
429
|
-
mathPrecision: 6,
|
|
430
427
|
indentSize: 4,
|
|
431
428
|
enableNesting: true,
|
|
432
429
|
importBaseUrl: '', // 导入基础URL
|
|
@@ -438,8 +435,8 @@ styimat.config({
|
|
|
438
435
|
});
|
|
439
436
|
|
|
440
437
|
// 数学工具
|
|
441
|
-
styimat.math.evaluate('100px
|
|
442
|
-
styimat.math.evaluate('(16px
|
|
438
|
+
styimat.math.evaluate('100px/2'); // "calc(100px / 2)"
|
|
439
|
+
styimat.math.evaluate('(16px*1.5)+4px'); // "calc((16px * 1.5) + 4px)"
|
|
443
440
|
|
|
444
441
|
// 颜色工具
|
|
445
442
|
const rgb = styimat.colorUtils.labToRGB(54.7, 77.9, 80.1);
|
|
@@ -491,7 +488,6 @@ for(const name in styimat.plugins) {
|
|
|
491
488
|
| `convertLchToRGB` | boolean | `true` | 是否转换 LCH 颜色为 RGB |
|
|
492
489
|
| `enableP3` | boolean | `true` | 是否启用 Display P3 广色域支持 |
|
|
493
490
|
| `enableMath` | boolean | `true` | 是否启用 `math()` 函数增强 |
|
|
494
|
-
| `mathPrecision` | number | `6` | 数学计算的精度 |
|
|
495
491
|
| `importBaseUrl` | string | ` | 导入的基础URL |
|
|
496
492
|
| `importCache` | boolean | `true` | 是否缓存导入的文件 |
|
|
497
493
|
| `importTimeout` | number | `5000` | 导入超时时间(毫秒) |
|
package/dist/styimat.js
CHANGED
|
@@ -29,7 +29,6 @@ const styimat = (function() {
|
|
|
29
29
|
convertLchToRGB: true,
|
|
30
30
|
enableP3: true,
|
|
31
31
|
enableMath: true,
|
|
32
|
-
mathPrecision: 6,
|
|
33
32
|
importBaseUrl: '',
|
|
34
33
|
importCache: true,
|
|
35
34
|
importTimeout: 5000,
|
|
@@ -510,71 +509,23 @@ const styimat = (function() {
|
|
|
510
509
|
|
|
511
510
|
try {
|
|
512
511
|
let cleanExpr = expression.replace(/\s+/g, '');
|
|
513
|
-
const result = parseMathExpression(cleanExpr, config);
|
|
514
512
|
|
|
515
|
-
|
|
516
|
-
return processUnitExpression(cleanExpr, result);
|
|
517
|
-
} else {
|
|
518
|
-
return roundNumber(result, config.mathPrecision);
|
|
519
|
-
}
|
|
513
|
+
return parseMathExpression(cleanExpr, config.enableMath);
|
|
520
514
|
} catch (error) {
|
|
521
515
|
console.warn('math()表达式解析失败:', expression, error);
|
|
522
|
-
return `
|
|
516
|
+
return `calc(${expression})`;
|
|
523
517
|
}
|
|
524
518
|
}
|
|
525
519
|
|
|
526
520
|
/**
|
|
527
521
|
* 解析数学表达式
|
|
528
522
|
*/
|
|
529
|
-
function parseMathExpression(expr,
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
if (end === -1) break;
|
|
535
|
-
|
|
536
|
-
const inner = expr.substring(start + 1, end);
|
|
537
|
-
const innerResult = parseMathExpression(inner, config);
|
|
538
|
-
|
|
539
|
-
expr = expr.substring(0, start) + innerResult + expr.substring(end + 1);
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
return evaluateSimpleExpression(expr, config);
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
/**
|
|
546
|
-
* 评估简单表达式(无括号)
|
|
547
|
-
*/
|
|
548
|
-
function evaluateSimpleExpression(expr, config) {
|
|
549
|
-
const operators = [
|
|
550
|
-
{ regex: /([\d.]+(?:[a-zA-Z%]+)?)\*([\d.]+(?:[a-zA-Z%]+)?)/, handler: multiply },
|
|
551
|
-
{ regex: /([\d.]+(?:[a-zA-Z%]+)?)\/([\d.]+(?:[a-zA-Z%]+)?)/, handler: divide },
|
|
552
|
-
{ regex: /([\d.]+(?:[a-zA-Z%]+)?)\+([\d.]+(?:[a-zA-Z%]+)?)/, handler: add },
|
|
553
|
-
{ regex: /([\d.]+(?:[a-zA-Z%]+)?)-([\d.]+(?:[a-zA-Z%]+)?)/, handler: subtract }
|
|
554
|
-
];
|
|
555
|
-
|
|
556
|
-
let lastExpr = expr;
|
|
557
|
-
|
|
558
|
-
for (const op of operators) {
|
|
559
|
-
let match;
|
|
560
|
-
while ((match = expr.match(op.regex)) !== null) {
|
|
561
|
-
const left = parseValueWithUnit(match[1]);
|
|
562
|
-
const right = parseValueWithUnit(match[2]);
|
|
563
|
-
const result = op.handler(left, right);
|
|
564
|
-
|
|
565
|
-
expr = expr.substring(0, match.index) +
|
|
566
|
-
result.value +
|
|
567
|
-
expr.substring(match.index + match[0].length);
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
if (expr !== lastExpr) {
|
|
572
|
-
return evaluateSimpleExpression(expr, config);
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
const parsed = parseValueWithUnit(expr);
|
|
576
|
-
return parsed.value;
|
|
523
|
+
function parseMathExpression(expr, enableMath) {
|
|
524
|
+
return `calc(${expr
|
|
525
|
+
.replace(/([\da-z])([+-])(-?[\da-z])/g, '$1 $2 $3')
|
|
526
|
+
.replace(/([\da-z])([+-])(-?[\da-z])/g, '$1 $2 $3')})`;
|
|
577
527
|
}
|
|
528
|
+
|
|
578
529
|
|
|
579
530
|
/**
|
|
580
531
|
* 解析带单位的数值
|
|
@@ -592,30 +543,6 @@ const styimat = (function() {
|
|
|
592
543
|
return { value, unit };
|
|
593
544
|
}
|
|
594
545
|
|
|
595
|
-
/**
|
|
596
|
-
* 检查表达式是否包含单位
|
|
597
|
-
*/
|
|
598
|
-
function hasUnits(expr) {
|
|
599
|
-
return /[a-zA-Z%]/.test(expr);
|
|
600
|
-
}
|
|
601
|
-
|
|
602
|
-
/**
|
|
603
|
-
* 处理带单位的表达式
|
|
604
|
-
*/
|
|
605
|
-
function processUnitExpression(originalExpr, result) {
|
|
606
|
-
const unitMatch = originalExpr.match(/([a-zA-Z%]+)(?!.*[a-zA-Z%])/);
|
|
607
|
-
|
|
608
|
-
if (unitMatch) {
|
|
609
|
-
return result + unitMatch[1];
|
|
610
|
-
}
|
|
611
|
-
|
|
612
|
-
if (originalExpr.includes('%')) {
|
|
613
|
-
return result + '%';
|
|
614
|
-
}
|
|
615
|
-
|
|
616
|
-
return result + 'px';
|
|
617
|
-
}
|
|
618
|
-
|
|
619
546
|
// 数学运算函数
|
|
620
547
|
function multiply(a, b) {
|
|
621
548
|
if (a.unit === b.unit || (!a.unit && !b.unit)) {
|
|
@@ -682,6 +609,7 @@ const styimat = (function() {
|
|
|
682
609
|
|
|
683
610
|
const processMath = (str) => {
|
|
684
611
|
return str.replace(mathRegex, (match, expression) => {
|
|
612
|
+
|
|
685
613
|
return evaluateMathExpression(expression, config);
|
|
686
614
|
});
|
|
687
615
|
};
|
|
@@ -1456,86 +1384,68 @@ const styimat = (function() {
|
|
|
1456
1384
|
/**
|
|
1457
1385
|
* 主转换函数
|
|
1458
1386
|
*/
|
|
1459
|
-
function convert(cssText, customConfig = {}) {
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1387
|
+
async function convert(cssText, customConfig = {}) {
|
|
1388
|
+
let { config: headerConfig, css: cleanedCSS } = parseConfigHeader(cssText);
|
|
1389
|
+
const finalConfig = { ...defaultConfig, ...customConfig, ...headerConfig };
|
|
1390
|
+
cleanedCSS = await processImports(cleanedCSS, finalConfig);
|
|
1391
|
+
|
|
1392
|
+
let cssWithAliases = cleanedCSS;
|
|
1393
|
+
let aliases = new Map();
|
|
1394
|
+
if (finalConfig.enableAlias) {
|
|
1395
|
+
const { aliases: parsedAliases, css: cssWithoutAliases } = parseAliasStatements(cssWithAliases);
|
|
1396
|
+
aliases = parsedAliases;
|
|
1397
|
+
cssWithAliases = cssWithoutAliases;
|
|
1398
|
+
}
|
|
1471
1399
|
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1400
|
+
let cssWithMacros = cssWithAliases;
|
|
1401
|
+
let macros = new Map();
|
|
1402
|
+
if (finalConfig.enableMacros) {
|
|
1403
|
+
const { macros: parsedMacros, css: cssWithoutMacros } = parseMacroStatements(cssWithMacros, finalConfig);
|
|
1404
|
+
macros = parsedMacros;
|
|
1405
|
+
cssWithMacros = cssWithoutMacros;
|
|
1478
1406
|
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
}
|
|
1482
|
-
}
|
|
1483
|
-
|
|
1484
|
-
let cssAfterMacros = cssWithMacros;
|
|
1485
|
-
if (finalConfig.enableMacros && macros.size > 0) {
|
|
1486
|
-
cssAfterMacros = applyMacroCalls(cssAfterMacros, macros, finalConfig);
|
|
1487
|
-
}
|
|
1488
|
-
|
|
1489
|
-
const { globalVariables, selectorVariables, cssWithoutVars } =
|
|
1490
|
-
extractVariablesAndCSS(cssAfterMacros, finalConfig);
|
|
1491
|
-
|
|
1492
|
-
let processedCSS = cssWithoutVars.trim();
|
|
1493
|
-
if (finalConfig.enableNesting && cssWithoutVars.includes('{')) {
|
|
1494
|
-
try {
|
|
1495
|
-
processedCSS = parseNestedRules(cssWithoutVars, finalConfig, aliases, macros);
|
|
1496
|
-
} catch (error) {
|
|
1497
|
-
console.warn('嵌套解析失败,使用原始CSS:', error);
|
|
1498
|
-
}
|
|
1407
|
+
for (const [name, macro] of macros) {
|
|
1408
|
+
macroRegistry.set(name, macro);
|
|
1499
1409
|
}
|
|
1410
|
+
}
|
|
1411
|
+
|
|
1412
|
+
let cssAfterMacros = cssWithMacros;
|
|
1413
|
+
if (finalConfig.enableMacros && macros.size > 0) {
|
|
1414
|
+
cssAfterMacros = applyMacroCalls(cssAfterMacros, macros, finalConfig);
|
|
1415
|
+
}
|
|
1416
|
+
|
|
1417
|
+
const { globalVariables, selectorVariables, cssWithoutVars } =
|
|
1418
|
+
extractVariablesAndCSS(cssAfterMacros, finalConfig);
|
|
1500
1419
|
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1420
|
+
let processedCSS = cssWithoutVars.trim();
|
|
1421
|
+
if (finalConfig.enableNesting && cssWithoutVars.includes('{')) {
|
|
1422
|
+
try {
|
|
1423
|
+
processedCSS = parseNestedRules(cssWithoutVars, finalConfig, aliases, macros);
|
|
1424
|
+
} catch (error) {
|
|
1425
|
+
console.warn('嵌套解析失败,使用原始CSS:', error);
|
|
1506
1426
|
}
|
|
1427
|
+
}
|
|
1428
|
+
|
|
1429
|
+
const rootRule = generateRootRule(globalVariables, finalConfig);
|
|
1430
|
+
|
|
1431
|
+
let finalCSS = processedCSS;
|
|
1432
|
+
if (selectorVariables.size > 0) {
|
|
1433
|
+
finalCSS = injectSelectorVariables(processedCSS, selectorVariables, finalConfig);
|
|
1434
|
+
}
|
|
1435
|
+
|
|
1436
|
+
finalCSS = replaceVariableUses(finalCSS, globalVariables, selectorVariables, finalConfig);
|
|
1507
1437
|
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
} catch (error) {
|
|
1514
|
-
console.error('插件处理失败:', error);
|
|
1515
|
-
}
|
|
1438
|
+
for (const [name, plugin] of pluginMap) {
|
|
1439
|
+
try {
|
|
1440
|
+
if(plugin.enable) finalCSS = plugin.convert(finalCSS, finalConfig);
|
|
1441
|
+
} catch (error) {
|
|
1442
|
+
console.error('插件处理失败:', error);
|
|
1516
1443
|
}
|
|
1444
|
+
}
|
|
1517
1445
|
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
return fullCSS;
|
|
1521
|
-
};
|
|
1522
|
-
|
|
1523
|
-
const hasImports = cssText && /@import\s+([^;]+?)\s*;/g.test(cssText);
|
|
1524
|
-
const finalConfig = { ...defaultConfig, ...customConfig };
|
|
1446
|
+
let fullCSS = rootRule + finalCSS;
|
|
1525
1447
|
|
|
1526
|
-
|
|
1527
|
-
return syncConvert(cssText, finalConfig);
|
|
1528
|
-
} else {
|
|
1529
|
-
return (async () => {
|
|
1530
|
-
try {
|
|
1531
|
-
const processedWithImports = await processImports(cssText, finalConfig);
|
|
1532
|
-
return syncConvert(processedWithImports, finalConfig);
|
|
1533
|
-
} catch (error) {
|
|
1534
|
-
console.error('@import处理失败:', error);
|
|
1535
|
-
return syncConvert(cssText, finalConfig);
|
|
1536
|
-
}
|
|
1537
|
-
})();
|
|
1538
|
-
}
|
|
1448
|
+
return fullCSS;
|
|
1539
1449
|
}
|
|
1540
1450
|
|
|
1541
1451
|
/**
|
|
@@ -1587,28 +1497,18 @@ const styimat = (function() {
|
|
|
1587
1497
|
const config = { ...defaultConfig, ...customConfig };
|
|
1588
1498
|
|
|
1589
1499
|
if (cssText) {
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
return null;
|
|
1603
|
-
}
|
|
1604
|
-
})();
|
|
1605
|
-
} else {
|
|
1606
|
-
const converted = convert(cssText, config);
|
|
1607
|
-
const styleEl = document.createElement('style');
|
|
1608
|
-
styleEl.textContent = converted;
|
|
1609
|
-
document.head.appendChild(styleEl);
|
|
1610
|
-
return styleEl;
|
|
1611
|
-
}
|
|
1500
|
+
return (async () => {
|
|
1501
|
+
try {
|
|
1502
|
+
const converted = await convert(cssText, config);
|
|
1503
|
+
const styleEl = document.createElement('style');
|
|
1504
|
+
styleEl.textContent = converted;
|
|
1505
|
+
document.head.appendChild(styleEl);
|
|
1506
|
+
return styleEl;
|
|
1507
|
+
} catch (error) {
|
|
1508
|
+
console.error('应用CSS失败:', error);
|
|
1509
|
+
return null;
|
|
1510
|
+
}
|
|
1511
|
+
})();
|
|
1612
1512
|
} else {
|
|
1613
1513
|
if (document.readyState === 'loading') {
|
|
1614
1514
|
document.addEventListener('DOMContentLoaded', () => {
|
|
@@ -1874,15 +1774,15 @@ const styimat = (function() {
|
|
|
1874
1774
|
};
|
|
1875
1775
|
|
|
1876
1776
|
// 创建主函数
|
|
1877
|
-
const styimat = function(...args) {
|
|
1777
|
+
const styimat = async function(...args) {
|
|
1878
1778
|
if (args.length > 1 || (args[0] && args[0].raw)) {
|
|
1879
|
-
return handleTemplateTag(...args);
|
|
1779
|
+
return await handleTemplateTag(...args);
|
|
1880
1780
|
}
|
|
1881
1781
|
|
|
1882
1782
|
const firstArg = args[0];
|
|
1883
1783
|
|
|
1884
1784
|
if (typeof firstArg === 'string') {
|
|
1885
|
-
const result = convert(firstArg, { ...defaultConfig, ...args[1] });
|
|
1785
|
+
const result = await convert(firstArg, { ...defaultConfig, ...args[1] });
|
|
1886
1786
|
|
|
1887
1787
|
if (result && typeof result.then === 'function') {
|
|
1888
1788
|
return result;
|
|
@@ -1902,7 +1802,7 @@ const styimat = (function() {
|
|
|
1902
1802
|
return styimat;
|
|
1903
1803
|
};
|
|
1904
1804
|
|
|
1905
|
-
function handleTemplateTag(strings, ...values) {
|
|
1805
|
+
async function handleTemplateTag(strings, ...values) {
|
|
1906
1806
|
let cssText = strings[0];
|
|
1907
1807
|
|
|
1908
1808
|
for (let i = 0; i < values.length; i++) {
|
|
@@ -1920,7 +1820,7 @@ const styimat = (function() {
|
|
|
1920
1820
|
cssText += result + strings[i + 1];
|
|
1921
1821
|
}
|
|
1922
1822
|
|
|
1923
|
-
const result = convert(cssText, defaultConfig);
|
|
1823
|
+
const result = await convert(cssText, defaultConfig);
|
|
1924
1824
|
|
|
1925
1825
|
if (result && typeof result.then === 'function') {
|
|
1926
1826
|
return result;
|
package/dist/styimat.min.js
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* MIT License
|
|
3
3
|
* Copyright (c) 2025 王小玗
|
|
4
|
-
*/const styimat=(function(){let g={rootSelector:":root",variablePrefix:"--",preserveOriginal:!1,indentSize:4,enableNesting:!0,autoProcessStyleTags:!0,styleTagAttribute:"e",convertLabToRGB:!0,convertLchToRGB:!0,enableP3:!0,enableMath:!0,
|
|
5
|
-
`),
|
|
6
|
-
`)}}function
|
|
7
|
-
`),
|
|
8
|
-
`)}}function
|
|
9
|
-
`),o=[];let
|
|
10
|
-
`)}),
|
|
11
|
-
`)}}function
|
|
12
|
-
`),
|
|
13
|
-
`;continue}if(u==="}"){if(c&&a&&o.has(a)){const
|
|
14
|
-
`}c=!1,a=null}
|
|
15
|
-
`,i.includes("{")&&(l+=e.indentSize),i.includes("}")&&(l=Math.max(0,l-e.indentSize))}return{globalVariables:
|
|
16
|
-
`),
|
|
17
|
-
`),
|
|
18
|
-
`;for(const u of c.properties)
|
|
19
|
-
`)});
|
|
4
|
+
*/const styimat=(function(){let g={rootSelector:":root",variablePrefix:"--",preserveOriginal:!1,indentSize:4,enableNesting:!0,autoProcessStyleTags:!0,styleTagAttribute:"e",convertLabToRGB:!0,convertLchToRGB:!0,enableP3:!0,enableMath:!0,importBaseUrl:"",importCache:!0,importTimeout:5e3,enableAlias:!0,enableMacros:!0},A=null;const V=new Map,E=new Map,M=new Map,v=new Map;function K(t){const e={...g},r=t.split(`
|
|
5
|
+
`),n=[];for(let o of r){const s=o.trim();if(s.startsWith("#")){const a=s.substring(1).trim(),c=a.indexOf(" ");if(c!==-1){const l=a.substring(0,c).trim(),i=a.substring(c+1).trim(),u=ne(l);u in e?i==="true"||i==="false"?e[u]=i==="true":!isNaN(i)&&i.trim()!==""?e[u]=Number(i):e[u]=i:console.warn(`\u672A\u77E5\u7684\u914D\u7F6E\u9879: ${l}`)}else console.warn(`\u65E0\u6548\u7684\u914D\u7F6E\u884C: ${s}`)}else n.push(o)}return{config:e,css:n.join(`
|
|
6
|
+
`)}}function J(t){const e=new Map,r=t.split(`
|
|
7
|
+
`),n=[];for(let o of r){const s=o.trim();if(s.startsWith("@alias")){const a=s.match(/^@alias\s+([a-zA-Z0-9_-]+)\s+(.+?)\s*;$/);if(a){const c=a[1],l=a[2];e.set(c,l);continue}}n.push(o)}return{aliases:e,css:n.join(`
|
|
8
|
+
`)}}function j(t,e){if(!e.enableMacros)return{macros:new Map,css:t};const r=new Map,n=t.split(`
|
|
9
|
+
`),o=[];let s=!1,a="",c=[],l=[],i=0;for(let u of n){const h=u.trim();if(!s&&h.startsWith("@macro")){const f=h.match(/^@macro\s+([a-zA-Z0-9_-]+)\s*\(([^)]*)\)\s*\{$/);if(f){s=!0,a=f[1],c=f[2].split(",").map(p=>p.trim()).filter(p=>p).map(p=>{const d=p.split(":").map(m=>m.trim());return{name:d[0].slice(1),defaultValue:d[1]||null}}),l=[],i=1;continue}}if(s){for(const f of u)f==="{"&&i++,f==="}"&&i--;i===0?(r.set(a,{params:c,body:l.join(`
|
|
10
|
+
`)}),s=!1,a="",c=[],l=[]):l.push(u);continue}o.push(u)}return{macros:r,css:o.join(`
|
|
11
|
+
`)}}function ee(t,e,r){if(!r.enableMacros||e.size===0)return t;const n=Array.from(e.keys()).map(a=>a.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")).join("|"),o=new RegExp(`@(${n})\\s*\\(([^)]*)\\)\\s*;?`,"g");return(a=>{let c=a,l=!1;do l=!1,c=c.replace(o,(i,u,h)=>{l=!0;const f=e.get(u);if(!f)return console.warn(`\u672A\u5B9A\u4E49\u7684\u5B8F: ${u}`),i;const p=W(h,f.params);return B(f.body,p,r)});while(l);return c})(t)}function W(t,e){const r=new Map,n=te(t);for(let o=0;o<e.length;o++){const s=e[o];let a;o<n.length?a=n[o]:s.defaultValue!==null?a=s.defaultValue:(console.warn(`\u5B8F\u8C03\u7528\u7F3A\u5C11\u5FC5\u9700\u53C2\u6570: ${s.name}`),a=""),r.set(s.name,a)}return n.length>e.length&&console.warn("\u5B8F\u8C03\u7528\u4F20\u9012\u4E86\u8FC7\u591A\u53C2\u6570\uFF0C\u591A\u4F59\u7684\u53C2\u6570\u5C06\u88AB\u5FFD\u7565"),r}function te(t){const e=[];let r="",n=!1,o="",s=0,a=0;for(let c=0;c<t.length;c++){const l=t[c];(l==='"'||l==="'")&&!n?(n=!0,o=l):l===o&&n&&(n=!1,o=""),n||(l==="("&&s++,l===")"&&s--,l==="["&&a++,l==="]"&&a--),l===","&&!n&&s===0&&a===0?(e.push(r.trim()),r=""):r+=l}return r.trim()&&e.push(r.trim()),e}function B(t,e,r){let n=t;for(const[o,s]of e){const a=new RegExp(`\\$${o}`,"g");n=n.replace(a,s)}if(r.enableMacros){const o=Array.from(M.keys()).map(s=>s.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")).join("|");if(o){const s=new RegExp(`@(${o})\\s*\\(([^)]*)\\)\\s*;?`,"g");let a;do a=!1,n=n.replace(s,(c,l,i)=>{a=!0;const u=M.get(l);if(!u)return c;const h=W(i,u.params);return B(u.body,h,r)});while(a)}}return n=L(n,r),n}function ne(t){return t.replace(/-([a-z])/g,function(e,r){return r.toUpperCase()})}function z(){if(A!==null)return A;if(typeof window>"u"||!window.CSS)return A=!1,!1;try{A=CSS.supports("color","color(display-p3 1 0 0)")}catch(t){console.warn("P3\u652F\u6301\u68C0\u6D4B\u5931\u8D25:",t),A=!1}return A}function N(t,e,r){let n=t.trim();if(n.endsWith(";")&&(n=n.slice(0,-1)),!n)return[];const o=[];let s="",a=0,c=!1,l="";for(let u=0;u<n.length;u++){const h=n[u];(h==='"'||h==="'")&&!c?(c=!0,l=h):h===l&&c&&(c=!1,l=""),c||(h==="("?a++:h===")"&&a--),h===";"&&!c&&a===0?s.trim()&&(o.push(s.trim()),s=""):s+=h}s.trim()&&o.push(s.trim());const i=[];for(const u of o){if(!u.trim())continue;const h=re(u);if(h===-1){console.warn(`\u65E0\u6548\u7684CSS\u58F0\u660E: "${u}"`);continue}let f=u.substring(0,h).trim(),p=u.substring(h+1).trim();if(p.endsWith(";")&&(p=p.slice(0,-1).trim()),e.get(f)&&(f=e.get(f)),f.startsWith("@")&&r.get(f.slice(1))){const d=r.get(f.slice(1)),m=W(p.split(" ").join(","),d.params);f=B(d.body,m,q),p=""}i.push({[f]:p})}return i}function re(t){let e=!1,r="";for(let n=0;n<t.length;n++){const o=t[n];if((o==='"'||o==="'")&&!e?(e=!0,r=o):o===r&&e&&(e=!1,r=""),o===":"&&!e)return n}return-1}function _(t,e){if(!e.enableMath)return`math(${t})`;try{let r=t.replace(/\s+/g,"");return se(r,e.enableMath)}catch(r){return console.warn("math()\u8868\u8FBE\u5F0F\u89E3\u6790\u5931\u8D25:",t,r),`calc(${t})`}}function se(t,e){return`calc(${t.replace(/([\da-z])([+-])(-?[\da-z])/g,"$1 $2 $3").replace(/([\da-z])([+-])(-?[\da-z])/g,"$1 $2 $3")})`}function H(t){const e=t.match(/^([\d.]+)([a-zA-Z%]*)$/);if(!e)throw new Error(`\u65E0\u6CD5\u89E3\u6790\u503C: ${t}`);const r=parseFloat(e[1]),n=e[2]||"";return{value:r,unit:n}}function ve(t,e){return t.unit===e.unit||!t.unit&&!e.unit?{value:t.value*e.value,unit:t.unit||e.unit}:{value:`${t.value}${t.unit}*${e.value}${e.unit}`,unit:""}}function Ce(t,e){if(e.value===0)throw new Error("\u9664\u4EE5\u96F6");return t.unit&&!e.unit?{value:t.value/e.value,unit:t.unit}:t.unit===e.unit?{value:t.value/e.value,unit:""}:{value:`${t.value}${t.unit}/${e.value}${e.unit}`,unit:""}}function Se(t,e){return t.unit===e.unit?{value:t.value+e.value,unit:t.unit}:{value:`${t.value}${t.unit}+${e.value}${e.unit}`,unit:""}}function xe(t,e){return t.unit===e.unit?{value:t.value-e.value,unit:t.unit}:{value:`${t.value}${t.unit}-${e.value}${e.unit}`,unit:""}}function oe(t,e=6){const r=Math.pow(10,e),o=(Math.round(t*r)/r).toString();return o.includes(".")?o.replace(/(\.\d*?)0+$/,"$1").replace(/\.$/,""):o}function ae(t,e){if(!e.enableMath)return t;let r=t;const n=/math\(([^)]+)\)/gi,o=a=>a.replace(n,(c,l)=>_(l,e));let s;do s=r,r=o(r);while(r!==s&&r.includes("math("));return r}function ce(t,e){if(!e.convertLabToRGB&&!e.convertLchToRGB)return t;let r=t;r=le(r,e);const n=/(lab|lch)\([^)]+\)/gi,o=new Map,s=c=>c.replace(n,l=>{if(o.has(l))return o.get(l);let i=l;return l.toLowerCase().startsWith("lab(")?e.convertLabToRGB&&(i=ie(l,e)):l.toLowerCase().startsWith("lch(")&&e.convertLchToRGB&&(i=ue(l,e)),o.set(l,i),i});let a;do a=r,r=s(r);while(r!==a);return r}function le(t,e){let r=t;const n=/lab#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/gi,o=/lch#([0-9a-f]{2})([0-9a-f]{2})(\d{1,3})/gi,s=new Map;return r=r.replace(n,(a,c,l,i)=>{if(s.has(a))return s.get(a);try{const u=parseInt(c,16)/255*100,h=(parseInt(l,16)-128)*1.5,f=(parseInt(i,16)-128)*1.5,p=x(u,h,f,e);return s.set(a,p),p}catch(u){return console.warn(`\u65E0\u6CD5\u89E3\u6790lab#\u5341\u516D\u8FDB\u5236\u989C\u8272: ${a}`,u),a}}),r=r.replace(o,(a,c,l,i)=>{if(s.has(a))return s.get(a);try{const u=parseInt(c,16)/255*100,h=parseInt(l,16)/255*150,f=parseInt(i)/100*360,p=S(u,h,f),d=x(p.L,p.a,p.b,e);return s.set(a,d),d}catch(u){return console.warn(`\u65E0\u6CD5\u89E3\u6790lch#\u5341\u516D\u8FDB\u5236\u989C\u8272: ${a}`,u),a}}),r}function ie(t,e){const r=/lab\(\s*([\d.]+)(%?)\s+([\d.-]+)\s+([\d.-]+)(?:\s*\/\s*([\d.%]+))?\s*\)/i,n=t.match(r);if(!n)return t;try{let o=parseFloat(n[1]);n[2]==="%"?o=o:(o<0&&(o=0),o>100&&(o=100));const s=parseFloat(n[3]),a=parseFloat(n[4]),c=n[5]!==void 0?n[5].includes("%")?parseFloat(n[5])/100:parseFloat(n[5]):null;return x(o,s,a,e,c)}catch(o){return console.warn(`\u65E0\u6CD5\u8F6C\u6362LAB\u989C\u8272: ${t}`,o),t}}function ue(t,e){const r=/lch\(\s*([\d.]+)(%?)\s+([\d.]+)\s+([\d.]+)(deg)?(?:\s*\/\s*([\d.%]+))?\s*\)/i,n=t.match(r);if(!n)return t;try{let o=parseFloat(n[1]);n[2]==="%"?o=o:(o<0&&(o=0),o>100&&(o=100));const s=parseFloat(n[3]);let a=parseFloat(n[4]);s<0&&console.warn(`LCH\u4E2D\u7684C\u503C\u4E0D\u80FD\u4E3A\u8D1F: ${s}`),a=(a%360+360)%360;const c=S(o,s,a),l=n[6]!==void 0?n[6].includes("%")?parseFloat(n[6])/100:parseFloat(n[6]):null;return x(c.L,c.a,c.b,e,l)}catch(o){return console.warn(`\u65E0\u6CD5\u8F6C\u6362LCH\u989C\u8272: ${t}`,o),t}}function S(t,e,r){const n=r*Math.PI/180,o=e*Math.cos(n),s=e*Math.sin(n);return{L:t,a:o,b:s}}function C(t,e,r){const n=(b,y,$)=>{const Q=(b+16)/116,I=y/500+Q,O=Q-$/200,$e=I**3>.008856?I**3:(116*I-16)/903.3,we=b>903.3*.008856?((b+16)/116)**3:b/903.3,Me=O**3>.008856?O**3:(116*O-16)/903.3;return[$e*.95047,we*1,Me*1.08883]},o=(b,y,$)=>{const w=[[3.2404542,-1.5371385,-.4985314],[-.969266,1.8760108,.041556],[.0556434,-.2040259,1.0572252]],R=w[0][0]*b+w[0][1]*y+w[0][2]*$,D=w[1][0]*b+w[1][1]*y+w[1][2]*$,Y=w[2][0]*b+w[2][1]*y+w[2][2]*$;return[R,D,Y]},s=b=>{const y=b<0?-1:1,$=Math.abs(b);return $<=.0031308?y*12.92*$:y*(1.055*Math.pow($,.4166666666666667)-.055)},a=b=>Math.max(0,Math.min(255,Math.round(b*255))),[c,l,i]=n(t,e,r),[u,h,f]=o(c,l,i),p=s(u),d=s(h),m=s(f);return{r:a(p),g:a(d),b:a(m)}}function P(t,e,r){const n=(c,l,i)=>{const m=(c+16)/116,b=l/500+m,y=m-i/200,$=b**3>.008856?b**3:(116*b-16)/903.3,w=c>903.3*.008856?((c+16)/116)**3:c/903.3,R=y**3>.008856?y**3:(116*y-16)/903.3;return[$*.95047,w*1,R*1.08883]},o=(c,l,i)=>{const u=[[2.493496911941425,-.9313836179191239,-.40271078445071684],[-.8294889695615747,1.7626640603183463,.023624685841943577],[.03584583024378447,-.07617238926804182,.9568845240076872]],h=u[0][0]*c+u[0][1]*l+u[0][2]*i,f=u[1][0]*c+u[1][1]*l+u[1][2]*i,p=u[2][0]*c+u[2][1]*l+u[2][2]*i;return[h,f,p]},s=c=>{const l=c<0?-1:1,i=Math.abs(c);return i<=.0031308?l*12.92*i:l*(1.055*Math.pow(i,.4166666666666667)-.055)},a=c=>Math.max(0,Math.min(255,Math.round(c*255)));try{const[c,l,i]=n(t,e,r),[u,h,f]=o(c,l,i),p=s(u),d=s(h),m=s(f);return{r:Math.max(0,Math.min(1,p)),g:Math.max(0,Math.min(1,d)),b:Math.max(0,Math.min(1,m))}}catch(c){console.warn("P3\u8F6C\u6362\u5931\u8D25:",c);const l=C(t,e,r);return{r:l.r/255,g:l.g/255,b:l.b/255}}}function x(t,e,r,n,o=null){if(!n.enableP3||!z()){const s=C(t,e,r);return o!==null?`rgba(${s.r}, ${s.g}, ${s.b}, ${o})`:`rgb(${s.r}, ${s.g}, ${s.b})`}else{const s=P(t,e,r);return o!==null?`color(display-p3 ${s.r.toFixed(4)} ${s.g.toFixed(4)} ${s.b.toFixed(4)} / ${o})`:`color(display-p3 ${s.r.toFixed(4)} ${s.g.toFixed(4)} ${s.b.toFixed(4)})`}}function L(t,e){let r=t;return e.enableMath&&(r=ae(r,e)),(e.convertLabToRGB||e.convertLchToRGB)&&(r=ce(r,e)),r}function fe(t,e){const r=t.split(`
|
|
12
|
+
`),n={},o=new Map;let s="",a=null,c=!1,l=0;for(let i of r){const u=i.trim(),h=u.match(/^\$([a-zA-Z0-9_-]+)\s*:\s*(.+?);?$/);if(h){const[,f,p]=h,d=L(U(p.trim(),{...n,...a?o.get(a)||{}:{}}),e);a?(o.has(a)||o.set(a,{}),o.get(a)[f]=d):n[f]=d;continue}if(u.endsWith("{")){a=u.slice(0,-1).trim(),c=!0,s+=i+`
|
|
13
|
+
`;continue}if(u==="}"){if(c&&a&&o.has(a)){const f=o.get(a),p=" ".repeat(l);for(const[d,m]of Object.entries(f))s+=`${p} --${d}: ${m};
|
|
14
|
+
`}c=!1,a=null}s+=i+`
|
|
15
|
+
`,i.includes("{")&&(l+=e.indentSize),i.includes("}")&&(l=Math.max(0,l-e.indentSize))}return{globalVariables:n,selectorVariables:o,cssWithoutVars:s.trim()}}function pe(t,e,r,n){const o=t.split(`
|
|
16
|
+
`),s=[],a=[];let c=0;for(let l=0;l<o.length;l++){const i=o[l],u=i.trim();if(!u)continue;const h=i.match(/^(\s*)/)[0].length;if(u==="}"){if(s.length>0){const f=s.pop();if(s.length>0){const p=s[s.length-1];p.children||(p.children=[]),p.children.push(f)}else a.push(f)}continue}if(u.endsWith("{")){const p={selector:u.slice(0,-1).trim(),properties:[],children:[]};s.push(p);continue}if(!u.includes("{")&&!u.includes("}")&&u.includes(":")){if(s.length>0){const f=s[s.length-1];N(u,r,n).forEach(d=>{const m=Object.keys(d)[0],b=L(d[m],e);f.properties.push(b===""?m:`${m}: ${b}`)})}continue}}for(;s.length>0;){const l=s.pop();if(s.length===0)a.push(l);else{const i=s[s.length-1];i.children||(i.children=[]),i.children.push(l)}}return G(a,e,"",r,n)}function G(t,e,r="",n,o){let s="";const a=r.startsWith("@");for(const c of t){const l=c.selector.startsWith("@");let i=(a?" ".repeat(e.indentSize):"")+c.selector;if(l&&(i=c.selector+` {
|
|
17
|
+
`),r&&(i.includes("&")?i=i.replace(/&/g,r):i.trim().startsWith(":")?i=r+i:i=r+(a?"":" ")+i),c.properties.length>0){s+=(l?"":i)+` {
|
|
18
|
+
`;for(const u of c.properties)N(u,n,o).forEach(f=>{const p=Object.keys(f)[0],d=L(f[p],e);s+=(a?" ".repeat(e.indentSize):"")+" ".repeat(e.indentSize)+(d===""?p:`${p}: ${d};
|
|
19
|
+
`)});s+=a?" ".repeat(e.indentSize)+`}
|
|
20
20
|
`:`}
|
|
21
21
|
|
|
22
|
-
`}c.children&&c.children.length>0&&(
|
|
22
|
+
`}c.children&&c.children.length>0&&(s+=G(c.children,e,i,n,o)),a&&(s+=`}
|
|
23
23
|
|
|
24
|
-
`)}return
|
|
24
|
+
`)}return s.trim()+`
|
|
25
25
|
|
|
26
|
-
`}function
|
|
26
|
+
`}function U(t,e){return t.replace(/\$([a-zA-Z0-9_-]+)/g,(r,n)=>e[n]?`var(--${n})`:r)}function he(t,e,r,n){let o=t;return o=o.replace(/(?:\$\$|\$)([a-zA-Z0-9_-]+)/g,(s,a)=>s.startsWith("$$")?`attr(${a})`:`var(--${a})`),o=L(o,n),o}function de(t,e){if(Object.keys(t).length===0)return"";const r=Object.entries(t).map(([n,o])=>{const s=L(U(o,t),e);return" ".repeat(e.indentSize)+`--${n}: ${s};`}).join(`
|
|
27
27
|
`);return`${e.rootSelector} {
|
|
28
|
-
${
|
|
28
|
+
${r}
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
`}function
|
|
32
|
-
`);let
|
|
33
|
-
`}return
|
|
31
|
+
`}function me(t,e,r){let n="";const o=t.split(`
|
|
32
|
+
`);let s=null;for(let a of o){const c=a.trim();c.endsWith("{")&&(s=c.slice(0,-1).trim()),c==="}"&&s&&(s=null),n+=L(a,r)+`
|
|
33
|
+
`}return n.trim()}async function Z(t,e){const r=/@import\s+([^;]+?)\s*;/g;return(async o=>{const s=[],a=o.replace(r,(c,l)=>{const i=ge(l,e).then(u=>Z(u,e)).catch(u=>(console.warn(`\u65E0\u6CD5\u5BFC\u5165CSS\u6587\u4EF6: ${l}`,u),""));return s.push(i),`__IMPORT_PLACEHOLDER_${s.length-1}__`});if(s.length>0){const c=await Promise.all(s);let l=a;for(let i=0;i<c.length;i++)l=l.replace(`__IMPORT_PLACEHOLDER_${i}__`,c[i]);return l}return a})(t)}async function ge(t,e){if(e.importCache&&V.has(t))return V.get(t);const r=e.importBaseUrl?new URL(t,e.importBaseUrl).href:t;let n;if(typeof process<"u"&&process.versions&&process.versions.node&&typeof require<"u")try{const s=require("fs"),a=require("path");let c=r;r.startsWith(".")&&(c=a.join(process.cwd(),r)),n=s.readFileSync(c,"utf-8")}catch(s){throw new Error(`Node.js\u6587\u4EF6\u8BFB\u53D6\u5931\u8D25: ${r} - ${s.message}`)}else try{const s=new AbortController,a=setTimeout(()=>s.abort(),e.importTimeout),c=await fetch(r,{signal:s.signal,headers:{Accept:"text/css"}});if(clearTimeout(a),!c.ok)throw new Error(`HTTP\u9519\u8BEF: ${c.status} ${c.statusText}`);n=await c.text()}catch(s){throw s.name==="AbortError"?new Error(`\u5BFC\u5165\u8D85\u65F6: ${r}`):new Error(`\u65E0\u6CD5\u83B7\u53D6CSS: ${r} - ${s.message}`)}return e.importCache&&V.set(t,n),n}async function T(t,e={}){let{config:r,css:n}=K(t);const o={...g,...e,...r};n=await Z(n,o);let s=n,a=new Map;if(o.enableAlias){const{aliases:y,css:$}=J(s);a=y,s=$}let c=s,l=new Map;if(o.enableMacros){const{macros:y,css:$}=j(c,o);l=y,c=$;for(const[w,R]of l)M.set(w,R)}let i=c;o.enableMacros&&l.size>0&&(i=ee(i,l,o));const{globalVariables:u,selectorVariables:h,cssWithoutVars:f}=fe(i,o);let p=f.trim();if(o.enableNesting&&f.includes("{"))try{p=pe(f,o,a,l)}catch(y){console.warn("\u5D4C\u5957\u89E3\u6790\u5931\u8D25\uFF0C\u4F7F\u7528\u539F\u59CBCSS:",y)}const d=de(u,o);let m=p;h.size>0&&(m=me(p,h,o)),m=he(m,u,h,o);for(const[y,$]of v)try{$.enable&&(m=$.convert(m,o))}catch(w){console.error("\u63D2\u4EF6\u5904\u7406\u5931\u8D25:",w)}return d+m}function X(t={}){const e={...g,...t},r=document.querySelectorAll(`style[${e.styleTagAttribute||"e"}]`);return r.forEach(n=>{let o=n.textContent;(async()=>{try{n.getAttribute("src")&&(o="@import "+n.getAttribute("src")+";");const a=await T(o,e),c=document.createElement("style");c.textContent=a,n.parentNode.insertBefore(c,n.nextSibling),e.preserveOriginal?n.style.display="none":n.remove()}catch(a){console.error("\u5904\u7406style\u6807\u7B7E\u5931\u8D25:",a)}})()}),r.length}function q(t={}){g={...g,...t}}function k(t,e={}){const r={...g,...e};if(t)return(async()=>{try{const n=await T(t,r),o=document.createElement("style");return o.textContent=n,document.head.appendChild(o),o}catch(n){return console.error("\u5E94\u7528CSS\u5931\u8D25:",n),null}})();document.readyState==="loading"?document.addEventListener("DOMContentLoaded",()=>{X(r)}):X(r)}const be={convert:T,apply:k,config:q,supportsP3:z(),detectP3Support:z,imports:{clearCache:function(){V.clear()},setBaseUrl:function(t){g.importBaseUrl=t},setCacheEnabled:function(t){g.importCache=t},setTimeout:function(t){g.importTimeout=t}},plugins:new Proxy({},{set(t,e,r){return(r.created??(()=>{}))(),v.set(e,r),!0},get(t,e){return v.get(e)},deleteProperty(t,e){return v.get(e)?.deleted(),v.delete(e),!0},has(t,e){return v.has(e)},ownKeys(){return Array.from(v.keys())},getOwnPropertyDescriptor(t,e){if(v.has(e))return{enumerable:!0,configurable:!0,writable:!0,value:v.get(e)}}}),aliases:{add:function(t,e){E.set(t,e)},remove:function(t){E.delete(t)},getAll:function(){return Array.from(E.entries())},clear:function(){E.clear()}},macros:{define:function(t,e,r=[]){if(typeof e=="function"){const o=e.toString().match(/{([\s\S]*)}/);o&&M.set(t,{params:r.map(s=>typeof s=="string"?{name:s}:s),body:o[1].trim()})}else M.set(t,{params:r.map(n=>typeof n=="string"?{name:n}:n),body:e})},call:function(t,...e){const r=M.get(t);if(!r)throw new Error(`\u672A\u5B9A\u4E49\u7684\u5B8F: ${t}`);const n=new Map;for(let o=0;o<r.params.length;o++){const s=r.params[o],a=o<e.length?e[o]:s.defaultValue;if(a===void 0&&s.defaultValue===null)throw new Error(`\u7F3A\u5C11\u5FC5\u9700\u53C2\u6570: ${s.name}`);n.set(s.name,a!==void 0?a:"")}return B(r.body,n,g)},getAll:function(){return Array.from(M.entries())},remove:function(t){M.delete(t)},clear:function(){M.clear()},parse:function(t){const{macros:e}=j(t,g);for(const[r,n]of e)M.set(r,n)}},math:{evaluate:function(t){return _(t,g)},parseUnit:H,round:oe,test:function(t,e={}){const r={...g,...e};try{const n=_(t,r);return{success:!0,expression:t,result:n,parsed:H(n.replace(/^calc\(|\)$/g,""))}}catch(n){return{success:!1,expression:t,error:n.message}}}},colorUtils:{labToRGB:C,lchToLab:S,lchToRGB:function(t,e,r){const n=S(t,e,r);return C(n.L,n.a,n.b)},labToP3:P,lchToP3:function(t,e,r){const n=S(t,e,r);return P(n.L,n.a,n.b)},parseHexLab:function(t){const e=t.match(/lab#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/i);if(!e)return null;const r=e[1],n=e[2],o=e[3],s=parseInt(r,16)/255*100,a=(parseInt(n,16)-128)*1.5,c=(parseInt(o,16)-128)*1.5;return C(s,a,c)},parseHexLch:function(t){const e=t.match(/lch#([0-9a-f]{2})([0-9a-f]{2})(\d{1,3})/i);if(!e)return null;const r=e[1],n=e[2],o=e[3],s=parseInt(r,16)/255*100,a=parseInt(n,16)/255*150,c=parseInt(o)/100*360,l=S(s,a,c);return C(l.L,l.a,l.b)},generateColor:function(t,e,r,n=null,o=!0){return x(t,e,r,{enableP3:o},n)},parseColor:function(t){try{const e=t.match(/lab\(\s*([\d.]+)(%?)\s+([\d.-]+)\s+([\d.-]+)(?:\s*\/\s*([\d.%]+))?\s*\)/i);if(e){let n=parseFloat(e[1]);const o=parseFloat(e[3]),s=parseFloat(e[4]),a=e[5]?e[5].includes("%")?parseFloat(e[5])/100:parseFloat(e[5]):null,c=x(n,o,s,g,a);return{L:n,A:o,B:s,alpha:a,rgb:C(n,o,s),p3:P(n,o,s),colorString:c}}const r=t.match(/lch\(\s*([\d.]+)(%?)\s+([\d.]+)\s+([\d.]+)(deg)?(?:\s*\/\s*([\d.%]+))?\s*\)/i);if(r){let n=parseFloat(r[1]);const o=parseFloat(r[3]);let s=parseFloat(r[4]);const a=r[6]?r[6].includes("%")?parseFloat(r[6])/100:parseFloat(r[6]):null,c=S(n,o,s),l=x(c.L,c.a,c.b,g,a);return{L:n,C:o,H:s,alpha:a,lab:c,rgb:C(c.L,c.a,c.b),p3:P(c.L,c.a,c.b),colorString:l}}return null}catch(e){return console.warn("\u65E0\u6CD5\u89E3\u6790\u989C\u8272:",t,e),null}}}},F=async function(...t){if(t.length>1||t[0]&&t[0].raw)return await ye(...t);const e=t[0];if(typeof e=="string"){const r=await T(e,{...g,...t[1]});return r&&typeof r.then=="function",r}return typeof e=="object"&&e!==null?(g={...g,...e},F):t.length===0?k():F};async function ye(t,...e){let r=t[0];for(let o=0;o<e.length;o++){const s=e[o];let a="";typeof s=="function"?a=s():Array.isArray(s)?a=s.join(" "):a=String(s??""),r+=a+t[o+1]}const n=await T(r,g);return n&&typeof n.then=="function",n}return Object.assign(F,be),Object.setPrototypeOf(F,Function.prototype),typeof window<"u"&&(k(),Object.defineProperty(window.HTMLElement.prototype,"cssVar",{get(){const t=this;return new Proxy(()=>{},{get(e,r){const n=r.startsWith("--")?r:`--${r}`;return t.style.getPropertyValue(n)},set(e,r,n){const o=r.startsWith("--")?r:`--${r}`;return t.style.setProperty(o,n),!0},apply(e,r,n){const o=n[0],s=n[1],a=o.startsWith("--")?o:`--${o}`;if(s===void 0)return t.style.getPropertyValue(a);t.style.setProperty(a,s)}})}})),F})();if(typeof define=="function"&&define.amd)define([],g=>styimat);else if(typeof module=="object"&&module.exports)module.exports=styimat;else{const g=globalThis??(typeof self<"u"&&self)??(typeof window<"u"&&window)??global??{};g.styimat=styimat}
|
package/dist/styimat.min.mjs
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* MIT License
|
|
3
3
|
* Copyright (c) 2025 王小玗
|
|
4
|
-
*/const
|
|
5
|
-
`),
|
|
6
|
-
`)}}function
|
|
7
|
-
`),
|
|
8
|
-
`)}}function
|
|
9
|
-
`),o=[];let
|
|
10
|
-
`)}),
|
|
11
|
-
`)}}function
|
|
12
|
-
`),
|
|
13
|
-
`;continue}if(u==="}"){if(c&&a&&o.has(a)){const
|
|
14
|
-
`}c=!1,a=null}
|
|
15
|
-
`,i.includes("{")&&(l+=e.indentSize),i.includes("}")&&(l=Math.max(0,l-e.indentSize))}return{globalVariables:
|
|
16
|
-
`),
|
|
17
|
-
`),
|
|
18
|
-
`;for(const u of c.properties)
|
|
19
|
-
`)});
|
|
4
|
+
*/const V=(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},A=null;const E=new Map,B=new Map,M=new Map,v=new Map;function J(t){const e={...g},r=t.split(`
|
|
5
|
+
`),n=[];for(let o of r){const s=o.trim();if(s.startsWith("#")){const a=s.substring(1).trim(),c=a.indexOf(" ");if(c!==-1){const l=a.substring(0,c).trim(),i=a.substring(c+1).trim(),u=re(l);u in e?i==="true"||i==="false"?e[u]=i==="true":!isNaN(i)&&i.trim()!==""?e[u]=Number(i):e[u]=i:console.warn(`\u672A\u77E5\u7684\u914D\u7F6E\u9879: ${l}`)}else console.warn(`\u65E0\u6548\u7684\u914D\u7F6E\u884C: ${s}`)}else n.push(o)}return{config:e,css:n.join(`
|
|
6
|
+
`)}}function ee(t){const e=new Map,r=t.split(`
|
|
7
|
+
`),n=[];for(let o of r){const s=o.trim();if(s.startsWith("@alias")){const a=s.match(/^@alias\s+([a-zA-Z0-9_-]+)\s+(.+?)\s*;$/);if(a){const c=a[1],l=a[2];e.set(c,l);continue}}n.push(o)}return{aliases:e,css:n.join(`
|
|
8
|
+
`)}}function N(t,e){if(!e.enableMacros)return{macros:new Map,css:t};const r=new Map,n=t.split(`
|
|
9
|
+
`),o=[];let s=!1,a="",c=[],l=[],i=0;for(let u of n){const h=u.trim();if(!s&&h.startsWith("@macro")){const f=h.match(/^@macro\s+([a-zA-Z0-9_-]+)\s*\(([^)]*)\)\s*\{$/);if(f){s=!0,a=f[1],c=f[2].split(",").map(p=>p.trim()).filter(p=>p).map(p=>{const d=p.split(":").map(m=>m.trim());return{name:d[0].slice(1),defaultValue:d[1]||null}}),l=[],i=1;continue}}if(s){for(const f of u)f==="{"&&i++,f==="}"&&i--;i===0?(r.set(a,{params:c,body:l.join(`
|
|
10
|
+
`)}),s=!1,a="",c=[],l=[]):l.push(u);continue}o.push(u)}return{macros:r,css:o.join(`
|
|
11
|
+
`)}}function te(t,e,r){if(!r.enableMacros||e.size===0)return t;const n=Array.from(e.keys()).map(a=>a.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")).join("|"),o=new RegExp(`@(${n})\\s*\\(([^)]*)\\)\\s*;?`,"g");return(a=>{let c=a,l=!1;do l=!1,c=c.replace(o,(i,u,h)=>{l=!0;const f=e.get(u);if(!f)return console.warn(`\u672A\u5B9A\u4E49\u7684\u5B8F: ${u}`),i;const p=z(h,f.params);return W(f.body,p,r)});while(l);return c})(t)}function z(t,e){const r=new Map,n=ne(t);for(let o=0;o<e.length;o++){const s=e[o];let a;o<n.length?a=n[o]:s.defaultValue!==null?a=s.defaultValue:(console.warn(`\u5B8F\u8C03\u7528\u7F3A\u5C11\u5FC5\u9700\u53C2\u6570: ${s.name}`),a=""),r.set(s.name,a)}return n.length>e.length&&console.warn("\u5B8F\u8C03\u7528\u4F20\u9012\u4E86\u8FC7\u591A\u53C2\u6570\uFF0C\u591A\u4F59\u7684\u53C2\u6570\u5C06\u88AB\u5FFD\u7565"),r}function ne(t){const e=[];let r="",n=!1,o="",s=0,a=0;for(let c=0;c<t.length;c++){const l=t[c];(l==='"'||l==="'")&&!n?(n=!0,o=l):l===o&&n&&(n=!1,o=""),n||(l==="("&&s++,l===")"&&s--,l==="["&&a++,l==="]"&&a--),l===","&&!n&&s===0&&a===0?(e.push(r.trim()),r=""):r+=l}return r.trim()&&e.push(r.trim()),e}function W(t,e,r){let n=t;for(const[o,s]of e){const a=new RegExp(`\\$${o}`,"g");n=n.replace(a,s)}if(r.enableMacros){const o=Array.from(M.keys()).map(s=>s.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")).join("|");if(o){const s=new RegExp(`@(${o})\\s*\\(([^)]*)\\)\\s*;?`,"g");let a;do a=!1,n=n.replace(s,(c,l,i)=>{a=!0;const u=M.get(l);if(!u)return c;const h=z(i,u.params);return W(u.body,h,r)});while(a)}}return n=L(n,r),n}function re(t){return t.replace(/-([a-z])/g,function(e,r){return r.toUpperCase()})}function _(){if(A!==null)return A;if(typeof window>"u"||!window.CSS)return A=!1,!1;try{A=CSS.supports("color","color(display-p3 1 0 0)")}catch(t){console.warn("P3\u652F\u6301\u68C0\u6D4B\u5931\u8D25:",t),A=!1}return A}function H(t,e,r){let n=t.trim();if(n.endsWith(";")&&(n=n.slice(0,-1)),!n)return[];const o=[];let s="",a=0,c=!1,l="";for(let u=0;u<n.length;u++){const h=n[u];(h==='"'||h==="'")&&!c?(c=!0,l=h):h===l&&c&&(c=!1,l=""),c||(h==="("?a++:h===")"&&a--),h===";"&&!c&&a===0?s.trim()&&(o.push(s.trim()),s=""):s+=h}s.trim()&&o.push(s.trim());const i=[];for(const u of o){if(!u.trim())continue;const h=se(u);if(h===-1){console.warn(`\u65E0\u6548\u7684CSS\u58F0\u660E: "${u}"`);continue}let f=u.substring(0,h).trim(),p=u.substring(h+1).trim();if(p.endsWith(";")&&(p=p.slice(0,-1).trim()),e.get(f)&&(f=e.get(f)),f.startsWith("@")&&r.get(f.slice(1))){const d=r.get(f.slice(1)),m=z(p.split(" ").join(","),d.params);f=W(d.body,m,D),p=""}i.push({[f]:p})}return i}function se(t){let e=!1,r="";for(let n=0;n<t.length;n++){const o=t[n];if((o==='"'||o==="'")&&!e?(e=!0,r=o):o===r&&e&&(e=!1,r=""),o===":"&&!e)return n}return-1}function k(t,e){if(!e.enableMath)return`math(${t})`;try{let r=t.replace(/\s+/g,"");return oe(r,e.enableMath)}catch(r){return console.warn("math()\u8868\u8FBE\u5F0F\u89E3\u6790\u5931\u8D25:",t,r),`calc(${t})`}}function oe(t,e){return`calc(${t.replace(/([\da-z])([+-])(-?[\da-z])/g,"$1 $2 $3").replace(/([\da-z])([+-])(-?[\da-z])/g,"$1 $2 $3")})`}function G(t){const e=t.match(/^([\d.]+)([a-zA-Z%]*)$/);if(!e)throw new Error(`\u65E0\u6CD5\u89E3\u6790\u503C: ${t}`);const r=parseFloat(e[1]),n=e[2]||"";return{value:r,unit:n}}function Ce(t,e){return t.unit===e.unit||!t.unit&&!e.unit?{value:t.value*e.value,unit:t.unit||e.unit}:{value:`${t.value}${t.unit}*${e.value}${e.unit}`,unit:""}}function Se(t,e){if(e.value===0)throw new Error("\u9664\u4EE5\u96F6");return t.unit&&!e.unit?{value:t.value/e.value,unit:t.unit}:t.unit===e.unit?{value:t.value/e.value,unit:""}:{value:`${t.value}${t.unit}/${e.value}${e.unit}`,unit:""}}function xe(t,e){return t.unit===e.unit?{value:t.value+e.value,unit:t.unit}:{value:`${t.value}${t.unit}+${e.value}${e.unit}`,unit:""}}function Le(t,e){return t.unit===e.unit?{value:t.value-e.value,unit:t.unit}:{value:`${t.value}${t.unit}-${e.value}${e.unit}`,unit:""}}function ae(t,e=6){const r=Math.pow(10,e),o=(Math.round(t*r)/r).toString();return o.includes(".")?o.replace(/(\.\d*?)0+$/,"$1").replace(/\.$/,""):o}function ce(t,e){if(!e.enableMath)return t;let r=t;const n=/math\(([^)]+)\)/gi,o=a=>a.replace(n,(c,l)=>k(l,e));let s;do s=r,r=o(r);while(r!==s&&r.includes("math("));return r}function le(t,e){if(!e.convertLabToRGB&&!e.convertLchToRGB)return t;let r=t;r=ie(r,e);const n=/(lab|lch)\([^)]+\)/gi,o=new Map,s=c=>c.replace(n,l=>{if(o.has(l))return o.get(l);let i=l;return l.toLowerCase().startsWith("lab(")?e.convertLabToRGB&&(i=ue(l,e)):l.toLowerCase().startsWith("lch(")&&e.convertLchToRGB&&(i=fe(l,e)),o.set(l,i),i});let a;do a=r,r=s(r);while(r!==a);return r}function ie(t,e){let r=t;const n=/lab#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/gi,o=/lch#([0-9a-f]{2})([0-9a-f]{2})(\d{1,3})/gi,s=new Map;return r=r.replace(n,(a,c,l,i)=>{if(s.has(a))return s.get(a);try{const u=parseInt(c,16)/255*100,h=(parseInt(l,16)-128)*1.5,f=(parseInt(i,16)-128)*1.5,p=x(u,h,f,e);return s.set(a,p),p}catch(u){return console.warn(`\u65E0\u6CD5\u89E3\u6790lab#\u5341\u516D\u8FDB\u5236\u989C\u8272: ${a}`,u),a}}),r=r.replace(o,(a,c,l,i)=>{if(s.has(a))return s.get(a);try{const u=parseInt(c,16)/255*100,h=parseInt(l,16)/255*150,f=parseInt(i)/100*360,p=S(u,h,f),d=x(p.L,p.a,p.b,e);return s.set(a,d),d}catch(u){return console.warn(`\u65E0\u6CD5\u89E3\u6790lch#\u5341\u516D\u8FDB\u5236\u989C\u8272: ${a}`,u),a}}),r}function ue(t,e){const r=/lab\(\s*([\d.]+)(%?)\s+([\d.-]+)\s+([\d.-]+)(?:\s*\/\s*([\d.%]+))?\s*\)/i,n=t.match(r);if(!n)return t;try{let o=parseFloat(n[1]);n[2]==="%"?o=o:(o<0&&(o=0),o>100&&(o=100));const s=parseFloat(n[3]),a=parseFloat(n[4]),c=n[5]!==void 0?n[5].includes("%")?parseFloat(n[5])/100:parseFloat(n[5]):null;return x(o,s,a,e,c)}catch(o){return console.warn(`\u65E0\u6CD5\u8F6C\u6362LAB\u989C\u8272: ${t}`,o),t}}function fe(t,e){const r=/lch\(\s*([\d.]+)(%?)\s+([\d.]+)\s+([\d.]+)(deg)?(?:\s*\/\s*([\d.%]+))?\s*\)/i,n=t.match(r);if(!n)return t;try{let o=parseFloat(n[1]);n[2]==="%"?o=o:(o<0&&(o=0),o>100&&(o=100));const s=parseFloat(n[3]);let a=parseFloat(n[4]);s<0&&console.warn(`LCH\u4E2D\u7684C\u503C\u4E0D\u80FD\u4E3A\u8D1F: ${s}`),a=(a%360+360)%360;const c=S(o,s,a),l=n[6]!==void 0?n[6].includes("%")?parseFloat(n[6])/100:parseFloat(n[6]):null;return x(c.L,c.a,c.b,e,l)}catch(o){return console.warn(`\u65E0\u6CD5\u8F6C\u6362LCH\u989C\u8272: ${t}`,o),t}}function S(t,e,r){const n=r*Math.PI/180,o=e*Math.cos(n),s=e*Math.sin(n);return{L:t,a:o,b:s}}function C(t,e,r){const n=(b,y,$)=>{const K=(b+16)/116,O=y/500+K,j=K-$/200,we=O**3>.008856?O**3:(116*O-16)/903.3,Me=b>903.3*.008856?((b+16)/116)**3:b/903.3,ve=j**3>.008856?j**3:(116*j-16)/903.3;return[we*.95047,Me*1,ve*1.08883]},o=(b,y,$)=>{const w=[[3.2404542,-1.5371385,-.4985314],[-.969266,1.8760108,.041556],[.0556434,-.2040259,1.0572252]],R=w[0][0]*b+w[0][1]*y+w[0][2]*$,Y=w[1][0]*b+w[1][1]*y+w[1][2]*$,Q=w[2][0]*b+w[2][1]*y+w[2][2]*$;return[R,Y,Q]},s=b=>{const y=b<0?-1:1,$=Math.abs(b);return $<=.0031308?y*12.92*$:y*(1.055*Math.pow($,.4166666666666667)-.055)},a=b=>Math.max(0,Math.min(255,Math.round(b*255))),[c,l,i]=n(t,e,r),[u,h,f]=o(c,l,i),p=s(u),d=s(h),m=s(f);return{r:a(p),g:a(d),b:a(m)}}function P(t,e,r){const n=(c,l,i)=>{const m=(c+16)/116,b=l/500+m,y=m-i/200,$=b**3>.008856?b**3:(116*b-16)/903.3,w=c>903.3*.008856?((c+16)/116)**3:c/903.3,R=y**3>.008856?y**3:(116*y-16)/903.3;return[$*.95047,w*1,R*1.08883]},o=(c,l,i)=>{const u=[[2.493496911941425,-.9313836179191239,-.40271078445071684],[-.8294889695615747,1.7626640603183463,.023624685841943577],[.03584583024378447,-.07617238926804182,.9568845240076872]],h=u[0][0]*c+u[0][1]*l+u[0][2]*i,f=u[1][0]*c+u[1][1]*l+u[1][2]*i,p=u[2][0]*c+u[2][1]*l+u[2][2]*i;return[h,f,p]},s=c=>{const l=c<0?-1:1,i=Math.abs(c);return i<=.0031308?l*12.92*i:l*(1.055*Math.pow(i,.4166666666666667)-.055)},a=c=>Math.max(0,Math.min(255,Math.round(c*255)));try{const[c,l,i]=n(t,e,r),[u,h,f]=o(c,l,i),p=s(u),d=s(h),m=s(f);return{r:Math.max(0,Math.min(1,p)),g:Math.max(0,Math.min(1,d)),b:Math.max(0,Math.min(1,m))}}catch(c){console.warn("P3\u8F6C\u6362\u5931\u8D25:",c);const l=C(t,e,r);return{r:l.r/255,g:l.g/255,b:l.b/255}}}function x(t,e,r,n,o=null){if(!n.enableP3||!_()){const s=C(t,e,r);return o!==null?`rgba(${s.r}, ${s.g}, ${s.b}, ${o})`:`rgb(${s.r}, ${s.g}, ${s.b})`}else{const s=P(t,e,r);return o!==null?`color(display-p3 ${s.r.toFixed(4)} ${s.g.toFixed(4)} ${s.b.toFixed(4)} / ${o})`:`color(display-p3 ${s.r.toFixed(4)} ${s.g.toFixed(4)} ${s.b.toFixed(4)})`}}function L(t,e){let r=t;return e.enableMath&&(r=ce(r,e)),(e.convertLabToRGB||e.convertLchToRGB)&&(r=le(r,e)),r}function pe(t,e){const r=t.split(`
|
|
12
|
+
`),n={},o=new Map;let s="",a=null,c=!1,l=0;for(let i of r){const u=i.trim(),h=u.match(/^\$([a-zA-Z0-9_-]+)\s*:\s*(.+?);?$/);if(h){const[,f,p]=h,d=L(Z(p.trim(),{...n,...a?o.get(a)||{}:{}}),e);a?(o.has(a)||o.set(a,{}),o.get(a)[f]=d):n[f]=d;continue}if(u.endsWith("{")){a=u.slice(0,-1).trim(),c=!0,s+=i+`
|
|
13
|
+
`;continue}if(u==="}"){if(c&&a&&o.has(a)){const f=o.get(a),p=" ".repeat(l);for(const[d,m]of Object.entries(f))s+=`${p} --${d}: ${m};
|
|
14
|
+
`}c=!1,a=null}s+=i+`
|
|
15
|
+
`,i.includes("{")&&(l+=e.indentSize),i.includes("}")&&(l=Math.max(0,l-e.indentSize))}return{globalVariables:n,selectorVariables:o,cssWithoutVars:s.trim()}}function he(t,e,r,n){const o=t.split(`
|
|
16
|
+
`),s=[],a=[];let c=0;for(let l=0;l<o.length;l++){const i=o[l],u=i.trim();if(!u)continue;const h=i.match(/^(\s*)/)[0].length;if(u==="}"){if(s.length>0){const f=s.pop();if(s.length>0){const p=s[s.length-1];p.children||(p.children=[]),p.children.push(f)}else a.push(f)}continue}if(u.endsWith("{")){const p={selector:u.slice(0,-1).trim(),properties:[],children:[]};s.push(p);continue}if(!u.includes("{")&&!u.includes("}")&&u.includes(":")){if(s.length>0){const f=s[s.length-1];H(u,r,n).forEach(d=>{const m=Object.keys(d)[0],b=L(d[m],e);f.properties.push(b===""?m:`${m}: ${b}`)})}continue}}for(;s.length>0;){const l=s.pop();if(s.length===0)a.push(l);else{const i=s[s.length-1];i.children||(i.children=[]),i.children.push(l)}}return U(a,e,"",r,n)}function U(t,e,r="",n,o){let s="";const a=r.startsWith("@");for(const c of t){const l=c.selector.startsWith("@");let i=(a?" ".repeat(e.indentSize):"")+c.selector;if(l&&(i=c.selector+` {
|
|
17
|
+
`),r&&(i.includes("&")?i=i.replace(/&/g,r):i.trim().startsWith(":")?i=r+i:i=r+(a?"":" ")+i),c.properties.length>0){s+=(l?"":i)+` {
|
|
18
|
+
`;for(const u of c.properties)H(u,n,o).forEach(f=>{const p=Object.keys(f)[0],d=L(f[p],e);s+=(a?" ".repeat(e.indentSize):"")+" ".repeat(e.indentSize)+(d===""?p:`${p}: ${d};
|
|
19
|
+
`)});s+=a?" ".repeat(e.indentSize)+`}
|
|
20
20
|
`:`}
|
|
21
21
|
|
|
22
|
-
`}c.children&&c.children.length>0&&(
|
|
22
|
+
`}c.children&&c.children.length>0&&(s+=U(c.children,e,i,n,o)),a&&(s+=`}
|
|
23
23
|
|
|
24
|
-
`)}return
|
|
24
|
+
`)}return s.trim()+`
|
|
25
25
|
|
|
26
|
-
`}function
|
|
26
|
+
`}function Z(t,e){return t.replace(/\$([a-zA-Z0-9_-]+)/g,(r,n)=>e[n]?`var(--${n})`:r)}function de(t,e,r,n){let o=t;return o=o.replace(/(?:\$\$|\$)([a-zA-Z0-9_-]+)/g,(s,a)=>s.startsWith("$$")?`attr(${a})`:`var(--${a})`),o=L(o,n),o}function me(t,e){if(Object.keys(t).length===0)return"";const r=Object.entries(t).map(([n,o])=>{const s=L(Z(o,t),e);return" ".repeat(e.indentSize)+`--${n}: ${s};`}).join(`
|
|
27
27
|
`);return`${e.rootSelector} {
|
|
28
|
-
${
|
|
28
|
+
${r}
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
`}function
|
|
32
|
-
`);let
|
|
33
|
-
`}return
|
|
31
|
+
`}function ge(t,e,r){let n="";const o=t.split(`
|
|
32
|
+
`);let s=null;for(let a of o){const c=a.trim();c.endsWith("{")&&(s=c.slice(0,-1).trim()),c==="}"&&s&&(s=null),n+=L(a,r)+`
|
|
33
|
+
`}return n.trim()}async function X(t,e){const r=/@import\s+([^;]+?)\s*;/g;return(async o=>{const s=[],a=o.replace(r,(c,l)=>{const i=be(l,e).then(u=>X(u,e)).catch(u=>(console.warn(`\u65E0\u6CD5\u5BFC\u5165CSS\u6587\u4EF6: ${l}`,u),""));return s.push(i),`__IMPORT_PLACEHOLDER_${s.length-1}__`});if(s.length>0){const c=await Promise.all(s);let l=a;for(let i=0;i<c.length;i++)l=l.replace(`__IMPORT_PLACEHOLDER_${i}__`,c[i]);return l}return a})(t)}async function be(t,e){if(e.importCache&&E.has(t))return E.get(t);const r=e.importBaseUrl?new URL(t,e.importBaseUrl).href:t;let n;if(typeof process<"u"&&process.versions&&process.versions.node&&typeof require<"u")try{const s=require("fs"),a=require("path");let c=r;r.startsWith(".")&&(c=a.join(process.cwd(),r)),n=s.readFileSync(c,"utf-8")}catch(s){throw new Error(`Node.js\u6587\u4EF6\u8BFB\u53D6\u5931\u8D25: ${r} - ${s.message}`)}else try{const s=new AbortController,a=setTimeout(()=>s.abort(),e.importTimeout),c=await fetch(r,{signal:s.signal,headers:{Accept:"text/css"}});if(clearTimeout(a),!c.ok)throw new Error(`HTTP\u9519\u8BEF: ${c.status} ${c.statusText}`);n=await c.text()}catch(s){throw s.name==="AbortError"?new Error(`\u5BFC\u5165\u8D85\u65F6: ${r}`):new Error(`\u65E0\u6CD5\u83B7\u53D6CSS: ${r} - ${s.message}`)}return e.importCache&&E.set(t,n),n}async function T(t,e={}){let{config:r,css:n}=J(t);const o={...g,...e,...r};n=await X(n,o);let s=n,a=new Map;if(o.enableAlias){const{aliases:y,css:$}=ee(s);a=y,s=$}let c=s,l=new Map;if(o.enableMacros){const{macros:y,css:$}=N(c,o);l=y,c=$;for(const[w,R]of l)M.set(w,R)}let i=c;o.enableMacros&&l.size>0&&(i=te(i,l,o));const{globalVariables:u,selectorVariables:h,cssWithoutVars:f}=pe(i,o);let p=f.trim();if(o.enableNesting&&f.includes("{"))try{p=he(f,o,a,l)}catch(y){console.warn("\u5D4C\u5957\u89E3\u6790\u5931\u8D25\uFF0C\u4F7F\u7528\u539F\u59CBCSS:",y)}const d=me(u,o);let m=p;h.size>0&&(m=ge(p,h,o)),m=de(m,u,h,o);for(const[y,$]of v)try{$.enable&&(m=$.convert(m,o))}catch(w){console.error("\u63D2\u4EF6\u5904\u7406\u5931\u8D25:",w)}return d+m}function q(t={}){const e={...g,...t},r=document.querySelectorAll(`style[${e.styleTagAttribute||"e"}]`);return r.forEach(n=>{let o=n.textContent;(async()=>{try{n.getAttribute("src")&&(o="@import "+n.getAttribute("src")+";");const a=await T(o,e),c=document.createElement("style");c.textContent=a,n.parentNode.insertBefore(c,n.nextSibling),e.preserveOriginal?n.style.display="none":n.remove()}catch(a){console.error("\u5904\u7406style\u6807\u7B7E\u5931\u8D25:",a)}})()}),r.length}function D(t={}){g={...g,...t}}function I(t,e={}){const r={...g,...e};if(t)return(async()=>{try{const n=await T(t,r),o=document.createElement("style");return o.textContent=n,document.head.appendChild(o),o}catch(n){return console.error("\u5E94\u7528CSS\u5931\u8D25:",n),null}})();document.readyState==="loading"?document.addEventListener("DOMContentLoaded",()=>{q(r)}):q(r)}const ye={convert:T,apply:I,config:D,supportsP3:_(),detectP3Support:_,imports:{clearCache:function(){E.clear()},setBaseUrl:function(t){g.importBaseUrl=t},setCacheEnabled:function(t){g.importCache=t},setTimeout:function(t){g.importTimeout=t}},plugins:new Proxy({},{set(t,e,r){return(r.created??(()=>{}))(),v.set(e,r),!0},get(t,e){return v.get(e)},deleteProperty(t,e){return v.get(e)?.deleted(),v.delete(e),!0},has(t,e){return v.has(e)},ownKeys(){return Array.from(v.keys())},getOwnPropertyDescriptor(t,e){if(v.has(e))return{enumerable:!0,configurable:!0,writable:!0,value:v.get(e)}}}),aliases:{add:function(t,e){B.set(t,e)},remove:function(t){B.delete(t)},getAll:function(){return Array.from(B.entries())},clear:function(){B.clear()}},macros:{define:function(t,e,r=[]){if(typeof e=="function"){const o=e.toString().match(/{([\s\S]*)}/);o&&M.set(t,{params:r.map(s=>typeof s=="string"?{name:s}:s),body:o[1].trim()})}else M.set(t,{params:r.map(n=>typeof n=="string"?{name:n}:n),body:e})},call:function(t,...e){const r=M.get(t);if(!r)throw new Error(`\u672A\u5B9A\u4E49\u7684\u5B8F: ${t}`);const n=new Map;for(let o=0;o<r.params.length;o++){const s=r.params[o],a=o<e.length?e[o]:s.defaultValue;if(a===void 0&&s.defaultValue===null)throw new Error(`\u7F3A\u5C11\u5FC5\u9700\u53C2\u6570: ${s.name}`);n.set(s.name,a!==void 0?a:"")}return W(r.body,n,g)},getAll:function(){return Array.from(M.entries())},remove:function(t){M.delete(t)},clear:function(){M.clear()},parse:function(t){const{macros:e}=N(t,g);for(const[r,n]of e)M.set(r,n)}},math:{evaluate:function(t){return k(t,g)},parseUnit:G,round:ae,test:function(t,e={}){const r={...g,...e};try{const n=k(t,r);return{success:!0,expression:t,result:n,parsed:G(n.replace(/^calc\(|\)$/g,""))}}catch(n){return{success:!1,expression:t,error:n.message}}}},colorUtils:{labToRGB:C,lchToLab:S,lchToRGB:function(t,e,r){const n=S(t,e,r);return C(n.L,n.a,n.b)},labToP3:P,lchToP3:function(t,e,r){const n=S(t,e,r);return P(n.L,n.a,n.b)},parseHexLab:function(t){const e=t.match(/lab#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/i);if(!e)return null;const r=e[1],n=e[2],o=e[3],s=parseInt(r,16)/255*100,a=(parseInt(n,16)-128)*1.5,c=(parseInt(o,16)-128)*1.5;return C(s,a,c)},parseHexLch:function(t){const e=t.match(/lch#([0-9a-f]{2})([0-9a-f]{2})(\d{1,3})/i);if(!e)return null;const r=e[1],n=e[2],o=e[3],s=parseInt(r,16)/255*100,a=parseInt(n,16)/255*150,c=parseInt(o)/100*360,l=S(s,a,c);return C(l.L,l.a,l.b)},generateColor:function(t,e,r,n=null,o=!0){return x(t,e,r,{enableP3:o},n)},parseColor:function(t){try{const e=t.match(/lab\(\s*([\d.]+)(%?)\s+([\d.-]+)\s+([\d.-]+)(?:\s*\/\s*([\d.%]+))?\s*\)/i);if(e){let n=parseFloat(e[1]);const o=parseFloat(e[3]),s=parseFloat(e[4]),a=e[5]?e[5].includes("%")?parseFloat(e[5])/100:parseFloat(e[5]):null,c=x(n,o,s,g,a);return{L:n,A:o,B:s,alpha:a,rgb:C(n,o,s),p3:P(n,o,s),colorString:c}}const r=t.match(/lch\(\s*([\d.]+)(%?)\s+([\d.]+)\s+([\d.]+)(deg)?(?:\s*\/\s*([\d.%]+))?\s*\)/i);if(r){let n=parseFloat(r[1]);const o=parseFloat(r[3]);let s=parseFloat(r[4]);const a=r[6]?r[6].includes("%")?parseFloat(r[6])/100:parseFloat(r[6]):null,c=S(n,o,s),l=x(c.L,c.a,c.b,g,a);return{L:n,C:o,H:s,alpha:a,lab:c,rgb:C(c.L,c.a,c.b),p3:P(c.L,c.a,c.b),colorString:l}}return null}catch(e){return console.warn("\u65E0\u6CD5\u89E3\u6790\u989C\u8272:",t,e),null}}}},F=async function(...t){if(t.length>1||t[0]&&t[0].raw)return await $e(...t);const e=t[0];if(typeof e=="string"){const r=await T(e,{...g,...t[1]});return r&&typeof r.then=="function",r}return typeof e=="object"&&e!==null?(g={...g,...e},F):t.length===0?I():F};async function $e(t,...e){let r=t[0];for(let o=0;o<e.length;o++){const s=e[o];let a="";typeof s=="function"?a=s():Array.isArray(s)?a=s.join(" "):a=String(s??""),r+=a+t[o+1]}const n=await T(r,g);return n&&typeof n.then=="function",n}return Object.assign(F,ye),Object.setPrototypeOf(F,Function.prototype),typeof window<"u"&&(I(),Object.defineProperty(window.HTMLElement.prototype,"cssVar",{get(){const t=this;return new Proxy(()=>{},{get(e,r){const n=r.startsWith("--")?r:`--${r}`;return t.style.getPropertyValue(n)},set(e,r,n){const o=r.startsWith("--")?r:`--${r}`;return t.style.setProperty(o,n),!0},apply(e,r,n){const o=n[0],s=n[1],a=o.startsWith("--")?o:`--${o}`;if(s===void 0)return t.style.getPropertyValue(a);t.style.setProperty(a,s)}})}})),F})();if(typeof define=="function"&&define.amd)define([],g=>V);else if(typeof module=="object"&&module.exports)module.exports=V;else{const g=globalThis??(typeof self<"u"&&self)??(typeof window<"u"&&window)??global??{};g.styimat=V}export default V;export const{convert,apply,config,supportsP3,detectP3Support,imports,plugins,aliases,macros,math,colorUtils}=V;
|
package/dist/styimat.mjs
CHANGED
|
@@ -29,7 +29,6 @@ const styimat = (function() {
|
|
|
29
29
|
convertLchToRGB: true,
|
|
30
30
|
enableP3: true,
|
|
31
31
|
enableMath: true,
|
|
32
|
-
mathPrecision: 6,
|
|
33
32
|
importBaseUrl: '',
|
|
34
33
|
importCache: true,
|
|
35
34
|
importTimeout: 5000,
|
|
@@ -510,71 +509,23 @@ const styimat = (function() {
|
|
|
510
509
|
|
|
511
510
|
try {
|
|
512
511
|
let cleanExpr = expression.replace(/\s+/g, '');
|
|
513
|
-
const result = parseMathExpression(cleanExpr, config);
|
|
514
512
|
|
|
515
|
-
|
|
516
|
-
return processUnitExpression(cleanExpr, result);
|
|
517
|
-
} else {
|
|
518
|
-
return roundNumber(result, config.mathPrecision);
|
|
519
|
-
}
|
|
513
|
+
return parseMathExpression(cleanExpr, config.enableMath);
|
|
520
514
|
} catch (error) {
|
|
521
515
|
console.warn('math()表达式解析失败:', expression, error);
|
|
522
|
-
return `
|
|
516
|
+
return `calc(${expression})`;
|
|
523
517
|
}
|
|
524
518
|
}
|
|
525
519
|
|
|
526
520
|
/**
|
|
527
521
|
* 解析数学表达式
|
|
528
522
|
*/
|
|
529
|
-
function parseMathExpression(expr,
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
if (end === -1) break;
|
|
535
|
-
|
|
536
|
-
const inner = expr.substring(start + 1, end);
|
|
537
|
-
const innerResult = parseMathExpression(inner, config);
|
|
538
|
-
|
|
539
|
-
expr = expr.substring(0, start) + innerResult + expr.substring(end + 1);
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
return evaluateSimpleExpression(expr, config);
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
/**
|
|
546
|
-
* 评估简单表达式(无括号)
|
|
547
|
-
*/
|
|
548
|
-
function evaluateSimpleExpression(expr, config) {
|
|
549
|
-
const operators = [
|
|
550
|
-
{ regex: /([\d.]+(?:[a-zA-Z%]+)?)\*([\d.]+(?:[a-zA-Z%]+)?)/, handler: multiply },
|
|
551
|
-
{ regex: /([\d.]+(?:[a-zA-Z%]+)?)\/([\d.]+(?:[a-zA-Z%]+)?)/, handler: divide },
|
|
552
|
-
{ regex: /([\d.]+(?:[a-zA-Z%]+)?)\+([\d.]+(?:[a-zA-Z%]+)?)/, handler: add },
|
|
553
|
-
{ regex: /([\d.]+(?:[a-zA-Z%]+)?)-([\d.]+(?:[a-zA-Z%]+)?)/, handler: subtract }
|
|
554
|
-
];
|
|
555
|
-
|
|
556
|
-
let lastExpr = expr;
|
|
557
|
-
|
|
558
|
-
for (const op of operators) {
|
|
559
|
-
let match;
|
|
560
|
-
while ((match = expr.match(op.regex)) !== null) {
|
|
561
|
-
const left = parseValueWithUnit(match[1]);
|
|
562
|
-
const right = parseValueWithUnit(match[2]);
|
|
563
|
-
const result = op.handler(left, right);
|
|
564
|
-
|
|
565
|
-
expr = expr.substring(0, match.index) +
|
|
566
|
-
result.value +
|
|
567
|
-
expr.substring(match.index + match[0].length);
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
if (expr !== lastExpr) {
|
|
572
|
-
return evaluateSimpleExpression(expr, config);
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
const parsed = parseValueWithUnit(expr);
|
|
576
|
-
return parsed.value;
|
|
523
|
+
function parseMathExpression(expr, enableMath) {
|
|
524
|
+
return `calc(${expr
|
|
525
|
+
.replace(/([\da-z])([+-])(-?[\da-z])/g, '$1 $2 $3')
|
|
526
|
+
.replace(/([\da-z])([+-])(-?[\da-z])/g, '$1 $2 $3')})`;
|
|
577
527
|
}
|
|
528
|
+
|
|
578
529
|
|
|
579
530
|
/**
|
|
580
531
|
* 解析带单位的数值
|
|
@@ -592,30 +543,6 @@ const styimat = (function() {
|
|
|
592
543
|
return { value, unit };
|
|
593
544
|
}
|
|
594
545
|
|
|
595
|
-
/**
|
|
596
|
-
* 检查表达式是否包含单位
|
|
597
|
-
*/
|
|
598
|
-
function hasUnits(expr) {
|
|
599
|
-
return /[a-zA-Z%]/.test(expr);
|
|
600
|
-
}
|
|
601
|
-
|
|
602
|
-
/**
|
|
603
|
-
* 处理带单位的表达式
|
|
604
|
-
*/
|
|
605
|
-
function processUnitExpression(originalExpr, result) {
|
|
606
|
-
const unitMatch = originalExpr.match(/([a-zA-Z%]+)(?!.*[a-zA-Z%])/);
|
|
607
|
-
|
|
608
|
-
if (unitMatch) {
|
|
609
|
-
return result + unitMatch[1];
|
|
610
|
-
}
|
|
611
|
-
|
|
612
|
-
if (originalExpr.includes('%')) {
|
|
613
|
-
return result + '%';
|
|
614
|
-
}
|
|
615
|
-
|
|
616
|
-
return result + 'px';
|
|
617
|
-
}
|
|
618
|
-
|
|
619
546
|
// 数学运算函数
|
|
620
547
|
function multiply(a, b) {
|
|
621
548
|
if (a.unit === b.unit || (!a.unit && !b.unit)) {
|
|
@@ -682,6 +609,7 @@ const styimat = (function() {
|
|
|
682
609
|
|
|
683
610
|
const processMath = (str) => {
|
|
684
611
|
return str.replace(mathRegex, (match, expression) => {
|
|
612
|
+
|
|
685
613
|
return evaluateMathExpression(expression, config);
|
|
686
614
|
});
|
|
687
615
|
};
|
|
@@ -1456,86 +1384,68 @@ const styimat = (function() {
|
|
|
1456
1384
|
/**
|
|
1457
1385
|
* 主转换函数
|
|
1458
1386
|
*/
|
|
1459
|
-
function convert(cssText, customConfig = {}) {
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1387
|
+
async function convert(cssText, customConfig = {}) {
|
|
1388
|
+
let { config: headerConfig, css: cleanedCSS } = parseConfigHeader(cssText);
|
|
1389
|
+
const finalConfig = { ...defaultConfig, ...customConfig, ...headerConfig };
|
|
1390
|
+
cleanedCSS = await processImports(cleanedCSS, finalConfig);
|
|
1391
|
+
|
|
1392
|
+
let cssWithAliases = cleanedCSS;
|
|
1393
|
+
let aliases = new Map();
|
|
1394
|
+
if (finalConfig.enableAlias) {
|
|
1395
|
+
const { aliases: parsedAliases, css: cssWithoutAliases } = parseAliasStatements(cssWithAliases);
|
|
1396
|
+
aliases = parsedAliases;
|
|
1397
|
+
cssWithAliases = cssWithoutAliases;
|
|
1398
|
+
}
|
|
1471
1399
|
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1400
|
+
let cssWithMacros = cssWithAliases;
|
|
1401
|
+
let macros = new Map();
|
|
1402
|
+
if (finalConfig.enableMacros) {
|
|
1403
|
+
const { macros: parsedMacros, css: cssWithoutMacros } = parseMacroStatements(cssWithMacros, finalConfig);
|
|
1404
|
+
macros = parsedMacros;
|
|
1405
|
+
cssWithMacros = cssWithoutMacros;
|
|
1478
1406
|
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
}
|
|
1482
|
-
}
|
|
1483
|
-
|
|
1484
|
-
let cssAfterMacros = cssWithMacros;
|
|
1485
|
-
if (finalConfig.enableMacros && macros.size > 0) {
|
|
1486
|
-
cssAfterMacros = applyMacroCalls(cssAfterMacros, macros, finalConfig);
|
|
1487
|
-
}
|
|
1488
|
-
|
|
1489
|
-
const { globalVariables, selectorVariables, cssWithoutVars } =
|
|
1490
|
-
extractVariablesAndCSS(cssAfterMacros, finalConfig);
|
|
1491
|
-
|
|
1492
|
-
let processedCSS = cssWithoutVars.trim();
|
|
1493
|
-
if (finalConfig.enableNesting && cssWithoutVars.includes('{')) {
|
|
1494
|
-
try {
|
|
1495
|
-
processedCSS = parseNestedRules(cssWithoutVars, finalConfig, aliases, macros);
|
|
1496
|
-
} catch (error) {
|
|
1497
|
-
console.warn('嵌套解析失败,使用原始CSS:', error);
|
|
1498
|
-
}
|
|
1407
|
+
for (const [name, macro] of macros) {
|
|
1408
|
+
macroRegistry.set(name, macro);
|
|
1499
1409
|
}
|
|
1410
|
+
}
|
|
1411
|
+
|
|
1412
|
+
let cssAfterMacros = cssWithMacros;
|
|
1413
|
+
if (finalConfig.enableMacros && macros.size > 0) {
|
|
1414
|
+
cssAfterMacros = applyMacroCalls(cssAfterMacros, macros, finalConfig);
|
|
1415
|
+
}
|
|
1416
|
+
|
|
1417
|
+
const { globalVariables, selectorVariables, cssWithoutVars } =
|
|
1418
|
+
extractVariablesAndCSS(cssAfterMacros, finalConfig);
|
|
1500
1419
|
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1420
|
+
let processedCSS = cssWithoutVars.trim();
|
|
1421
|
+
if (finalConfig.enableNesting && cssWithoutVars.includes('{')) {
|
|
1422
|
+
try {
|
|
1423
|
+
processedCSS = parseNestedRules(cssWithoutVars, finalConfig, aliases, macros);
|
|
1424
|
+
} catch (error) {
|
|
1425
|
+
console.warn('嵌套解析失败,使用原始CSS:', error);
|
|
1506
1426
|
}
|
|
1427
|
+
}
|
|
1428
|
+
|
|
1429
|
+
const rootRule = generateRootRule(globalVariables, finalConfig);
|
|
1430
|
+
|
|
1431
|
+
let finalCSS = processedCSS;
|
|
1432
|
+
if (selectorVariables.size > 0) {
|
|
1433
|
+
finalCSS = injectSelectorVariables(processedCSS, selectorVariables, finalConfig);
|
|
1434
|
+
}
|
|
1435
|
+
|
|
1436
|
+
finalCSS = replaceVariableUses(finalCSS, globalVariables, selectorVariables, finalConfig);
|
|
1507
1437
|
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
} catch (error) {
|
|
1514
|
-
console.error('插件处理失败:', error);
|
|
1515
|
-
}
|
|
1438
|
+
for (const [name, plugin] of pluginMap) {
|
|
1439
|
+
try {
|
|
1440
|
+
if(plugin.enable) finalCSS = plugin.convert(finalCSS, finalConfig);
|
|
1441
|
+
} catch (error) {
|
|
1442
|
+
console.error('插件处理失败:', error);
|
|
1516
1443
|
}
|
|
1444
|
+
}
|
|
1517
1445
|
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
return fullCSS;
|
|
1521
|
-
};
|
|
1522
|
-
|
|
1523
|
-
const hasImports = cssText && /@import\s+([^;]+?)\s*;/g.test(cssText);
|
|
1524
|
-
const finalConfig = { ...defaultConfig, ...customConfig };
|
|
1446
|
+
let fullCSS = rootRule + finalCSS;
|
|
1525
1447
|
|
|
1526
|
-
|
|
1527
|
-
return syncConvert(cssText, finalConfig);
|
|
1528
|
-
} else {
|
|
1529
|
-
return (async () => {
|
|
1530
|
-
try {
|
|
1531
|
-
const processedWithImports = await processImports(cssText, finalConfig);
|
|
1532
|
-
return syncConvert(processedWithImports, finalConfig);
|
|
1533
|
-
} catch (error) {
|
|
1534
|
-
console.error('@import处理失败:', error);
|
|
1535
|
-
return syncConvert(cssText, finalConfig);
|
|
1536
|
-
}
|
|
1537
|
-
})();
|
|
1538
|
-
}
|
|
1448
|
+
return fullCSS;
|
|
1539
1449
|
}
|
|
1540
1450
|
|
|
1541
1451
|
/**
|
|
@@ -1587,28 +1497,18 @@ const styimat = (function() {
|
|
|
1587
1497
|
const config = { ...defaultConfig, ...customConfig };
|
|
1588
1498
|
|
|
1589
1499
|
if (cssText) {
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
return null;
|
|
1603
|
-
}
|
|
1604
|
-
})();
|
|
1605
|
-
} else {
|
|
1606
|
-
const converted = convert(cssText, config);
|
|
1607
|
-
const styleEl = document.createElement('style');
|
|
1608
|
-
styleEl.textContent = converted;
|
|
1609
|
-
document.head.appendChild(styleEl);
|
|
1610
|
-
return styleEl;
|
|
1611
|
-
}
|
|
1500
|
+
return (async () => {
|
|
1501
|
+
try {
|
|
1502
|
+
const converted = await convert(cssText, config);
|
|
1503
|
+
const styleEl = document.createElement('style');
|
|
1504
|
+
styleEl.textContent = converted;
|
|
1505
|
+
document.head.appendChild(styleEl);
|
|
1506
|
+
return styleEl;
|
|
1507
|
+
} catch (error) {
|
|
1508
|
+
console.error('应用CSS失败:', error);
|
|
1509
|
+
return null;
|
|
1510
|
+
}
|
|
1511
|
+
})();
|
|
1612
1512
|
} else {
|
|
1613
1513
|
if (document.readyState === 'loading') {
|
|
1614
1514
|
document.addEventListener('DOMContentLoaded', () => {
|
|
@@ -1874,15 +1774,15 @@ const styimat = (function() {
|
|
|
1874
1774
|
};
|
|
1875
1775
|
|
|
1876
1776
|
// 创建主函数
|
|
1877
|
-
const styimat = function(...args) {
|
|
1777
|
+
const styimat = async function(...args) {
|
|
1878
1778
|
if (args.length > 1 || (args[0] && args[0].raw)) {
|
|
1879
|
-
return handleTemplateTag(...args);
|
|
1779
|
+
return await handleTemplateTag(...args);
|
|
1880
1780
|
}
|
|
1881
1781
|
|
|
1882
1782
|
const firstArg = args[0];
|
|
1883
1783
|
|
|
1884
1784
|
if (typeof firstArg === 'string') {
|
|
1885
|
-
const result = convert(firstArg, { ...defaultConfig, ...args[1] });
|
|
1785
|
+
const result = await convert(firstArg, { ...defaultConfig, ...args[1] });
|
|
1886
1786
|
|
|
1887
1787
|
if (result && typeof result.then === 'function') {
|
|
1888
1788
|
return result;
|
|
@@ -1902,7 +1802,7 @@ const styimat = (function() {
|
|
|
1902
1802
|
return styimat;
|
|
1903
1803
|
};
|
|
1904
1804
|
|
|
1905
|
-
function handleTemplateTag(strings, ...values) {
|
|
1805
|
+
async function handleTemplateTag(strings, ...values) {
|
|
1906
1806
|
let cssText = strings[0];
|
|
1907
1807
|
|
|
1908
1808
|
for (let i = 0; i < values.length; i++) {
|
|
@@ -1920,7 +1820,7 @@ const styimat = (function() {
|
|
|
1920
1820
|
cssText += result + strings[i + 1];
|
|
1921
1821
|
}
|
|
1922
1822
|
|
|
1923
|
-
const result = convert(cssText, defaultConfig);
|
|
1823
|
+
const result = await convert(cssText, defaultConfig);
|
|
1924
1824
|
|
|
1925
1825
|
if (result && typeof result.then === 'function') {
|
|
1926
1826
|
return result;
|