@tbela99/css-parser 1.2.0 → 1.3.1

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 (70) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/README.md +29 -11
  3. package/dist/index-umd-web.js +2041 -1967
  4. package/dist/index.cjs +2129 -1964
  5. package/dist/index.d.ts +1936 -77
  6. package/dist/lib/ast/expand.js +4 -65
  7. package/dist/lib/ast/features/calc.js +3 -27
  8. package/dist/lib/ast/features/inlinecssvariables.js +2 -21
  9. package/dist/lib/ast/features/prefix.js +2 -1
  10. package/dist/lib/ast/features/transform.js +4 -3
  11. package/dist/lib/ast/features/type.js +9 -0
  12. package/dist/lib/ast/math/expression.js +26 -149
  13. package/dist/lib/ast/math/math.js +9 -9
  14. package/dist/lib/ast/minify.js +82 -171
  15. package/dist/lib/ast/transform/compute.js +4 -37
  16. package/dist/lib/ast/transform/matrix.js +33 -34
  17. package/dist/lib/ast/transform/minify.js +32 -51
  18. package/dist/lib/ast/transform/perspective.js +1 -1
  19. package/dist/lib/ast/transform/rotate.js +13 -13
  20. package/dist/lib/ast/transform/scale.js +8 -8
  21. package/dist/lib/ast/transform/skew.js +4 -4
  22. package/dist/lib/ast/transform/translate.js +8 -8
  23. package/dist/lib/ast/transform/utils.js +31 -39
  24. package/dist/lib/ast/types.js +459 -5
  25. package/dist/lib/ast/walk.js +18 -0
  26. package/dist/lib/fs/resolve.js +11 -3
  27. package/dist/lib/parser/declaration/map.js +1 -0
  28. package/dist/lib/parser/declaration/set.js +2 -2
  29. package/dist/lib/parser/parse.js +139 -32
  30. package/dist/lib/parser/tokenize.js +41 -93
  31. package/dist/lib/parser/utils/type.js +1 -1
  32. package/dist/lib/renderer/render.js +61 -30
  33. package/dist/lib/renderer/sourcemap/sourcemap.js +34 -0
  34. package/dist/lib/syntax/color/cmyk.js +2 -2
  35. package/dist/lib/syntax/color/color-mix.js +11 -12
  36. package/dist/lib/syntax/color/color.js +14 -7
  37. package/dist/lib/syntax/color/hsl.js +4 -4
  38. package/dist/lib/syntax/color/hwb.js +27 -8
  39. package/dist/lib/syntax/color/lab.js +4 -4
  40. package/dist/lib/syntax/color/lch.js +4 -4
  41. package/dist/lib/syntax/color/oklab.js +4 -4
  42. package/dist/lib/syntax/color/oklch.js +4 -4
  43. package/dist/lib/syntax/color/relativecolor.js +1 -1
  44. package/dist/lib/syntax/color/rgb.js +4 -4
  45. package/dist/lib/syntax/color/utils/components.js +15 -3
  46. package/dist/lib/syntax/color/utils/distance.js +15 -1
  47. package/dist/lib/syntax/syntax.js +18 -17
  48. package/dist/lib/syntax/utils.js +1 -1
  49. package/dist/lib/validation/at-rules/document.js +1 -1
  50. package/dist/lib/validation/at-rules/import.js +4 -4
  51. package/dist/lib/validation/at-rules/keyframes.js +0 -11
  52. package/dist/lib/validation/at-rules/supports.js +6 -6
  53. package/dist/lib/validation/config.js +0 -4
  54. package/dist/lib/validation/config.json.js +33 -30
  55. package/dist/lib/validation/parser/parse.js +0 -8
  56. package/dist/lib/validation/selector.js +0 -9
  57. package/dist/lib/validation/syntax.js +17 -135
  58. package/dist/lib/validation/syntaxes/complex-selector-list.js +0 -11
  59. package/dist/lib/validation/syntaxes/family-name.js +0 -32
  60. package/dist/lib/validation/syntaxes/keyframe-selector.js +0 -11
  61. package/dist/lib/validation/syntaxes/relative-selector-list.js +0 -26
  62. package/dist/lib/validation/syntaxes/url.js +0 -33
  63. package/dist/lib/validation/utils/list.js +0 -8
  64. package/dist/node.js +229 -0
  65. package/dist/web.js +158 -0
  66. package/package.json +14 -11
  67. package/dist/node/index.js +0 -57
  68. package/dist/node/load.js +0 -20
  69. package/dist/web/index.js +0 -66
  70. package/dist/web/load.js +0 -31
@@ -5,30 +5,16 @@ import '../parser/utils/config.js';
5
5
  import { EnumToken } from './types.js';
6
6
  import { walkValues } from './walk.js';
7
7
  import { renderToken } from '../renderer/render.js';
8
+ import '../renderer/sourcemap/lib/encode.js';
8
9
  import '../syntax/color/utils/constants.js';
9
10
 
10
11
  /**
11
- * expand nested css ast
12
+ * expand css nesting ast nodes
12
13
  * @param ast
14
+ *
15
+ * @private
13
16
  */
14
17
  function expand(ast) {
15
- //
16
- // if (![EnumToken.RuleNodeType, EnumToken.StyleSheetNodeType, EnumToken.AtRuleNodeType].includes(ast.typ)) {
17
- //
18
- // return ast;
19
- // }
20
- // if (EnumToken.RuleNodeType == ast.typ) {
21
- //
22
- // return <AstRuleStyleSheet>{
23
- // typ: EnumToken.StyleSheetNodeType,
24
- // chi: expandRule(<AstRule>ast)
25
- // }
26
- // }
27
- //
28
- // if (!('chi' in ast)) {
29
- //
30
- // return ast;
31
- // }
32
18
  const result = { ...ast, chi: [] };
33
19
  // @ts-ignore
34
20
  for (let i = 0; i < ast.chi.length; i++) {
@@ -52,11 +38,6 @@ function expand(ast) {
52
38
  // @ts-ignore
53
39
  result.chi.push({ ...(hasRule ? expand(node) : node) });
54
40
  }
55
- // else {
56
- //
57
- // // @ts-ignore
58
- // result.chi.push(node);
59
- // }
60
41
  }
61
42
  return result;
62
43
  }
@@ -70,14 +51,6 @@ function expandRule(node) {
70
51
  const rule = ast.chi[i];
71
52
  if (!rule.sel.includes('&')) {
72
53
  const selRule = splitRule(rule.sel);
73
- // if (selRule.length > 1) {
74
- //
75
- // const r: string = ':is(' + selRule.map(a => a.join('')).join(',') + ')';
76
- // rule.sel = splitRule(ast.sel).reduce((a: string[], b: string[]): string[] => a.concat([b.join('') + r]), <string[]>[]).join(',');
77
- //
78
- // }
79
- // else {
80
- // selRule = splitRule(selRule.reduce((acc, curr) => acc + (acc.length > 0 ? ',' : '') + curr.join(''), ''));
81
54
  const arSelf = splitRule(ast.sel).filter((r) => r.every((t) => t != ':before' && t != ':after' && !t.startsWith('::'))).reduce((acc, curr) => acc.concat([curr.join('')]), []).join(',');
82
55
  if (arSelf.length == 0) {
83
56
  ast.chi.splice(i--, 1);
@@ -115,10 +88,6 @@ function expandRule(node) {
115
88
  if (s == '&' || parentSelector) {
116
89
  withCompound.push(s);
117
90
  }
118
- // else {
119
- //
120
- // withoutCompound.push(s.slice(1));
121
- // }
122
91
  }
123
92
  }
124
93
  else {
@@ -129,10 +98,6 @@ function expandRule(node) {
129
98
  withCompound.push(s);
130
99
  }
131
100
  }
132
- // else {
133
- //
134
- // withoutCompound.push(s);
135
- // }
136
101
  }
137
102
  const selectors = [];
138
103
  const selector = rules.length > 1 ? ':is(' + rules.map(a => a.join('')).join(',') + ')' : rules[0].join('');
@@ -164,13 +129,6 @@ function expandRule(node) {
164
129
  if (withCompound.length == 1) {
165
130
  selectors.push(replaceCompound(withCompound[0], selector));
166
131
  }
167
- // else {
168
- //
169
- // for (const w of withCompound) {
170
- //
171
- // selectors.push(replaceCompound(w, selector));
172
- // }
173
- // }
174
132
  }
175
133
  rule.sel = selectors.reduce((acc, curr) => curr.length == 0 ? acc : acc + (acc.length > 0 ? ',' : '') + curr, '');
176
134
  }
@@ -211,11 +169,6 @@ function expandRule(node) {
211
169
  // @ts-ignore
212
170
  astAtRule.chi.push(...expandRule(r));
213
171
  }
214
- // else {
215
- //
216
- // // @ts-ignore
217
- // astAtRule.chi.push(r);
218
- // }
219
172
  }
220
173
  }
221
174
  // @ts-ignore
@@ -242,12 +195,6 @@ function replaceCompound(input, replace) {
242
195
  if (replacement == null) {
243
196
  replacement = parseString(replace);
244
197
  }
245
- // if (tokens[1].typ == EnumToken.IdenTokenType) {
246
- //
247
- //
248
- // (t.value as LiteralToken).val = (replacement as Token[]).length == 1 || (!replace.includes(' ') && replace.charAt(0).match(/[:.]/)) ? (tokens[1] as IdentToken).val + replace : replaceCompoundLiteral((tokens[1] as IdentToken).val + '&', replace);
249
- // tokens.splice(1, 1);
250
- // } else {
251
198
  t.value.val = replaceCompoundLiteral(t.value.val, replace);
252
199
  // }
253
200
  continue;
@@ -255,10 +202,6 @@ function replaceCompound(input, replace) {
255
202
  const rule = splitRule(replace);
256
203
  t.value.val = rule.length > 1 ? ':is(' + replace + ')' : replace;
257
204
  }
258
- // else if ((t.value as LiteralToken).val.length > 1 && (t.value as LiteralToken).val.charAt(0) == '&') {
259
- //
260
- // (t.value as LiteralToken).val = replaceCompoundLiteral((t.value as LiteralToken).val, replace);
261
- // }
262
205
  }
263
206
  }
264
207
  return tokens.reduce((acc, curr) => acc + renderToken(curr), '');
@@ -271,10 +214,6 @@ function replaceCompoundLiteral(selector, replace) {
271
214
  tokens.push('&');
272
215
  tokens.push('');
273
216
  }
274
- // else {
275
- //
276
- // tokens[tokens.length - 1] += selector.charAt(i);
277
- // }
278
217
  }
279
218
  return tokens.sort((a, b) => {
280
219
  if (a == '&') {
@@ -2,12 +2,12 @@ import { EnumToken } from '../types.js';
2
2
  import { walkValues, WalkerValueEvent, WalkerOptionEnum } from '../walk.js';
3
3
  import { evaluate } from '../math/expression.js';
4
4
  import { renderToken } from '../../renderer/render.js';
5
- import '../../syntax/color/utils/constants.js';
5
+ import '../../renderer/sourcemap/lib/encode.js';
6
+ import { mathFuncs } from '../../syntax/syntax.js';
6
7
  import '../minify.js';
7
8
  import '../../parser/parse.js';
8
9
  import '../../parser/tokenize.js';
9
10
  import '../../parser/utils/config.js';
10
- import { mathFuncs } from '../../syntax/syntax.js';
11
11
 
12
12
  class ComputeCalcExpressionFeature {
13
13
  get ordering() {
@@ -56,8 +56,6 @@ class ComputeCalcExpressionFeature {
56
56
  const slice = (node.typ == EnumToken.FunctionTokenType ? node.chi : (node.typ == EnumToken.DeclarationNodeType ? node.val : node.chi))?.slice();
57
57
  if (slice != null && node.typ == EnumToken.FunctionTokenType && mathFuncs.includes(node.val)) {
58
58
  // @ts-ignore
59
- const cp = (node.typ == EnumToken.FunctionTokenType && mathFuncs.includes(node.val) && node.val != 'calc' ? [node] : (node.typ == EnumToken.DeclarationNodeType ? node.val : node.chi)).slice();
60
- evaluate(cp);
61
59
  const key = 'chi' in node ? 'chi' : 'val';
62
60
  const str1 = renderToken({ ...node, [key]: slice });
63
61
  const str2 = renderToken(node); // values.reduce((acc: string, curr: Token): string => acc + renderToken(curr), '');
@@ -65,11 +63,6 @@ class ComputeCalcExpressionFeature {
65
63
  // @ts-ignore
66
64
  node[key] = slice;
67
65
  }
68
- // else {
69
- //
70
- // // @ts-ignore
71
- // node[key] = values;
72
- // }
73
66
  return WalkerOptionEnum.Ignore;
74
67
  }
75
68
  return null;
@@ -85,20 +78,9 @@ class ComputeCalcExpressionFeature {
85
78
  // @ts-ignore
86
79
  const children = parent.typ == EnumToken.DeclarationNodeType ? parent.val : parent.chi;
87
80
  if (values.length == 1 && values[0].typ != EnumToken.BinaryExpressionTokenType) {
88
- // if (parent.typ == EnumToken.BinaryExpressionTokenType) {
89
- //
90
- // if ((parent as BinaryExpressionToken).l == value) {
91
- //
92
- // (parent as BinaryExpressionToken).l = values[0];
93
- // } else {
94
- //
95
- // (parent as BinaryExpressionToken).r = values[0];
96
- // }
97
- // } else {
98
81
  for (let i = 0; i < children.length; i++) {
99
82
  if (children[i] == value) {
100
- // @ts-ignore
101
- children.splice(i, 1, !(parent.typ == EnumToken.FunctionTokenType && parent.val == 'calc') && typeof values[0].val != 'string' ? {
83
+ children.splice(i, 1, !(parent.typ == EnumToken.FunctionTokenType && parent.val == 'calc') && (typeof values[0].val != 'number' && !(values[0].typ == EnumToken.FunctionTokenType && mathFuncs.includes(values[0].val))) ? {
102
84
  typ: EnumToken.FunctionTokenType,
103
85
  val: 'calc',
104
86
  chi: values
@@ -106,21 +88,15 @@ class ComputeCalcExpressionFeature {
106
88
  break;
107
89
  }
108
90
  }
109
- // }
110
91
  }
111
92
  else {
112
93
  for (let i = 0; i < children.length; i++) {
113
94
  if (children[i] == value) {
114
- // if (parent.typ == EnumToken.FunctionTokenType && (parent as FunctionToken).val == 'calc') {
115
- //
116
- // children.splice(i, 1, ...values);
117
- // } else {
118
95
  children.splice(i, 1, {
119
96
  typ: EnumToken.FunctionTokenType,
120
97
  val: 'calc',
121
98
  chi: values
122
99
  });
123
- // }
124
100
  break;
125
101
  }
126
102
  }
@@ -1,12 +1,12 @@
1
1
  import { EnumToken } from '../types.js';
2
2
  import { walkValues } from '../walk.js';
3
3
  import { renderToken } from '../../renderer/render.js';
4
- import '../../syntax/color/utils/constants.js';
4
+ import '../../renderer/sourcemap/lib/encode.js';
5
+ import { mathFuncs } from '../../syntax/syntax.js';
5
6
  import { splitRule } from '../minify.js';
6
7
  import '../../parser/parse.js';
7
8
  import '../../parser/tokenize.js';
8
9
  import '../../parser/utils/config.js';
9
- import { mathFuncs } from '../../syntax/syntax.js';
10
10
 
11
11
  function inlineExpression(token) {
12
12
  const result = [];
@@ -106,25 +106,6 @@ class InlineCssVariablesFeature {
106
106
  replace(node, variableScope);
107
107
  }
108
108
  }
109
- // else {
110
- //
111
- // const info: VariableScopeInfo = <VariableScopeInfo>variableScope.get((<AstDeclaration>node).nam);
112
- //
113
- // info.globalScope = isRoot;
114
- //
115
- // if (!isRoot) {
116
- //
117
- // ++info.declarationCount;
118
- // }
119
- //
120
- // if (info.replaceable) {
121
- //
122
- // info.replaceable = isRoot && info.declarationCount == 1;
123
- // }
124
- //
125
- // info.parent.add(ast);
126
- // info.node = (<AstDeclaration>node);
127
- // }
128
109
  }
129
110
  else {
130
111
  replace(node, variableScope);
@@ -9,9 +9,10 @@ import '../../parser/tokenize.js';
9
9
  import '../../parser/utils/config.js';
10
10
  import { pseudoAliasMap } from '../../syntax/syntax.js';
11
11
  import { renderToken } from '../../renderer/render.js';
12
- import { funcLike } from '../../syntax/color/utils/constants.js';
12
+ import '../../renderer/sourcemap/lib/encode.js';
13
13
  import '../../validation/syntaxes/complex-selector.js';
14
14
  import { evaluateSyntax } from '../../validation/syntax.js';
15
+ import { funcLike } from '../../syntax/color/utils/constants.js';
15
16
 
16
17
  const config = getSyntaxConfig();
17
18
  function replacePseudo(tokens) {
@@ -5,8 +5,9 @@ import '../walk.js';
5
5
  import '../../parser/parse.js';
6
6
  import '../../parser/tokenize.js';
7
7
  import '../../parser/utils/config.js';
8
- import { filterValues, renderToken } from '../../renderer/render.js';
9
8
  import '../../syntax/color/utils/constants.js';
9
+ import { filterValues, renderToken } from '../../renderer/render.js';
10
+ import '../../renderer/sourcemap/lib/encode.js';
10
11
  import { compute } from '../transform/compute.js';
11
12
  import { eqMatrix } from '../transform/minify.js';
12
13
 
@@ -43,11 +44,11 @@ class TransformCssFeature {
43
44
  const children = node.val.reduce((acc, curr) => {
44
45
  if (curr.typ == EnumToken.FunctionTokenType && 'skew' == curr.val.toLowerCase()) {
45
46
  if (curr.chi.length == 3) {
46
- if (curr.chi[2].val == '0') {
47
+ if (curr.chi[2].val == 0) {
47
48
  curr.chi.length = 1;
48
49
  curr.val = 'skew';
49
50
  }
50
- else if (curr.chi[0].val == '0') {
51
+ else if (curr.chi[0].val == 0) {
51
52
  curr.chi = [curr.chi[2]];
52
53
  curr.val = 'skewY';
53
54
  }
@@ -1,6 +1,15 @@
1
+ /**
2
+ * feature walk mode
3
+ */
1
4
  var FeatureWalkMode;
2
5
  (function (FeatureWalkMode) {
6
+ /**
7
+ * pre process
8
+ */
3
9
  FeatureWalkMode[FeatureWalkMode["Pre"] = 0] = "Pre";
10
+ /**
11
+ * post process
12
+ */
4
13
  FeatureWalkMode[FeatureWalkMode["Post"] = 1] = "Post";
5
14
  })(FeatureWalkMode || (FeatureWalkMode = {}));
6
15