@tbela99/css-parser 2.0.1 → 2.2.0

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 (128) hide show
  1. package/CHANGELOG.md +86 -1
  2. package/README.md +15 -7
  3. package/architecture.png +0 -0
  4. package/dist/{src/data → data}/properties.json.js +86 -2
  5. package/dist/{src/data → data}/validation.json.js +4848 -3750
  6. package/dist/index-umd-web.js +18234 -9498
  7. package/dist/index.d.ts +1251 -373
  8. package/dist/{src/lib → lib}/ast/arena-node.js +119 -57
  9. package/dist/{src/lib → lib}/ast/arena.js +1034 -655
  10. package/dist/{src/lib → lib}/ast/cache.js +36 -31
  11. package/dist/{src/lib → lib}/ast/features/calc.js +37 -27
  12. package/dist/{src/lib → lib}/ast/features/if.js +24 -19
  13. package/dist/{src/lib → lib}/ast/features/index.js +3 -2
  14. package/dist/{src/lib → lib}/ast/features/inlinecssvariables.js +42 -23
  15. package/dist/lib/ast/features/mixin.js +953 -0
  16. package/dist/{src/lib → lib}/ast/features/prefix.js +82 -69
  17. package/dist/lib/ast/features/shorthand.js +129 -0
  18. package/dist/lib/ast/features/transform.js +188 -0
  19. package/dist/{src/lib → lib}/ast/features/type.js +2 -2
  20. package/dist/{src/lib → lib}/ast/math/expression.js +150 -150
  21. package/dist/{src/lib → lib}/ast/math/math.js +8 -8
  22. package/dist/{src/lib → lib}/ast/minify.js +932 -354
  23. package/dist/lib/ast/mixins/custom-function.js +393 -0
  24. package/dist/lib/ast/mixins/utils.js +53 -0
  25. package/dist/lib/ast/mixins/variables.js +678 -0
  26. package/dist/lib/ast/strings.js +906 -0
  27. package/dist/lib/ast/transform/compute.js +560 -0
  28. package/dist/lib/ast/transform/matrix.js +67 -0
  29. package/dist/lib/ast/transform/minify.js +325 -0
  30. package/dist/lib/ast/transform/perspective.js +24 -0
  31. package/dist/{src/lib → lib}/ast/transform/rotate.js +16 -7
  32. package/dist/lib/ast/transform/scale.js +43 -0
  33. package/dist/lib/ast/transform/skew.js +38 -0
  34. package/dist/lib/ast/transform/translate.js +56 -0
  35. package/dist/lib/ast/transform/utils.js +390 -0
  36. package/dist/{src/lib → lib}/ast/types.js +7 -15
  37. package/dist/{src/lib → lib}/ast/walk.js +19 -19
  38. package/dist/{src/lib → lib}/fs/resolve.js +15 -15
  39. package/dist/{src/lib → lib}/parser/declaration/list.js +55 -26
  40. package/dist/{src/lib → lib}/parser/declaration/map.js +85 -72
  41. package/dist/{src/lib → lib}/parser/declaration/set.js +32 -27
  42. package/dist/lib/parser/input.js +85 -0
  43. package/dist/{src/lib → lib}/parser/linesmap.js +3 -16
  44. package/dist/{src/lib → lib}/parser/parse.js +852 -619
  45. package/dist/lib/parser/source.js +71 -0
  46. package/dist/{src/lib → lib}/parser/tokenize.js +886 -722
  47. package/dist/{src/lib → lib}/parser/utils/angle.js +3 -3
  48. package/dist/{src/lib → lib}/parser/utils/at-rule-container.js +68 -35
  49. package/dist/{src/lib → lib}/parser/utils/at-rule-font-feature-values.js +3 -3
  50. package/dist/{src/lib → lib}/parser/utils/at-rule-generic.js +31 -13
  51. package/dist/{src/lib → lib}/parser/utils/at-rule-import.js +29 -16
  52. package/dist/{src/lib → lib}/parser/utils/at-rule-media.js +77 -58
  53. package/dist/{src/lib → lib}/parser/utils/at-rule-support.js +18 -20
  54. package/dist/{src/lib → lib}/parser/utils/at-rule-when-else.js +9 -8
  55. package/dist/{src/lib → lib}/parser/utils/at-rule.js +3 -3
  56. package/dist/{src/lib → lib}/parser/utils/base64.js +2 -2
  57. package/dist/lib/parser/utils/cache.js +53 -0
  58. package/dist/lib/parser/utils/canonical.js +78 -0
  59. package/dist/lib/parser/utils/codes.js +93 -0
  60. package/dist/{src/lib → lib}/parser/utils/config.js +2 -2
  61. package/dist/{src/lib → lib}/parser/utils/declaration.js +67 -49
  62. package/dist/{src/lib → lib}/parser/utils/eq.js +14 -14
  63. package/dist/{src/lib → lib}/parser/utils/hash.js +4 -4
  64. package/dist/lib/parser/utils/selector.js +720 -0
  65. package/dist/{src/lib → lib}/parser/utils/text.js +2 -2
  66. package/dist/{src/lib → lib}/parser/utils/token.js +2 -2
  67. package/dist/{src/lib → lib}/parser/utils/type.js +11 -9
  68. package/dist/{src/lib → lib}/printer/render.js +536 -243
  69. package/dist/lib/printer/result.js +148 -0
  70. package/dist/{src/lib → lib}/printer/sourcemap/lib/codec.js +5 -8
  71. package/dist/{src/lib → lib}/printer/sourcemap/sourcemap.js +13 -13
  72. package/dist/lib/printer/writer.js +252 -0
  73. package/dist/{src/lib → lib}/syntax/color/a98rgb.js +6 -6
  74. package/dist/{src/lib → lib}/syntax/color/alpha.js +12 -11
  75. package/dist/{src/lib → lib}/syntax/color/cmyk.js +11 -6
  76. package/dist/{src/lib → lib}/syntax/color/color-mix.js +78 -67
  77. package/dist/{src/lib → lib}/syntax/color/color.js +52 -47
  78. package/dist/{src/lib → lib}/syntax/color/hex.js +17 -17
  79. package/dist/{src/lib → lib}/syntax/color/hsl.js +8 -8
  80. package/dist/{src/lib → lib}/syntax/color/hwb.js +6 -7
  81. package/dist/{src/lib → lib}/syntax/color/lab.js +8 -8
  82. package/dist/{src/lib → lib}/syntax/color/lch.js +12 -11
  83. package/dist/{src/lib → lib}/syntax/color/oklab.js +9 -9
  84. package/dist/{src/lib → lib}/syntax/color/oklch.js +5 -5
  85. package/dist/{src/lib → lib}/syntax/color/p3.js +2 -2
  86. package/dist/{src/lib → lib}/syntax/color/prophotorgb.js +4 -4
  87. package/dist/{src/lib → lib}/syntax/color/rec2020.js +3 -3
  88. package/dist/{src/lib → lib}/syntax/color/relative-color.js +41 -34
  89. package/dist/{src/lib → lib}/syntax/color/rgb.js +17 -17
  90. package/dist/{src/lib → lib}/syntax/color/srgb.js +20 -20
  91. package/dist/{src/lib → lib}/syntax/color/utils/color-type.js +6 -4
  92. package/dist/{src/lib → lib}/syntax/color/utils/colorspace.js +12 -14
  93. package/dist/{src/lib → lib}/syntax/color/utils/components.js +13 -13
  94. package/dist/{src/lib → lib}/syntax/color/utils/distance.js +2 -2
  95. package/dist/{src/lib → lib}/syntax/color/utils/make-color.js +6 -4
  96. package/dist/{src/lib → lib}/syntax/color/utils/matrix.js +2 -2
  97. package/dist/{src/lib → lib}/syntax/color/xyz.js +5 -10
  98. package/dist/{src/lib → lib}/syntax/color/xyzd50.js +3 -3
  99. package/dist/{src/lib → lib}/syntax/constants.js +30 -3
  100. package/dist/{src/lib → lib}/syntax/syntax.js +151 -116
  101. package/dist/lib/validation/config.js +173 -0
  102. package/dist/lib/validation/first.js +571 -0
  103. package/dist/{src/lib → lib}/validation/match.js +1256 -579
  104. package/dist/{src/lib → lib}/validation/parser/parse.js +85 -59
  105. package/dist/{src/lib → lib}/validation/parser/typedef.js +6 -2
  106. package/dist/{src/lib → lib}/validation/utils/list.js +3 -19
  107. package/dist/{src/node.js → node.js} +92 -80
  108. package/dist/{src/types.js → types.js} +2 -2
  109. package/dist/{src/utils → utils}/sync.js +13 -8
  110. package/dist/version.d.ts +4 -0
  111. package/dist/version.js +13 -0
  112. package/dist/{src/web.js → web.js} +80 -75
  113. package/package.json +13 -13
  114. package/dist/package.json.js +0 -10
  115. package/dist/src/lib/ast/features/shorthand.js +0 -87
  116. package/dist/src/lib/ast/features/transform.js +0 -85
  117. package/dist/src/lib/ast/transform/compute.js +0 -358
  118. package/dist/src/lib/ast/transform/matrix.js +0 -88
  119. package/dist/src/lib/ast/transform/minify.js +0 -267
  120. package/dist/src/lib/ast/transform/perspective.js +0 -16
  121. package/dist/src/lib/ast/transform/scale.js +0 -39
  122. package/dist/src/lib/ast/transform/skew.js +0 -30
  123. package/dist/src/lib/ast/transform/translate.js +0 -39
  124. package/dist/src/lib/ast/transform/utils.js +0 -287
  125. package/dist/src/lib/parser/source.js +0 -127
  126. package/dist/src/lib/parser/utils/cache.js +0 -54
  127. package/dist/src/lib/parser/utils/selector.js +0 -440
  128. package/dist/src/lib/validation/config.js +0 -120
@@ -0,0 +1,173 @@
1
+ /*
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
+
4
+ Author(s): Thierry Bela
5
+ Version: 2.2.0
6
+ License: MIT
7
+ */
8
+ import config from '../../data/validation.json.js';
9
+ import { trimSyntaxArray, parseSyntax } from './parser/parse.js';
10
+ import { ValidationTokenEnum, ValidationSyntaxGroupEnum } from './parser/typedef.js';
11
+ import { memoize } from '../parser/utils/cache.js';
12
+
13
+ /**
14
+ * parsed syntaxes, by syntax node: the names that share a definition (`-webkit-transform` and `transform`, the aliases)
15
+ * share the parsed syntax, and the validations cached by syntax (see `matchDeclarationSyntax()`)
16
+ */
17
+ const parsedSyntaxes = new WeakMap();
18
+ Object.freeze(config);
19
+ function getSyntaxConfig() {
20
+ // @ts-expect-error
21
+ return config;
22
+ }
23
+ /**
24
+ * find a syntax definition in a group.
25
+ *
26
+ * the syntaxes are generated from webref (see scripts/webref.ts): the functions and the patched syntaxes are stored in
27
+ * the {@link ValidationSyntaxGroupEnum.Syntaxes} group, the value types are stored in the
28
+ * {@link ValidationSyntaxGroupEnum.Types} group with their name without the angle brackets (`color`).
29
+ * a {@link ValidationSyntaxGroupEnum.Syntaxes} lookup falls back to the {@link ValidationSyntaxGroupEnum.Types} group
30
+ * @param group
31
+ * @param key
32
+ */
33
+ function lookupNode(group, key) {
34
+ const groups = config;
35
+ if (groups[group]?.[key] != null) {
36
+ let node = groups[group][key];
37
+ // an alias uses the syntax of the standard property: `-webkit-align-content` is an alias of `align-content`.
38
+ // the name of the declaration is not changed
39
+ for (let i = 0; node.alias != null && i < 4; i++) {
40
+ const target = groups[group][node.alias];
41
+ if (target == null) {
42
+ return null;
43
+ }
44
+ node = target;
45
+ }
46
+ return node.alias != null ? null : node;
47
+ }
48
+ if (group === ValidationSyntaxGroupEnum.Syntaxes) {
49
+ return groups[ValidationSyntaxGroupEnum.Types]?.[key] ?? null;
50
+ }
51
+ return null;
52
+ }
53
+ /**
54
+ * true if a type or a function syntax is defined
55
+ * @param key the type name without the angle brackets, or the function name followed by `()`
56
+ */
57
+ function hasSyntax(key) {
58
+ let defined = definedSyntaxes.get(key);
59
+ if (defined === undefined) {
60
+ defined = lookupNode(ValidationSyntaxGroupEnum.Syntaxes, key) != null;
61
+ definedSyntaxes.set(key, defined);
62
+ }
63
+ return defined;
64
+ }
65
+ /**
66
+ * the types and the functions that are defined, by name, see {@link hasSyntax}
67
+ */
68
+ const definedSyntaxes = new Map();
69
+ /**
70
+ * return the raw syntax of a type or a function
71
+ * @param key the type name without the angle brackets, or the function name followed by `()`
72
+ */
73
+ function getSyntax(key) {
74
+ return lookupNode(ValidationSyntaxGroupEnum.Syntaxes, key)?.syntax ?? null;
75
+ }
76
+ function findNode(group, key) {
77
+ let obj = lookupNode(group, key);
78
+ if (obj == null) {
79
+ if (key.charAt(0) === "@" || key.charAt(0) === "-") {
80
+ const matches = key.match(/^(@?)(-[a-zA-Z]+)-(.*?)$/);
81
+ if (matches != null) {
82
+ key = matches[1] + matches[3];
83
+ }
84
+ }
85
+ obj = lookupNode(group, key);
86
+ if (obj == null) {
87
+ return null;
88
+ }
89
+ }
90
+ return obj;
91
+ }
92
+ const getParsedSyntax = memoize((group, key) => {
93
+ // @ts-expect-error
94
+ const obj = findNode(group, key);
95
+ if (obj == null) {
96
+ return null;
97
+ }
98
+ let syntax = parsedSyntaxes.get(obj);
99
+ if (syntax === undefined) {
100
+ syntax = Object.freeze(trimSyntaxArray(parseSyntax(obj.syntax)));
101
+ parsedSyntaxes.set(obj, syntax);
102
+ }
103
+ return syntax;
104
+ });
105
+ const getSyntaxRule = memoize((group, key) => {
106
+ const node = findNode(group, key);
107
+ if (node == null) {
108
+ return null;
109
+ }
110
+ let syntaxRules = getParsedSyntax(group, key);
111
+ if (syntaxRules == null) {
112
+ return null;
113
+ }
114
+ let blockStart = -1;
115
+ let blockEnd = -1;
116
+ let i;
117
+ for (i = 0; i < syntaxRules.length; i++) {
118
+ if (syntaxRules[i].typ === ValidationTokenEnum.OpenCurlyBrace ||
119
+ syntaxRules[i].typ === ValidationTokenEnum.SemiColon) {
120
+ blockStart = i;
121
+ break;
122
+ }
123
+ }
124
+ if (blockStart !== -1) {
125
+ i = syntaxRules.length;
126
+ while (i--) {
127
+ if (syntaxRules[i].typ === ValidationTokenEnum.CloseCurlyBrace) {
128
+ blockEnd = i;
129
+ break;
130
+ }
131
+ }
132
+ }
133
+ let block = blockStart === -1 ? null : trimSyntaxArray(syntaxRules.slice(blockStart + 1, blockEnd));
134
+ let prelude = trimSyntaxArray(blockStart === -1 ? syntaxRules.slice() : syntaxRules.slice(0, blockStart));
135
+ let propertyDescriptors = null;
136
+ // @ts-expect-error
137
+ if (node.descriptors != null) {
138
+ propertyDescriptors = {};
139
+ // @ts-expect-error
140
+ for (const [key, value] of Object.entries(node.descriptors)) {
141
+ // @ts-expect-error
142
+ propertyDescriptors[key] = parseSyntax(typeof value === "string" ? value : value.syntax);
143
+ }
144
+ }
145
+ let optionalBody = false;
146
+ if (block == null || block.length === 0) {
147
+ let k = prelude.length - 1;
148
+ if (ValidationTokenEnum.SemiColon === prelude[k].typ) {
149
+ k--;
150
+ }
151
+ if (k >= 0 && ValidationTokenEnum.Bracket === prelude[k].typ) {
152
+ if (prelude[k].chi[0].typ ===
153
+ ValidationTokenEnum.OpenCurlyBrace &&
154
+ prelude[k].chi.at(-1).typ ===
155
+ ValidationTokenEnum.CloseCurlyBrace) {
156
+ block = prelude[k].chi.slice(1, -1);
157
+ optionalBody = prelude[k].isOptional ?? false;
158
+ }
159
+ }
160
+ }
161
+ return {
162
+ optionalBody,
163
+ acceptAnyDeclaration: node.syntax.includes("<declaration-list>"),
164
+ acceptAnyRule: node.syntax.includes("<group-rule-body>") ||
165
+ node.syntax.includes("<stylesheet>"),
166
+ getPreludeRules: () => prelude,
167
+ getBlockRules: () => (block == null || block.length === 0 ? null : block),
168
+ // getRules: () => syntaxRules,
169
+ getPropertyDescriptors: () => propertyDescriptors,
170
+ };
171
+ });
172
+
173
+ export { getParsedSyntax, getSyntax, getSyntaxConfig, getSyntaxRule, hasSyntax };