@tbela99/css-parser 1.6.6 → 1.6.7

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.
package/dist/index.d.ts CHANGED
@@ -16,35 +16,43 @@ declare enum EnumAstNodeStatus$1 {
16
16
  /**
17
17
  * Node passed validation
18
18
  */
19
- Validated = 0,
19
+ Validated = 1,
20
20
  /**
21
21
  * Node is invalid
22
22
  */
23
- Invalid = 1,
23
+ Invalid = 2,
24
24
  /**
25
25
  * node is not validated
26
26
  */
27
- Unvalidated = 2,
27
+ Unvalidated = 4,
28
28
  /**
29
29
  * Node did not pass validation, but is preserved.
30
30
  */
31
- ValidationFailed = 3,
31
+ ValidationFailed = 8,
32
32
  /**
33
33
  * Parsing the node is not supported yet or the node syntax definition does not exist.
34
34
  */
35
- Unknown = 4,
35
+ Unknown = 16,
36
36
  /**
37
37
  * Failed to parse the node.
38
38
  */
39
- Unparsed = 5,
39
+ Unparsed = 32,
40
40
  /**
41
41
  * Node is disallowed in the context
42
42
  */
43
- Disallowed = 6,
43
+ Disallowed = 64,
44
44
  /**
45
45
  * Node is malformed
46
46
  */
47
- Malformed = 7
47
+ Malformed = 128,
48
+ /**
49
+ * Important declaration
50
+ */
51
+ ImportantDeclaration = 256,
52
+ /**
53
+ * Color component without unit
54
+ */
55
+ PowerlessColorComponent = 512
48
56
  }
49
57
  /**
50
58
  * Enum of validation levels
@@ -13,11 +13,12 @@ import { cloneNode } from './clone.js';
13
13
  * @private
14
14
  */
15
15
  function expand(ast) {
16
- if (ast[STATE] == EnumAstNodeStatus.Invalid ||
17
- ast[STATE] == EnumAstNodeStatus.Disallowed ||
18
- ast[STATE] == EnumAstNodeStatus.Unknown ||
19
- ast[STATE] == EnumAstNodeStatus.Unparsed ||
20
- ast[STATE] == EnumAstNodeStatus.Malformed) {
16
+ if (ast[STATE] &
17
+ (EnumAstNodeStatus.Invalid |
18
+ EnumAstNodeStatus.Disallowed |
19
+ EnumAstNodeStatus.Unknown |
20
+ EnumAstNodeStatus.Unparsed |
21
+ EnumAstNodeStatus.Malformed)) {
21
22
  return ast;
22
23
  }
23
24
  const result = Object.assign(cloneNode(ast), { chi: [] });
@@ -65,11 +66,12 @@ function expand(ast) {
65
66
  return result;
66
67
  }
67
68
  function expandRule(node) {
68
- if (node[STATE] == EnumAstNodeStatus.Invalid ||
69
- node[STATE] == EnumAstNodeStatus.Disallowed ||
70
- node[STATE] == EnumAstNodeStatus.Unknown ||
71
- node[STATE] == EnumAstNodeStatus.Unparsed ||
72
- node[STATE] == EnumAstNodeStatus.Malformed) {
69
+ if (node[STATE] &
70
+ (EnumAstNodeStatus.Invalid |
71
+ EnumAstNodeStatus.Disallowed |
72
+ EnumAstNodeStatus.Unknown |
73
+ EnumAstNodeStatus.Unparsed |
74
+ EnumAstNodeStatus.Malformed)) {
73
75
  return [node];
74
76
  }
75
77
  const ast = Object.assign(cloneNode(node), { chi: node.chi.slice() });
@@ -121,9 +121,7 @@ class ComputePrefixFeature {
121
121
  }
122
122
  }
123
123
  run(node) {
124
- if (node[STATE] == EnumAstNodeStatus.Disallowed ||
125
- node[STATE] == EnumAstNodeStatus.Invalid ||
126
- node[STATE] == EnumAstNodeStatus.Malformed) {
124
+ if (node[STATE] & (EnumAstNodeStatus.Disallowed | EnumAstNodeStatus.Invalid | EnumAstNodeStatus.Malformed)) {
127
125
  return null;
128
126
  }
129
127
  if (node.typ == EnumToken.RuleNodeType) {
@@ -34,7 +34,7 @@ class TransformCssFeature {
34
34
  for (; i < ast.chi.length; i++) {
35
35
  // @ts-ignore
36
36
  node = ast.chi[i];
37
- if (node[STATE] == EnumAstNodeStatus.Invalid || node[STATE] == EnumAstNodeStatus.ValidationFailed) {
37
+ if (node[STATE] & (EnumAstNodeStatus.Invalid | EnumAstNodeStatus.ValidationFailed)) {
38
38
  continue;
39
39
  }
40
40
  if (node.typ != EnumToken.DeclarationNodeType ||
@@ -18,35 +18,43 @@ var EnumAstNodeStatus;
18
18
  /**
19
19
  * Node passed validation
20
20
  */
21
- EnumAstNodeStatus[EnumAstNodeStatus["Validated"] = 0] = "Validated";
21
+ EnumAstNodeStatus[EnumAstNodeStatus["Validated"] = 1] = "Validated";
22
22
  /**
23
23
  * Node is invalid
24
24
  */
25
- EnumAstNodeStatus[EnumAstNodeStatus["Invalid"] = 1] = "Invalid";
25
+ EnumAstNodeStatus[EnumAstNodeStatus["Invalid"] = 2] = "Invalid";
26
26
  /**
27
27
  * node is not validated
28
28
  */
29
- EnumAstNodeStatus[EnumAstNodeStatus["Unvalidated"] = 2] = "Unvalidated";
29
+ EnumAstNodeStatus[EnumAstNodeStatus["Unvalidated"] = 4] = "Unvalidated";
30
30
  /**
31
31
  * Node did not pass validation, but is preserved.
32
32
  */
33
- EnumAstNodeStatus[EnumAstNodeStatus["ValidationFailed"] = 3] = "ValidationFailed";
33
+ EnumAstNodeStatus[EnumAstNodeStatus["ValidationFailed"] = 8] = "ValidationFailed";
34
34
  /**
35
35
  * Parsing the node is not supported yet or the node syntax definition does not exist.
36
36
  */
37
- EnumAstNodeStatus[EnumAstNodeStatus["Unknown"] = 4] = "Unknown";
37
+ EnumAstNodeStatus[EnumAstNodeStatus["Unknown"] = 16] = "Unknown";
38
38
  /**
39
39
  * Failed to parse the node.
40
40
  */
41
- EnumAstNodeStatus[EnumAstNodeStatus["Unparsed"] = 5] = "Unparsed";
41
+ EnumAstNodeStatus[EnumAstNodeStatus["Unparsed"] = 32] = "Unparsed";
42
42
  /**
43
43
  * Node is disallowed in the context
44
44
  */
45
- EnumAstNodeStatus[EnumAstNodeStatus["Disallowed"] = 6] = "Disallowed";
45
+ EnumAstNodeStatus[EnumAstNodeStatus["Disallowed"] = 64] = "Disallowed";
46
46
  /**
47
47
  * Node is malformed
48
48
  */
49
- EnumAstNodeStatus[EnumAstNodeStatus["Malformed"] = 7] = "Malformed";
49
+ EnumAstNodeStatus[EnumAstNodeStatus["Malformed"] = 128] = "Malformed";
50
+ /**
51
+ * Important declaration
52
+ */
53
+ EnumAstNodeStatus[EnumAstNodeStatus["ImportantDeclaration"] = 256] = "ImportantDeclaration";
54
+ /**
55
+ * Color component without unit
56
+ */
57
+ EnumAstNodeStatus[EnumAstNodeStatus["PowerlessColorComponent"] = 512] = "PowerlessColorComponent";
50
58
  })(EnumAstNodeStatus || (EnumAstNodeStatus = {}));
51
59
  /**
52
60
  * Enum of validation levels
@@ -36,6 +36,7 @@ var WalkerEvent;
36
36
  */
37
37
  WalkerEvent[WalkerEvent["Leave"] = 2] = "Leave";
38
38
  })(WalkerEvent || (WalkerEvent = {}));
39
+ const maxQueueLength = 25000;
39
40
  /**
40
41
  * Walk ast nodes
41
42
  * @param node initial node
@@ -139,6 +140,10 @@ function* walk(node, filter, reverse) {
139
140
  },
140
141
  };
141
142
  while ((node = parents[i++])) {
143
+ if (i > maxQueueLength) {
144
+ parents.splice(0, maxQueueLength);
145
+ i -= maxQueueLength;
146
+ }
142
147
  let option = null;
143
148
  if (filter != null) {
144
149
  // @ts-expect-error
@@ -282,6 +287,10 @@ function* walkValues(values, root = null, filter, reverse) {
282
287
  while (++i < stack.length) {
283
288
  value = stack[i];
284
289
  option = null;
290
+ if (i > maxQueueLength) {
291
+ stack.splice(0, maxQueueLength);
292
+ i -= maxQueueLength;
293
+ }
285
294
  if (filter.fn != null && eventType & WalkerEvent.Enter) {
286
295
  const isValid = filter.type == null ||
287
296
  value.typ == filter.type ||
@@ -26,9 +26,8 @@ class PropertyList {
26
26
  let result;
27
27
  for (const declaration of declarations) {
28
28
  name = declaration.typ != EnumToken.DeclarationNodeType ? null : declaration.nam;
29
- if (declaration[STATE] == EnumAstNodeStatus.Invalid ||
30
- declaration[STATE] == EnumAstNodeStatus.Unknown ||
31
- declaration[STATE] == EnumAstNodeStatus.ValidationFailed ||
29
+ if (declaration[STATE] &
30
+ (EnumAstNodeStatus.Invalid | EnumAstNodeStatus.Unknown | EnumAstNodeStatus.ValidationFailed) ||
32
31
  declaration.typ != EnumToken.DeclarationNodeType ||
33
32
  equalsIgnoreCase("composes", name) ||
34
33
  (typeof this.options.removeDuplicateDeclarations === "string" &&
@@ -47,17 +46,17 @@ class PropertyList {
47
46
  this.declarations.set(declaration.nam, declaration);
48
47
  continue;
49
48
  }
50
- if (declaration[STATE] == EnumAstNodeStatus.Unvalidated) {
49
+ if (declaration[STATE] & EnumAstNodeStatus.Unvalidated) {
51
50
  syntaxRules = getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, declaration.nam.toLowerCase());
52
51
  if (syntaxRules != null) {
53
52
  result = matchAllSyntaxes(syntaxRules, createValidationContext(declaration.val), this.options);
54
- declaration[STATE] = result.success
55
- ? EnumAstNodeStatus.Validated
56
- : EnumAstNodeStatus.ValidationFailed;
53
+ declaration[STATE] |=
54
+ (result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.ValidationFailed) &
55
+ ~EnumAstNodeStatus.Unvalidated;
57
56
  }
58
57
  }
59
58
  // do not compute shorthand for invalid declarations
60
- if (declaration[STATE] !== EnumAstNodeStatus.Validated) {
59
+ if (!(declaration[STATE] & EnumAstNodeStatus.Validated)) {
61
60
  this.declarations.set(objectHash(declaration), declaration);
62
61
  continue;
63
62
  }
@@ -87,6 +86,16 @@ class PropertyList {
87
86
  // @ts-ignore
88
87
  shorthand = config.property[propertyName];
89
88
  }
89
+ if (this.declarations.has(shorthand)) {
90
+ let existing = this.declarations.get(shorthand);
91
+ if (existing instanceof PropertySet || existing instanceof PropertyMap) {
92
+ existing = existing.declarations.get(declaration.nam);
93
+ }
94
+ if (existing?.[STATE] & EnumAstNodeStatus.ImportantDeclaration &&
95
+ declaration[STATE] & ~EnumAstNodeStatus.ImportantDeclaration) {
96
+ continue;
97
+ }
98
+ }
90
99
  // @ts-ignore
91
100
  if (shortHandType == "map") {
92
101
  let owner = this;
@@ -42,7 +42,8 @@ class PropertyMap {
42
42
  const tokens = {};
43
43
  const values = [];
44
44
  const val = [[]];
45
- for (const curr of this.declarations.get(this.config.shorthand).val) { // @ts-ignore
45
+ for (const curr of this.declarations.get(this.config.shorthand).val) {
46
+ // @ts-ignore
46
47
  // @ts-ignore
47
48
  if (separator != null && separator.typ == curr.typ && separator.val == curr.val) {
48
49
  val.push([]);
@@ -836,14 +836,14 @@ function doParseSync(tokenizer, options = {}) {
836
836
  if (invalidNodes.length > 0) {
837
837
  let count = invalidNodes.length;
838
838
  for (const { node, parent } of walk(ast)) {
839
- if (options.lenient && node[STATE] == EnumAstNodeStatus.Unknown) {
839
+ if (options.lenient && node[STATE] & EnumAstNodeStatus.Unknown) {
840
840
  continue;
841
841
  }
842
- if (node[STATE] === EnumAstNodeStatus.Invalid ||
843
- node[STATE] === EnumAstNodeStatus.Unknown ||
844
- node[STATE] === EnumAstNodeStatus.Unparsed ||
845
- node[STATE] === EnumAstNodeStatus.Malformed ||
846
- node[STATE] === EnumAstNodeStatus.Disallowed) {
842
+ if (node[STATE] & EnumAstNodeStatus.Invalid ||
843
+ node[STATE] & EnumAstNodeStatus.Unknown ||
844
+ node[STATE] & EnumAstNodeStatus.Unparsed ||
845
+ node[STATE] & EnumAstNodeStatus.Malformed ||
846
+ node[STATE] & EnumAstNodeStatus.Disallowed) {
847
847
  // @ts-ignore
848
848
  parent.chi.splice(parent.chi.indexOf(node), 1);
849
849
  node[PARENT] = null;
@@ -1831,14 +1831,14 @@ async function doParse(iter, options = {}) {
1831
1831
  if (invalidNodes.length > 0) {
1832
1832
  let count = invalidNodes.length;
1833
1833
  for (const { node, parent } of walk(ast)) {
1834
- if (options.lenient && node[STATE] == EnumAstNodeStatus.Unknown) {
1834
+ if (options.lenient && node[STATE] & EnumAstNodeStatus.Unknown) {
1835
1835
  continue;
1836
1836
  }
1837
- if (node[STATE] === EnumAstNodeStatus.Invalid ||
1838
- node[STATE] === EnumAstNodeStatus.Unknown ||
1839
- node[STATE] === EnumAstNodeStatus.Unparsed ||
1840
- node[STATE] === EnumAstNodeStatus.Malformed ||
1841
- node[STATE] === EnumAstNodeStatus.Disallowed) {
1837
+ if (node[STATE] & EnumAstNodeStatus.Invalid ||
1838
+ node[STATE] & EnumAstNodeStatus.Unknown ||
1839
+ node[STATE] & EnumAstNodeStatus.Unparsed ||
1840
+ node[STATE] & EnumAstNodeStatus.Malformed ||
1841
+ node[STATE] & EnumAstNodeStatus.Disallowed) {
1842
1842
  // @ts-ignore
1843
1843
  parent.chi.splice(parent.chi.indexOf(node), 1);
1844
1844
  node[PARENT] = null;
@@ -2569,11 +2569,12 @@ function parseNode(tokens, context, options, errors, stats, invalidNodes) {
2569
2569
  if (node == null) {
2570
2570
  return null;
2571
2571
  }
2572
- if (node[STATE] == EnumAstNodeStatus.Invalid ||
2573
- node[STATE] == EnumAstNodeStatus.Disallowed ||
2574
- node[STATE] == EnumAstNodeStatus.Unknown ||
2575
- node[STATE] == EnumAstNodeStatus.Unparsed ||
2576
- node[STATE] == EnumAstNodeStatus.Malformed) {
2572
+ if (node[STATE] &
2573
+ (EnumAstNodeStatus.Invalid |
2574
+ EnumAstNodeStatus.Disallowed |
2575
+ EnumAstNodeStatus.Unknown |
2576
+ EnumAstNodeStatus.Unparsed |
2577
+ EnumAstNodeStatus.Malformed)) {
2577
2578
  invalidNodes.push(node);
2578
2579
  }
2579
2580
  stats.nodesCount++;
@@ -2591,11 +2592,11 @@ function parseNode(tokens, context, options, errors, stats, invalidNodes) {
2591
2592
  context.chi.push(node);
2592
2593
  node[PARENT] = context;
2593
2594
  node[ROOT] = context[ROOT];
2594
- if (node[STATE] == EnumAstNodeStatus.Invalid ||
2595
- node[STATE] == EnumAstNodeStatus.Disallowed ||
2596
- node[STATE] == EnumAstNodeStatus.Unknown ||
2597
- node[STATE] == EnumAstNodeStatus.Unparsed ||
2598
- node[STATE] == EnumAstNodeStatus.Malformed) {
2595
+ if (node[STATE] & EnumAstNodeStatus.Invalid ||
2596
+ node[STATE] & EnumAstNodeStatus.Disallowed ||
2597
+ node[STATE] & EnumAstNodeStatus.Unknown ||
2598
+ node[STATE] & EnumAstNodeStatus.Unparsed ||
2599
+ node[STATE] & EnumAstNodeStatus.Malformed) {
2599
2600
  invalidNodes.push(node);
2600
2601
  }
2601
2602
  return node;
@@ -2605,7 +2606,9 @@ function parseNode(tokens, context, options, errors, stats, invalidNodes) {
2605
2606
  node[PARENT] = context;
2606
2607
  node[ROOT] = context[ROOT];
2607
2608
  if (context.typ === EnumToken.StyleSheetNodeType && node.typ === EnumToken.DeclarationNodeType) {
2608
- node[STATE] = EnumAstNodeStatus.Invalid;
2609
+ node[STATE] |=
2610
+ EnumAstNodeStatus.Invalid &
2611
+ ~(EnumAstNodeStatus.Validated | EnumAstNodeStatus.Unvalidated | EnumAstNodeStatus.Unparsed);
2609
2612
  errors.push({
2610
2613
  message: "<declaration> not allowed in <stylesheet>",
2611
2614
  action: "drop",
@@ -2616,11 +2619,12 @@ function parseNode(tokens, context, options, errors, stats, invalidNodes) {
2616
2619
  else if (options.lenient || node.typ === EnumToken.DeclarationNodeType) {
2617
2620
  context.chi.push(node);
2618
2621
  }
2619
- if (node[STATE] == EnumAstNodeStatus.Invalid ||
2620
- node[STATE] == EnumAstNodeStatus.Disallowed ||
2621
- node[STATE] == EnumAstNodeStatus.Unknown ||
2622
- node[STATE] == EnumAstNodeStatus.Unparsed ||
2623
- node[STATE] == EnumAstNodeStatus.Malformed) {
2622
+ if (node[STATE] &
2623
+ (EnumAstNodeStatus.Invalid |
2624
+ EnumAstNodeStatus.Disallowed |
2625
+ EnumAstNodeStatus.Unknown |
2626
+ EnumAstNodeStatus.Unparsed |
2627
+ EnumAstNodeStatus.Malformed)) {
2624
2628
  invalidNodes.push(node);
2625
2629
  }
2626
2630
  }
@@ -55,6 +55,7 @@ function parseDeclaration(tokens, parent, options, errors) {
55
55
  let rules = null;
56
56
  let syntaxRules = null;
57
57
  let success = true;
58
+ let hasImportantFlag = false;
58
59
  let validate = typeof options.validation === "boolean" ? options.validation : !!options.validation;
59
60
  if (renamedStandardProperties.has(name.val)) {
60
61
  name.val = renamedStandardProperties.get(name.val);
@@ -356,6 +357,9 @@ function parseDeclaration(tokens, parent, options, errors) {
356
357
  }
357
358
  }
358
359
  break;
360
+ case EnumToken.ImportantTokenType:
361
+ hasImportantFlag = true;
362
+ break;
359
363
  }
360
364
  if (trimTokenSpace.has(token.typ)) {
361
365
  if (tokens[i + 1]?.typ === EnumToken.WhitespaceTokenType) {
@@ -422,6 +426,9 @@ function parseDeclaration(tokens, parent, options, errors) {
422
426
  }
423
427
  name[STATE] = EnumAstNodeStatus.Unknown;
424
428
  name[ERRORS] = result?.errors ?? [];
429
+ if (hasImportantFlag) {
430
+ name[STATE] |= EnumAstNodeStatus.ImportantDeclaration;
431
+ }
425
432
  // @ts-expect-error
426
433
  return Object.assign(name, {
427
434
  typ: EnumToken.DeclarationNodeType,
@@ -459,6 +466,9 @@ function parseDeclaration(tokens, parent, options, errors) {
459
466
  : EnumAstNodeStatus.Validated
460
467
  : EnumAstNodeStatus.ValidationFailed;
461
468
  name[ERRORS] = result?.errors ?? [];
469
+ if (hasImportantFlag) {
470
+ name[STATE] |= EnumAstNodeStatus.ImportantDeclaration;
471
+ }
462
472
  // @ts-expect-error
463
473
  return Object.assign(name, {
464
474
  typ: EnumToken.DeclarationNodeType,
@@ -1284,17 +1284,20 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
1284
1284
  ? ""
1285
1285
  : token.l.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), "") +
1286
1286
  ":" +
1287
+ (options.indent ?? "") +
1287
1288
  token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), "");
1288
1289
  case EnumToken.IfElseConditionTokenType:
1289
- return renderValue(token.l) + renderValue(token.r);
1290
+ return (renderValue(token.l, options, cache, reducer) +
1291
+ (options.indent ?? "") +
1292
+ renderValue(token.r, options, cache, reducer));
1290
1293
  case EnumToken.DeclarationNodeType:
1291
1294
  return (token.nam +
1292
1295
  ":" +
1293
- (options.minifyValues ? filterValues(token.val) : token.val).reduce((acc, curr) => acc + renderValue(curr, options, cache), ""));
1296
+ (options.minifyValues ? filterValues(token.val) : token.val).reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), ""));
1294
1297
  case EnumToken.MediaQueryUnaryFeatureTokenType:
1295
1298
  return (renderValue(token.l, options, cache, reducer) +
1296
1299
  " " +
1297
- token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache), ""));
1300
+ token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), ""));
1298
1301
  case EnumToken.MediaQueryConditionTokenType: {
1299
1302
  const indent = token.op.typ == EnumToken.LtTokenType ||
1300
1303
  token.op.typ == EnumToken.GtTokenType ||
@@ -1308,14 +1311,14 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
1308
1311
  indent +
1309
1312
  renderValue(token.op, options, cache, reducer) +
1310
1313
  indent +
1311
- token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache), ""));
1314
+ token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), ""));
1312
1315
  }
1313
1316
  case EnumToken.MediaRangeQueryTokenType:
1314
- return (token.l.reduce((acc, curr) => acc + renderValue(curr, options, cache), "") +
1315
- renderValue(token.op1) +
1317
+ return (token.l.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), "") +
1318
+ renderValue(token.op1, options, cache, reducer) +
1316
1319
  token.val.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), "") +
1317
- renderValue(token.op2) +
1318
- token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache), ""));
1320
+ renderValue(token.op2, options, cache, reducer) +
1321
+ token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), ""));
1319
1322
  case EnumToken.MediaFeatureTokenType:
1320
1323
  return token.val;
1321
1324
  case EnumToken.NotTokenType: