@tbela99/css-parser 1.6.3 → 1.6.5

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 (46) hide show
  1. package/CHANGELOG.md +56 -0
  2. package/README.md +112 -8
  3. package/dist/{lib/data → data}/properties.json.js +296 -29
  4. package/dist/{lib/data → data}/validation.json.js +6 -0
  5. package/dist/index-umd-web.js +962 -744
  6. package/dist/index.cjs +961 -744
  7. package/dist/index.d.ts +24 -6
  8. package/dist/lib/ast/expand.js +1 -1
  9. package/dist/lib/ast/features/calc.js +16 -4
  10. package/dist/lib/ast/features/if.js +2 -2
  11. package/dist/lib/ast/features/inlinecssvariables.js +3 -3
  12. package/dist/lib/ast/features/prefix.js +2 -2
  13. package/dist/lib/ast/features/shorthand.js +8 -6
  14. package/dist/lib/ast/features/transform.js +2 -2
  15. package/dist/lib/ast/math/expression.js +24 -1
  16. package/dist/lib/ast/minify.js +3 -2
  17. package/dist/lib/ast/transform/utils.js +2 -2
  18. package/dist/lib/ast/walk.js +108 -90
  19. package/dist/lib/parser/declaration/list.js +29 -26
  20. package/dist/lib/parser/declaration/map.js +186 -54
  21. package/dist/lib/parser/parse.js +11 -9
  22. package/dist/lib/parser/source.js +2 -2
  23. package/dist/lib/parser/tokenize.js +99 -124
  24. package/dist/lib/parser/utils/config.js +1 -1
  25. package/dist/lib/parser/utils/declaration.js +0 -18
  26. package/dist/lib/parser/utils/hash.js +10 -5
  27. package/dist/lib/parser/utils/selector.js +1 -1
  28. package/dist/lib/{renderer → printer}/render.js +29 -53
  29. package/dist/lib/syntax/color/a98rgb.js +1 -1
  30. package/dist/lib/syntax/color/alpha.js +19 -20
  31. package/dist/lib/syntax/color/color-mix.js +15 -14
  32. package/dist/lib/syntax/color/hex.js +4 -1
  33. package/dist/lib/syntax/color/lch.js +0 -1
  34. package/dist/lib/syntax/color/oklch.js +0 -1
  35. package/dist/lib/syntax/color/relative-color.js +11 -11
  36. package/dist/lib/syntax/color/utils/components.js +0 -20
  37. package/dist/lib/syntax/color/xyzd50.js +0 -17
  38. package/dist/lib/syntax/constants.js +6 -1
  39. package/dist/lib/syntax/syntax.js +2 -2
  40. package/dist/lib/validation/config.js +5 -33
  41. package/dist/lib/validation/match.js +52 -181
  42. package/dist/node.js +3 -3
  43. package/dist/web.js +4 -3
  44. package/package.json +6 -5
  45. /package/dist/lib/{renderer → printer}/sourcemap/lib/codec.js +0 -0
  46. /package/dist/lib/{renderer → printer}/sourcemap/sourcemap.js +0 -0
@@ -1,4 +1,4 @@
1
- import { TOKENS } from '../syntax/constants.js';
1
+ import { PARENT, TOKENS } from '../syntax/constants.js';
2
2
 
3
3
  /**
4
4
  * Options for the walk function
@@ -114,20 +114,34 @@ var WalkerEvent;
114
114
  function* walk(node, filter, reverse) {
115
115
  const parents = [node];
116
116
  const root = node;
117
- const map = new Map();
118
117
  let options = filter;
119
118
  let isNumeric = false;
120
- let includeValues = false;
119
+ let children = true;
120
+ let attributes = false;
121
121
  let i = 0;
122
122
  if (options != null && typeof options == "object") {
123
123
  filter = options.filter;
124
124
  reverse = options.reverse;
125
- includeValues = options.inludeValues;
125
+ attributes = options.attributes;
126
+ children = options.children ?? true;
126
127
  }
128
+ // @ts-expect-error
129
+ const result = {
130
+ node: null,
131
+ parent: null,
132
+ root,
133
+ parents: function* () {
134
+ let parent = node[PARENT];
135
+ while (parent != null) {
136
+ yield parent;
137
+ parent = parent[PARENT];
138
+ }
139
+ },
140
+ };
127
141
  while ((node = parents[i++])) {
128
142
  let option = null;
129
143
  if (filter != null) {
130
- // @ts-ignore
144
+ // @ts-expect-error
131
145
  option = filter(node);
132
146
  isNumeric = typeof option == "number";
133
147
  if (isNumeric) {
@@ -140,38 +154,43 @@ function* walk(node, filter, reverse) {
140
154
  }
141
155
  }
142
156
  if (!isNumeric || (option & WalkerOptionEnum.Children) === 0) {
143
- // @ts-ignore
144
- yield {
145
- node,
146
- parent: map.get(node),
147
- root,
148
- // @ts-expect-error
149
- parents: function* () {
150
- let parent = map.get(node);
151
- while (parent != null) {
152
- yield parent;
153
- parent = map.get(parent);
154
- }
155
- },
156
- };
157
+ result.node = node;
158
+ // @ts-expect-error
159
+ result.parent = node[PARENT];
160
+ yield result;
157
161
  }
158
- if (includeValues) {
162
+ if (attributes) {
159
163
  if (node[TOKENS] != null) {
160
- // @ts-ignore
164
+ // @ts-expect-error
161
165
  parents.splice(i, 0, ...(reverse ? node[TOKENS].toReversed() : node[TOKENS]));
162
- // @ts-ignore
166
+ for (const child of node[TOKENS]) {
167
+ if (child[PARENT] != node) {
168
+ child[PARENT] = node;
169
+ }
170
+ }
171
+ // @ts-expect-error
163
172
  }
164
173
  else if (Array.isArray(node.val)) {
165
- // @ts-ignore
174
+ // @ts-expect-error
175
+ for (const val of node.val) {
176
+ if (val[PARENT] != node) {
177
+ val[PARENT] = node;
178
+ }
179
+ }
180
+ // @ts-expect-error
166
181
  parents.splice(i, 0, ...(reverse ? node.val.toReversed() : node.val));
167
182
  }
168
183
  }
169
- // @ts-ignore
170
- if (node["chi"] != null && (!isNumeric || (option & WalkerOptionEnum.IgnoreChildren) === 0)) {
171
- // @ts-ignore
184
+ if (children &&
185
+ // @ts-expect-error
186
+ node["chi"] != null &&
187
+ (!isNumeric || (option & WalkerOptionEnum.IgnoreChildren) === 0)) {
188
+ // @ts-expect-error
172
189
  parents.splice(i, 0, ...(reverse ? node.chi.toReversed() : node.chi));
173
190
  for (const child of node.chi) {
174
- map.set(child, node);
191
+ if (child[PARENT] != node) {
192
+ child[PARENT] = node;
193
+ }
175
194
  }
176
195
  }
177
196
  }
@@ -225,8 +244,6 @@ function* walk(node, filter, reverse) {
225
244
  */
226
245
  function* walkValues(values, root = null, filter, reverse) {
227
246
  const stack = values.slice();
228
- const map = new Map();
229
- const used = new Set();
230
247
  let previous = null;
231
248
  if (filter != null && typeof filter == "function") {
232
249
  filter = {
@@ -243,31 +260,43 @@ function* walkValues(values, root = null, filter, reverse) {
243
260
  let value;
244
261
  let option;
245
262
  let node;
246
- // const parents: Token[] = [];
263
+ let i = -1;
247
264
  const eventType = filter.event ?? WalkerEvent.Enter;
248
- while (stack.length > 0) {
249
- value = reverse ? stack.pop() : stack.shift();
265
+ // @ts-ignore
266
+ const result = {
267
+ value: null,
268
+ parent: null,
269
+ previousValue: null,
270
+ nextValue: null,
271
+ //
272
+ root: root ?? null,
273
+ parents: function* () {
274
+ // @ts-ignore
275
+ let result = root;
276
+ while (result != null) {
277
+ yield result;
278
+ result = result[PARENT] ?? null;
279
+ }
280
+ },
281
+ };
282
+ while (++i < stack.length) {
283
+ value = stack[i];
250
284
  option = null;
251
- node = map.get(value) ?? null;
252
- if (used.has(value)) {
253
- continue;
254
- }
255
- used.add(value);
256
285
  if (filter.fn != null && eventType & WalkerEvent.Enter) {
257
286
  const isValid = filter.type == null ||
258
287
  value.typ == filter.type ||
259
288
  (Array.isArray(filter.type) && filter.type.includes(value.typ)) ||
260
289
  (typeof filter.type == "function" && filter.type(value));
261
290
  if (isValid) {
262
- // @ts-ignore
263
- option = filter.fn(value, map.get(value) ?? root, WalkerEvent.Enter,
291
+ // @ts-expect-error
292
+ option = filter.fn(value, value[PARENT] ?? root, WalkerEvent.Enter,
264
293
  // @ts-expect-error
265
294
  function* () {
266
295
  // @ts-expect-error
267
- let parent = map.get(node);
296
+ let parent = node[PARENT];
268
297
  while (parent != null) {
269
298
  yield parent;
270
- parent = map.get(parent);
299
+ parent = parent[PARENT] ?? null;
271
300
  }
272
301
  });
273
302
  isNumeric = typeof option == "number";
@@ -277,48 +306,30 @@ function* walkValues(values, root = null, filter, reverse) {
277
306
  if (isNumeric && option & WalkerOptionEnum.Ignore) {
278
307
  continue;
279
308
  }
280
- // @ts-ignore
309
+ //
281
310
  if (option != null && typeof option == "object" && ("typ" in option || Array.isArray(option))) {
282
311
  const op = Array.isArray(option) ? option : [option];
312
+ stack.splice(i, 0, ...(reverse ? op.toReversed() : op));
283
313
  for (const o of op) {
284
- map.set(o, map.get(value) ?? root);
285
- if (reverse) {
286
- stack.unshift(o);
287
- }
288
- else {
289
- stack.push(o);
314
+ if (o[PARENT] != value) {
315
+ o[PARENT] = value;
290
316
  }
291
317
  }
292
318
  }
293
319
  }
294
320
  }
295
- yield {
296
- value,
297
- parent: map.get(value) ?? root,
298
- previousValue: previous,
299
- nextValue: stack[0] ?? null,
300
- // @ts-ignore
301
- root: root ?? null,
302
- // @ts-expect-error
303
- parents: function* () {
304
- // @ts-expect-error
305
- let result = map.get(node) ?? root;
306
- let next;
307
- do {
308
- yield result;
309
- next = map.get(result) ?? root;
310
- if (next == result) {
311
- break;
312
- }
313
- result = next;
314
- } while (result != null);
315
- },
316
- };
321
+ result.value = value;
322
+ result.parent = value[PARENT] ?? root;
323
+ result.previousValue = previous;
324
+ result.nextValue = stack[0] ?? null;
325
+ yield result;
317
326
  if (!isNumeric || (option & WalkerOptionEnum.IgnoreChildren) === 0) {
318
327
  if ("chi" in value) {
319
328
  const sliced = value.chi.slice();
320
329
  for (const child of sliced) {
321
- map.set(child, value);
330
+ if (child[PARENT] != value) {
331
+ child[PARENT] = value;
332
+ }
322
333
  if (reverse) {
323
334
  stack.unshift(child);
324
335
  }
@@ -330,31 +341,41 @@ function* walkValues(values, root = null, filter, reverse) {
330
341
  else {
331
342
  const values = [];
332
343
  if ("l" in value && value.l != null) {
333
- // @ts-ignore
344
+ // @ts-expect-error
334
345
  values.push(value.l);
335
- // @ts-ignore
336
- map.set(value.l, value);
346
+ // @ts-expect-error
347
+ if (value.l[PARENT] != value) {
348
+ // @ts-expect-error
349
+ value.l[PARENT] = value;
350
+ }
337
351
  }
338
352
  if ("op" in value && typeof value.op == "object") {
339
- // @ts-ignore
353
+ // @ts-expect-error
340
354
  values.push(value.op);
341
- // @ts-ignore
342
- map.set(value.op, value);
355
+ // @ts-expect-error
356
+ if (value.op[PARENT] != value) {
357
+ // @ts-expect-error
358
+ value.op[PARENT] = value;
359
+ }
343
360
  }
344
361
  if ("r" in value && value.r != null) {
345
362
  if (Array.isArray(value.r)) {
346
363
  for (const r of value.r) {
347
- // @ts-ignore
364
+ //
348
365
  values.push(r);
349
- // @ts-ignore
350
- map.set(r, value);
366
+ if (r[PARENT] != value) {
367
+ //
368
+ r[PARENT] = value;
369
+ }
351
370
  }
352
371
  }
353
372
  else {
354
- // @ts-ignore
373
+ //
355
374
  values.push(value.r);
356
- // @ts-ignore
357
- map.set(value.r, value);
375
+ if (value.r[PARENT] != value) {
376
+ //
377
+ value.r[PARENT] = value;
378
+ }
358
379
  }
359
380
  }
360
381
  if (values.length > 0) {
@@ -376,17 +397,14 @@ function* walkValues(values, root = null, filter, reverse) {
376
397
  (typeof filter.type == "function" && filter.type(value));
377
398
  if (isValid) {
378
399
  // @ts-ignore
379
- option = filter.fn(value, map.get(value), WalkerEvent.Leave);
400
+ option = filter.fn(value, value[PARENT], WalkerEvent.Leave);
380
401
  // @ts-ignore
381
402
  if (option != null && ("typ" in option || Array.isArray(option))) {
382
403
  const op = Array.isArray(option) ? option : [option];
404
+ stack.splice(i, 0, ...(reverse ? op.toReversed() : op));
383
405
  for (const o of op) {
384
- map.set(o, map.get(value) ?? root);
385
- if (reverse) {
386
- stack.unshift(o);
387
- }
388
- else {
389
- stack.push(o);
406
+ if (o[PARENT] != value) {
407
+ o[PARENT] = value;
390
408
  }
391
409
  }
392
410
  }
@@ -10,6 +10,9 @@ import { objectHash } from '../utils/hash.js';
10
10
  import { equalsIgnoreCase } from '../utils/text.js';
11
11
 
12
12
  const config = getConfig();
13
+ /**
14
+ * Represents a list of property declarations, compute shorthand and remove duplicate declarations.
15
+ */
13
16
  class PropertyList {
14
17
  options = { removeDuplicateDeclarations: true, computeShorthand: true };
15
18
  declarations;
@@ -17,14 +20,7 @@ class PropertyList {
17
20
  this.options = options;
18
21
  this.declarations = new Map();
19
22
  }
20
- // set(nam: string, value: string | Token[]) {
21
- // return this.add({
22
- // typ: EnumToken.DeclarationNodeType,
23
- // nam,
24
- // val: Array.isArray(value) ? value : parseString(String(value)),
25
- // });
26
- // }
27
- add(...declarations) {
23
+ add(declarations) {
28
24
  let name;
29
25
  let syntaxRules = null;
30
26
  let result;
@@ -62,22 +58,8 @@ class PropertyList {
62
58
  }
63
59
  // do not compute shorthand for invalid declarations
64
60
  if (declaration[STATE] !== EnumAstNodeStatus.Validated) {
65
- // const key = objectHash(declaration);
66
- // if (!this.ketsey.has(key)) {
67
- // this.ketsey.set(key, [declaration.nam]);
68
- // console.error(
69
- // `Adding declaration : ${(<AstDeclaration>declaration).nam} with key : ${key}`
70
- // )
71
- // }
72
- // else {
73
- // console.error(
74
- // `Duplicate declaration found: ${(<AstDeclaration>declaration).nam} with key : [ ${key} => ${this.ketsey.get(key)} ]`
75
- // )
76
- // console.error(JSON.stringify(toSortedString(declaration)))
77
- // this.ketsey.get(key).push(declaration.nam);
78
- // }
79
61
  this.declarations.set(objectHash(declaration), declaration);
80
- return this;
62
+ continue;
81
63
  }
82
64
  let propertyName = declaration.nam;
83
65
  let shortHandType;
@@ -107,13 +89,33 @@ class PropertyList {
107
89
  }
108
90
  // @ts-ignore
109
91
  if (shortHandType == "map") {
92
+ let owner = this;
93
+ const mapName =
110
94
  // @ts-ignore
111
- if (!this.declarations.has(shorthand)) {
95
+ config.map[propertyName]?.map ?? config.map[shorthand]?.properties?.[propertyName]?.map;
96
+ if (typeof mapName === "string") {
97
+ if (!this.declarations.has(mapName)) {
98
+ // @ts-ignore
99
+ this.declarations.set(mapName, new PropertyMap(config.map[mapName]));
100
+ }
101
+ owner = this.declarations.get(mapName);
102
+ }
103
+ else if (!this.declarations.has(shorthand)) {
112
104
  // @ts-ignore
113
105
  this.declarations.set(shorthand, new PropertyMap(config.map[shorthand]));
114
106
  }
115
- // @ts-ignore
116
- this.declarations.get(shorthand).add(declaration);
107
+ if (owner == this) {
108
+ // @ts-ignore
109
+ owner.declarations.get(shorthand).add(declaration);
110
+ }
111
+ else {
112
+ if (!owner.declarations.has(shorthand)) {
113
+ // @ts-ignore
114
+ owner.declarations.set(shorthand, new PropertyMap(config.map[shorthand]));
115
+ }
116
+ // @ts-ignore
117
+ owner.declarations.get(shorthand).add(declaration);
118
+ }
117
119
  }
118
120
  // @ts-ignore
119
121
  else if (shortHandType == "set") {
@@ -200,6 +202,7 @@ class PropertyList {
200
202
  [Symbol.iterator]() {
201
203
  let iterator = this.declarations.values();
202
204
  const iterators = [];
205
+ // @ts-ignore
203
206
  return {
204
207
  next() {
205
208
  let value = iterator.next();