@master/css 2.0.0-rc.3 → 2.0.0-rc.31

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