@tbela99/css-parser 1.4.2 → 1.4.4

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 (122) hide show
  1. package/CHANGELOG.md +354 -4
  2. package/README.md +69 -1077
  3. package/dist/config.json.js +76 -3
  4. package/dist/index-umd-web.js +26838 -18743
  5. package/dist/index.cjs +26819 -18731
  6. package/dist/index.d.ts +2471 -1884
  7. package/dist/lib/ast/clone.js +46 -0
  8. package/dist/lib/ast/expand.js +90 -65
  9. package/dist/lib/ast/features/calc.js +86 -43
  10. package/dist/lib/ast/features/if.js +221 -0
  11. package/dist/lib/ast/features/index.js +1 -0
  12. package/dist/lib/ast/features/inlinecssvariables.js +33 -28
  13. package/dist/lib/ast/features/prefix.js +406 -44
  14. package/dist/lib/ast/features/shorthand.js +6 -9
  15. package/dist/lib/ast/features/transform.js +14 -15
  16. package/dist/lib/ast/find.js +175 -0
  17. package/dist/lib/ast/math/expression.js +145 -99
  18. package/dist/lib/ast/math/math.js +30 -41
  19. package/dist/lib/ast/minify.js +551 -232
  20. package/dist/lib/ast/transform/compute.js +82 -69
  21. package/dist/lib/ast/transform/matrix.js +12 -26
  22. package/dist/lib/ast/transform/minify.js +154 -150
  23. package/dist/lib/ast/transform/perspective.js +1 -1
  24. package/dist/lib/ast/transform/rotate.js +1 -1
  25. package/dist/lib/ast/transform/scale.js +1 -1
  26. package/dist/lib/ast/transform/skew.js +1 -1
  27. package/dist/lib/ast/transform/translate.js +1 -1
  28. package/dist/lib/ast/transform/utils.js +3 -2
  29. package/dist/lib/ast/types.js +345 -30
  30. package/dist/lib/ast/walk.js +119 -64
  31. package/dist/lib/fs/resolve.js +94 -88
  32. package/dist/lib/parser/declaration/list.js +117 -26
  33. package/dist/lib/parser/declaration/map.js +141 -96
  34. package/dist/lib/parser/declaration/set.js +30 -25
  35. package/dist/lib/parser/parse.js +1810 -1739
  36. package/dist/lib/parser/tokenize.js +662 -403
  37. package/dist/lib/parser/utils/angle.js +29 -0
  38. package/dist/lib/parser/utils/at-rule-container.js +486 -0
  39. package/dist/lib/parser/utils/at-rule-font-feature-values.js +13 -0
  40. package/dist/lib/parser/utils/at-rule-generic.js +118 -0
  41. package/dist/lib/parser/utils/at-rule-import.js +393 -0
  42. package/dist/lib/parser/utils/at-rule-media.js +603 -0
  43. package/dist/lib/parser/utils/at-rule-support.js +431 -0
  44. package/dist/lib/parser/utils/at-rule-when-else.js +363 -0
  45. package/dist/lib/parser/utils/at-rule.js +31 -0
  46. package/dist/lib/parser/utils/cache.js +19 -0
  47. package/dist/lib/parser/utils/config.js +1 -0
  48. package/dist/lib/parser/utils/declaration.js +672 -91
  49. package/dist/lib/parser/utils/hash.js +1 -1
  50. package/dist/lib/parser/utils/selector.js +518 -0
  51. package/dist/lib/parser/utils/text.js +17 -1
  52. package/dist/lib/parser/utils/token.js +104 -0
  53. package/dist/lib/parser/utils/type.js +29 -18
  54. package/dist/lib/renderer/render.js +870 -269
  55. package/dist/lib/renderer/sourcemap/sourcemap.js +17 -7
  56. package/dist/lib/syntax/color/a98rgb.js +8 -12
  57. package/dist/lib/syntax/color/alpha.js +64 -0
  58. package/dist/lib/syntax/color/cmyk.js +23 -21
  59. package/dist/lib/syntax/color/color-mix.js +311 -237
  60. package/dist/lib/syntax/color/color.js +98 -92
  61. package/dist/lib/syntax/color/hex.js +29 -33
  62. package/dist/lib/syntax/color/hsl.js +7 -14
  63. package/dist/lib/syntax/color/hwb.js +14 -27
  64. package/dist/lib/syntax/color/lab.js +23 -24
  65. package/dist/lib/syntax/color/lch.js +16 -18
  66. package/dist/lib/syntax/color/oklab.js +21 -40
  67. package/dist/lib/syntax/color/oklch.js +16 -18
  68. package/dist/lib/syntax/color/p3.js +0 -8
  69. package/dist/lib/syntax/color/rec2020.js +9 -13
  70. package/dist/lib/syntax/color/relativecolor.js +160 -48
  71. package/dist/lib/syntax/color/rgb.js +14 -18
  72. package/dist/lib/syntax/color/srgb.js +30 -29
  73. package/dist/lib/syntax/color/utils/color-type.js +18 -0
  74. package/dist/lib/syntax/color/utils/colorspace.js +46 -0
  75. package/dist/lib/syntax/color/utils/components.js +36 -16
  76. package/dist/lib/syntax/color/utils/distance.js +21 -11
  77. package/dist/lib/syntax/color/utils/make-color.js +56 -0
  78. package/dist/lib/syntax/color/xyz.js +10 -22
  79. package/dist/lib/syntax/color/xyzd50.js +6 -20
  80. package/dist/lib/syntax/constants.js +464 -0
  81. package/dist/lib/syntax/syntax.js +825 -592
  82. package/dist/lib/validation/config.js +72 -33
  83. package/dist/lib/validation/config.json.js +1185 -85
  84. package/dist/lib/validation/match.js +3120 -0
  85. package/dist/lib/validation/parser/parse.js +683 -935
  86. package/dist/lib/validation/parser/typedef.js +106 -0
  87. package/dist/lib/validation/utils/list.js +4 -11
  88. package/dist/lib/validation/utils/whitespace.js +2 -8
  89. package/dist/node.js +102 -57
  90. package/dist/web.js +100 -48
  91. package/package.json +31 -17
  92. package/dist/lib/syntax/color/utils/constants.js +0 -214
  93. package/dist/lib/syntax/utils.js +0 -70
  94. package/dist/lib/validation/at-rules/container.js +0 -342
  95. package/dist/lib/validation/at-rules/counter-style.js +0 -90
  96. package/dist/lib/validation/at-rules/custom-media.js +0 -50
  97. package/dist/lib/validation/at-rules/document.js +0 -89
  98. package/dist/lib/validation/at-rules/else.js +0 -5
  99. package/dist/lib/validation/at-rules/font-feature-values.js +0 -63
  100. package/dist/lib/validation/at-rules/import.js +0 -150
  101. package/dist/lib/validation/at-rules/keyframes.js +0 -67
  102. package/dist/lib/validation/at-rules/layer.js +0 -41
  103. package/dist/lib/validation/at-rules/media.js +0 -255
  104. package/dist/lib/validation/at-rules/namespace.js +0 -81
  105. package/dist/lib/validation/at-rules/page-margin-box.js +0 -64
  106. package/dist/lib/validation/at-rules/page.js +0 -100
  107. package/dist/lib/validation/at-rules/supports.js +0 -295
  108. package/dist/lib/validation/at-rules/when.js +0 -185
  109. package/dist/lib/validation/atrule.js +0 -184
  110. package/dist/lib/validation/selector.js +0 -36
  111. package/dist/lib/validation/syntax.js +0 -1073
  112. package/dist/lib/validation/syntaxes/complex-selector-list.js +0 -27
  113. package/dist/lib/validation/syntaxes/complex-selector.js +0 -52
  114. package/dist/lib/validation/syntaxes/compound-selector.js +0 -196
  115. package/dist/lib/validation/syntaxes/family-name.js +0 -57
  116. package/dist/lib/validation/syntaxes/keyframe-selector.js +0 -36
  117. package/dist/lib/validation/syntaxes/layer-name.js +0 -57
  118. package/dist/lib/validation/syntaxes/relative-selector-list.js +0 -31
  119. package/dist/lib/validation/syntaxes/relative-selector.js +0 -38
  120. package/dist/lib/validation/syntaxes/selector-list.js +0 -5
  121. package/dist/lib/validation/syntaxes/selector.js +0 -5
  122. package/dist/lib/validation/syntaxes/url.js +0 -40
@@ -1,13 +1,12 @@
1
1
  import { PropertySet } from './set.js';
2
2
  import { getConfig } from '../utils/config.js';
3
- import { EnumToken } from '../../ast/types.js';
4
- import '../../ast/minify.js';
5
- import '../../ast/walk.js';
6
- import { parseString } from '../parse.js';
7
- import '../tokenize.js';
8
- import '../../syntax/color/utils/constants.js';
9
- import '../../renderer/sourcemap/lib/encode.js';
10
3
  import { PropertyMap } from './map.js';
4
+ import { parseString } from '../parse.js';
5
+ import { EnumToken, EnumAstNodeStatus } from '../../ast/types.js';
6
+ import { getParsedSyntax } from '../../validation/config.js';
7
+ import { ValidationSyntaxGroupEnum } from '../../validation/parser/typedef.js';
8
+ import { matchAllSyntaxes, createValidationContext } from '../../validation/match.js';
9
+ import { definedPropertySettings } from '../../syntax/constants.js';
11
10
 
12
11
  const config = getConfig();
13
12
  class PropertyList {
@@ -15,23 +14,34 @@ class PropertyList {
15
14
  declarations;
16
15
  constructor(options = {}) {
17
16
  this.options = options;
18
- this.declarations = new Map;
17
+ this.declarations = new Map();
19
18
  }
20
19
  set(nam, value) {
21
20
  return this.add({
22
21
  typ: EnumToken.DeclarationNodeType,
23
22
  nam,
24
- val: Array.isArray(value) ? value : parseString(String(value))
23
+ val: Array.isArray(value) ? value : parseString(String(value)),
25
24
  });
26
25
  }
27
26
  add(...declarations) {
28
27
  let name;
28
+ let syntaxRules = null;
29
+ let result;
29
30
  for (const declaration of declarations) {
30
- name = declaration.typ != EnumToken.DeclarationNodeType ? null : declaration.nam.toLowerCase();
31
- if (declaration.typ != EnumToken.DeclarationNodeType ||
32
- 'composes' === name ||
33
- (typeof this.options.removeDuplicateDeclarations === 'string' && this.options.removeDuplicateDeclarations === name) ||
34
- (Array.isArray(this.options.removeDuplicateDeclarations) ? this.options.removeDuplicateDeclarations.includes(declaration.nam) : !this.options.removeDuplicateDeclarations)) {
31
+ name =
32
+ declaration.typ != EnumToken.DeclarationNodeType
33
+ ? null
34
+ : declaration.nam.toLowerCase();
35
+ if (declaration.state == EnumAstNodeStatus.Invalid ||
36
+ declaration.state == EnumAstNodeStatus.Unknown ||
37
+ declaration.state == EnumAstNodeStatus.ValidationFailed ||
38
+ declaration.typ != EnumToken.DeclarationNodeType ||
39
+ "composes" === name ||
40
+ (typeof this.options.removeDuplicateDeclarations === "string" &&
41
+ this.options.removeDuplicateDeclarations === name) ||
42
+ (Array.isArray(this.options.removeDuplicateDeclarations)
43
+ ? this.options.removeDuplicateDeclarations.includes(declaration.nam)
44
+ : !this.options.removeDuplicateDeclarations)) {
35
45
  this.declarations.set(Number(Math.random().toString().slice(2)).toString(36), declaration);
36
46
  continue;
37
47
  }
@@ -39,29 +49,49 @@ class PropertyList {
39
49
  this.declarations.set(declaration.nam, declaration);
40
50
  continue;
41
51
  }
52
+ if (declaration.state == EnumAstNodeStatus.Unvalidated) {
53
+ syntaxRules = getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, declaration.nam.toLowerCase());
54
+ if (syntaxRules != null) {
55
+ result = matchAllSyntaxes(syntaxRules, createValidationContext(declaration.val), this.options);
56
+ Object.defineProperty(declaration, "state", {
57
+ ...definedPropertySettings,
58
+ value: result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.ValidationFailed,
59
+ });
60
+ }
61
+ }
62
+ // do not compute shorthand for invalid declarations
63
+ if (declaration.state !== EnumAstNodeStatus.Validated) {
64
+ this.declarations.set(declaration.nam, declaration);
65
+ return this;
66
+ }
42
67
  let propertyName = declaration.nam;
43
68
  let shortHandType;
44
- let shorthand;
69
+ let shorthand = null;
45
70
  if (propertyName in config.properties) {
46
71
  // @ts-ignore
47
- if ('map' in config.properties[propertyName]) {
48
- shortHandType = 'map';
72
+ if ("map" in config.properties[propertyName]) {
73
+ shortHandType = "map";
49
74
  // @ts-ignore
50
75
  shorthand = config.properties[propertyName].map;
51
76
  }
52
77
  else {
53
- shortHandType = 'set';
78
+ shortHandType = "set";
54
79
  // @ts-ignore
55
80
  shorthand = config.properties[propertyName].shorthand;
56
81
  }
57
82
  }
58
83
  else if (propertyName in config.map) {
59
- shortHandType = 'map';
84
+ shortHandType = "map";
60
85
  // @ts-ignore
61
86
  shorthand = config.map[propertyName].shorthand;
62
87
  }
88
+ else if (propertyName in config.property) {
89
+ shortHandType = "single";
90
+ // @ts-ignore
91
+ shorthand = config.property[propertyName];
92
+ }
63
93
  // @ts-ignore
64
- if (shortHandType == 'map') {
94
+ if (shortHandType == "map") {
65
95
  // @ts-ignore
66
96
  if (!this.declarations.has(shorthand)) {
67
97
  // @ts-ignore
@@ -69,26 +99,87 @@ class PropertyList {
69
99
  }
70
100
  // @ts-ignore
71
101
  this.declarations.get(shorthand).add(declaration);
72
- // return this;
73
102
  }
74
103
  // @ts-ignore
75
- else if (shortHandType == 'set') {
104
+ else if (shortHandType == "set") {
76
105
  // @ts-ignore
77
- // const shorthand: string = <string>config.properties[propertyName].shorthand;
78
106
  if (!this.declarations.has(shorthand)) {
79
107
  // @ts-ignore
80
- this.declarations.set(shorthand, new PropertySet(config.properties[shorthand]));
108
+ this.declarations.set(
109
+ // @ts-ignore
110
+ shorthand,
111
+ // @ts-ignore
112
+ new PropertySet(config.properties[shorthand]));
81
113
  }
82
114
  // @ts-ignore
83
115
  this.declarations.get(shorthand).add(declaration);
84
- // return this;
85
116
  }
86
117
  else {
118
+ if (shorthand != null) {
119
+ this.mapValues(declaration, shorthand);
120
+ }
87
121
  this.declarations.set(propertyName, declaration);
88
122
  }
89
123
  }
90
124
  return this;
91
125
  }
126
+ mapValues(declaration, mapping) {
127
+ let name;
128
+ let values = declaration.val;
129
+ if (mapping.pattern != null) {
130
+ // match pattern
131
+ for (const patterns of mapping.pattern) {
132
+ const set = new Set(values.filter((v) => v.typ !== EnumToken.CommentTokenType && v.typ !== EnumToken.WhitespaceTokenType));
133
+ const val = [];
134
+ for (const pattern of patterns) {
135
+ switch (pattern) {
136
+ case "<length>":
137
+ for (const v of set) {
138
+ if (v.typ === EnumToken.LengthTokenType ||
139
+ (v.typ === EnumToken.NumberTokenType && v.val === 0)) {
140
+ val.push(v);
141
+ set.delete(v);
142
+ }
143
+ }
144
+ default: {
145
+ for (const v of set) {
146
+ if (v.typ === EnumToken.IdenTokenType) {
147
+ const value = v.val.toLowerCase();
148
+ if (value === pattern || new RegExp(`(^|\|)${pattern}(\||$)`).test(value)) {
149
+ val.push(v);
150
+ set.delete(v);
151
+ }
152
+ }
153
+ }
154
+ }
155
+ }
156
+ }
157
+ if (set.size === 0) {
158
+ values = val.reduce((acc, curr) => {
159
+ if (acc.length > 0) {
160
+ acc.push({ typ: EnumToken.WhitespaceTokenType });
161
+ }
162
+ acc.push(curr);
163
+ return acc;
164
+ }, []);
165
+ break;
166
+ }
167
+ }
168
+ }
169
+ for (const val of declaration.val) {
170
+ if (val.typ === EnumToken.IdenTokenType) {
171
+ name = val.val.toLowerCase();
172
+ if (mapping.mapping[name] != null) {
173
+ // @ts-expect-error
174
+ Object.assign(val, mapping.mapping[name], { typ: EnumToken[mapping.mapping[name].typ] });
175
+ }
176
+ }
177
+ }
178
+ if (values != declaration.val) {
179
+ declaration.val.length = 0;
180
+ declaration.val.push(...values);
181
+ }
182
+ }
92
183
  [Symbol.iterator]() {
93
184
  let iterator = this.declarations.values();
94
185
  const iterators = [];
@@ -110,7 +201,7 @@ class PropertyList {
110
201
  }
111
202
  }
112
203
  return value;
113
- }
204
+ },
114
205
  };
115
206
  }
116
207
  }