@master/css 2.0.0-rc.47 → 2.0.0-rc.48

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.
Files changed (72) hide show
  1. package/dist/common.cjs +1 -1
  2. package/dist/common.mjs +1 -1
  3. package/dist/config/functions.d.ts +1 -1
  4. package/dist/config/index.cjs +3 -3
  5. package/dist/config/index.d.ts +3 -48
  6. package/dist/config/index.mjs +3 -3
  7. package/dist/config/modes.d.ts +1 -1
  8. package/dist/config/{syntaxes.cjs → rules.cjs} +282 -282
  9. package/dist/config/rules.d.ts +282 -0
  10. package/dist/config/{syntaxes.mjs → rules.mjs} +282 -282
  11. package/dist/core.cjs +225 -607
  12. package/dist/core.d.ts +36 -67
  13. package/dist/core.mjs +225 -607
  14. package/dist/index.cjs +14 -6
  15. package/dist/index.d.ts +8 -4
  16. package/dist/index.mjs +7 -3
  17. package/dist/layer.cjs +107 -23
  18. package/dist/layer.d.ts +13 -27
  19. package/dist/layer.mjs +107 -23
  20. package/dist/non-layer.cjs +65 -0
  21. package/dist/non-layer.d.ts +12 -0
  22. package/dist/non-layer.mjs +63 -0
  23. package/dist/rule.cjs +8 -708
  24. package/dist/rule.d.ts +6 -147
  25. package/dist/rule.mjs +8 -708
  26. package/dist/shared/utils/find-native-css-rule-index.cjs +12 -0
  27. package/dist/shared/utils/find-native-css-rule-index.mjs +10 -0
  28. package/dist/syntax-layer.cjs +291 -0
  29. package/dist/syntax-layer.d.ts +17 -0
  30. package/dist/syntax-layer.mjs +289 -0
  31. package/dist/syntax-rule-type.cjs +27 -0
  32. package/dist/syntax-rule-type.d.ts +28 -0
  33. package/dist/syntax-rule-type.mjs +25 -0
  34. package/dist/syntax-rule.cjs +717 -0
  35. package/dist/syntax-rule.d.ts +47 -0
  36. package/dist/syntax-rule.mjs +715 -0
  37. package/dist/types/config.cjs +2 -0
  38. package/dist/types/config.d.ts +70 -0
  39. package/dist/types/config.mjs +1 -0
  40. package/dist/types/syntax.cjs +2 -0
  41. package/dist/types/syntax.d.ts +106 -0
  42. package/dist/types/syntax.mjs +1 -0
  43. package/dist/utils/are-rule-at-equal.cjs +7 -7
  44. package/dist/utils/are-rule-at-equal.d.ts +2 -2
  45. package/dist/utils/are-rule-at-equal.mjs +7 -7
  46. package/dist/utils/are-rule-modes-equal.cjs +2 -2
  47. package/dist/utils/are-rule-modes-equal.d.ts +2 -2
  48. package/dist/utils/are-rule-modes-equal.mjs +2 -2
  49. package/dist/utils/are-rule-selectors-equal.cjs +11 -11
  50. package/dist/utils/are-rule-selectors-equal.d.ts +2 -2
  51. package/dist/utils/are-rule-selectors-equal.mjs +11 -11
  52. package/dist/utils/are-rule-states-equal.cjs +2 -2
  53. package/dist/utils/are-rule-states-equal.d.ts +2 -2
  54. package/dist/utils/are-rule-states-equal.mjs +2 -2
  55. package/dist/utils/are-rules-duplicated.cjs +17 -0
  56. package/dist/utils/are-rules-duplicated.d.ts +2 -0
  57. package/dist/utils/are-rules-duplicated.mjs +15 -0
  58. package/dist/utils/autofill-solid-style.d.ts +2 -2
  59. package/dist/utils/extend-config.cjs +5 -5
  60. package/dist/utils/extend-config.d.ts +1 -1
  61. package/dist/utils/extend-config.mjs +5 -5
  62. package/dist/utils/generate-css.cjs +1 -1
  63. package/dist/utils/generate-css.mjs +1 -1
  64. package/dist/utils/is-core-rule.cjs +2 -2
  65. package/dist/utils/is-core-rule.mjs +2 -2
  66. package/dist/utils/reorder-for-readable-classes.cjs +11 -11
  67. package/dist/utils/reorder-for-readable-classes.mjs +11 -11
  68. package/package.json +1 -1
  69. package/dist/config/syntaxes.d.ts +0 -282
  70. package/dist/utils/are-syntaxes-duplicated.cjs +0 -17
  71. package/dist/utils/are-syntaxes-duplicated.d.ts +0 -2
  72. package/dist/utils/are-syntaxes-duplicated.mjs +0 -15
@@ -0,0 +1,715 @@
1
+ import cssEscape from './shared/utils/css-escape.mjs';
2
+ import SyntaxRuleType from './syntax-rule-type.mjs';
3
+ import { VALUE_DELIMITERS, UNIT_REGEX, BASE_UNIT_REGEX } from './common.mjs';
4
+ import { Rule } from './rule.mjs';
5
+
6
+ class SyntaxRule extends Rule {
7
+ name;
8
+ css;
9
+ registeredSyntax;
10
+ fixedClass;
11
+ at;
12
+ priority;
13
+ order;
14
+ type;
15
+ declarations;
16
+ layer;
17
+ animationNames;
18
+ variableNames;
19
+ constructor(name, css, registeredSyntax, fixedClass, mode){
20
+ super(name), this.name = name, this.css = css, this.registeredSyntax = registeredSyntax, this.fixedClass = fixedClass, this.at = {}, this.priority = -1, this.order = 0, this.type = 0, this.resolveValue = (valueComponents, unit, bypassVariableNames, bypassParsing)=>{
21
+ const { functions } = this.css.config;
22
+ let currentValue = '';
23
+ for (const eachValueComponent of valueComponents){
24
+ switch(eachValueComponent.type){
25
+ case 'function':
26
+ const functionDefinition = functions && functions[eachValueComponent.name];
27
+ if (functionDefinition?.transform && !eachValueComponent.bypassTransform) {
28
+ const result = functionDefinition.transform.call(this, this.resolveValue(eachValueComponent.children, functionDefinition.unit ?? unit, bypassVariableNames, bypassParsing || eachValueComponent.name === 'calc'), bypassVariableNames);
29
+ currentValue += eachValueComponent.token = eachValueComponent.text = typeof result === 'string' ? result : this.resolveValue(result, functionDefinition?.unit ?? unit, bypassVariableNames, bypassParsing);
30
+ } else {
31
+ currentValue += eachValueComponent.token = eachValueComponent.text = eachValueComponent.name + eachValueComponent.symbol + this.resolveValue(eachValueComponent.children, functionDefinition?.unit ?? unit, bypassVariableNames, bypassParsing) + VALUE_DELIMITERS[eachValueComponent.symbol];
32
+ }
33
+ break;
34
+ // todo: 應挪到 parseValues 階段處理才能支援 variables: { x: 'calc(20vw-30px)' } 這種情況,並且解析上可能會比較合理、精簡
35
+ case 'variable':
36
+ const variable = this.css.variables[eachValueComponent.name];
37
+ if (variable) {
38
+ const handleVariable = (normalHandler, varHandler)=>{
39
+ if (variable.modes) {
40
+ if (this.mode) {
41
+ const themeVariable = variable.modes[this.mode] ?? variable;
42
+ if (themeVariable?.value) {
43
+ normalHandler(themeVariable);
44
+ }
45
+ } else {
46
+ if (!this.variableNames) {
47
+ this.variableNames = [];
48
+ }
49
+ if (!this.variableNames.includes(eachValueComponent.name)) {
50
+ this.variableNames.push(eachValueComponent.name);
51
+ }
52
+ varHandler();
53
+ }
54
+ } else {
55
+ normalHandler(variable);
56
+ }
57
+ };
58
+ switch(variable.type){
59
+ case 'string':
60
+ handleVariable((variable)=>{
61
+ const valueComponents = [];
62
+ this.parseValues(valueComponents, 0, variable.value, unit, '', undefined, bypassParsing, [
63
+ ...bypassVariableNames,
64
+ eachValueComponent.name
65
+ ]);
66
+ currentValue += eachValueComponent.text = this.resolveValue(valueComponents, unit, [
67
+ ...bypassVariableNames,
68
+ eachValueComponent.name
69
+ ], bypassParsing);
70
+ }, ()=>{
71
+ currentValue += eachValueComponent.text = `var(--${eachValueComponent.name})`;
72
+ });
73
+ break;
74
+ case 'number':
75
+ handleVariable((variable)=>{
76
+ if (bypassParsing) {
77
+ currentValue += eachValueComponent.text = String(variable.value);
78
+ } else {
79
+ const valueComponent = this.parseValue(variable.value, unit);
80
+ currentValue += eachValueComponent.text = valueComponent.value + (valueComponent.unit ?? '');
81
+ }
82
+ }, ()=>{
83
+ currentValue += eachValueComponent.text = unit ? `calc(var(--${eachValueComponent.name}) / 16 * 1rem)` : `var(--${eachValueComponent.name})`;
84
+ });
85
+ break;
86
+ case 'color':
87
+ const alpha = eachValueComponent.alpha ? '/' + eachValueComponent.alpha : '';
88
+ handleVariable((variable)=>{
89
+ currentValue += eachValueComponent.text = `${variable['space']}(${variable.value}${alpha})`;
90
+ }, ()=>{
91
+ currentValue += eachValueComponent.text = `${variable.space}(var(--${eachValueComponent.name})${alpha})`;
92
+ });
93
+ break;
94
+ }
95
+ } else {
96
+ currentValue += eachValueComponent.text = `var(--${eachValueComponent.name}${eachValueComponent.fallback ? ',' + eachValueComponent.fallback : ''})`;
97
+ }
98
+ break;
99
+ case 'separator':
100
+ currentValue += eachValueComponent.text ? eachValueComponent.text : eachValueComponent.text = eachValueComponent.value;
101
+ break;
102
+ case 'number':
103
+ currentValue += eachValueComponent.text = eachValueComponent.value + (eachValueComponent.unit || '');
104
+ break;
105
+ default:
106
+ currentValue += eachValueComponent.text = eachValueComponent.value;
107
+ break;
108
+ }
109
+ }
110
+ return currentValue;
111
+ }, this.parseValues = (currentValueComponents, i, value, unit, endSymbol, parentFunctionName, bypassParsing = false, bypassVariableNames = [])=>{
112
+ const root = parentFunctionName === undefined;
113
+ const isVarFunction = !root && (parentFunctionName.endsWith('$') || parentFunctionName.endsWith('var'));
114
+ const checkIsString = (value)=>value === '\'' || value === '"';
115
+ const isString = checkIsString(endSymbol);
116
+ const separators = [
117
+ ','
118
+ ];
119
+ if (this.definition.separators?.length) {
120
+ separators.push(...this.definition.separators);
121
+ }
122
+ let currentValue = '';
123
+ const parse = ()=>{
124
+ if (currentValue) {
125
+ let handled = false;
126
+ if (!isVarFunction || currentValueComponents.length) {
127
+ const handleVariable = (variableName, alpha)=>{
128
+ const variable = Object.prototype.hasOwnProperty.call(this.variables, variableName) ? this.variables[variableName] : Object.prototype.hasOwnProperty.call(this.css.variables, variableName) ? this.css.variables[variableName] : undefined;
129
+ if (variable) {
130
+ const name = variable.name ?? variableName;
131
+ if (!bypassVariableNames.includes(name)) {
132
+ handled = true;
133
+ const valueComponent = {
134
+ type: 'variable',
135
+ name,
136
+ variable: this.css.variables[name],
137
+ token: currentValue
138
+ };
139
+ if (alpha) valueComponent.alpha = alpha;
140
+ currentValueComponents.push(valueComponent);
141
+ }
142
+ }
143
+ };
144
+ handleVariable(currentValue);
145
+ if (!handled) {
146
+ const [colorName, alpha] = currentValue.split('/');
147
+ handleVariable(colorName, alpha);
148
+ }
149
+ }
150
+ if (!handled) {
151
+ if (!isVarFunction) {
152
+ const result = BASE_UNIT_REGEX.exec(currentValue);
153
+ if (result) {
154
+ currentValue = String(+result[1] * (this.css.config.baseUnit ?? 1));
155
+ }
156
+ }
157
+ if (bypassParsing) {
158
+ currentValueComponents.push({
159
+ type: 'string',
160
+ value: currentValue,
161
+ token: currentValue
162
+ });
163
+ } else {
164
+ currentValueComponents.push({
165
+ ...this.parseValue(currentValue, unit),
166
+ token: currentValue
167
+ });
168
+ }
169
+ }
170
+ currentValue = '';
171
+ }
172
+ };
173
+ for(; i < value.length; i++){
174
+ const val = value[i];
175
+ if (val === endSymbol) {
176
+ if (isString) {
177
+ let count = 0;
178
+ for(let j = currentValue.length - 1;; j--){
179
+ if (currentValue[j] !== '\\') break;
180
+ count++;
181
+ }
182
+ if (count % 2) {
183
+ currentValue += val;
184
+ continue;
185
+ } else {
186
+ parse();
187
+ }
188
+ } else {
189
+ parse();
190
+ }
191
+ return i;
192
+ } else if (!isString && val in VALUE_DELIMITERS) {
193
+ const functionName = currentValue;
194
+ const newValueComponent = {
195
+ type: 'function',
196
+ name: functionName,
197
+ symbol: val,
198
+ children: [],
199
+ token: ''
200
+ };
201
+ currentValueComponents.push(newValueComponent);
202
+ currentValue = '';
203
+ const functionDefinition = val === '(' ? this.css.config.functions?.[functionName] : undefined;
204
+ i = this.parseValues(newValueComponent.children, ++i, value, functionDefinition?.unit ?? unit, VALUE_DELIMITERS[val], functionName || parentFunctionName || '', bypassParsing || functionName === 'calc');
205
+ } else if ((val === '|' || val === ' ') && endSymbol !== '}' && (!isString || parentFunctionName === 'path')) {
206
+ parse();
207
+ currentValueComponents.push({
208
+ type: 'separator',
209
+ value: ' ',
210
+ token: val
211
+ });
212
+ } else {
213
+ if (!isString) {
214
+ if (val === '.') {
215
+ if (isNaN(+value[i + 1])) {
216
+ if (root) break;
217
+ } else if (value[i - 1] === '-') {
218
+ currentValue += '0';
219
+ }
220
+ } else if (separators.includes(val)) {
221
+ parse();
222
+ currentValueComponents.push({
223
+ type: 'separator',
224
+ value: val,
225
+ text: (val === ',' ? '' : ' ') + val + (val === ',' ? '' : ' '),
226
+ token: val
227
+ });
228
+ continue;
229
+ } else if (root && (val === '#' && (currentValue || currentValueComponents.length && currentValueComponents[currentValueComponents.length - 1]['type'] !== 'separator') || [
230
+ '!',
231
+ '*',
232
+ '>',
233
+ '+',
234
+ '~',
235
+ ':',
236
+ '[',
237
+ '@',
238
+ '_'
239
+ ].includes(val))) {
240
+ break;
241
+ }
242
+ }
243
+ currentValue += val;
244
+ }
245
+ }
246
+ parse();
247
+ return i;
248
+ };
249
+ this.mode = mode;
250
+ this.layer = css.generalLayer;
251
+ Object.assign(this, registeredSyntax);
252
+ const { id, definition } = registeredSyntax;
253
+ const { analyze, transformValue, declare, transformValueComponents, create, type, unit } = definition;
254
+ this.type = type;
255
+ const { scope, important, modes } = css.config;
256
+ const { selectors, at, animations } = css;
257
+ if (create) create.call(this, name);
258
+ // 1. value / selectorToken
259
+ this.declarations = definition.declarations;
260
+ let stateToken;
261
+ let prefixToken;
262
+ if (this.type === SyntaxRuleType.Utility) {
263
+ // TODO: id 使用其他方式傳遞
264
+ stateToken = name.slice(id.length - 1);
265
+ } else {
266
+ let valueToken;
267
+ if (analyze) {
268
+ [valueToken, prefixToken] = analyze.call(this, name);
269
+ } else {
270
+ const indexOfColon = name.indexOf(':');
271
+ this.keyToken = name.slice(0, indexOfColon + 1);
272
+ valueToken = name.slice(indexOfColon + 1);
273
+ }
274
+ this.valueComponents = [];
275
+ const parsedValueIndex = this.parseValues(this.valueComponents, 0, valueToken, unit, '', undefined, false, definition.includeAnimations ? Object.keys(this.css.animations) : []);
276
+ this.valueToken = valueToken.slice(0, parsedValueIndex);
277
+ stateToken = valueToken.slice(parsedValueIndex);
278
+ }
279
+ // 2. !important
280
+ if (stateToken[0] === '!') {
281
+ this.important = true;
282
+ stateToken = stateToken.slice(1);
283
+ }
284
+ this.stateToken = stateToken;
285
+ // 3. prefix selector
286
+ const generateVendorSelectors = (selectorText, vendorSelectors)=>{
287
+ const transformSelector = (selectorText, selectorValues, selectors, matched)=>{
288
+ for (const [regexp, newSelectorTexts] of selectorValues){
289
+ if (regexp.test(selectorText)) {
290
+ for (const eachNewSelectorText of newSelectorTexts){
291
+ transformSelector(selectorText.replace(regexp, eachNewSelectorText), selectorValues, selectors, true);
292
+ }
293
+ return;
294
+ }
295
+ }
296
+ if (matched) {
297
+ selectors.push(selectorText);
298
+ }
299
+ };
300
+ const spacedSelectorToken = (selectorText)=>{
301
+ // 1. \'123\'
302
+ // 2. [href='http://localhost']
303
+ const transformedSelectorText = selectorText.split(/(\\'(?:.*?)[^\\]\\')(?=[*_>~+,)])|(\[[^=]+='(?:.*?)[^\\]'\])/).map((eachToken, i)=>i % 3 ? eachToken : eachToken.replace(/(^|[^_])_(?!_)/g, '$1 ')).join('');
304
+ const selectors = [];
305
+ let currentSelector = '';
306
+ for(let i = 0; i < transformedSelectorText.length; i++){
307
+ const char = transformedSelectorText[i];
308
+ if (char === '\\') {
309
+ currentSelector += char + transformedSelectorText[++i];
310
+ continue;
311
+ }
312
+ currentSelector += char;
313
+ }
314
+ if (currentSelector) {
315
+ selectors.push(currentSelector);
316
+ }
317
+ return selectors;
318
+ };
319
+ const transformedSelectors = [];
320
+ if ('' in selectors) {
321
+ transformSelector(selectorText, selectors[''], transformedSelectors, true);
322
+ } else {
323
+ transformedSelectors.push(selectorText);
324
+ }
325
+ const unspacedVendorSelectors = {};
326
+ for (const [vendor, selectorValues] of Object.entries(selectors)){
327
+ if (!vendor) continue;
328
+ const newUnspacedVendorSelectors = [];
329
+ for (const eachTransformedSelector of transformedSelectors){
330
+ transformSelector(eachTransformedSelector, selectorValues, newUnspacedVendorSelectors, false);
331
+ }
332
+ if (newUnspacedVendorSelectors.length) {
333
+ unspacedVendorSelectors[vendor] = newUnspacedVendorSelectors;
334
+ }
335
+ }
336
+ const insertVendorSelectors = (vendor, selectorTexts)=>{
337
+ const groupedSelectorTexts = selectorTexts.reduce((arr, eachSuffixSelector)=>{
338
+ arr.push(...spacedSelectorToken(eachSuffixSelector));
339
+ return arr;
340
+ }, []);
341
+ if (vendor in vendorSelectors) {
342
+ vendorSelectors[vendor].push(...groupedSelectorTexts);
343
+ } else {
344
+ vendorSelectors[vendor] = groupedSelectorTexts;
345
+ }
346
+ };
347
+ const vendors = Object.keys(unspacedVendorSelectors);
348
+ if (vendors.length) {
349
+ for (const eachVendor of vendors){
350
+ insertVendorSelectors(eachVendor, unspacedVendorSelectors[eachVendor]);
351
+ }
352
+ } else {
353
+ insertVendorSelectors('', transformedSelectors);
354
+ }
355
+ };
356
+ if (prefixToken) {
357
+ this.vendorPrefixSelectors = {};
358
+ generateVendorSelectors(prefixToken, this.vendorPrefixSelectors);
359
+ } else {
360
+ this.vendorPrefixSelectors = {
361
+ '': [
362
+ ''
363
+ ]
364
+ };
365
+ }
366
+ // 4. suffix selector
367
+ const stateTokens = stateToken.split('@');
368
+ const suffixSelector = stateTokens[0];
369
+ if (suffixSelector) {
370
+ this.vendorSuffixSelectors = {};
371
+ generateVendorSelectors(suffixSelector, this.vendorSuffixSelectors);
372
+ for (const suffixSelectors of Object.values(this.vendorSuffixSelectors)){
373
+ for (const eachSuffixSelector of suffixSelectors){
374
+ const SORTED_SELECTORS = [
375
+ ':disabled',
376
+ ':active',
377
+ ':focus',
378
+ ':hover'
379
+ ];
380
+ for(let i = 0; i < SORTED_SELECTORS.length; i++){
381
+ if (eachSuffixSelector.includes(SORTED_SELECTORS[i])) {
382
+ if (this.priority === -1 || this.priority > i) {
383
+ this.priority = i;
384
+ }
385
+ break;
386
+ }
387
+ }
388
+ }
389
+ }
390
+ } else {
391
+ this.vendorSuffixSelectors = {
392
+ '': [
393
+ ''
394
+ ]
395
+ };
396
+ }
397
+ // selector combinations
398
+ const suffixSelectorVendorsByPrefixSelectorVendor = {};
399
+ const isPrefixSelectorWithoutVendor = Object.prototype.hasOwnProperty.call(this.vendorPrefixSelectors, '');
400
+ const isSuffixSelectorWithoutVendor = Object.prototype.hasOwnProperty.call(this.vendorSuffixSelectors, '');
401
+ if (isPrefixSelectorWithoutVendor) {
402
+ suffixSelectorVendorsByPrefixSelectorVendor[''] = isSuffixSelectorWithoutVendor ? [
403
+ ''
404
+ ] : Object.keys(this.vendorSuffixSelectors);
405
+ } else {
406
+ if (isSuffixSelectorWithoutVendor) {
407
+ for(const vendor in this.vendorPrefixSelectors){
408
+ suffixSelectorVendorsByPrefixSelectorVendor[vendor] = [
409
+ ''
410
+ ];
411
+ }
412
+ } else {
413
+ for(const prefixSelectorVendor in this.vendorPrefixSelectors){
414
+ const suffixSelectorVendors = suffixSelectorVendorsByPrefixSelectorVendor[prefixSelectorVendor] = [];
415
+ if (Object.prototype.hasOwnProperty.call(this.vendorSuffixSelectors, prefixSelectorVendor)) {
416
+ suffixSelectorVendors.push(prefixSelectorVendor);
417
+ } else {
418
+ for(const suffixVendor in this.vendorSuffixSelectors){
419
+ suffixSelectorVendors.push(suffixVendor);
420
+ }
421
+ }
422
+ }
423
+ }
424
+ }
425
+ // 5. atTokens
426
+ for(let i = 1; i < stateTokens.length; i++){
427
+ const atToken = stateTokens[i];
428
+ this.atToken = (this.atToken || '') + '@' + atToken;
429
+ if (atToken) {
430
+ if (atToken === 'base') {
431
+ this.layer = css.baseLayer;
432
+ continue;
433
+ } else if (atToken === 'preset') {
434
+ this.layer = css.presetLayer;
435
+ continue;
436
+ } else if (atToken === 'rtl' || atToken === 'ltr') {
437
+ this.direction = atToken;
438
+ } else {
439
+ let queryType;
440
+ const atComponents = [];
441
+ // x font-face, counter-style, keyframes, font-feature-values, property, layer
442
+ const queryTypeRegExp = /^(media|supports|container|layer)/;
443
+ const atRuleResult = queryTypeRegExp.exec(atToken);
444
+ if (atRuleResult) {
445
+ queryType = atRuleResult[1];
446
+ atComponents.push({
447
+ type: 'arbitrary',
448
+ token: atToken,
449
+ value: atToken.slice(queryType.length).replace(/\|/g, ' ')
450
+ });
451
+ } else {
452
+ const analyzeToken = (atComponentToken)=>{
453
+ if (atComponentToken === '&') {
454
+ atComponents.push({
455
+ type: 'operator',
456
+ token: atComponentToken,
457
+ value: 'and'
458
+ });
459
+ } else if (atComponentToken.startsWith('')) {
460
+ const targetAt = at[atComponentToken];
461
+ if (targetAt && typeof targetAt === 'string') {
462
+ const match = targetAt.match(queryTypeRegExp);
463
+ queryType = match ? match[1] : '';
464
+ if (!queryType) throw new Error(`Invalid query '${atComponentToken}': '${targetAt}'`);
465
+ atComponents.push({
466
+ type: 'arbitrary',
467
+ value: targetAt.slice(match ? match[1].length + 1 : 0)
468
+ });
469
+ } else {
470
+ // todo: container queries
471
+ queryType = 'media';
472
+ let featureName = '';
473
+ let extremumOperator = '';
474
+ let correction = 0;
475
+ if (atComponentToken.startsWith('<=')) {
476
+ extremumOperator = '<=';
477
+ featureName = 'max-width';
478
+ } else if (atComponentToken.startsWith('>') && !atComponentToken.startsWith('>=')) {
479
+ extremumOperator = '>';
480
+ featureName = 'min-width';
481
+ correction = .02;
482
+ } else if (atComponentToken.startsWith('<')) {
483
+ extremumOperator = '<';
484
+ featureName = 'max-width';
485
+ correction = -0.02;
486
+ } else if (atComponentToken.startsWith('>=') || targetAt) {
487
+ extremumOperator = '>=';
488
+ featureName = 'min-width';
489
+ }
490
+ const token = extremumOperator ? atComponentToken.replace(extremumOperator, '') : atComponentToken;
491
+ const viewport = at[token];
492
+ switch(featureName){
493
+ case 'max-width':
494
+ case 'min-width':
495
+ if (typeof viewport === 'number') {
496
+ atComponents.push({
497
+ type: 'feature',
498
+ name: featureName,
499
+ valueType: 'number',
500
+ value: viewport + correction,
501
+ unit: 'px'
502
+ });
503
+ } else {
504
+ const valueComponent = this.parseValue(token, 'px');
505
+ if (valueComponent.type === 'number') {
506
+ atComponents.push({
507
+ type: 'feature',
508
+ name: featureName,
509
+ valueType: 'number',
510
+ value: valueComponent.value + correction,
511
+ unit: valueComponent.unit
512
+ });
513
+ } else {
514
+ atComponents.push({
515
+ type: 'feature',
516
+ name: featureName,
517
+ valueType: 'string',
518
+ value: token
519
+ });
520
+ }
521
+ }
522
+ break;
523
+ }
524
+ }
525
+ }
526
+ };
527
+ const isAmpersandIncluded = atToken.includes('&');
528
+ if (isAmpersandIncluded) {
529
+ const typeOrFeatureTokens = atToken.split(/(&|,)/);
530
+ for (const atComponentToken of typeOrFeatureTokens){
531
+ analyzeToken(atComponentToken);
532
+ }
533
+ } else {
534
+ analyzeToken(atToken);
535
+ }
536
+ if (!atComponents.length) {
537
+ if (!isAmpersandIncluded && modes?.[atToken]) {
538
+ this.mode = atToken;
539
+ continue;
540
+ }
541
+ }
542
+ }
543
+ if (queryType) {
544
+ const eachAtComponents = this.at[queryType];
545
+ if (eachAtComponents) {
546
+ eachAtComponents.push(...atComponents);
547
+ } else {
548
+ this.at[queryType] = atComponents;
549
+ }
550
+ }
551
+ }
552
+ }
553
+ }
554
+ // 7. value
555
+ let newValue;
556
+ if (this.valueComponents) {
557
+ if (transformValueComponents) {
558
+ this.valueComponents = transformValueComponents.call(this, this.valueComponents);
559
+ }
560
+ newValue = this.resolveValue(this.valueComponents, unit, [], false);
561
+ if (transformValue) {
562
+ newValue = transformValue.call(this, newValue);
563
+ }
564
+ if (declare) {
565
+ this.declarations = declare.call(this, newValue, this.valueComponents);
566
+ } else if (id) {
567
+ this.declarations = {
568
+ [id]: newValue
569
+ };
570
+ }
571
+ }
572
+ const propertiesText = [];
573
+ for(const propertyName in this.declarations){
574
+ const push = (propertyText)=>{
575
+ // animations
576
+ if (animations && (propertyText.startsWith('animation') || propertyText.startsWith('animation-name'))) {
577
+ const animationNames = propertyText.split(':')[1].split('!important')[0].split(' ').filter((eachValue)=>eachValue in this.css.animations && (!this.animationNames || !this.animationNames.includes(eachValue)));
578
+ if (animationNames.length) {
579
+ if (!this.animationNames) {
580
+ this.animationNames = [];
581
+ }
582
+ this.animationNames = animationNames;
583
+ }
584
+ }
585
+ propertiesText.push(propertyText + ((this.important || important) && !propertyText.endsWith('!important') ? '!important' : ''));
586
+ };
587
+ const keyToken = propertyName + ':';
588
+ const declarations = this.declarations[propertyName];
589
+ if (Array.isArray(declarations)) {
590
+ for (const value of declarations){
591
+ push(keyToken + String(value));
592
+ }
593
+ } else {
594
+ push(keyToken + String(declarations));
595
+ }
596
+ }
597
+ // 創建 Natives
598
+ if (propertiesText.length) {
599
+ for(const prefixSelectorVendor in suffixSelectorVendorsByPrefixSelectorVendor){
600
+ for (const eachSuffixSelectorVendor of suffixSelectorVendorsByPrefixSelectorVendor[prefixSelectorVendor]){
601
+ let prefixText = '';
602
+ if (this.direction) {
603
+ prefixText += '[dir=' + this.direction + '] ';
604
+ }
605
+ const prefixSelectors = this.vendorPrefixSelectors[prefixSelectorVendor];
606
+ const suffixSelectors = this.vendorSuffixSelectors[eachSuffixSelectorVendor];
607
+ const prefixTexts = prefixSelectors.map((eachPrefixSelector)=>eachPrefixSelector + prefixText);
608
+ const getCssText = (name)=>prefixTexts.map((eachPrefixText)=>{
609
+ let modePrefix = '';
610
+ if (this.mode) {
611
+ const modeSelector = this.css.getModeSelector(this.mode);
612
+ if (modeSelector) {
613
+ modePrefix = modeSelector + ' ';
614
+ }
615
+ }
616
+ return modePrefix + (scope ? scope + ' ' : '') + eachPrefixText;
617
+ }).reduce((arr, eachPrefixText)=>{
618
+ arr.push(suffixSelectors.reduce((_arr, eachSuffixSelector)=>{
619
+ _arr.push(eachPrefixText + '.' + cssEscape(name) + eachSuffixSelector);
620
+ return _arr;
621
+ }, []).join(','));
622
+ return arr;
623
+ }, []).join(',');
624
+ let cssText = getCssText(fixedClass ?? name) + '{' + propertiesText.join(';') + '}';
625
+ for (const key of Object.keys(this.at).sort((_a, b)=>b === 'supports' ? -1 : 1)){
626
+ cssText = '@' + key + (key.includes(' ') ? '' : ' ') + this.at[key].map((eachAtComponent)=>this.resolveAtComponent(eachAtComponent)).join(' ') + '{' + cssText + '}';
627
+ }
628
+ if (this.mode && modes?.[this.mode] === 'media') {
629
+ cssText = `@media(prefers-color-scheme:${this.mode}){` + cssText + '}';
630
+ }
631
+ this.nodes.push({
632
+ text: cssText
633
+ });
634
+ }
635
+ }
636
+ }
637
+ if (this.fixedClass) {
638
+ this.layer = css.stylesLayer;
639
+ }
640
+ }
641
+ resolveAtComponent(atComponent) {
642
+ switch(atComponent.type){
643
+ case 'arbitrary':
644
+ return atComponent.value;
645
+ case 'feature':
646
+ return '(' + atComponent.name + ':' + atComponent.value + (atComponent.unit || '') + ')';
647
+ case 'operator':
648
+ return atComponent.value;
649
+ }
650
+ }
651
+ resolveValue;
652
+ parseValues;
653
+ parseValue(token, unit = this.definition.unit) {
654
+ const defaultUnit = unit ?? this.definition.unit;
655
+ let newUnit = '';
656
+ let value;
657
+ if (typeof token === 'number') {
658
+ /**
659
+ * 當無單位值且 defaultUnit === 'rem',
660
+ * 將 pxValue / 16 轉為 remValue
661
+ */ if (defaultUnit && !newUnit) {
662
+ if (defaultUnit === 'rem' || defaultUnit === 'em') {
663
+ value = token / (this.css.config.rootSize || 1);
664
+ }
665
+ newUnit = defaultUnit || '';
666
+ } else {
667
+ value = token;
668
+ }
669
+ return {
670
+ value,
671
+ unit: newUnit,
672
+ type: 'number'
673
+ };
674
+ } else if (defaultUnit) {
675
+ // w:1/2 -> width: 50%
676
+ if (/^\d+\/\d+/.test(token)) {
677
+ const [dividend, divisor] = token.split('/');
678
+ return {
679
+ value: +dividend / +divisor * 100,
680
+ unit: '%',
681
+ type: 'number'
682
+ };
683
+ }
684
+ const matches = token.match(UNIT_REGEX);
685
+ // ['0.5deg', '0.5', 'deg', index: 0, input: '0.5deg', groups: undefined]
686
+ if (matches) {
687
+ value = +matches[1];
688
+ newUnit = matches[3] || '';
689
+ /**
690
+ * 當無單位值且 defaultUnit === 'rem',
691
+ * 將 pxValue / 16 轉為 remValue
692
+ */ if (!newUnit) {
693
+ if (defaultUnit === 'rem' || defaultUnit === 'em') {
694
+ value = value / (this.css.config.rootSize || 1);
695
+ }
696
+ newUnit = defaultUnit || '';
697
+ }
698
+ return {
699
+ value,
700
+ unit: newUnit,
701
+ type: 'number'
702
+ };
703
+ }
704
+ }
705
+ return {
706
+ value: token,
707
+ type: 'string'
708
+ };
709
+ }
710
+ get key() {
711
+ return (this.fixedClass ? this.fixedClass + ' ' : '') + this.name;
712
+ }
713
+ }
714
+
715
+ export { SyntaxRule };