@tbela99/css-parser 1.8.2 → 2.0.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 (105) hide show
  1. package/CHANGELOG.md +62 -0
  2. package/README.md +129 -31
  3. package/dist/index-umd-web.js +27383 -24083
  4. package/dist/index.d.ts +2737 -2200
  5. package/dist/package.json.js +2 -2
  6. package/dist/src/data/properties.json.js +1 -1
  7. package/dist/src/data/validation.json.js +1 -1
  8. package/dist/src/lib/ast/arena-node.js +548 -0
  9. package/dist/src/lib/ast/arena.js +3198 -0
  10. package/dist/src/lib/ast/features/calc.js +85 -76
  11. package/dist/src/lib/ast/features/if.js +155 -115
  12. package/dist/src/lib/ast/features/index.js +1 -1
  13. package/dist/src/lib/ast/features/inlinecssvariables.js +77 -84
  14. package/dist/src/lib/ast/features/prefix.js +184 -183
  15. package/dist/src/lib/ast/features/shorthand.js +40 -46
  16. package/dist/src/lib/ast/features/transform.js +24 -24
  17. package/dist/src/lib/ast/features/type.js +1 -1
  18. package/dist/src/lib/ast/fingerprint.js +333 -0
  19. package/dist/src/lib/ast/math/expression.js +458 -543
  20. package/dist/src/lib/ast/math/math.js +19 -41
  21. package/dist/src/lib/ast/minify.js +590 -647
  22. package/dist/src/lib/ast/transform/compute.js +82 -85
  23. package/dist/src/lib/ast/transform/matrix.js +21 -38
  24. package/dist/src/lib/ast/transform/minify.js +82 -193
  25. package/dist/src/lib/ast/transform/perspective.js +2 -3
  26. package/dist/src/lib/ast/transform/rotate.js +1 -1
  27. package/dist/src/lib/ast/transform/scale.js +1 -1
  28. package/dist/src/lib/ast/transform/skew.js +1 -1
  29. package/dist/src/lib/ast/transform/translate.js +1 -1
  30. package/dist/src/lib/ast/transform/utils.js +2 -2
  31. package/dist/src/lib/ast/types.js +263 -258
  32. package/dist/src/lib/ast/walk.js +246 -610
  33. package/dist/src/lib/fs/resolve.js +1 -1
  34. package/dist/src/lib/parser/declaration/list.js +223 -139
  35. package/dist/src/lib/parser/declaration/map.js +251 -238
  36. package/dist/src/lib/parser/declaration/set.js +74 -66
  37. package/dist/src/lib/parser/linesmap.js +1 -1
  38. package/dist/src/lib/parser/parse.js +1379 -1658
  39. package/dist/src/lib/parser/source.js +7 -4
  40. package/dist/src/lib/parser/tokenize.js +406 -199
  41. package/dist/src/lib/parser/utils/angle.js +11 -13
  42. package/dist/src/lib/parser/utils/at-rule-container.js +183 -168
  43. package/dist/src/lib/parser/utils/at-rule-font-feature-values.js +5 -5
  44. package/dist/src/lib/parser/utils/at-rule-generic.js +31 -24
  45. package/dist/src/lib/parser/utils/at-rule-import.js +44 -44
  46. package/dist/src/lib/parser/utils/at-rule-media.js +216 -205
  47. package/dist/src/lib/parser/utils/at-rule-support.js +83 -88
  48. package/dist/src/lib/parser/utils/at-rule-when-else.js +47 -47
  49. package/dist/src/lib/parser/utils/at-rule.js +5 -5
  50. package/dist/src/lib/parser/utils/base64.js +2 -2
  51. package/dist/src/lib/parser/utils/cache.js +1 -1
  52. package/dist/src/lib/parser/utils/config.js +1 -1
  53. package/dist/src/lib/parser/utils/declaration.js +233 -234
  54. package/dist/src/lib/parser/utils/eq.js +50 -3
  55. package/dist/src/lib/parser/utils/hash.js +2 -35
  56. package/dist/src/lib/parser/utils/selector.js +309 -387
  57. package/dist/src/lib/parser/utils/text.js +18 -4
  58. package/dist/src/lib/parser/utils/token.js +16 -69
  59. package/dist/src/lib/parser/utils/type.js +37 -24
  60. package/dist/src/lib/printer/render.js +965 -511
  61. package/dist/src/lib/printer/sourcemap/lib/codec.js +1 -1
  62. package/dist/src/lib/printer/sourcemap/sourcemap.js +1 -1
  63. package/dist/src/lib/syntax/color/a98rgb.js +1 -1
  64. package/dist/src/lib/syntax/color/alpha.js +36 -37
  65. package/dist/src/lib/syntax/color/cmyk.js +35 -52
  66. package/dist/src/lib/syntax/color/color-mix.js +96 -132
  67. package/dist/src/lib/syntax/color/color.js +222 -231
  68. package/dist/src/lib/syntax/color/hex.js +45 -55
  69. package/dist/src/lib/syntax/color/hsl.js +54 -62
  70. package/dist/src/lib/syntax/color/hwb.js +60 -68
  71. package/dist/src/lib/syntax/color/lab.js +59 -67
  72. package/dist/src/lib/syntax/color/lch.js +59 -67
  73. package/dist/src/lib/syntax/color/oklab.js +60 -68
  74. package/dist/src/lib/syntax/color/oklch.js +60 -67
  75. package/dist/src/lib/syntax/color/p3.js +1 -1
  76. package/dist/src/lib/syntax/color/prophotorgb.js +1 -1
  77. package/dist/src/lib/syntax/color/rec2020.js +1 -1
  78. package/dist/src/lib/syntax/color/relative-color.js +94 -144
  79. package/dist/src/lib/syntax/color/rgb.js +54 -57
  80. package/dist/src/lib/syntax/color/srgb.js +55 -53
  81. package/dist/src/lib/syntax/color/utils/color-type.js +13 -10
  82. package/dist/src/lib/syntax/color/utils/colorspace.js +11 -11
  83. package/dist/src/lib/syntax/color/utils/components.js +19 -20
  84. package/dist/src/lib/syntax/color/utils/distance.js +15 -13
  85. package/dist/src/lib/syntax/color/utils/make-color.js +15 -19
  86. package/dist/src/lib/syntax/color/utils/matrix.js +2 -2
  87. package/dist/src/lib/syntax/color/xyz.js +1 -1
  88. package/dist/src/lib/syntax/color/xyzd50.js +1 -1
  89. package/dist/src/lib/syntax/constants.js +2 -16
  90. package/dist/src/lib/syntax/syntax.js +301 -407
  91. package/dist/src/lib/validation/config.js +1 -1
  92. package/dist/src/lib/validation/match.js +959 -723
  93. package/dist/src/lib/validation/parser/parse.js +230 -112
  94. package/dist/src/lib/validation/parser/typedef.js +1 -1
  95. package/dist/src/lib/validation/utils/list.js +27 -12
  96. package/dist/src/node.js +12 -72
  97. package/dist/src/types.js +1 -1
  98. package/dist/src/utils/sync.js +9 -6
  99. package/dist/src/web.js +11 -73
  100. package/package.json +9 -10
  101. package/dist/index.cjs +0 -35870
  102. package/dist/src/lib/ast/clone.js +0 -51
  103. package/dist/src/lib/ast/expand.js +0 -311
  104. package/dist/src/lib/ast/find.js +0 -192
  105. package/dist/src/lib/ast/node.js +0 -59
@@ -2,7 +2,7 @@
2
2
  CSS parser, minifier and validator for node and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 1.8.2
5
+ Version: 2.0.0
6
6
  License: MIT
7
7
  */
8
8
  /**
@@ -11,7 +11,7 @@
11
11
  * @returns
12
12
  */
13
13
  function dasherize(value) {
14
- return value.replace(/([A-Z])/g, (all, one) => `-${one.toLowerCase()}`);
14
+ return value.replace(/[A-Z]/g, (one) => `-${one.toLowerCase()}`);
15
15
  }
16
16
  /**
17
17
  *
@@ -19,7 +19,21 @@ function dasherize(value) {
19
19
  * @returns
20
20
  */
21
21
  function camelize(value) {
22
- return value.replace(/-([a-z])/g, (all, one) => one.toUpperCase());
22
+ return value.replace(/-[a-z]/g, (match) => match[1].toUpperCase());
23
+ }
24
+ /**
25
+ * lowercase string, like String.prototype.toLowerCase(). the string is returned when it has no ascii uppercase and
26
+ * no non ascii character: no new string is created
27
+ * @param value
28
+ */
29
+ function lowercase(value) {
30
+ for (let i = 0; i < value.length; i++) {
31
+ const code = value.charCodeAt(i);
32
+ if ((code >= 65 && code <= 90) || code > 0x7f) {
33
+ return value.toLowerCase();
34
+ }
35
+ }
36
+ return value;
23
37
  }
24
38
  /**
25
39
  *
@@ -46,4 +60,4 @@ function equalsIgnoreCase(a, b) {
46
60
  return true;
47
61
  }
48
62
 
49
- export { camelize, dasherize, equalsIgnoreCase };
63
+ export { camelize, dasherize, equalsIgnoreCase, lowercase };
@@ -2,11 +2,11 @@
2
2
  CSS parser, minifier and validator for node and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 1.8.2
5
+ Version: 2.0.0
6
6
  License: MIT
7
7
  */
8
8
  import { EnumToken } from '../../ast/types.js';
9
- import { PARENT, tokensfuncSet } from '../../syntax/constants.js';
9
+ import { tokensfuncSet } from '../../syntax/constants.js';
10
10
 
11
11
  const trimTokenSpace = new Set([
12
12
  EnumToken.CommaTokenType,
@@ -28,94 +28,41 @@ const trimSpaceAfter = new Set([
28
28
  EnumToken.PipeTokenType,
29
29
  ]);
30
30
  /**
31
- * Replace token in its parent node
32
- * @param parent
33
- * @param node
34
- * @param replacement
35
- * @throws TypeError replacement is null
36
- * @throws ReferenceError node not found in parent
31
+ * remove leading, trailing and redundant whitespace tokens (in place)
32
+ * @param arena
33
+ * @param tokens
37
34
  */
38
- function replaceNodeOrValue(parent, node, replacement) {
39
- if (replacement == null || (Array.isArray(replacement) && replacement.length === 0)) {
40
- throw new TypeError(`replacement is null`);
41
- }
42
- if (!Array.isArray(replacement)) {
43
- if (replacement[PARENT] != parent) {
44
- replacement[PARENT] = parent;
45
- }
46
- }
47
- else {
48
- for (const newNode of replacement) {
49
- if (newNode[PARENT] != parent) {
50
- newNode[PARENT] = parent;
51
- }
52
- }
53
- }
54
- if (parent.typ == EnumToken.BinaryExpressionTokenType) {
55
- if (parent.l == node) {
56
- parent.l = replacement;
57
- }
58
- else if (parent.r == node) {
59
- parent.r = replacement;
60
- }
61
- else {
62
- throw new ReferenceError("Node not found");
63
- }
64
- }
65
- else {
66
- const target = "val" in parent && Array.isArray(parent.val)
67
- ? parent.val
68
- : (parent.chi ?? parent);
69
- if (Array.isArray(target)) {
70
- // @ts-ignore
71
- const index = target.indexOf(node);
72
- if (index == -1) {
73
- throw new ReferenceError("Node not found");
74
- }
75
- target.splice(index, 1, ...(Array.isArray(replacement) ? replacement : [replacement]));
76
- }
77
- else if ("l" in target && target.l == node) {
78
- target.l = replacement;
79
- }
80
- else if ("r" in target && target.r == node) {
81
- target.r = replacement;
82
- }
83
- else {
84
- throw new ReferenceError("Node not found");
85
- }
86
- }
87
- node[PARENT] = null;
88
- return true;
89
- }
90
- function trimWhiteSpaceTokens(tokens) {
35
+ function trimWhiteSpaceTokens(arena, tokens) {
91
36
  let i = 0;
92
- if (tokens[0]?.typ === EnumToken.WhitespaceTokenType) {
37
+ if (tokens.length > 0 && arena.dataTypes[tokens[0]] === EnumToken.WhitespaceTokenType) {
93
38
  tokens.shift();
94
39
  }
95
- if (tokens[tokens.length - 1]?.typ === EnumToken.WhitespaceTokenType) {
40
+ if (tokens.length > 0 && arena.dataTypes[tokens[tokens.length - 1]] === EnumToken.WhitespaceTokenType) {
96
41
  tokens.pop();
97
42
  }
98
43
  if (tokens.length === 1) {
99
- if (tokens[0].typ === EnumToken.WhitespaceTokenType) {
44
+ if (arena.dataTypes[tokens[0]] === EnumToken.WhitespaceTokenType) {
100
45
  tokens.length = 0;
101
46
  }
102
47
  return tokens;
103
48
  }
104
49
  for (; i < tokens.length; i++) {
105
- if (trimTokenSpace.has(tokens[i].typ)) {
106
- if (tokens[i + 1]?.typ === EnumToken.WhitespaceTokenType) {
50
+ if (trimTokenSpace.has(arena.dataTypes[tokens[i]])) {
51
+ if (i + 1 < tokens.length && arena.dataTypes[tokens[i + 1]] === EnumToken.WhitespaceTokenType) {
107
52
  tokens.splice(i + 1, 1);
108
53
  }
109
- if (i > 0 && tokens[i - 1].typ === EnumToken.Whitespace) {
54
+ if (i > 0 && arena.dataTypes[tokens[i - 1]] === EnumToken.WhitespaceTokenType) {
110
55
  tokens.splice(--i, 1);
111
56
  continue;
112
57
  }
113
58
  }
114
- if (trimSpaceAfter.has(tokens[i].typ) && tokens[i + 1]?.typ === EnumToken.WhitespaceTokenType) {
59
+ if (trimSpaceAfter.has(arena.dataTypes[tokens[i]]) &&
60
+ i + 1 < tokens.length &&
61
+ arena.dataTypes[tokens[i + 1]] === EnumToken.WhitespaceTokenType) {
115
62
  tokens.splice(i + 1, 1);
116
63
  }
117
64
  }
118
65
  return tokens;
119
66
  }
120
67
 
121
- export { replaceNodeOrValue, trimWhiteSpaceTokens };
68
+ export { trimWhiteSpaceTokens };
@@ -2,25 +2,49 @@
2
2
  CSS parser, minifier and validator for node and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 1.8.2
5
+ Version: 2.0.0
6
6
  License: MIT
7
7
  */
8
8
  import { EnumToken } from '../../ast/types.js';
9
9
  import { tokensfuncSet } from '../../syntax/constants.js';
10
10
  import { isColor } from '../../syntax/syntax.js';
11
11
 
12
- function matchType(val, properties) {
13
- if ((val.typ === EnumToken.IdenTokenType && properties.keywords.includes(val.val)) ||
12
+ const mathTokenTypes = [
13
+ EnumToken.Add,
14
+ EnumToken.Mul,
15
+ EnumToken.Div,
16
+ EnumToken.Sub,
17
+ EnumToken.LiteralTokenType,
18
+ EnumToken.CommaTokenType,
19
+ EnumToken.WhitespaceTokenType,
20
+ EnumToken.DimensionTokenType,
21
+ EnumToken.NumberTokenType,
22
+ EnumToken.LengthTokenType,
23
+ EnumToken.AngleTokenType,
24
+ EnumToken.PercentageTokenType,
25
+ EnumToken.ResolutionTokenType,
26
+ EnumToken.TimeTokenType,
27
+ EnumToken.BinaryExpressionTokenType,
28
+ ];
29
+ /**
30
+ * check if a token matches a shorthand property type
31
+ * @param arena
32
+ * @param val
33
+ * @param properties
34
+ */
35
+ function matchType(arena, val, properties) {
36
+ const typ = arena.dataTypes[val];
37
+ if ((typ === EnumToken.IdenTokenType && properties.keywords.includes(arena.strings[arena.stringValue[val]])) ||
14
38
  properties.types.some(
15
39
  // @ts-expect-error
16
40
  (t) =>
17
41
  // @ts-expect-error
18
- (val.typ === EnumToken.IdenTokenType && EnumToken[t] === EnumToken.ColorTokenType && isColor(val)) ||
42
+ (typ === EnumToken.IdenTokenType && EnumToken[t] === EnumToken.ColorTokenType && isColor(arena, val)) ||
19
43
  // @ts-expect-error
20
- EnumToken[t] === val.typ)) {
44
+ EnumToken[t] === typ)) {
21
45
  return true;
22
46
  }
23
- if (val.typ === EnumToken.NumberTokenType && val.val === 0) {
47
+ if (typ === EnumToken.NumberTokenType && arena.numericValue[val] === 0) {
24
48
  // @ts-ignore
25
49
  return properties.types.some((type) => {
26
50
  // @ts-ignore
@@ -28,24 +52,13 @@ function matchType(val, properties) {
28
52
  return typ == EnumToken.LengthTokenType || typ == EnumToken.AngleTokenType;
29
53
  });
30
54
  }
31
- if (tokensfuncSet.has(val.typ) && val.typ === EnumToken.MathFunctionTokenType) {
32
- return val.chi.every((t) => [
33
- EnumToken.Add,
34
- EnumToken.Mul,
35
- EnumToken.Div,
36
- EnumToken.Sub,
37
- EnumToken.LiteralTokenType,
38
- EnumToken.CommaTokenType,
39
- EnumToken.WhitespaceTokenType,
40
- EnumToken.DimensionTokenType,
41
- EnumToken.NumberTokenType,
42
- EnumToken.LengthTokenType,
43
- EnumToken.AngleTokenType,
44
- EnumToken.PercentageTokenType,
45
- EnumToken.ResolutionTokenType,
46
- EnumToken.TimeTokenType,
47
- EnumToken.BinaryExpressionTokenType,
48
- ].includes(t.typ) || matchType(t, properties));
55
+ if (tokensfuncSet.has(typ) && typ === EnumToken.MathFunctionTokenType) {
56
+ for (let t = arena.firstToken[val]; t != 0; t = arena.nextSibling[t]) {
57
+ if (!mathTokenTypes.includes(arena.dataTypes[t]) && !matchType(arena, t, properties)) {
58
+ return false;
59
+ }
60
+ }
61
+ return true;
49
62
  }
50
63
  return false;
51
64
  }