@master/css 2.0.0-rc.4 → 2.0.0-rc.41

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