@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
package/dist/core.cjs ADDED
@@ -0,0 +1,739 @@
1
+ 'use strict';
2
+
3
+ var syntaxRule = require('./syntax-rule.cjs');
4
+ var index = require('./config/index.cjs');
5
+ var hexToRgb = require('./utils/hex-to-rgb.cjs');
6
+ var flattenObject = require('./utils/flatten-object.cjs');
7
+ var extendConfig = require('./utils/extend-config.cjs');
8
+ var rule = require('./rule.cjs');
9
+ var syntaxRuleType = require('./syntax-rule-type.cjs');
10
+ var layer = require('./layer.cjs');
11
+ var syntaxLayer = require('./syntax-layer.cjs');
12
+ var nonLayer = require('./non-layer.cjs');
13
+ var registerGlobal = require('./register-global.cjs');
14
+
15
+ class MasterCSS {
16
+ customConfig;
17
+ static config = index.config;
18
+ definedRules;
19
+ config;
20
+ layerStatementRule;
21
+ rules;
22
+ animationsNonLayer;
23
+ baseLayer;
24
+ themeLayer;
25
+ presetLayer;
26
+ componentsLayer;
27
+ generalLayer;
28
+ components;
29
+ selectors;
30
+ variables;
31
+ at;
32
+ animations;
33
+ constructor(customConfig){
34
+ this.customConfig = customConfig;
35
+ this.definedRules = [];
36
+ this.layerStatementRule = new rule.Rule('layer-statement', [
37
+ {
38
+ text: '@layer base,theme,preset,components,general;'
39
+ }
40
+ ]);
41
+ this.rules = [
42
+ this.layerStatementRule
43
+ ];
44
+ this.animationsNonLayer = new nonLayer(this);
45
+ this.baseLayer = new syntaxLayer('base', this);
46
+ this.themeLayer = new layer('theme', this);
47
+ this.presetLayer = new syntaxLayer('preset', this);
48
+ this.componentsLayer = new syntaxLayer('components', this);
49
+ this.generalLayer = new syntaxLayer('general', this);
50
+ this.components = new Map();
51
+ this.selectors = new Map();
52
+ this.variables = new Map();
53
+ this.at = new Map();
54
+ this.animations = new Map();
55
+ this.config = customConfig?.override ? extendConfig(customConfig) : extendConfig(index.config, customConfig);
56
+ this.resolve();
57
+ }
58
+ get text() {
59
+ return this.rules.sort((a, b)=>{
60
+ const order = [
61
+ 'layer-statement',
62
+ 'base',
63
+ 'theme',
64
+ 'preset',
65
+ 'components',
66
+ 'general'
67
+ ];
68
+ const indexA = order.indexOf(a.name) === -1 ? Infinity : order.indexOf(a.name);
69
+ const indexB = order.indexOf(b.name) === -1 ? Infinity : order.indexOf(b.name);
70
+ return indexA - indexB;
71
+ }).map(({ text })=>text).join('');
72
+ }
73
+ resolve() {
74
+ this.components.clear();
75
+ this.selectors.clear();
76
+ this.variables.clear();
77
+ this.at.clear();
78
+ this.animations.clear();
79
+ this.definedRules.length = 0;
80
+ const colorVariableNames = {
81
+ current: undefined,
82
+ currentColor: undefined,
83
+ transparent: undefined
84
+ };
85
+ const { components, selectors, variables, utilities, at, rules, animations } = this.config;
86
+ function escapeString(str) {
87
+ return str.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
88
+ }
89
+ if (selectors) {
90
+ const resolvedSelectors = flattenObject.flattenObject(selectors);
91
+ for(const eachSelectorName in resolvedSelectors){
92
+ const eachResolvedSelectorText = resolvedSelectors[eachSelectorName];
93
+ const regexp = new RegExp(escapeString(eachSelectorName) + '(?![a-z-])');
94
+ for (const eachNewSelectorText of Array.isArray(eachResolvedSelectorText) ? eachResolvedSelectorText : [
95
+ eachResolvedSelectorText
96
+ ]){
97
+ const vendor = eachNewSelectorText.match(/^:(?::)?-(webkit|moz|ms|o)-/m)?.[1] ?? '';
98
+ let selectorValues = this.selectors.get(vendor);
99
+ if (!selectorValues) {
100
+ this.selectors.set(vendor, selectorValues = []);
101
+ }
102
+ let currentSelectValue = selectorValues.find(([_valueRegexp])=>_valueRegexp === regexp);
103
+ if (!currentSelectValue) {
104
+ currentSelectValue = [
105
+ regexp,
106
+ []
107
+ ];
108
+ selectorValues.push(currentSelectValue);
109
+ }
110
+ currentSelectValue[1].push(eachNewSelectorText);
111
+ }
112
+ }
113
+ }
114
+ if (variables) {
115
+ const aliasVariableModeResolvers = new Map;
116
+ const resolveVariable = (variableDefinition, name, mode)=>{
117
+ if (variableDefinition === undefined || variableDefinition === null) return;
118
+ const addVariable = (name, variable, replacedMode, alpha)=>{
119
+ if (variable === undefined) return;
120
+ const flatName = name.join('-');
121
+ const groups = name.slice(0, -1).filter(Boolean);
122
+ const key = (name[0] === '' ? '-' : '') + name[name.length - 1];
123
+ variable.key = key;
124
+ variable.name = flatName;
125
+ if (groups.length) variable.group = groups.join('.');
126
+ if (variable.type === 'color') {
127
+ if (alpha) {
128
+ const slashIndex = variable.value.indexOf('/');
129
+ variable = {
130
+ ...variable,
131
+ value: slashIndex === -1 ? variable.value + ' / ' + (alpha.startsWith('0.') ? alpha.slice(1) : alpha) : variable.value.slice(0, slashIndex + 2) + String(+variable.value.slice(slashIndex + 2) * +alpha).slice(1)
132
+ };
133
+ }
134
+ colorVariableNames[flatName] = undefined;
135
+ }
136
+ /**
137
+ * resolve `variables.screen-*` to `at.*`
138
+ */ if (variable.name.startsWith('screen-') && variable.type === 'number') {
139
+ this.at.set(variable.name.slice(7), variable.value);
140
+ }
141
+ const currentMode = replacedMode ?? mode;
142
+ if (currentMode !== undefined) {
143
+ const foundVariable = this.variables.get(flatName);
144
+ if (foundVariable) {
145
+ if (currentMode) {
146
+ if (!foundVariable.modes) {
147
+ foundVariable.modes = {};
148
+ }
149
+ foundVariable.modes[currentMode] = variable;
150
+ } else {
151
+ foundVariable.value = variable.value;
152
+ if (variable.type === 'color') {
153
+ foundVariable.space = variable.space;
154
+ }
155
+ }
156
+ } else {
157
+ if (currentMode) {
158
+ const newVariable = {
159
+ key: variable.key,
160
+ name: variable.name,
161
+ group: variable.group,
162
+ type: variable.type,
163
+ modes: {
164
+ [currentMode]: variable
165
+ }
166
+ };
167
+ if (variable.type === 'color') {
168
+ newVariable.space = variable.space;
169
+ }
170
+ this.variables.set(flatName, newVariable);
171
+ } else {
172
+ this.variables.set(flatName, variable);
173
+ }
174
+ }
175
+ } else {
176
+ this.variables.set(flatName, variable);
177
+ }
178
+ };
179
+ if (typeof variableDefinition === 'object') {
180
+ if (Array.isArray(variableDefinition)) {
181
+ addVariable(name, {
182
+ type: 'string',
183
+ value: variableDefinition.join(',')
184
+ });
185
+ } else {
186
+ const keys = Object.keys(variableDefinition);
187
+ for (const eachKey of keys){
188
+ if (eachKey === '' || eachKey.startsWith('@')) {
189
+ resolveVariable(variableDefinition[eachKey], name, eachKey || keys.some((eachKey)=>eachKey.startsWith('@')) ? eachKey.slice(1) : undefined);
190
+ } else {
191
+ resolveVariable(variableDefinition[eachKey], [
192
+ ...name,
193
+ eachKey
194
+ ]);
195
+ }
196
+ }
197
+ }
198
+ } else if (typeof variableDefinition === 'number') {
199
+ addVariable(name, {
200
+ type: 'number',
201
+ value: variableDefinition
202
+ });
203
+ addVariable([
204
+ '',
205
+ ...name
206
+ ], {
207
+ type: 'number',
208
+ value: variableDefinition * -1
209
+ });
210
+ } else if (typeof variableDefinition === 'string') {
211
+ const aliasResult = /^\$\((.*?)\)(?: ?\/ ?(.+?))?$/.exec(variableDefinition);
212
+ const flatName = name.join('-');
213
+ if (aliasResult) {
214
+ let aliasVariableModeResolver = aliasVariableModeResolvers.get(flatName);
215
+ if (!aliasVariableModeResolver) {
216
+ aliasVariableModeResolvers.set(flatName, aliasVariableModeResolver = {});
217
+ }
218
+ aliasVariableModeResolver[mode] = ()=>{
219
+ delete aliasVariableModeResolver[mode];
220
+ const [alias, aliasMode] = aliasResult[1].split('@');
221
+ if (alias) {
222
+ const eachAliasModeVariableResolver = aliasVariableModeResolvers.get(alias);
223
+ if (eachAliasModeVariableResolver) {
224
+ for (const mode of Object.keys(eachAliasModeVariableResolver)){
225
+ eachAliasModeVariableResolver[mode]?.();
226
+ }
227
+ }
228
+ const aliasVariable = this.variables.get(alias);
229
+ if (aliasVariable) {
230
+ if (aliasMode === undefined && aliasVariable.modes) {
231
+ addVariable(name, {
232
+ type: aliasVariable.type,
233
+ value: aliasVariable.value,
234
+ space: aliasVariable.space
235
+ }, '', aliasResult[2]);
236
+ for(const mode in aliasVariable.modes){
237
+ addVariable(name, aliasVariable.modes[mode], mode, aliasResult[2]);
238
+ }
239
+ } else {
240
+ const variable = aliasMode !== undefined ? aliasVariable.modes?.[aliasMode] : aliasVariable;
241
+ if (variable) {
242
+ const newVariable = {
243
+ type: variable.type,
244
+ value: variable.value
245
+ };
246
+ if (variable.type === 'color') {
247
+ newVariable.space = variable.space;
248
+ }
249
+ addVariable(name, newVariable, undefined, aliasResult[2]);
250
+ }
251
+ }
252
+ }
253
+ }
254
+ };
255
+ } else {
256
+ const hexColorResult = /^#([A-Fa-f0-9]{3,4}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$/.exec(variableDefinition);
257
+ if (hexColorResult) {
258
+ const [r, g, b, a] = hexToRgb(hexColorResult[1]);
259
+ addVariable(name, {
260
+ type: 'color',
261
+ value: `${r} ${g} ${b}${a === 1 ? '' : ' / ' + a}`,
262
+ space: 'rgb'
263
+ });
264
+ } else {
265
+ const rgbFunctionResult = /^rgb\( *([0-9]{1,3})(?: *, *| +)([0-9]{1,3})(?: *, *| +)([0-9]{1,3}) *(?:(?:,|\/) *(.*?) *)?\)$/.exec(variableDefinition);
266
+ if (rgbFunctionResult) {
267
+ addVariable(name, {
268
+ type: 'color',
269
+ value: rgbFunctionResult[1] + ' ' + rgbFunctionResult[2] + ' ' + rgbFunctionResult[3] + (rgbFunctionResult[4] ? ' / ' + (rgbFunctionResult[4].startsWith('0.') ? rgbFunctionResult[4].slice(1) : rgbFunctionResult[4]) : ''),
270
+ space: 'rgb'
271
+ });
272
+ } else {
273
+ const hslFunctionResult = /^hsl\((.*?)\)$/.exec(variableDefinition);
274
+ if (hslFunctionResult) {
275
+ addVariable(name, {
276
+ type: 'color',
277
+ value: hslFunctionResult[1],
278
+ space: 'hsl'
279
+ });
280
+ } else {
281
+ addVariable(name, {
282
+ type: 'string',
283
+ value: variableDefinition
284
+ });
285
+ }
286
+ }
287
+ }
288
+ }
289
+ }
290
+ };
291
+ for(const parnetKey in variables){
292
+ resolveVariable(variables[parnetKey], [
293
+ parnetKey
294
+ ]);
295
+ }
296
+ // todo: address to the target variable
297
+ aliasVariableModeResolvers.forEach((aliasVariableModeResolver)=>{
298
+ for (const mode of Object.keys(aliasVariableModeResolver)){
299
+ aliasVariableModeResolver[mode]?.();
300
+ }
301
+ });
302
+ }
303
+ if (at) {
304
+ const resolvedAt = flattenObject.flattenObject(at);
305
+ for(const eachAt in resolvedAt){
306
+ this.at.set(eachAt, resolvedAt[eachAt]);
307
+ }
308
+ }
309
+ if (animations) {
310
+ for(const animationName in animations){
311
+ const eachAnimation = {};
312
+ this.animations.set(animationName, eachAnimation);
313
+ const eachKeyframes = animations[animationName];
314
+ for(const eachKeyframeValue in eachKeyframes){
315
+ const newValueByPropertyName = eachAnimation[eachKeyframeValue] = {};
316
+ const eachKeyframeDeclarations = eachKeyframes[eachKeyframeValue];
317
+ for(const propertyName in eachKeyframeDeclarations){
318
+ newValueByPropertyName[propertyName] = eachKeyframeDeclarations[propertyName];
319
+ }
320
+ }
321
+ }
322
+ }
323
+ const flattedStyles = components ? flattenObject.flattenObject(components) : {};
324
+ const utilityNames = Object.keys(flattedStyles);
325
+ const handleUtilityName = (utilityName)=>{
326
+ if (this.components.has(utilityName)) return;
327
+ const currentClass = [];
328
+ this.components.set(utilityName, currentClass);
329
+ const className = flattedStyles[utilityName];
330
+ if (!className) return;
331
+ const classNames = className.replace(/(?:\n(?:\s*))+/g, ' ').trim().split(' ');
332
+ for (const eachClassName of classNames){
333
+ const handle = (className)=>{
334
+ if (!currentClass.includes(className)) {
335
+ currentClass.push(className);
336
+ }
337
+ };
338
+ if (utilityNames.includes(eachClassName)) {
339
+ handleUtilityName(eachClassName);
340
+ const componentClasses = this.components.get(eachClassName);
341
+ componentClasses?.forEach(handle);
342
+ } else {
343
+ handle(eachClassName);
344
+ }
345
+ }
346
+ };
347
+ for (const eachUtilityName of utilityNames){
348
+ handleUtilityName(eachUtilityName);
349
+ }
350
+ if (rules || utilities) {
351
+ const rulesEntries = [];
352
+ if (utilities) {
353
+ for(const utilityName in utilities){
354
+ const declarations = utilities[utilityName];
355
+ rulesEntries.push([
356
+ utilityName,
357
+ {
358
+ declarations,
359
+ type: syntaxRuleType.Utility
360
+ }
361
+ ]);
362
+ }
363
+ }
364
+ if (rules) {
365
+ rulesEntries.push(...Object.entries(rules));
366
+ }
367
+ const rulesEntriesLength = rulesEntries.length;
368
+ const colorNames = Object.keys(colorVariableNames);
369
+ rulesEntries.sort((a, b)=>{
370
+ if (a[1].type !== b[1].type) {
371
+ return (b[1].type || 0) - (a[1].type || 0);
372
+ }
373
+ return b[0].localeCompare(a[0]);
374
+ }).forEach(([id, eachSyntaxRuleDefinition], index)=>{
375
+ const syntax = {
376
+ id,
377
+ keys: [],
378
+ variables: {},
379
+ matchers: {},
380
+ order: rulesEntriesLength - 1 - index,
381
+ definition: eachSyntaxRuleDefinition
382
+ };
383
+ if (!eachSyntaxRuleDefinition.unit) {
384
+ eachSyntaxRuleDefinition.unit = '';
385
+ }
386
+ if (!eachSyntaxRuleDefinition.separators) {
387
+ eachSyntaxRuleDefinition.separators = [
388
+ ','
389
+ ];
390
+ }
391
+ this.definedRules.push(syntax);
392
+ const { matcher, type, subkey, ambiguousKeys, ambiguousValues, sign } = eachSyntaxRuleDefinition;
393
+ if (type === syntaxRuleType.Utility) {
394
+ syntax.id = '.' + id;
395
+ syntax.matchers.arbitrary = new RegExp('^' + escapeString(id) + '(?=!|\\*|>|\\+|~|:|\\[|@|_|\\.|$)', 'm');
396
+ }
397
+ // todo: 不可使用 startsWith 判斷,應改為更精準的從 config.variables 取得目標變數群組,但 config.variables 中的值還沒被 resolve 像是 Array
398
+ const addResolvedVariables = (groupName)=>{
399
+ this.variables.forEach((eachVariable)=>{
400
+ if (eachVariable.group === groupName) {
401
+ syntax.variables[eachVariable.key] = eachVariable;
402
+ }
403
+ });
404
+ };
405
+ // 1. custom `config.rules[id].variables`
406
+ if (eachSyntaxRuleDefinition.variables) {
407
+ for (const eachVariableGroup of eachSyntaxRuleDefinition.variables){
408
+ addResolvedVariables(eachVariableGroup);
409
+ }
410
+ }
411
+ // 2. custom `config.variables`
412
+ addResolvedVariables(id);
413
+ const keys = [];
414
+ let { key } = eachSyntaxRuleDefinition;
415
+ if (type === syntaxRuleType.NativeShorthand || type === syntaxRuleType.Native) {
416
+ if (!key) eachSyntaxRuleDefinition.key = key = id;
417
+ keys.push(id);
418
+ }
419
+ if (sign) {
420
+ syntax.matchers.arbitrary = new RegExp(`^${sign}[^!*>+~:[@_]+\\|`);
421
+ } else if (!matcher) {
422
+ const colorsPatten = colorNames.join('|');
423
+ if (!key && !subkey) {
424
+ keys.push(id);
425
+ } else {
426
+ if (key && !keys.includes(key)) keys.push(key);
427
+ if (subkey) keys.push(subkey);
428
+ if (type === syntaxRuleType.Shorthand) {
429
+ keys.push(id);
430
+ }
431
+ }
432
+ if (ambiguousKeys?.length) {
433
+ const ambiguousKeyPattern = ambiguousKeys.length > 1 ? `(?:${ambiguousKeys.join('|')})` : ambiguousKeys[0];
434
+ const variableKeys = Object.keys(syntax.variables);
435
+ if (ambiguousValues?.length) {
436
+ const ambiguousValuePatterns = [];
437
+ for (const eachAmbiguousValue of ambiguousValues){
438
+ if (eachAmbiguousValue instanceof RegExp) {
439
+ ambiguousValuePatterns.push(eachAmbiguousValue.source.replace('\\$colors', colorsPatten));
440
+ } else {
441
+ ambiguousValuePatterns.unshift(`${eachAmbiguousValue}(?:\\b|_)`);
442
+ }
443
+ }
444
+ syntax.matchers.value = new RegExp(`^${ambiguousKeyPattern}:(?:${ambiguousValuePatterns.join('|')})[^|]*?(?:@|$)`);
445
+ }
446
+ if (variableKeys.length) {
447
+ syntax.matchers.variable = new RegExp(`^${ambiguousKeyPattern}:(?:${variableKeys.join('|')}(?![a-zA-Z0-9-]))[^|]*?(?:@|$)`);
448
+ }
449
+ }
450
+ } else {
451
+ syntax.matchers.arbitrary = matcher;
452
+ }
453
+ if (keys.length) {
454
+ syntax.keys = keys;
455
+ syntax.matchers.key = new RegExp(`^${keys.length > 1 ? `(${keys.join('|')})` : keys[0]}:.`);
456
+ }
457
+ });
458
+ }
459
+ this.components.forEach((componentClasses, componentClass)=>{
460
+ const syntaxRulesByStateToken = componentClasses.map((eachSyntax)=>this.create(eachSyntax)).filter((eachSyntax)=>eachSyntax?.text).reduce((obj, eachSyntaxRule)=>{
461
+ if (eachSyntaxRule.stateToken in obj) {
462
+ obj[eachSyntaxRule.stateToken].push(eachSyntaxRule);
463
+ } else {
464
+ obj[eachSyntaxRule.stateToken] = [
465
+ eachSyntaxRule
466
+ ];
467
+ }
468
+ return obj;
469
+ }, {});
470
+ this.components.set(componentClass, Object.keys(syntaxRulesByStateToken).map((stateToken)=>{
471
+ const syntaxRules = syntaxRulesByStateToken[stateToken];
472
+ return syntaxRules.length === 1 ? syntaxRules[0].name : '{' + syntaxRulesByStateToken[stateToken].map((eachSyntaxRule)=>eachSyntaxRule.name.slice(0, eachSyntaxRule.name.length - eachSyntaxRule.stateToken.length)).join(';') + '}' + stateToken;
473
+ }));
474
+ });
475
+ }
476
+ /**
477
+ * Match check if Master CSS syntax
478
+ * @param className
479
+ * @returns css text
480
+ */ match(className) {
481
+ /**
482
+ * 1. variable
483
+ * @example fg:primary bg:blue
484
+ */ for (const eachSyntax of this.definedRules){
485
+ if (eachSyntax.matchers.variable?.test(className)) return eachSyntax;
486
+ }
487
+ /**
488
+ * 2. value (ambiguous.key * ambiguous.values)
489
+ * @example bg:current box:content font:12
490
+ */ for (const eachSyntax of this.definedRules){
491
+ if (eachSyntax.matchers.value?.test(className)) return eachSyntax;
492
+ }
493
+ /**
494
+ * 3. full key
495
+ * @example text-align:center color:blue-40
496
+ */ for (const eachSyntax of this.definedRules){
497
+ if (eachSyntax.matchers.key?.test(className)) return eachSyntax;
498
+ }
499
+ /**
500
+ * 4. arbitrary
501
+ * @example custom RegExp, utility
502
+ */ for (const eachSyntax of this.definedRules){
503
+ if (eachSyntax.matchers.arbitrary?.test(className)) return eachSyntax;
504
+ }
505
+ }
506
+ /**
507
+ * Generate syntax rules from class name
508
+ * @param className
509
+ * @returns SyntaxRule[]
510
+ */ generate(className, mode) {
511
+ let syntaxRules = [];
512
+ const componentClasses = this.components.get(className);
513
+ if (componentClasses) {
514
+ componentClasses.forEach((eachSyntax)=>{
515
+ const syntaxRule = this.create(eachSyntax, className, mode);
516
+ if (syntaxRule?.text) syntaxRules.push(syntaxRule);
517
+ });
518
+ } else {
519
+ const atIndex = className.indexOf('@');
520
+ if (atIndex !== -1) {
521
+ const name = className.slice(0, atIndex);
522
+ const componentClasses = this.components.get(name);
523
+ if (componentClasses) {
524
+ const atToken = className.slice(atIndex);
525
+ componentClasses.forEach((eachSyntax)=>{
526
+ const syntaxRule = this.create(eachSyntax + atToken, className, mode);
527
+ if (syntaxRule?.text) syntaxRules.push(syntaxRule);
528
+ });
529
+ }
530
+ }
531
+ const syntaxRule = this.create(className, undefined, mode);
532
+ if (syntaxRule?.text) syntaxRules.push(syntaxRule);
533
+ }
534
+ return syntaxRules;
535
+ }
536
+ generateVendorSelectors(selectorText) {
537
+ const vendorSelectors = {};
538
+ const transformSelector = (selectorText, selectorValues, selectors, matched)=>{
539
+ for (const [regexp, newSelectorTexts] of selectorValues){
540
+ if (regexp.test(selectorText)) {
541
+ for (const eachNewSelectorText of newSelectorTexts){
542
+ transformSelector(selectorText.replace(regexp, eachNewSelectorText), selectorValues, selectors, true);
543
+ }
544
+ return;
545
+ }
546
+ }
547
+ if (matched) {
548
+ selectors.push(selectorText);
549
+ }
550
+ };
551
+ const spacedSelectorToken = (selectorText)=>{
552
+ // 1. \'123\'
553
+ // 2. [href='http://localhost']
554
+ const transformedSelectorText = selectorText.split(/(\\'(?:.*?)[^\\]\\')(?=[*_>~+,)])|(\[[^=]+='(?:.*?)[^\\]'\])/).map((eachToken, i)=>i % 3 ? eachToken : eachToken.replace(/(^|[^_])_(?!_)/g, '$1 ')).join('');
555
+ const selectors = [];
556
+ let currentSelector = '';
557
+ let symbolCount = 0;
558
+ for(let i = 0; i < transformedSelectorText.length; i++){
559
+ const char = transformedSelectorText[i];
560
+ if (char === '\\') {
561
+ currentSelector += char + transformedSelectorText[++i];
562
+ continue;
563
+ }
564
+ if (!symbolCount && char === ',') {
565
+ selectors.push(currentSelector);
566
+ currentSelector = '';
567
+ } else {
568
+ currentSelector += char;
569
+ if (symbolCount && char === ')') {
570
+ symbolCount--;
571
+ } else if (char === '(') {
572
+ symbolCount++;
573
+ }
574
+ }
575
+ }
576
+ if (currentSelector) {
577
+ selectors.push(currentSelector);
578
+ }
579
+ return selectors;
580
+ };
581
+ const transformedSelectors = [];
582
+ const selectorValues = this.selectors.get('');
583
+ if (selectorValues) {
584
+ transformSelector(selectorText, selectorValues, transformedSelectors, true);
585
+ } else {
586
+ transformedSelectors.push(selectorText);
587
+ }
588
+ const unspacedVendorSelectors = {};
589
+ this.selectors.forEach((selectorValues, vendor)=>{
590
+ if (!vendor) return;
591
+ const newUnspacedVendorSelectors = [];
592
+ for (const eachTransformedSelector of transformedSelectors){
593
+ transformSelector(eachTransformedSelector, selectorValues, newUnspacedVendorSelectors, false);
594
+ }
595
+ if (newUnspacedVendorSelectors.length) {
596
+ unspacedVendorSelectors[vendor] = newUnspacedVendorSelectors;
597
+ }
598
+ });
599
+ const insertVendorSelectors = (vendor, selectorTexts)=>{
600
+ const groupedSelectorTexts = selectorTexts.reduce((arr, eachSuffixSelector)=>{
601
+ arr.push(...spacedSelectorToken(eachSuffixSelector));
602
+ return arr;
603
+ }, []);
604
+ if (vendor in vendorSelectors) {
605
+ vendorSelectors[vendor].push(...groupedSelectorTexts);
606
+ } else {
607
+ vendorSelectors[vendor] = groupedSelectorTexts;
608
+ }
609
+ };
610
+ const vendors = Object.keys(unspacedVendorSelectors);
611
+ if (vendors.length) {
612
+ for (const eachVendor of vendors){
613
+ insertVendorSelectors(eachVendor, unspacedVendorSelectors[eachVendor]);
614
+ }
615
+ } else {
616
+ insertVendorSelectors('', transformedSelectors);
617
+ }
618
+ return vendorSelectors;
619
+ }
620
+ /**
621
+ * Create syntax rule from given class name
622
+ * @param className
623
+ * @returns SyntaxRule
624
+ */ create(className, fixedClass, mode) {
625
+ const syntaxRule$1 = this.generalLayer.rules.find(({ key })=>key === (fixedClass ? fixedClass + ' ' : '') + className);
626
+ if (syntaxRule$1) return syntaxRule$1;
627
+ const registeredRule = this.match(className);
628
+ if (registeredRule) return new syntaxRule.SyntaxRule(className, this, registeredRule, fixedClass, mode);
629
+ }
630
+ /**
631
+ * Create syntax rule from given selector text
632
+ * @param selectorText
633
+ */ createFromSelectorText(selectorText) {
634
+ const selectorTextSplits = selectorText.split(' ');
635
+ const stopChars = /[.#\[!\*>+~:,\s]/;
636
+ for(let i = 0; i < selectorTextSplits.length; i++){
637
+ const eachField = selectorTextSplits[i];
638
+ const modeSelector = this.getModeSelector(eachField);
639
+ if (i === 0 && eachField === modeSelector) continue;
640
+ if (eachField.startsWith('.')) {
641
+ const eachFieldName = eachField.slice(1);
642
+ let className = '';
643
+ let l = 0;
644
+ while(l < eachFieldName.length){
645
+ const char = eachFieldName[l];
646
+ const nextChar = eachFieldName[l + 1];
647
+ if (char === '\\' && nextChar) {
648
+ className += nextChar;
649
+ l += 2;
650
+ continue;
651
+ }
652
+ if (stopChars.test(char)) break;
653
+ className += char;
654
+ l++;
655
+ }
656
+ const syntaxRules = this.generate(className);
657
+ if (syntaxRules.length) return syntaxRules;
658
+ }
659
+ }
660
+ }
661
+ /**
662
+ * 根據蒐集到的所有 DOM class 重新 create
663
+ */ refresh(customConfig) {
664
+ this.reset();
665
+ if (customConfig) {
666
+ this.customConfig = customConfig;
667
+ } else {
668
+ customConfig = this.customConfig;
669
+ }
670
+ // @ts-ignore
671
+ this.config = customConfig?.override ? extendConfig(customConfig) : extendConfig(index.config, customConfig);
672
+ this.resolve();
673
+ return this;
674
+ }
675
+ reset() {
676
+ this.baseLayer.reset();
677
+ this.themeLayer.reset();
678
+ this.presetLayer.reset();
679
+ this.componentsLayer.reset();
680
+ this.generalLayer.reset();
681
+ this.animationsNonLayer.reset();
682
+ return this;
683
+ }
684
+ destroy() {
685
+ this.reset();
686
+ return this;
687
+ }
688
+ add(...classNames) {
689
+ for (const className of classNames){
690
+ this.generate(className).forEach((eachSyntaxRule)=>eachSyntaxRule.layer.insert(eachSyntaxRule));
691
+ }
692
+ return this;
693
+ }
694
+ remove(...classNames) {
695
+ /**
696
+ * class name 從 DOM tree 中被移除,
697
+ * 匹配並刪除對應的 rule
698
+ */ for (const className of classNames){
699
+ const componentClasses = this.components.get(className);
700
+ if (componentClasses) {
701
+ componentClasses.forEach((componentClass)=>{
702
+ this.componentsLayer.delete(className + ' ' + componentClass);
703
+ });
704
+ } else {
705
+ const atIndex = className.indexOf('@');
706
+ if (atIndex !== -1) {
707
+ const name = className.slice(0, atIndex);
708
+ const componentClasses = this.components.get(name);
709
+ if (componentClasses) {
710
+ const atToken = className.slice(atIndex);
711
+ componentClasses.forEach((componentClass)=>{
712
+ this.componentsLayer.delete(componentClass + atToken + ' ' + className);
713
+ });
714
+ } else {
715
+ this.generalLayer.delete(className);
716
+ }
717
+ } else {
718
+ this.generalLayer.delete(className);
719
+ }
720
+ }
721
+ }
722
+ }
723
+ getModeSelector(modeName) {
724
+ const mode = this.config.modes?.[modeName];
725
+ if (mode) {
726
+ switch(mode){
727
+ case 'class':
728
+ return '.' + modeName;
729
+ case 'host':
730
+ return ':host(.' + modeName + ')';
731
+ }
732
+ }
733
+ }
734
+ }
735
+ (function(MasterCSS) {
736
+ registerGlobal(MasterCSS);
737
+ })(MasterCSS);
738
+
739
+ module.exports = MasterCSS;