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