@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
@@ -1,8 +1,8 @@
1
1
  import { EnumToken } from '../../ast/types.js';
2
2
  import { evaluate } from '../../ast/math/expression.js';
3
3
  import { gcd } from '../../ast/math/math.js';
4
- import { tokensfuncDefMap, mediaTypes, definedPropertySettings, mFLT, mFGT } from '../../syntax/constants.js';
5
- import { trimArray, matchAllSyntaxes, createValidationContext, getMFInfo, isMFValue, isMFName } from '../../validation/match.js';
4
+ import { tokensfuncDefMap, mediaTypes, LOC, mFLT, mFGT } from '../../syntax/constants.js';
5
+ import { trimArray, matchAllSyntaxes, createValidationContext, getMFInfo, isMFValue } from '../../validation/match.js';
6
6
  import { ValidationSyntaxGroupEnum, MediaFeatureType } from '../../validation/parser/typedef.js';
7
7
  import { getParsedSyntax } from '../../validation/config.js';
8
8
 
@@ -51,61 +51,66 @@ function parseMediaqueryList(stream, options) {
51
51
  if (i < stream.length) {
52
52
  if (stream[i].typ === EnumToken.IdenTokenType) {
53
53
  const val = stream[i].val.toLowerCase();
54
- if ("not" === val || "only" === val) {
55
- tokens.push(stream[i]);
56
- stack.push(stream[i]);
57
- i++;
58
- while (i < stream.length &&
59
- (stream[i]?.typ === EnumToken.WhitespaceTokenType ||
60
- stream[i]?.typ === EnumToken.CommentTokenType)) {
61
- tokens.push(stream[i]);
62
- i++;
63
- }
64
- if (stream[i]?.typ !== EnumToken.IdenTokenType ||
65
- !mediaTypes.includes(stream[i].val.toLowerCase())) {
66
- errors.push({
67
- action: "drop",
68
- node: stream[i],
69
- message: `expecting '<media-type>' at ${stream[i]?.loc?.src}:${stream[i]?.loc?.sta.lin}:${stream[i]?.loc?.sta.col}`,
70
- });
71
- continue;
72
- }
73
- const index = tokens.indexOf(stack[stack.length - 1]);
74
- tokens[index] = Object.defineProperty(Object.assign(tokens[index], {
75
- typ: val === "not" ? EnumToken.NotTokenType : EnumToken.OnlyTokenType,
76
- val: stream[i],
77
- }), "loc", { ...definedPropertySettings, value: { ...tokens[index].loc, end: stream[i].loc.end } });
78
- tokens.length = index + 1;
79
- i++;
80
- // expect end of stream | and | or | not
81
- while (i < stream.length &&
82
- (stream[i]?.typ === EnumToken.WhitespaceTokenType ||
83
- stream[i]?.typ === EnumToken.CommentTokenType)) {
84
- tokens.push(stream[i]);
85
- i++;
86
- }
87
- if (i < stream.length) {
88
- if (stream[i].typ === EnumToken.AndTokenType) {
89
- tokens.push(Object.assign(stream[i], { typ: EnumToken.AndTokenType }));
90
- stack.push(stream[i]);
91
- i++;
92
- }
93
- else if (stream[i].typ === EnumToken.OrTokenType) {
94
- tokens.push(Object.assign(stream[i], { typ: EnumToken.OrTokenType }));
95
- stack.push(stream[i]);
96
- i++;
97
- }
98
- else {
99
- success = false;
100
- errors.push({
101
- action: "drop",
102
- node: stream[i],
103
- message: `expecting 'and' or 'or' at ${stream[i]?.loc?.src}:${stream[i]?.loc?.sta.lin}:${stream[i]?.loc?.sta.col}`,
104
- });
105
- }
106
- }
107
- }
108
- else if (mediaTypes.includes(val)) {
54
+ // if ("not" === val || "only" === val) {
55
+ // tokens.push(stream[i]);
56
+ // stack.push(stream[i]);
57
+ // i++;
58
+ // while (
59
+ // i < stream.length &&
60
+ // (stream[i]?.typ === EnumToken.WhitespaceTokenType ||
61
+ // stream[i]?.typ === EnumToken.CommentTokenType)
62
+ // ) {
63
+ // tokens.push(stream[i]);
64
+ // i++;
65
+ // }
66
+ // if (
67
+ // stream[i]?.typ !== EnumToken.IdenTokenType ||
68
+ // !mediaTypes.includes((stream[i] as IdentToken).val.toLowerCase())
69
+ // ) {
70
+ // errors.push({
71
+ // action: "drop",
72
+ // node: stream[i],
73
+ // message: `expecting '<media-type>' at ${stream[i]?.[LOC]?.src}:${stream[i]?.[LOC]?.sta.lin}:${stream[i]?.[LOC]?.sta.col}`,
74
+ // });
75
+ // continue;
76
+ // }
77
+ // const index: number = tokens.indexOf(stack[stack.length - 1]);
78
+ // tokens[index][LOC] = { ...tokens[index][LOC]!, end: stream[i][LOC]!.end };
79
+ // tokens[index] = Object.assign(tokens[index], {
80
+ // typ: val === "not" ? EnumToken.NotTokenType : EnumToken.OnlyTokenType,
81
+ // val: stream[i],
82
+ // });
83
+ // tokens.length = index + 1;
84
+ // i++;
85
+ // // expect end of stream | and | or | not
86
+ // while (
87
+ // i < stream.length &&
88
+ // (stream[i]?.typ === EnumToken.WhitespaceTokenType ||
89
+ // stream[i]?.typ === EnumToken.CommentTokenType)
90
+ // ) {
91
+ // tokens.push(stream[i]);
92
+ // i++;
93
+ // }
94
+ // if (i < stream.length) {
95
+ // if (stream[i].typ === EnumToken.AndTokenType) {
96
+ // tokens.push(Object.assign(stream[i], { typ: EnumToken.AndTokenType }));
97
+ // stack.push(stream[i]);
98
+ // i++;
99
+ // } else if (stream[i].typ === EnumToken.OrTokenType) {
100
+ // tokens.push(Object.assign(stream[i], { typ: EnumToken.OrTokenType }));
101
+ // stack.push(stream[i]);
102
+ // i++;
103
+ // } else {
104
+ // success = false;
105
+ // errors.push({
106
+ // action: "drop",
107
+ // node: stream[i],
108
+ // message: `expecting 'and' or 'or' at ${stream[i]?.[LOC]?.src}:${stream[i]?.[LOC]?.sta.lin}:${stream[i]?.[LOC]?.sta.col}`,
109
+ // });
110
+ // }
111
+ // }
112
+ // } else
113
+ if (mediaTypes.includes(val)) {
109
114
  tokens.push(stream[i]);
110
115
  i++;
111
116
  }
@@ -113,9 +118,9 @@ function parseMediaqueryList(stream, options) {
113
118
  success = false;
114
119
  errors.push({
115
120
  action: "drop",
116
- message: `expecting '<media-type>' at ${stream[i]?.loc?.src}:${stream[i]?.loc?.sta.lin}:${stream[i]?.loc?.sta.col}`,
121
+ message: `expecting '<media-type>' at ${stream[i]?.[LOC]?.src}:${stream[i]?.[LOC]?.sta.lin}:${stream[i]?.[LOC]?.sta.col}`,
117
122
  node: stream[i],
118
- location: stream[i].loc,
123
+ location: stream[i][LOC],
119
124
  });
120
125
  }
121
126
  }
@@ -123,9 +128,9 @@ function parseMediaqueryList(stream, options) {
123
128
  success = false;
124
129
  errors.push({
125
130
  action: "drop",
126
- message: `expecting '(' at ${stream[i]?.loc?.src}:${stream[i]?.loc?.sta.lin}:${stream[i]?.loc?.sta.col}`,
131
+ message: `expecting '(' at ${stream[i]?.[LOC]?.src}:${stream[i]?.[LOC]?.sta.lin}:${stream[i]?.[LOC]?.sta.col}`,
127
132
  node: stream[i],
128
- location: stream[i].loc,
133
+ location: stream[i][LOC],
129
134
  });
130
135
  }
131
136
  }
@@ -135,23 +140,18 @@ function parseMediaqueryList(stream, options) {
135
140
  continue;
136
141
  }
137
142
  if (expectAndOrComma) {
138
- let valid = true;
139
143
  if (stream[i].typ === EnumToken.IdenTokenType) {
140
- const val = stream[i].val.toLowerCase();
141
- valid = val === "and" || val === "or";
142
- }
143
- else {
144
- valid = stream[i].typ !== EnumToken.CommaTokenType;
145
- }
146
- if (!valid) {
147
- success = false;
148
- errors.push({
149
- action: "drop",
150
- node: stream[i],
151
- message: `expecting <and>, <or> or comma at ${stream[i]?.loc?.src}:${stream[i]?.loc?.sta.lin}:${stream[i]?.loc?.sta.col}`,
152
- });
153
- break;
144
+ stream[i].val.toLowerCase();
154
145
  }
146
+ // if (!valid) {
147
+ // success = false;
148
+ // errors.push({
149
+ // action: "drop",
150
+ // node: stream[i],
151
+ // message: `expecting <and>, <or> or comma at ${stream[i]?.[LOC]?.src}:${stream[i]?.[LOC]?.sta.lin}:${stream[i]?.[LOC]?.sta.col}`,
152
+ // });
153
+ // break;
154
+ // }
155
155
  expectAndOrComma = false;
156
156
  }
157
157
  if (stream[i].typ === EnumToken.StartParensTokenType || tokensfuncDefMap.has(stream[i].typ)) {
@@ -160,14 +160,14 @@ function parseMediaqueryList(stream, options) {
160
160
  continue;
161
161
  }
162
162
  switch (stream[i].typ) {
163
- case EnumToken.LiteralTokenType:
164
- if (stream[i].val === "<") {
165
- stack.push(Object.assign(stream[i], { typ: EnumToken.LtTokenType }));
166
- }
167
- if (stream[i].val === ">") {
168
- stack.push(Object.assign(stream[i], { typ: EnumToken.GtTokenType }));
169
- }
170
- break;
163
+ // case EnumToken.LiteralTokenType:
164
+ // if ((stream[i] as LiteralToken).val === "<") {
165
+ // stack.push(Object.assign(stream[i], { typ: EnumToken.LtTokenType }));
166
+ // }
167
+ // if ((stream[i] as LiteralToken).val === ">") {
168
+ // stack.push(Object.assign(stream[i], { typ: EnumToken.GtTokenType }));
169
+ // }
170
+ // break;
171
171
  case EnumToken.ColonTokenType:
172
172
  case EnumToken.LtTokenType:
173
173
  case EnumToken.LteTokenType:
@@ -179,13 +179,13 @@ function parseMediaqueryList(stream, options) {
179
179
  case EnumToken.IdenTokenType:
180
180
  {
181
181
  const val = stream[i].val.toLowerCase();
182
- if (val === "not" || val === "only") {
183
- Object.assign(stream[i], {
184
- typ: val === "not" ? EnumToken.NotTokenType : EnumToken.OnlyTokenType,
185
- });
186
- stack.push(stream[i]);
187
- }
188
- else if (val === "and" || val === "or") {
182
+ // if (val === "not" || val === "only") {
183
+ // Object.assign(stream[i], {
184
+ // typ: val === "not" ? EnumToken.NotTokenType : EnumToken.OnlyTokenType,
185
+ // });
186
+ // stack.push(stream[i]);
187
+ // } else
188
+ if (val === "and" || val === "or") {
189
189
  Object.assign(stream[i], {
190
190
  typ: val === "and" ? EnumToken.AndTokenType : EnumToken.OrTokenType,
191
191
  });
@@ -194,7 +194,7 @@ function parseMediaqueryList(stream, options) {
194
194
  errors.push({
195
195
  action: "drop",
196
196
  node: stream[i],
197
- message: `<or> is not allowed outside of parentheses ${stream[i]?.loc?.src}:${stream[i]?.loc?.sta.lin}:${stream[i]?.loc?.sta.col}`,
197
+ message: `<or> is not allowed outside of parentheses ${stream[i]?.[LOC]?.src}:${stream[i]?.[LOC]?.sta.lin}:${stream[i]?.[LOC]?.sta.col}`,
198
198
  });
199
199
  break;
200
200
  }
@@ -203,7 +203,7 @@ function parseMediaqueryList(stream, options) {
203
203
  errors.push({
204
204
  action: "drop",
205
205
  node: stream[i],
206
- message: `cannot mix <and> and <or> at the same level at ${stream[i]?.loc?.src}:${stream[i]?.loc?.sta.lin}:${stream[i]?.loc?.sta.col}`,
206
+ message: `cannot mix <and> and <or> at the same level at ${stream[i]?.[LOC]?.src}:${stream[i]?.[LOC]?.sta.lin}:${stream[i]?.[LOC]?.sta.col}`,
207
207
  });
208
208
  }
209
209
  currentScope.add(stream[i].typ);
@@ -214,12 +214,10 @@ function parseMediaqueryList(stream, options) {
214
214
  case EnumToken.EndParensTokenType:
215
215
  if (tokensfuncDefMap.has(stack.at(-1)?.typ)) {
216
216
  const index = tokens.indexOf(stack.at(-1));
217
- Object.defineProperty(Object.assign(tokens[index], {
217
+ tokens[index][LOC] = { ...tokens[index][LOC], end: stream[i][LOC].end };
218
+ Object.assign(tokens[index], {
218
219
  typ: tokensfuncDefMap.get(stack.at(-1)?.typ),
219
220
  chi: trimArray(tokens.slice(index + 1, tokens.length - 1)),
220
- }), "loc", {
221
- ...definedPropertySettings,
222
- value: { ...tokens[index].loc, end: stream[i].loc.end },
223
221
  });
224
222
  tokens.length = index + 1;
225
223
  const result = matchAllSyntaxes(getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, tokens[index].val + "()")?.[0]?.chi, createValidationContext(tokens[index].chi), options);
@@ -238,33 +236,32 @@ function parseMediaqueryList(stream, options) {
238
236
  const index = tokens.indexOf(stack.at(-1));
239
237
  const prevToken = stack[stack.length - 2];
240
238
  if (mFLT.has(prevToken?.typ) || mFGT.has(prevToken?.typ)) {
241
- if (stack[stack.length - 3]?.typ !== EnumToken.StartParensTokenType) {
242
- success = false;
243
- errors.push({
244
- action: "drop",
245
- node: stream[i],
246
- message: `unmatched '(' at ${stream[i]?.loc?.src}:${stream[i]?.loc?.sta.lin}:${stream[i]?.loc?.sta.col}`,
247
- });
248
- break;
249
- }
250
- if (!mFLT.has(stack.at(-1)?.typ) && mFLT.has(prevToken?.typ)) {
251
- success = false;
252
- errors.push({
253
- action: "drop",
254
- node: stack.at(-1),
255
- message: `expected <mf-lt> at ${stack.at(-1)?.loc?.src}:${stack.at(-1)?.loc?.sta.lin}:${stack.at(-1)?.loc?.sta.col}`,
256
- });
257
- break;
258
- }
259
- else if (!mFGT.has(stack.at(-1)?.typ) && mFGT.has(prevToken?.typ)) {
260
- success = false;
261
- errors.push({
262
- action: "drop",
263
- node: stream[i],
264
- message: `expected <mf-gt> at ${stack.at(-1)?.loc?.src}:${stack.at(-1)?.loc?.sta.lin}:${stack.at(-1)?.loc?.sta.col}`,
265
- });
266
- break;
267
- }
239
+ // if (stack[stack.length - 3]?.typ !== EnumToken.StartParensTokenType) {
240
+ // success = false;
241
+ // errors.push({
242
+ // action: "drop",
243
+ // node: stream[i],
244
+ // message: `unmatched '(' at ${stream[i]?.[LOC]?.src}:${stream[i]?.[LOC]?.sta.lin}:${stream[i]?.[LOC]?.sta.col}`,
245
+ // });
246
+ // break;
247
+ // }
248
+ // if (!mFLT.has(stack.at(-1)?.typ) && mFLT.has(prevToken?.typ)) {
249
+ // success = false;
250
+ // errors.push({
251
+ // action: "drop",
252
+ // node: stack.at(-1),
253
+ // message: `expected <mf-lt> at ${stack.at(-1)?.[LOC]?.src}:${stack.at(-1)?.[LOC]?.sta.lin}:${stack.at(-1)?.[LOC]?.sta.col}`,
254
+ // });
255
+ // break;
256
+ // } else if (!mFGT.has(stack.at(-1)?.typ) && mFGT.has(prevToken?.typ)) {
257
+ // success = false;
258
+ // errors.push({
259
+ // action: "drop",
260
+ // node: stream[i],
261
+ // message: `expected <mf-gt> at ${stack.at(-1)?.[LOC]?.src}:${stack.at(-1)?.[LOC]?.sta.lin}:${stack.at(-1)?.[LOC]?.sta.col}`,
262
+ // });
263
+ // break;
264
+ // }
268
265
  // const index: number = tokens.indexOf(stack.at(-1)!);
269
266
  // <mf-lt> | <mf-name>
270
267
  const index2 = tokens.indexOf(prevToken);
@@ -274,15 +271,15 @@ function parseMediaqueryList(stream, options) {
274
271
  const right = trimArray(tokens.slice(index + 1, tokens.length - 1));
275
272
  const names = trimArray(tokens.slice(index2 + 1, index));
276
273
  const filteredNames = names.filter((n) => n.typ !== EnumToken.WhitespaceTokenType && n.typ !== EnumToken.CommentTokenType);
277
- if (filteredNames.length !== 1 || filteredNames[0].typ !== EnumToken.IdenTokenType) {
278
- success = false;
279
- errors.push({
280
- action: "drop",
281
- node: stream[i],
282
- message: `expected <mf-name>> at ${filteredNames[0]?.loc?.src}:${filteredNames[0]?.loc?.sta.lin}:${filteredNames[0]?.loc?.sta.col}`,
283
- });
284
- break;
285
- }
274
+ // if (filteredNames.length !== 1 || filteredNames[0].typ !== EnumToken.IdenTokenType) {
275
+ // success = false;
276
+ // errors.push({
277
+ // action: "drop",
278
+ // node: stream[i],
279
+ // message: `expected <mf-name>> at ${filteredNames[0]?.[LOC]?.src}:${filteredNames[0]?.[LOC]?.sta.lin}:${filteredNames[0]?.[LOC]?.sta.col}`,
280
+ // });
281
+ // break;
282
+ // }
286
283
  const name = filteredNames[0].val;
287
284
  const mfInfo = getMFInfo(name);
288
285
  for (const val of [left, right]) {
@@ -295,17 +292,18 @@ function parseMediaqueryList(stream, options) {
295
292
  val[l].val === "calc") {
296
293
  const value = evaluate([val[l]]);
297
294
  if (value.length == 1) {
298
- if (mfInfo?.type === MediaFeatureType.RatioType) {
299
- if (value[0].typ !== EnumToken.NumberTokenType ||
300
- !(value[0].val
301
- ?.typ === EnumToken.FractionTokenType)) {
302
- continue;
303
- }
304
- }
305
- Object.defineProperty(value[0], "loc", {
306
- ...definedPropertySettings,
307
- value: val[l].loc,
308
- });
295
+ // if (mfInfo?.type === MediaFeatureType.RatioType) {
296
+ // if (
297
+ // value[0].typ !== EnumToken.NumberTokenType ||
298
+ // !(
299
+ // ((value[0] as NumberToken).val as FractionToken)
300
+ // ?.typ === EnumToken.FractionTokenType
301
+ // )
302
+ // ) {
303
+ // continue;
304
+ // }
305
+ // }
306
+ value[0][LOC] = val[l][LOC];
309
307
  val[l] = value[0];
310
308
  }
311
309
  }
@@ -313,27 +311,27 @@ function parseMediaqueryList(stream, options) {
313
311
  }
314
312
  }
315
313
  }
316
- let isValidMFValue = isMFValue(name, left, true);
317
- if (isValidMFValue.valid && !isValidMFValue.success) {
318
- success = false;
319
- errors.push({
320
- action: "drop",
321
- node: left[0] ?? prevToken,
322
- message: `${isValidMFValue.isValueAllowed === false ? "invalid <mf-name>" : "expected <mf-value>"} at ${(left[0] ?? prevToken)?.loc?.src}:${(left[0] ?? prevToken)?.loc?.sta.lin}:${(left[0] ?? prevToken)?.loc?.sta.col}`,
323
- });
324
- break;
325
- }
326
- isValidMFValue = isMFValue(name, right, true);
327
- if (isValidMFValue.valid && !isValidMFValue.success) {
328
- success = false;
329
- errors.push({
330
- action: "drop",
331
- node: right[0] ?? stream[i],
332
- location: (right[0] ?? stream[i])?.loc,
333
- message: `${isValidMFValue.isValueAllowed === false ? "invalid <mf-name>" : "expected <mf-value>"} at ${(right[0] ?? stream[i])?.loc?.src}:${(right[0] ?? stream[i])?.loc?.sta.lin}:${(left[0] ?? stream[i])?.loc?.sta.col}`,
334
- });
335
- break;
336
- }
314
+ isMFValue(name, left, true);
315
+ // if (isValidMFValue.valid && !isValidMFValue.success) {
316
+ // success = false;
317
+ // errors.push({
318
+ // action: "drop",
319
+ // node: left[0] ?? prevToken,
320
+ // message: `${isValidMFValue.isValueAllowed === false ? "invalid <mf-name>" : "expected <mf-value>"} at ${(left[0] ?? prevToken)?.[LOC]?.src}:${(left[0] ?? prevToken)?.[LOC]?.sta.lin}:${(left[0] ?? prevToken)?.[LOC]?.sta.col}`,
321
+ // });
322
+ // break;
323
+ // }
324
+ isMFValue(name, right, true);
325
+ // if (isValidMFValue.valid && !isValidMFValue.success) {
326
+ // success = false;
327
+ // errors.push({
328
+ // action: "drop",
329
+ // node: right[0] ?? stream[i],
330
+ // location: (right[0] ?? stream[i])?.[LOC],
331
+ // message: `${isValidMFValue.isValueAllowed === false ? "invalid <mf-name>" : "expected <mf-value>"} at ${(right[0] ?? stream[i])?.[LOC]?.src}:${(right[0] ?? stream[i])?.[LOC]?.sta.lin}:${(left[0] ?? stream[i])?.[LOC]?.sta.col}`,
332
+ // });
333
+ // break;
334
+ // }
337
335
  for (const val of [left, right]) {
338
336
  if (mfInfo?.type === MediaFeatureType.RatioType) {
339
337
  const filteredValues = val.filter((n) => n.typ !== EnumToken.WhitespaceTokenType &&
@@ -352,126 +350,132 @@ function parseMediaqueryList(stream, options) {
352
350
  }
353
351
  }
354
352
  }
355
- tokens.splice(index3 + 1, tokens.length - index3 - 2, Object.defineProperty({
353
+ tokens.splice(index3 + 1, tokens.length - index3 - 2, {
356
354
  typ: EnumToken.MediaRangeQueryTokenType,
357
355
  l: left,
358
356
  val: filteredNames,
359
357
  op1: prevToken,
360
358
  op2: stack.at(-1),
361
359
  r: right,
362
- }, "loc", {
363
- ...definedPropertySettings,
364
- value: { ...left[0].loc, end: right.at(-1).loc.end },
365
- }));
360
+ [LOC]: { ...left[0][LOC], end: right.at(-1)[LOC].end },
361
+ });
366
362
  stack.pop();
367
363
  stack.pop();
368
364
  }
369
- else if (stack[stack.length - 2]?.typ !== EnumToken.StartParensTokenType) {
370
- success = false;
371
- errors.push({
372
- action: "drop",
373
- node: stream[i],
374
- location: stream[i]?.loc,
375
- message: `expected '(' at ${stream[i]?.loc?.src}:${stream[i]?.loc?.sta.lin}:${stream[i]?.loc?.sta.col}`,
376
- });
377
- break;
378
- }
365
+ // else if (stack[stack.length - 2]?.typ !== EnumToken.StartParensTokenType) {
366
+ // success = false;
367
+ // errors.push({
368
+ // action: "drop",
369
+ // node: stream[i],
370
+ // location: stream[i]?.[LOC],
371
+ // message: `expected '(' at ${stream[i]?.[LOC]?.src}:${stream[i]?.[LOC]?.sta.lin}:${stream[i]?.[LOC]?.sta.col}`,
372
+ // });
373
+ // break;
374
+ // }
379
375
  }
380
- if (mFGT.has(stack.at(-1)?.typ) ||
381
- mFLT.has(stack.at(-1)?.typ) ||
382
- stack.at(-1)?.typ === EnumToken.DelimTokenType ||
383
- stack.at(-1)?.typ === EnumToken.ColonTokenType) {
384
- if (stack[stack.length - 2]?.typ !== EnumToken.StartParensTokenType) {
385
- success = false;
386
- errors.push({
387
- action: "drop",
388
- node: stream[i],
389
- location: stream[i]?.loc,
390
- message: `expected '(' at ${(stack[stack.length - 2] ?? tokens[0])?.loc?.src}:${(stack[stack.length - 2] ?? tokens[0])?.loc?.sta.lin}:${(stack[stack.length - 2] ?? tokens[0])?.loc?.sta.col}`,
391
- });
392
- break;
393
- }
376
+ if (stack.length > 0 &&
377
+ (mFGT.has(stack.at(-1)?.typ) ||
378
+ mFLT.has(stack.at(-1)?.typ) ||
379
+ stack.at(-1)?.typ === EnumToken.DelimTokenType ||
380
+ stack.at(-1)?.typ === EnumToken.ColonTokenType)) {
381
+ // if (stack[stack.length - 2]?.typ !== EnumToken.StartParensTokenType) {
382
+ // success = false;
383
+ // errors.push({
384
+ // action: "drop",
385
+ // node: stream[i],
386
+ // location: stream[i]?.[LOC],
387
+ // message: `expected '(' at ${(stack[stack.length - 2] ?? tokens[0])?.[LOC]?.src}:${(stack[stack.length - 2] ?? tokens[0])?.[LOC]?.sta.lin}:${(stack[stack.length - 2] ?? tokens[0])?.[LOC]?.sta.col}`,
388
+ // });
389
+ // break;
390
+ // }
394
391
  const index2 = tokens.indexOf(stack.at(-1));
395
392
  const index3 = tokens.indexOf(stack.at(-2));
396
393
  let names = trimArray(tokens.slice(index3 + 1, index2));
397
394
  let values = trimArray(tokens.slice(index2 + 1, tokens.length - 1));
398
- let swapped = false;
399
- if (stack.at(-1)?.typ !== EnumToken.ColonTokenType) {
400
- const filteredNames = names.filter((n) => n.typ !== EnumToken.WhitespaceTokenType && n.typ !== EnumToken.CommentTokenType);
401
- if (filteredNames.length !== 1 ||
402
- (filteredNames[0].typ !== EnumToken.IdenTokenType &&
403
- filteredNames[0].typ !== EnumToken.DashedIdenTokenType)) {
404
- swapped = true;
405
- }
406
- }
407
- const filteredNames = (swapped ? values : names).filter((n) => n.typ !== EnumToken.WhitespaceTokenType && n.typ !== EnumToken.CommentTokenType);
408
- if (filteredNames.length !== 1 ||
409
- (filteredNames[0].typ !== EnumToken.IdenTokenType &&
410
- filteredNames[0].typ !== EnumToken.DashedIdenTokenType)) {
411
- success = false;
412
- errors.push({
413
- action: "drop",
414
- node: names[0] ?? stack.at(-1),
415
- location: names[0]?.loc,
416
- message: `expected <mf-name> at ${(names[0] ?? stack.at(-1))?.loc?.src}:${(names[0] ?? stack.at(-1))?.loc?.sta.lin}:${(names[0] ?? stack.at(-1))?.loc?.sta.col}`,
417
- });
418
- break;
419
- }
395
+ // if (stack.at(-1)?.typ !== EnumToken.ColonTokenType) {
396
+ // const filteredNames = names.filter(
397
+ // (n) =>
398
+ // n.typ !== EnumToken.WhitespaceTokenType && n.typ !== EnumToken.CommentTokenType,
399
+ // );
400
+ // if (
401
+ // filteredNames.length !== 1 ||
402
+ // (filteredNames[0].typ !== EnumToken.IdenTokenType &&
403
+ // filteredNames[0].typ !== EnumToken.DashedIdenTokenType)
404
+ // ) {
405
+ // swapped = true;
406
+ // }
407
+ // }
408
+ const filteredNames = (names).filter((n) => n.typ !== EnumToken.WhitespaceTokenType && n.typ !== EnumToken.CommentTokenType);
409
+ // if (
410
+ // filteredNames.length !== 1 ||
411
+ // (filteredNames[0].typ !== EnumToken.IdenTokenType &&
412
+ // filteredNames[0].typ !== EnumToken.DashedIdenTokenType)
413
+ // ) {
414
+ // success = false;
415
+ // errors.push({
416
+ // action: "drop",
417
+ // node: names[0] ?? stack.at(-1),
418
+ // location: names[0]?.[LOC],
419
+ // message: `expected <mf-name> at ${(names[0] ?? stack.at(-1))?.[LOC]?.src}:${(names[0] ?? stack.at(-1))?.[LOC]?.sta.lin}:${(names[0] ?? stack.at(-1))?.[LOC]?.sta.col}`,
420
+ // });
421
+ // break;
422
+ // }
420
423
  const name = filteredNames[0].val;
421
- if (!isMFName(name)) {
422
- success = false;
423
- errors.push({
424
- action: "drop",
425
- node: names[0] ?? stack.at(-1),
426
- location: names[0]?.loc ?? stack.at(-1)?.loc,
427
- message: `expected <mf-name> at ${names[0]?.loc?.src}:${names[0]?.loc?.sta.lin}:${names[0]?.loc?.sta.col}`,
428
- });
429
- break;
430
- }
424
+ // if (!isMFName(name)) {
425
+ // success = false;
426
+ // errors.push({
427
+ // action: "drop",
428
+ // node: names[0] ?? stack.at(-1),
429
+ // location: names[0]?.[LOC] ?? stack.at(-1)?.[LOC],
430
+ // message: `expected <mf-name> at ${names[0]?.[LOC]?.src}:${names[0]?.[LOC]?.sta.lin}:${names[0]?.[LOC]?.sta.col}`,
431
+ // });
432
+ // break;
433
+ // }
431
434
  const mfInfo = getMFInfo(name);
432
435
  if (options.computeCalcExpression) {
433
436
  if (mfInfo == null ||
434
437
  (mfInfo.type != MediaFeatureType.KeywordType &&
435
438
  mfInfo.type != MediaFeatureType.StringType)) {
436
- const val = swapped ? names : values;
439
+ const val = values;
437
440
  for (let l = 0; l < val.length; l++) {
438
441
  if (val[l].typ === EnumToken.MathFunctionTokenType &&
439
442
  val[l].val === "calc") {
440
443
  const value = evaluate([val[l]]);
441
444
  if (value.length == 1) {
442
- if (mfInfo?.type === MediaFeatureType.RatioType) {
443
- if (value[0].typ !== EnumToken.NumberTokenType ||
444
- !(value[0].val?.typ ===
445
- EnumToken.FractionTokenType)) {
446
- continue;
447
- }
448
- }
449
- Object.defineProperty(value[0], "loc", {
450
- ...definedPropertySettings,
451
- value: val[l].loc,
452
- });
445
+ // if (mfInfo?.type === MediaFeatureType.RatioType) {
446
+ // if (
447
+ // value[0].typ !== EnumToken.NumberTokenType ||
448
+ // !(
449
+ // ((value[0] as NumberToken).val as FractionToken)?.typ ===
450
+ // EnumToken.FractionTokenType
451
+ // )
452
+ // ) {
453
+ // continue;
454
+ // }
455
+ // }
456
+ value[0][LOC] = val[l][LOC];
453
457
  val[l] = value[0];
454
458
  }
455
459
  }
456
460
  }
457
461
  }
458
462
  }
459
- const mfValue = isMFValue(name, swapped ? names : values, mFGT.has(stack.at(-1)?.typ) ||
463
+ const mfValue = isMFValue(name, values, mFGT.has(stack.at(-1)?.typ) ||
460
464
  mFLT.has(stack.at(-1)?.typ) ||
461
465
  stack.at(-1)?.typ === EnumToken.DelimTokenType);
462
466
  if (!mfValue.success) {
463
467
  success = false;
464
- const arr = swapped ? names : values;
468
+ const arr = values;
465
469
  errors.push({
466
470
  action: "drop",
467
471
  node: arr[0],
468
- location: arr[0]?.loc,
469
- message: `${mfValue.isValueAllowed === false ? "invalid <mf-name>" : "expected <mf-value>"} at ${arr[0]?.loc?.src}:${arr[0]?.loc?.sta.lin}:${arr[0]?.loc?.sta.col}`,
472
+ location: arr[0]?.[LOC],
473
+ message: `${mfValue.isValueAllowed === false ? "invalid <mf-name>" : "expected <mf-value>"} at ${arr[0]?.[LOC]?.src}:${arr[0]?.[LOC]?.sta.lin}:${arr[0]?.[LOC]?.sta.col}`,
470
474
  });
471
475
  break;
472
476
  }
473
477
  if (mfInfo?.type === MediaFeatureType.RatioType) {
474
- const val = swapped ? names : values;
478
+ const val = values;
475
479
  const filteredValues = val.filter((n) => n.typ !== EnumToken.WhitespaceTokenType && n.typ !== EnumToken.CommentTokenType);
476
480
  if (filteredValues.length === 3) {
477
481
  if (options.computeCalcExpression) {
@@ -486,53 +490,52 @@ function parseMediaqueryList(stream, options) {
486
490
  val.splice(0, val.length, ...filteredValues);
487
491
  }
488
492
  }
489
- tokens.splice(index3 + 1, tokens.length - index3 - 2, Object.defineProperty({
493
+ tokens.splice(index3 + 1, tokens.length - index3 - 2, {
490
494
  typ: EnumToken.MediaQueryConditionTokenType,
491
495
  l: names,
492
496
  op: stack.pop(),
493
497
  r: values,
494
- }, "loc", {
495
- ...definedPropertySettings,
496
- value: { ...names[0].loc, end: values.at(-1).loc.end },
497
- }));
498
+ // @ts-expect-error
499
+ [LOC]: { ...names[0][LOC], end: values.at(-1)[LOC].end },
500
+ });
498
501
  }
499
502
  if (stack.length === 0) {
500
503
  success = false;
501
504
  errors.push({
502
505
  action: "drop",
503
506
  node: stream[i],
504
- location: stream[i]?.loc,
505
- message: `unmatched ')' at ${stream[i]?.loc?.src}:${stream[i]?.loc?.sta.lin}:${stream[i]?.loc?.sta.col}`,
507
+ location: stream[i]?.[LOC],
508
+ message: `unmatched ')' at ${stream[i]?.[LOC]?.src}:${stream[i]?.[LOC]?.sta.lin}:${stream[i]?.[LOC]?.sta.col}`,
506
509
  });
507
510
  break;
508
511
  }
509
512
  {
510
513
  const index = tokens.indexOf(stack.at(-1));
511
- tokens[index] = Object.defineProperty({
514
+ tokens[index] = {
512
515
  typ: EnumToken.ParensTokenType,
513
516
  chi: tokens.slice(index + 1, tokens.length - 1),
514
- }, "loc", {
515
- ...definedPropertySettings,
516
- value: { ...tokens[index].loc, end: stream[i].loc.end },
517
- });
517
+ // @ts-expect-error
518
+ [LOC]: { ...tokens[index][LOC], end: stream[i][LOC].end },
519
+ };
518
520
  tokens.length = index + 1;
519
521
  scopes.pop();
520
522
  currentScope = scopes.at(-1);
521
523
  stack.pop();
522
- if (stack.at(-1)?.typ === EnumToken.NotTokenType ||
523
- stack.at(-1)?.typ === EnumToken.OnlyTokenType) {
524
- const index = tokens.indexOf(stack.at(-1));
525
- const slice = trimArray(tokens.slice(index + 1));
526
- tokens[index] = Object.defineProperty({
527
- typ: EnumToken.MediaQueryUnaryFeatureTokenType,
528
- l: stack.pop(),
529
- r: slice,
530
- }, "loc", {
531
- ...definedPropertySettings,
532
- value: { ...tokens[index].loc, end: slice.at(-1).loc.end },
533
- });
534
- tokens.length = index + 1;
535
- }
524
+ // if (
525
+ // stack.at(-1)?.typ === EnumToken.NotTokenType ||
526
+ // stack.at(-1)?.typ === EnumToken.OnlyTokenType
527
+ // ) {
528
+ // const index = tokens.indexOf(stack.at(-1)!);
529
+ // const slice = trimArray(tokens.slice(index + 1));
530
+ // tokens[index] = {
531
+ // typ: EnumToken.MediaQueryUnaryFeatureTokenType,
532
+ // l: stack.pop()!,
533
+ // r: slice,
534
+ // // @ts-expect-error
535
+ // [LOC]: { ...tokens[index][LOC]!, end: slice.at(-1)![LOC]!.end } as Location,
536
+ // };
537
+ // tokens.length = index + 1;
538
+ // }
536
539
  if (stack.at(-1)?.typ === EnumToken.AndTokenType ||
537
540
  stack.at(-1)?.typ === EnumToken.OrTokenType) {
538
541
  const index = tokens.indexOf(stack.at(-1));
@@ -544,15 +547,13 @@ function parseMediaqueryList(stream, options) {
544
547
  }
545
548
  const left = trimArray(tokens.slice(l, index));
546
549
  const right = trimArray(tokens.slice(index + 1));
547
- tokens[l] = Object.defineProperty({
550
+ tokens[l] = {
548
551
  typ: EnumToken.MediaQueryConditionTokenType,
549
552
  op: stack.pop(),
550
553
  l: left,
551
554
  r: right,
552
- }, "loc", {
553
- ...definedPropertySettings,
554
- value: { ...left[0].loc, end: right.at(-1).loc.end },
555
- });
555
+ [LOC]: { ...left[0][LOC], end: right.at(-1)[LOC].end },
556
+ };
556
557
  tokens.length = l + 1;
557
558
  expectAndOrComma = true;
558
559
  }
@@ -563,15 +564,15 @@ function parseMediaqueryList(stream, options) {
563
564
  break;
564
565
  }
565
566
  }
566
- if (success && stack.length > 0) {
567
- success = false;
568
- errors.push({
569
- action: "drop",
570
- node: stack.at(-1),
571
- location: stack.at(-1)?.loc,
572
- message: `unmatched token '${EnumToken[stack.at(-1)?.typ]}' at ${stack.at(-1)?.loc?.src}:${stack.at(-1)?.loc?.sta.lin}:${stack.at(-1)?.loc?.sta.col}`,
573
- });
574
- }
567
+ // if (success && stack.length > 0) {
568
+ // success = false;
569
+ // errors.push({
570
+ // action: "drop",
571
+ // node: stack.at(-1),
572
+ // location: stack.at(-1)?.[LOC],
573
+ // message: `unmatched token '${EnumToken[stack.at(-1)?.typ]}' at ${stack.at(-1)?.[LOC]?.src}:${stack.at(-1)?.[LOC]?.sta.lin}:${stack.at(-1)?.[LOC]?.sta.col}`,
574
+ // });
575
+ // }
575
576
  if (!success) {
576
577
  hasErrors = true;
577
578
  parts.splice(parts.indexOf(stream), 1);