@tbela99/css-parser 1.3.2 → 1.3.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 (59) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +59 -20
  3. package/dist/index-umd-web.js +1846 -1075
  4. package/dist/index.cjs +1941 -1202
  5. package/dist/index.d.ts +914 -181
  6. package/dist/lib/ast/expand.js +5 -10
  7. package/dist/lib/ast/features/calc.js +8 -8
  8. package/dist/lib/ast/features/inlinecssvariables.js +9 -8
  9. package/dist/lib/ast/features/prefix.js +5 -15
  10. package/dist/lib/ast/features/shorthand.js +5 -6
  11. package/dist/lib/ast/features/transform.js +18 -25
  12. package/dist/lib/ast/features/type.js +4 -2
  13. package/dist/lib/ast/minify.js +56 -112
  14. package/dist/lib/ast/transform/compute.js +2 -4
  15. package/dist/lib/ast/transform/matrix.js +20 -20
  16. package/dist/lib/ast/transform/minify.js +105 -12
  17. package/dist/lib/ast/transform/rotate.js +11 -11
  18. package/dist/lib/ast/transform/scale.js +6 -6
  19. package/dist/lib/ast/transform/skew.js +4 -4
  20. package/dist/lib/ast/transform/translate.js +3 -3
  21. package/dist/lib/ast/transform/utils.js +30 -37
  22. package/dist/lib/ast/types.js +16 -4
  23. package/dist/lib/ast/walk.js +172 -70
  24. package/dist/lib/fs/resolve.js +12 -7
  25. package/dist/lib/parser/declaration/list.js +3 -1
  26. package/dist/lib/parser/parse.js +441 -161
  27. package/dist/lib/parser/tokenize.js +12 -14
  28. package/dist/lib/renderer/render.js +7 -7
  29. package/dist/lib/syntax/color/cmyk.js +6 -3
  30. package/dist/lib/syntax/color/color-mix.js +2 -3
  31. package/dist/lib/syntax/color/color.js +28 -6
  32. package/dist/lib/syntax/color/hex.js +3 -0
  33. package/dist/lib/syntax/color/hsl.js +18 -7
  34. package/dist/lib/syntax/color/hwb.js +3 -3
  35. package/dist/lib/syntax/color/lab.js +4 -4
  36. package/dist/lib/syntax/color/lch.js +7 -4
  37. package/dist/lib/syntax/color/oklab.js +4 -4
  38. package/dist/lib/syntax/color/oklch.js +18 -6
  39. package/dist/lib/syntax/color/relativecolor.js +9 -56
  40. package/dist/lib/syntax/color/srgb.js +1 -1
  41. package/dist/lib/syntax/syntax.js +36 -18
  42. package/dist/lib/validation/at-rules/container.js +11 -0
  43. package/dist/lib/validation/at-rules/counter-style.js +11 -0
  44. package/dist/lib/validation/at-rules/font-feature-values.js +11 -0
  45. package/dist/lib/validation/at-rules/keyframes.js +11 -0
  46. package/dist/lib/validation/at-rules/layer.js +11 -0
  47. package/dist/lib/validation/at-rules/media.js +11 -0
  48. package/dist/lib/validation/at-rules/page-margin-box.js +11 -0
  49. package/dist/lib/validation/at-rules/page.js +11 -0
  50. package/dist/lib/validation/at-rules/supports.js +11 -0
  51. package/dist/lib/validation/at-rules/when.js +11 -0
  52. package/dist/lib/validation/config.js +0 -2
  53. package/dist/lib/validation/config.json.js +21 -9
  54. package/dist/lib/validation/parser/parse.js +53 -2
  55. package/dist/lib/validation/syntax.js +199 -36
  56. package/dist/node.js +63 -36
  57. package/dist/web.js +84 -25
  58. package/package.json +7 -5
  59. package/dist/lib/validation/parser/types.js +0 -54
@@ -16,14 +16,11 @@ import '../syntax/color/utils/constants.js';
16
16
  */
17
17
  function expand(ast) {
18
18
  const result = { ...ast, chi: [] };
19
- // @ts-ignore
20
19
  for (let i = 0; i < ast.chi.length; i++) {
21
- // @ts-ignore
22
20
  const node = ast.chi[i];
23
21
  if (node.typ == EnumToken.RuleNodeType) {
24
22
  // @ts-ignore
25
23
  result.chi.push(...expandRule(node));
26
- // i += expanded.length - 1;
27
24
  }
28
25
  else if (node.typ == EnumToken.AtRuleNodeType && 'chi' in node) {
29
26
  let hasRule = false;
@@ -38,6 +35,10 @@ function expand(ast) {
38
35
  // @ts-ignore
39
36
  result.chi.push({ ...(hasRule ? expand(node) : node) });
40
37
  }
38
+ else {
39
+ // @ts-ignore
40
+ result.chi.push(node);
41
+ }
41
42
  }
42
43
  return result;
43
44
  }
@@ -220,13 +221,7 @@ function replaceCompoundLiteral(selector, replace) {
220
221
  return 1;
221
222
  }
222
223
  return b == '&' ? -1 : 0;
223
- }).reduce((acc, curr) => {
224
- // if (acc.length > 0 && curr == '&' && (replace.charAt(0) != '.' || replace.includes(' '))) {
225
- //
226
- // return acc + ':is(' + replace + ')';
227
- // }
228
- return acc + (curr == '&' ? replace : curr);
229
- }, '');
224
+ }).reduce((acc, curr) => acc + (curr == '&' ? replace : curr), '');
230
225
  }
231
226
 
232
227
  export { expand, replaceCompound };
@@ -1,5 +1,5 @@
1
1
  import { EnumToken } from '../types.js';
2
- import { walkValues, WalkerValueEvent, WalkerOptionEnum } from '../walk.js';
2
+ import { walkValues, WalkerEvent, WalkerOptionEnum } from '../walk.js';
3
3
  import { evaluate } from '../math/expression.js';
4
4
  import { renderToken } from '../../renderer/render.js';
5
5
  import '../../renderer/sourcemap/lib/encode.js';
@@ -8,16 +8,15 @@ import '../minify.js';
8
8
  import '../../parser/parse.js';
9
9
  import '../../parser/tokenize.js';
10
10
  import '../../parser/utils/config.js';
11
+ import { FeatureWalkMode } from './type.js';
11
12
 
12
13
  class ComputeCalcExpressionFeature {
14
+ accept = new Set([EnumToken.RuleNodeType, EnumToken.AtRuleNodeType]);
13
15
  get ordering() {
14
16
  return 1;
15
17
  }
16
- get preProcess() {
17
- return false;
18
- }
19
- get postProcess() {
20
- return true;
18
+ get processMode() {
19
+ return FeatureWalkMode.Post;
21
20
  }
22
21
  static register(options) {
23
22
  if (options.computeCalcExpression) {
@@ -27,7 +26,7 @@ class ComputeCalcExpressionFeature {
27
26
  }
28
27
  run(ast) {
29
28
  if (!('chi' in ast)) {
30
- return;
29
+ return null;
31
30
  }
32
31
  for (const node of ast.chi) {
33
32
  if (node.typ != EnumToken.DeclarationNodeType) {
@@ -35,7 +34,7 @@ class ComputeCalcExpressionFeature {
35
34
  }
36
35
  const set = new Set;
37
36
  for (const { value, parent } of walkValues(node.val, node, {
38
- event: WalkerValueEvent.Enter,
37
+ event: WalkerEvent.Enter,
39
38
  // @ts-ignore
40
39
  fn(node, parent) {
41
40
  if (parent != null &&
@@ -106,6 +105,7 @@ class ComputeCalcExpressionFeature {
106
105
  }
107
106
  }
108
107
  }
108
+ return null;
109
109
  }
110
110
  }
111
111
 
@@ -7,6 +7,7 @@ import { splitRule } from '../minify.js';
7
7
  import '../../parser/parse.js';
8
8
  import '../../parser/tokenize.js';
9
9
  import '../../parser/utils/config.js';
10
+ import { FeatureWalkMode } from './type.js';
10
11
 
11
12
  function inlineExpression(token) {
12
13
  const result = [];
@@ -51,14 +52,12 @@ function replace(node, variableScope) {
51
52
  }
52
53
  }
53
54
  class InlineCssVariablesFeature {
55
+ accept = new Set([EnumToken.RuleNodeType, EnumToken.AtRuleNodeType]);
54
56
  get ordering() {
55
57
  return 0;
56
58
  }
57
- get preProcess() {
58
- return true;
59
- }
60
- get postProcess() {
61
- return false;
59
+ get processMode() {
60
+ return FeatureWalkMode.Pre;
62
61
  }
63
62
  static register(options) {
64
63
  if (options.inlineCssVariables) {
@@ -67,9 +66,10 @@ class InlineCssVariablesFeature {
67
66
  }
68
67
  }
69
68
  run(ast, options = {}, parent, context) {
70
- if (!('chi' in ast)) {
71
- return;
72
- }
69
+ // if (!('chi' in ast)) {
70
+ //
71
+ // return null;
72
+ // }
73
73
  if (!('variableScope' in context)) {
74
74
  context.variableScope = new Map;
75
75
  }
@@ -111,6 +111,7 @@ class InlineCssVariablesFeature {
111
111
  replace(node, variableScope);
112
112
  }
113
113
  }
114
+ return null;
114
115
  }
115
116
  cleanup(ast, options = {}, context) {
116
117
  const variableScope = context.variableScope;
@@ -1,6 +1,5 @@
1
1
  import { EnumToken, SyntaxValidationResult } from '../types.js';
2
2
  import { getSyntaxConfig } from '../../validation/config.js';
3
- import '../../validation/parser/types.js';
4
3
  import '../../validation/parser/parse.js';
5
4
  import { splitRule } from '../minify.js';
6
5
  import { walkValues } from '../walk.js';
@@ -13,6 +12,7 @@ import '../../renderer/sourcemap/lib/encode.js';
13
12
  import '../../validation/syntaxes/complex-selector.js';
14
13
  import { evaluateSyntax } from '../../validation/syntax.js';
15
14
  import { funcLike } from '../../syntax/color/utils/constants.js';
15
+ import { FeatureWalkMode } from './type.js';
16
16
 
17
17
  const config = getSyntaxConfig();
18
18
  function replacePseudo(tokens) {
@@ -59,11 +59,8 @@ class ComputePrefixFeature {
59
59
  get ordering() {
60
60
  return 2;
61
61
  }
62
- get preProcess() {
63
- return true;
64
- }
65
- get postProcess() {
66
- return false;
62
+ get processMode() {
63
+ return FeatureWalkMode.Pre;
67
64
  }
68
65
  static register(options) {
69
66
  if (options.removePrefix) {
@@ -123,12 +120,13 @@ class ComputePrefixFeature {
123
120
  }
124
121
  }
125
122
  }
123
+ // @ts-ignore
126
124
  if (SyntaxValidationResult.Valid == evaluateSyntax({ ...node, val: nodes }, {}).valid) {
127
125
  node.val = nodes;
128
126
  }
129
127
  }
130
128
  }
131
- else if (node.typ == EnumToken.AtRuleNodeType || node.typ == EnumToken.KeyframeAtRuleNodeType) {
129
+ else if (node.typ == EnumToken.AtRuleNodeType || node.typ == EnumToken.KeyframesAtRuleNodeType) {
132
130
  if (node.nam.startsWith('-')) {
133
131
  const match = node.nam.match(/^-([^-]+)-(.+)$/);
134
132
  if (match != null && '@' + match[2] in config.atRules) {
@@ -136,14 +134,6 @@ class ComputePrefixFeature {
136
134
  }
137
135
  }
138
136
  if (node.typ == EnumToken.AtRuleNodeType && node.val !== '') {
139
- // if ((node as AstAtRule).tokens == null) {
140
- //
141
- // Object.defineProperty(node, 'tokens', {
142
- // // @ts-ignore
143
- // ...definedPropertySettings,
144
- // value: parseAtRulePrelude(parseString((node as AstAtRule).val), node as AstAtRule),
145
- // })
146
- // }
147
137
  if (replaceAstNodes(node.tokens)) {
148
138
  node.val = node.tokens.reduce((acc, curr) => acc + renderToken(curr), '');
149
139
  }
@@ -7,16 +7,15 @@ import '../../parser/tokenize.js';
7
7
  import '../../parser/utils/config.js';
8
8
  import '../../syntax/color/utils/constants.js';
9
9
  import '../../renderer/sourcemap/lib/encode.js';
10
+ import { FeatureWalkMode } from './type.js';
10
11
 
11
12
  class ComputeShorthandFeature {
13
+ accept = new Set([EnumToken.RuleNodeType, EnumToken.AtRuleNodeType, EnumToken.KeyFramesRuleNodeType]);
12
14
  get ordering() {
13
15
  return 3;
14
16
  }
15
- get preProcess() {
16
- return false;
17
- }
18
- get postProcess() {
19
- return true;
17
+ get processMode() {
18
+ return FeatureWalkMode.Post;
20
19
  }
21
20
  static register(options) {
22
21
  if (options.computeShorthand) {
@@ -26,7 +25,7 @@ class ComputeShorthandFeature {
26
25
  }
27
26
  run(ast, options = {}, parent, context) {
28
27
  if (!('chi' in ast)) {
29
- return ast;
28
+ return null;
30
29
  }
31
30
  // @ts-ignore
32
31
  const j = ast.chi.length;
@@ -9,17 +9,16 @@ import '../../syntax/color/utils/constants.js';
9
9
  import { filterValues, renderToken } from '../../renderer/render.js';
10
10
  import '../../renderer/sourcemap/lib/encode.js';
11
11
  import { compute } from '../transform/compute.js';
12
- import { eqMatrix } from '../transform/minify.js';
12
+ import { minifyTransformFunctions, eqMatrix } from '../transform/minify.js';
13
+ import { FeatureWalkMode } from './type.js';
13
14
 
14
15
  class TransformCssFeature {
16
+ accept = new Set([EnumToken.RuleNodeType, EnumToken.KeyFramesRuleNodeType]);
15
17
  get ordering() {
16
18
  return 4;
17
19
  }
18
- get preProcess() {
19
- return false;
20
- }
21
- get postProcess() {
22
- return true;
20
+ get processMode() {
21
+ return FeatureWalkMode.Post;
23
22
  }
24
23
  static register(options) {
25
24
  // @ts-ignore
@@ -30,7 +29,7 @@ class TransformCssFeature {
30
29
  }
31
30
  run(ast) {
32
31
  if (!('chi' in ast)) {
33
- return;
32
+ return null;
34
33
  }
35
34
  let i = 0;
36
35
  let node;
@@ -41,28 +40,21 @@ class TransformCssFeature {
41
40
  if (node.typ != EnumToken.DeclarationNodeType || !node.nam.match(/^(-[a-z]+-)?transform$/)) {
42
41
  continue;
43
42
  }
44
- const children = node.val.reduce((acc, curr) => {
45
- if (curr.typ == EnumToken.FunctionTokenType && 'skew' == curr.val.toLowerCase()) {
46
- if (curr.chi.length == 3) {
47
- if (curr.chi[2].val == 0) {
48
- curr.chi.length = 1;
49
- curr.val = 'skew';
50
- }
51
- else if (curr.chi[0].val == 0) {
52
- curr.chi = [curr.chi[2]];
53
- curr.val = 'skewY';
54
- }
55
- }
56
- }
57
- acc.push(curr);
58
- return acc;
59
- }, []);
43
+ const children = [];
44
+ for (const child of node.val) {
45
+ children.push(child.typ == EnumToken.FunctionTokenType ? minifyTransformFunctions(child) : child);
46
+ }
60
47
  consumeWhitespace(children);
61
- let { matrix, cumulative, minified } = compute(children) ?? {};
48
+ let { matrix, cumulative, minified } = compute(children) ?? {
49
+ matrix: null,
50
+ cumulative: null,
51
+ minified: null
52
+ };
62
53
  if (matrix == null || cumulative == null || minified == null) {
54
+ node.val = children;
63
55
  continue;
64
56
  }
65
- let r = [filterValues(node.val.slice())];
57
+ let r = [filterValues(children)];
66
58
  if (eqMatrix(matrix, cumulative)) {
67
59
  r.push(cumulative);
68
60
  }
@@ -77,6 +69,7 @@ class TransformCssFeature {
77
69
  return acc;
78
70
  }, [matrix]);
79
71
  }
72
+ return null;
80
73
  }
81
74
  }
82
75
 
@@ -1,16 +1,18 @@
1
1
  /**
2
2
  * feature walk mode
3
+ *
4
+ * @internal
3
5
  */
4
6
  var FeatureWalkMode;
5
7
  (function (FeatureWalkMode) {
6
8
  /**
7
9
  * pre process
8
10
  */
9
- FeatureWalkMode[FeatureWalkMode["Pre"] = 0] = "Pre";
11
+ FeatureWalkMode[FeatureWalkMode["Pre"] = 1] = "Pre";
10
12
  /**
11
13
  * post process
12
14
  */
13
- FeatureWalkMode[FeatureWalkMode["Post"] = 1] = "Post";
15
+ FeatureWalkMode[FeatureWalkMode["Post"] = 2] = "Post";
14
16
  })(FeatureWalkMode || (FeatureWalkMode = {}));
15
17
 
16
18
  export { FeatureWalkMode };