@tbela99/css-parser 1.4.6 → 1.4.8

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 (55) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/README.md +1 -0
  3. package/dist/index-umd-web.js +6578 -6969
  4. package/dist/index.cjs +7808 -8197
  5. package/dist/index.d.ts +923 -51
  6. package/dist/lib/ast/clone.js +16 -23
  7. package/dist/lib/ast/expand.js +2 -2
  8. package/dist/lib/ast/features/if.js +88 -87
  9. package/dist/lib/ast/features/inlinecssvariables.js +2 -2
  10. package/dist/lib/ast/features/prefix.js +26 -22
  11. package/dist/lib/ast/features/transform.js +3 -3
  12. package/dist/lib/ast/find.js +17 -14
  13. package/dist/lib/ast/minify.js +97 -94
  14. package/dist/lib/ast/types.js +10 -2
  15. package/dist/lib/ast/walk.js +1 -1
  16. package/dist/lib/fs/resolve.js +47 -45
  17. package/dist/lib/parser/arena.js +206 -0
  18. package/dist/lib/parser/declaration/list.js +9 -10
  19. package/dist/lib/parser/declaration/map.js +13 -18
  20. package/dist/lib/parser/linemap.js +62 -0
  21. package/dist/lib/parser/parse.js +331 -504
  22. package/dist/lib/parser/source.js +72 -0
  23. package/dist/lib/parser/tokenize.js +39 -33
  24. package/dist/lib/parser/utils/at-rule-container.js +244 -247
  25. package/dist/lib/parser/utils/at-rule-generic.js +25 -25
  26. package/dist/lib/parser/utils/at-rule-import.js +194 -234
  27. package/dist/lib/parser/utils/at-rule-media.js +284 -283
  28. package/dist/lib/parser/utils/at-rule-support.js +279 -251
  29. package/dist/lib/parser/utils/at-rule-when-else.js +256 -259
  30. package/dist/lib/parser/utils/at-rule.js +19 -16
  31. package/dist/lib/parser/utils/cache.js +33 -5
  32. package/dist/lib/parser/utils/declaration.js +260 -298
  33. package/dist/lib/parser/utils/hash.js +15 -15
  34. package/dist/lib/parser/utils/selector.js +182 -222
  35. package/dist/lib/parser/utils/token.js +4 -7
  36. package/dist/lib/renderer/render.js +152 -121
  37. package/dist/lib/syntax/color/alpha.js +1 -4
  38. package/dist/lib/syntax/color/color-mix.js +8 -1
  39. package/dist/lib/syntax/color/color.js +17 -9
  40. package/dist/lib/syntax/color/hex.js +1 -1
  41. package/dist/lib/syntax/color/p3.js +9 -1
  42. package/dist/lib/syntax/color/utils/colorspace.js +3 -3
  43. package/dist/lib/syntax/color/utils/components.js +21 -22
  44. package/dist/lib/syntax/color/utils/distance.js +6 -1
  45. package/dist/lib/syntax/color/utils/matrix.js +12 -11
  46. package/dist/lib/syntax/constants.js +25 -3
  47. package/dist/lib/syntax/syntax.js +240 -311
  48. package/dist/lib/validation/config.js +16 -17
  49. package/dist/lib/validation/match.js +1118 -1131
  50. package/dist/lib/validation/parser/parse.js +130 -286
  51. package/dist/node.js +18 -14
  52. package/dist/web.js +12 -10
  53. package/package.json +4 -4
  54. package/dist/lib/validation/utils/whitespace.js +0 -17
  55. /package/dist/lib/syntax/color/{relativecolor.js → relative-color.js} +0 -0
@@ -0,0 +1,206 @@
1
+ import { EnumToken } from '../ast/types.js';
2
+ import { SourceFile } from './source.js';
3
+
4
+ // Node Data layout
5
+ // (node area)
6
+ // node type -> 1 byte
7
+ // parent -> 4 bytes
8
+ // firstChild -> 4 bytes
9
+ // nextSibling -> 4 bytes
10
+ // firstToken -> 4 bytes // parsed selector or at-rule prelude
11
+ // nextToken -> 4 bytes
12
+ // resolvedTokens -> 4 bytes // selector or at-rule prelude as string
13
+ // flags -> 1 byte (validation state, resolved state, resolved prelude state, decoded string state)
14
+ // SpanArena: (start offset 26)
15
+ // sourceId -> 2 bytes
16
+ // sourceSpanStart -> 4 byte
17
+ // sourceSpanLength -> 4 bytes
18
+ // payloadOffset -> 4 bytes
19
+ // payloadType -> 1 byte
20
+ // (data area)
21
+ // data -> (start offset 41): union of ColorArena (ColorKind, r, g, b, a) | string intern id | number | DimensionArena
22
+ // ColorArena (11 bytes):
23
+ // ColorType -> 1 byte
24
+ // resolved -> 4 bytes
25
+ // componentsCount -> 1 byte
26
+ // components -> 3 to 5 bytes (r,g,b / l, c, h / h , w, b / x, y, z / h, s, l), alpha or (c, m, y, k, alpha)
27
+ // DimensionArena (8 bytes):
28
+ // value -> 4 bytes
29
+ // unit -> 4 bytes
30
+ // String (4 bytes):
31
+ // string id -> 4 bytes
32
+ // Number (4 bytes):
33
+ // number -> 4 bytes
34
+ // Percentage (4 bytes):
35
+ // number -> 4 bytes
36
+ // absolute sizes
37
+ const NODE_AREA_SIZE = 41;
38
+ const COLOR_AREA_SIZE = 11;
39
+ const DIMENSION_AREA_SIZE = 8;
40
+ const STRING_AREA_SIZE = 4;
41
+ const NUMBER_AREA_SIZE = 4;
42
+ const PERCENTAGE_AREA_SIZE = 4;
43
+ const NODE_SPAN_OFFSET = 26;
44
+ const NODE_PAYLOAD_OFFSET = 36;
45
+ const NODE_PAYLOAD_TYPE_OFFSET = 40;
46
+ // relative offsets
47
+ const COLOR_TYPE_OFFSET = 0;
48
+ const COLOR_RESOLVED_OFFSET = 1;
49
+ const COLOR_COMPONENTS_COUNT_OFFSET = 5;
50
+ const COLOR_COMPONENTS_OFFSET = 6;
51
+ var PayloadType;
52
+ (function (PayloadType) {
53
+ PayloadType[PayloadType["Unset"] = 0] = "Unset";
54
+ PayloadType[PayloadType["Color"] = 1] = "Color";
55
+ PayloadType[PayloadType["Dimension"] = 2] = "Dimension";
56
+ PayloadType[PayloadType["String"] = 3] = "String";
57
+ PayloadType[PayloadType["Number"] = 4] = "Number";
58
+ PayloadType[PayloadType["Percentage"] = 5] = "Percentage";
59
+ })(PayloadType || (PayloadType = {}));
60
+ class Arena {
61
+ capacity;
62
+ buffer;
63
+ view;
64
+ count = NODE_AREA_SIZE; // node 0 = null
65
+ // root ast node
66
+ sources = [];
67
+ strings;
68
+ constructor(strings, size = 1024) {
69
+ // Heuristic: one node per source character.
70
+ this.capacity = Math.max(1024, size >>> 0);
71
+ this.buffer = new ArrayBuffer(this.capacity * NODE_AREA_SIZE);
72
+ this.view = new DataView(this.buffer);
73
+ this.strings = strings;
74
+ }
75
+ get root() {
76
+ return this.view.getInt32(NODE_AREA_SIZE);
77
+ }
78
+ allocateRoot() {
79
+ if (this.root !== 0) {
80
+ throw new Error("Root node already allocated");
81
+ }
82
+ return this.allocate(EnumToken.StyleSheetNodeType);
83
+ }
84
+ // =====================================================================
85
+ // === API ===
86
+ // ====================================================================
87
+ getFile(id) {
88
+ return this.sources[id].getFile();
89
+ }
90
+ getPosition(id) {
91
+ return this.sources[this.view.getInt16(id + NODE_SPAN_OFFSET)].position(this.view.getInt16(id + NODE_SPAN_OFFSET + 2));
92
+ }
93
+ addSource(content, lines, file = null) {
94
+ this.sources.push(new SourceFile(this.sources.length, content, lines, file));
95
+ return this.sources[this.sources.length - 1];
96
+ }
97
+ allocate(type) {
98
+ if (this.count + NODE_AREA_SIZE >= this.capacity) {
99
+ this.grow();
100
+ }
101
+ const node = this.count;
102
+ this.count += NODE_AREA_SIZE;
103
+ this.view.setUint8(node, type);
104
+ // Other arrays are already zero-initialized.
105
+ // if (typeof data === "string") {
106
+ // this.data[node] = this.allocate(EnumToken.ValueTokenType, this.strings.intern(data));
107
+ // } else if (typeof data === "number") {
108
+ // this.data[node] = this.allocate(EnumToken.ValueTokenType, data);
109
+ // }
110
+ return node;
111
+ }
112
+ allocateNumber(id, data) {
113
+ if (this.view.getUint8(id + NODE_PAYLOAD_TYPE_OFFSET) === PayloadType.Number) {
114
+ this.view.setFloat32(this.view.getInt32(id + NODE_PAYLOAD_OFFSET), data);
115
+ return;
116
+ }
117
+ if (this.count + NUMBER_AREA_SIZE >= this.capacity) {
118
+ this.grow();
119
+ }
120
+ this.view.setUint8(id + NODE_PAYLOAD_TYPE_OFFSET, PayloadType.Number);
121
+ this.view.setInt32(id + NODE_PAYLOAD_OFFSET, this.count);
122
+ this.view.setFloat32(this.count, data);
123
+ this.count += NUMBER_AREA_SIZE;
124
+ }
125
+ allocatePercentage(id, data) {
126
+ if (this.view.getUint8(id + NODE_PAYLOAD_TYPE_OFFSET) === PayloadType.Percentage) {
127
+ this.view.setFloat32(this.view.getInt32(id + NODE_PAYLOAD_OFFSET), data);
128
+ return;
129
+ }
130
+ if (this.count + PERCENTAGE_AREA_SIZE >= this.capacity) {
131
+ this.grow();
132
+ }
133
+ this.view.setUint8(id + NODE_PAYLOAD_TYPE_OFFSET, PayloadType.Percentage);
134
+ this.view.setInt32(id + NODE_PAYLOAD_OFFSET, this.count);
135
+ this.view.setFloat32(this.count, data);
136
+ this.count += PERCENTAGE_AREA_SIZE;
137
+ }
138
+ allocateString(id, data) {
139
+ if (this.view.getUint8(id + NODE_PAYLOAD_TYPE_OFFSET) === PayloadType.String) {
140
+ this.view.setInt32(this.view.getInt32(id + NODE_PAYLOAD_OFFSET), this.strings.intern(data));
141
+ return;
142
+ }
143
+ if (this.count + STRING_AREA_SIZE >= this.capacity) {
144
+ this.grow();
145
+ }
146
+ this.view.setUint8(id + NODE_PAYLOAD_TYPE_OFFSET, PayloadType.String);
147
+ this.view.setInt32(id + NODE_PAYLOAD_OFFSET, this.count);
148
+ this.view.setInt32(this.count, this.strings.intern(data));
149
+ this.count += STRING_AREA_SIZE;
150
+ }
151
+ allocateDimension(id, value, dimension) {
152
+ if (this.view.getUint8(id + NODE_PAYLOAD_TYPE_OFFSET) === PayloadType.Dimension) {
153
+ const offset = this.view.getInt32(id + NODE_PAYLOAD_OFFSET);
154
+ this.view.setFloat32(offset, value);
155
+ this.view.setInt32(offset + 4, this.strings.intern(dimension));
156
+ return;
157
+ }
158
+ if (this.count + DIMENSION_AREA_SIZE >= this.capacity) {
159
+ this.grow();
160
+ }
161
+ this.view.setUint8(id + NODE_PAYLOAD_TYPE_OFFSET, PayloadType.Dimension);
162
+ this.view.setInt32(id + NODE_PAYLOAD_OFFSET, this.count);
163
+ this.view.setFloat32(this.count, value);
164
+ this.view.setFloat32(this.count + 4, this.strings.intern(dimension));
165
+ this.count += DIMENSION_AREA_SIZE;
166
+ }
167
+ allocateColor(id, type) {
168
+ if (this.view.getUint8(id + NODE_PAYLOAD_TYPE_OFFSET) === PayloadType.Color) {
169
+ this.view.setUint8(this.view.getInt32(id + NODE_PAYLOAD_OFFSET) + COLOR_TYPE_OFFSET, type);
170
+ return;
171
+ }
172
+ if (this.count + COLOR_AREA_SIZE >= this.capacity) {
173
+ this.grow();
174
+ }
175
+ this.view.setUint8(id + NODE_PAYLOAD_TYPE_OFFSET, PayloadType.Color);
176
+ this.view.setInt32(id + NODE_PAYLOAD_OFFSET, this.count);
177
+ this.view.setUint8(this.view.getInt32(id + NODE_PAYLOAD_OFFSET) + COLOR_TYPE_OFFSET, type);
178
+ this.count += COLOR_AREA_SIZE;
179
+ }
180
+ resolveColor(id, resolved) {
181
+ this.view.setInt32(this.view.getInt32(id + NODE_PAYLOAD_OFFSET) + COLOR_RESOLVED_OFFSET, this.strings.intern(resolved));
182
+ }
183
+ setColorComponents(id, data) {
184
+ const offset = this.view.getInt32(id + NODE_PAYLOAD_OFFSET);
185
+ this.view.setUint8(offset + COLOR_COMPONENTS_COUNT_OFFSET, data.length);
186
+ for (let i = 0; i < data.length; i++) {
187
+ this.view.setUint8(offset + COLOR_COMPONENTS_OFFSET + i, data[i]);
188
+ }
189
+ }
190
+ setSpans(id, sourceId, start, end) {
191
+ const offset = id + NODE_SPAN_OFFSET;
192
+ this.view.setInt16(offset, sourceId);
193
+ this.view.setInt32(offset + 2, start);
194
+ this.view.setInt32(offset + 6, end);
195
+ }
196
+ grow() {
197
+ const newCapacity = this.capacity * 2;
198
+ const buffer = new ArrayBuffer(newCapacity);
199
+ this.view = new DataView(buffer);
200
+ new Uint8Array(buffer).set(new Uint8Array(this.buffer));
201
+ this.buffer = buffer;
202
+ this.capacity = newCapacity;
203
+ }
204
+ }
205
+
206
+ export { Arena };
@@ -6,7 +6,7 @@ import { EnumToken, EnumAstNodeStatus } from '../../ast/types.js';
6
6
  import { getParsedSyntax } from '../../validation/config.js';
7
7
  import { ValidationSyntaxGroupEnum } from '../../validation/parser/typedef.js';
8
8
  import { matchAllSyntaxes, createValidationContext } from '../../validation/match.js';
9
- import { definedPropertySettings } from '../../syntax/constants.js';
9
+ import { STATE } from '../../syntax/constants.js';
10
10
 
11
11
  const config = getConfig();
12
12
  class PropertyList {
@@ -32,9 +32,9 @@ class PropertyList {
32
32
  declaration.typ != EnumToken.DeclarationNodeType
33
33
  ? null
34
34
  : declaration.nam.toLowerCase();
35
- if (declaration.state == EnumAstNodeStatus.Invalid ||
36
- declaration.state == EnumAstNodeStatus.Unknown ||
37
- declaration.state == EnumAstNodeStatus.ValidationFailed ||
35
+ if (declaration[STATE] == EnumAstNodeStatus.Invalid ||
36
+ declaration[STATE] == EnumAstNodeStatus.Unknown ||
37
+ declaration[STATE] == EnumAstNodeStatus.ValidationFailed ||
38
38
  declaration.typ != EnumToken.DeclarationNodeType ||
39
39
  "composes" === name ||
40
40
  (typeof this.options.removeDuplicateDeclarations === "string" &&
@@ -49,18 +49,17 @@ class PropertyList {
49
49
  this.declarations.set(declaration.nam, declaration);
50
50
  continue;
51
51
  }
52
- if (declaration.state == EnumAstNodeStatus.Unvalidated) {
52
+ if (declaration[STATE] == EnumAstNodeStatus.Unvalidated) {
53
53
  syntaxRules = getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, declaration.nam.toLowerCase());
54
54
  if (syntaxRules != null) {
55
55
  result = matchAllSyntaxes(syntaxRules, createValidationContext(declaration.val), this.options);
56
- Object.defineProperty(declaration, "state", {
57
- ...definedPropertySettings,
58
- value: result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.ValidationFailed,
59
- });
56
+ declaration[STATE] = result.success
57
+ ? EnumAstNodeStatus.Validated
58
+ : EnumAstNodeStatus.ValidationFailed;
60
59
  }
61
60
  }
62
61
  // do not compute shorthand for invalid declarations
63
- if (declaration.state !== EnumAstNodeStatus.Validated) {
62
+ if (declaration[STATE] !== EnumAstNodeStatus.Validated) {
64
63
  this.declarations.set(declaration.nam, declaration);
65
64
  return this;
66
65
  }
@@ -5,6 +5,7 @@ import { EnumToken } from '../../ast/types.js';
5
5
  import { renderValue } from '../../renderer/render.js';
6
6
  import { parseString } from '../parse.js';
7
7
  import { PropertySet } from './set.js';
8
+ import { PROPERTYNAME } from '../../syntax/constants.js';
8
9
 
9
10
  const propertiesConfig = getConfig();
10
11
  class PropertyMap {
@@ -68,7 +69,7 @@ class PropertyMap {
68
69
  }
69
70
  if (
70
71
  // @ts-ignore
71
- ("propertyName" in acc[i] && acc[i].propertyName == property) ||
72
+ acc[i][PROPERTYNAME] == property ||
72
73
  matchType(acc[i], props)) {
73
74
  if ("prefix" in props &&
74
75
  props.previous != null &&
@@ -240,15 +241,15 @@ class PropertyMap {
240
241
  }
241
242
  value.push(t);
242
243
  // @ts-ignore
243
- if ("propertyName" in t) {
244
+ if (t[PROPERTYNAME] != null) {
244
245
  // @ts-ignore
245
- if (!map.has(t.propertyName)) {
246
+ if (!map.has(t[PROPERTYNAME])) {
246
247
  // @ts-ignore
247
- map.set(t.propertyName, { t: [t], value: [cache.get(t)] });
248
+ map.set(t[PROPERTYNAME], { t: [t], value: [cache.get(t)] });
248
249
  }
249
250
  else {
250
251
  // @ts-ignore
251
- const v = map.get(t.propertyName);
252
+ const v = map.get(t[PROPERTYNAME]);
252
253
  v.t.push(t);
253
254
  v.value.push(cache.get(t));
254
255
  }
@@ -383,7 +384,7 @@ class PropertyMap {
383
384
  isShorthand = match;
384
385
  }
385
386
  // @ts-ignore
386
- if (("propertyName" in val && val.propertyName == property) || match) {
387
+ if (val[PROPERTYNAME] == property || match) {
387
388
  if (!(curr[0] in tokens)) {
388
389
  tokens[curr[0]] = [[]];
389
390
  }
@@ -602,11 +603,8 @@ class PropertyMap {
602
603
  }
603
604
  let count = map.get(patterns[i]);
604
605
  if (count > 0 && matchType(values[j], this.config.properties[patterns[i]])) {
605
- Object.defineProperty(values[j], "propertyName", {
606
- enumerable: false,
607
- writable: true,
608
- value: patterns[i],
609
- });
606
+ // @ts-expect-error
607
+ values[j][PROPERTYNAME] = patterns[i];
610
608
  map.set(patterns[i], --count);
611
609
  values.splice(j--, 1);
612
610
  }
@@ -620,14 +618,11 @@ class PropertyMap {
620
618
  if (map.get(v) == 1) {
621
619
  let i = declaration.val.length;
622
620
  while (i--) {
623
- // @ts-ignore
624
- if (declaration.val[i].propertyName == key) {
621
+ // @ts-expect-error
622
+ if (declaration.val[i][PROPERTYNAME] == key) {
625
623
  const val = { ...declaration.val[i] };
626
- Object.defineProperty(val, "propertyName", {
627
- enumerable: false,
628
- writable: true,
629
- value: v,
630
- });
624
+ // @ts-expect-error
625
+ val[PROPERTYNAME] = v;
631
626
  declaration.val.splice(i, 0, val, { typ: EnumToken.WhitespaceTokenType });
632
627
  }
633
628
  }
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Compute line and column of the offset
3
+ */
4
+ class LineMap {
5
+ /**
6
+ * line starts
7
+ */
8
+ lineStarts;
9
+ constructor(lines) {
10
+ this.lineStarts = lines;
11
+ }
12
+ /**
13
+ * Compute line and column of the offset
14
+ * @param offset
15
+ * @returns
16
+ */
17
+ position(offset) {
18
+ const line = this.search(offset);
19
+ const column = offset - this.lineStarts[line];
20
+ // [line, column]
21
+ return [line + 1, column == 0 ? 1 : column];
22
+ }
23
+ /**
24
+ * search the greatest index of the value less than or equal to offset
25
+ * @param offset
26
+ * @returns
27
+ */
28
+ search(offset) {
29
+ // search lineStarts using binary search
30
+ let start = 0;
31
+ let end = this.lineStarts.length - 1;
32
+ let mid = 0;
33
+ let result = -1;
34
+ while (start <= end) {
35
+ mid = start + ((end - start) >>> 1);
36
+ if (this.lineStarts[mid] <= offset) {
37
+ result = mid;
38
+ start = mid + 1;
39
+ }
40
+ else if (this.lineStarts[mid] > offset) {
41
+ end = mid - 1;
42
+ }
43
+ }
44
+ return result;
45
+ }
46
+ /**
47
+ * set line starts
48
+ * @param lines
49
+ */
50
+ setLineStarts(lines) {
51
+ this.lineStarts = lines;
52
+ }
53
+ /**
54
+ * get line starts
55
+ * @returns
56
+ */
57
+ getLineStarts() {
58
+ return this.lineStarts;
59
+ }
60
+ }
61
+
62
+ export { LineMap };