@master/css 2.0.0-rc.6 → 2.0.0-rc.61

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