@tbela99/css-parser 0.0.1 → 0.1.0

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.
@@ -1,6 +1,11 @@
1
1
  import { isWhiteSpace, isNewLine, isDigit, isNonPrintable } from './utils/syntax.js';
2
+ import { EnumToken } from '../ast/types.js';
3
+ import '../ast/minify.js';
4
+ import './parse.js';
5
+ import '../renderer/utils/color.js';
6
+ import '../renderer/sourcemap/lib/encode.js';
2
7
 
3
- function* tokenize(iterator) {
8
+ function* tokenize(stream) {
4
9
  let ind = -1;
5
10
  let lin = 1;
6
11
  let col = 0;
@@ -11,9 +16,10 @@ function* tokenize(iterator) {
11
16
  };
12
17
  let value;
13
18
  let buffer = '';
19
+ // let input: string = '';
14
20
  function consumeWhiteSpace() {
15
21
  let count = 0;
16
- while (isWhiteSpace(iterator.charAt(count + ind + 1).charCodeAt(0))) {
22
+ while (isWhiteSpace(stream.charAt(count + ind + 1).charCodeAt(0))) {
17
23
  count++;
18
24
  }
19
25
  next(count);
@@ -80,7 +86,7 @@ function* tokenize(iterator) {
80
86
  }
81
87
  if (value == quote) {
82
88
  buffer += value;
83
- yield pushToken(buffer, hasNewLine ? 'Bad-string' : 'String');
89
+ yield pushToken(buffer, hasNewLine ? EnumToken.BadStringTokenType : EnumToken.StringTokenType);
84
90
  next();
85
91
  // i += value.length;
86
92
  buffer = '';
@@ -90,7 +96,7 @@ function* tokenize(iterator) {
90
96
  hasNewLine = true;
91
97
  }
92
98
  if (hasNewLine && value == ';') {
93
- yield pushToken(buffer + value, 'Bad-string');
99
+ yield pushToken(buffer + value, EnumToken.BadStringTokenType);
94
100
  buffer = '';
95
101
  next();
96
102
  break;
@@ -99,25 +105,25 @@ function* tokenize(iterator) {
99
105
  next();
100
106
  }
101
107
  if (hasNewLine) {
102
- yield pushToken(buffer, 'Bad-string');
108
+ yield pushToken(buffer, EnumToken.BadStringTokenType);
103
109
  }
104
110
  else {
105
111
  // EOF - 'Unclosed-string' fixed
106
- yield pushToken(buffer + quote, 'String');
112
+ yield pushToken(buffer + quote, EnumToken.StringTokenType);
107
113
  }
108
114
  buffer = '';
109
115
  }
110
116
  function peek(count = 1) {
111
117
  if (count == 1) {
112
- return iterator.charAt(ind + 1);
118
+ return stream.charAt(ind + 1);
113
119
  }
114
- return iterator.slice(ind + 1, ind + count + 1);
120
+ return stream.slice(ind + 1, ind + count + 1);
115
121
  }
116
122
  function prev(count = 1) {
117
123
  if (count == 1) {
118
- return ind == 0 ? '' : iterator.charAt(ind - 1);
124
+ return ind == 0 ? '' : stream.charAt(ind - 1);
119
125
  }
120
- return iterator.slice(ind - 1 - count, ind - 1);
126
+ return stream.slice(ind - 1 - count, ind - 1);
121
127
  }
122
128
  function next(count = 1) {
123
129
  let char = '';
@@ -125,9 +131,9 @@ function* tokenize(iterator) {
125
131
  if (count < 0) {
126
132
  return '';
127
133
  }
128
- while (count-- && (chr = iterator.charAt(ind + 1))) {
134
+ while (count-- && (chr = stream.charAt(ind + 1))) {
129
135
  char += chr;
130
- const codepoint = iterator.charCodeAt(++ind);
136
+ const codepoint = stream.charCodeAt(++ind);
131
137
  if (isNaN(codepoint)) {
132
138
  return char;
133
139
  }
@@ -152,7 +158,7 @@ function* tokenize(iterator) {
152
158
  break;
153
159
  }
154
160
  }
155
- yield pushToken('', 'Whitespace');
161
+ yield pushToken('', EnumToken.WhitespaceTokenType);
156
162
  buffer = '';
157
163
  }
158
164
  switch (value) {
@@ -172,7 +178,7 @@ function* tokenize(iterator) {
172
178
  if (value == '*') {
173
179
  buffer += value;
174
180
  if (peek() == '/') {
175
- yield pushToken(buffer + next(), 'Comment');
181
+ yield pushToken(buffer + next(), EnumToken.CommentTokenType);
176
182
  buffer = '';
177
183
  break;
178
184
  }
@@ -181,7 +187,7 @@ function* tokenize(iterator) {
181
187
  buffer += value;
182
188
  }
183
189
  }
184
- yield pushToken(buffer, 'Bad-comment');
190
+ yield pushToken(buffer, EnumToken.BadCommentTokenType);
185
191
  buffer = '';
186
192
  }
187
193
  break;
@@ -191,7 +197,7 @@ function* tokenize(iterator) {
191
197
  buffer = '';
192
198
  }
193
199
  if (peek() == '=') {
194
- yield pushToken('', 'Lte');
200
+ yield pushToken('', EnumToken.LteTokenType);
195
201
  next();
196
202
  break;
197
203
  }
@@ -205,10 +211,10 @@ function* tokenize(iterator) {
205
211
  }
206
212
  }
207
213
  if (value === '') {
208
- yield pushToken(buffer, 'Bad-cdo');
214
+ yield pushToken(buffer, EnumToken.BadCdoTokenType);
209
215
  }
210
216
  else {
211
- yield pushToken(buffer + next(2), 'CDOCOMM');
217
+ yield pushToken(buffer + next(2), EnumToken.CDOCOMMTokenType);
212
218
  }
213
219
  buffer = '';
214
220
  }
@@ -241,7 +247,7 @@ function* tokenize(iterator) {
241
247
  }
242
248
  if (value == '=') {
243
249
  buffer += value;
244
- yield pushToken(buffer, buffer[0] == '~' ? 'Includes' : 'Dash-matches');
250
+ yield pushToken(buffer, buffer[0] == '~' ? EnumToken.IncludesTokenType : EnumToken.DashMatchTokenType);
245
251
  buffer = '';
246
252
  break;
247
253
  }
@@ -257,11 +263,11 @@ function* tokenize(iterator) {
257
263
  buffer = '';
258
264
  }
259
265
  if (peek() == '=') {
260
- yield pushToken('', 'Gte');
266
+ yield pushToken('', EnumToken.GteTokenType);
261
267
  next();
262
268
  }
263
269
  else {
264
- yield pushToken('', 'Gt');
270
+ yield pushToken('', EnumToken.GtTokenType);
265
271
  }
266
272
  consumeWhiteSpace();
267
273
  break;
@@ -275,6 +281,7 @@ function* tokenize(iterator) {
275
281
  buffer += value;
276
282
  break;
277
283
  case '+':
284
+ case '*':
278
285
  case ':':
279
286
  case ',':
280
287
  case '=':
@@ -288,7 +295,7 @@ function* tokenize(iterator) {
288
295
  }
289
296
  yield pushToken(value);
290
297
  buffer = '';
291
- if (value == '+' && isWhiteSpace(peek().charCodeAt(0))) {
298
+ if (['+', '*', '/'].includes(value) && isWhiteSpace(peek().charCodeAt(0))) {
292
299
  yield pushToken(next());
293
300
  }
294
301
  while (isWhiteSpace(peek().charCodeAt(0))) {
@@ -300,7 +307,7 @@ function* tokenize(iterator) {
300
307
  yield pushToken(buffer);
301
308
  buffer = '';
302
309
  }
303
- yield pushToken('', 'End-parens');
310
+ yield pushToken('', EnumToken.EndParensTokenType);
304
311
  break;
305
312
  case '(':
306
313
  if (buffer.length == 0) {
@@ -338,7 +345,7 @@ function* tokenize(iterator) {
338
345
  }
339
346
  }
340
347
  if (value === '') {
341
- yield pushToken(buffer, 'Bad-string');
348
+ yield pushToken(buffer, EnumToken.BadUrlTokenType);
342
349
  buffer = '';
343
350
  break;
344
351
  }
@@ -365,7 +372,7 @@ function* tokenize(iterator) {
365
372
  break;
366
373
  }
367
374
  if (!(value = next())) {
368
- yield pushToken(buffer, hasNewLine ? 'Bad-url-token' : 'Url-token');
375
+ yield pushToken(buffer, hasNewLine ? EnumToken.BadUrlTokenType : EnumToken.UrlTokenTokenType);
369
376
  buffer = '';
370
377
  break;
371
378
  }
@@ -373,8 +380,8 @@ function* tokenize(iterator) {
373
380
  cp = value.charCodeAt(0);
374
381
  // ')'
375
382
  if (cp == 0x29) {
376
- yield pushToken(buffer, hasNewLine ? 'Bad-string' : 'String');
377
- yield pushToken('', 'End-parens');
383
+ yield pushToken(buffer, hasNewLine ? EnumToken.BadStringTokenType : EnumToken.StringTokenType);
384
+ yield pushToken('', EnumToken.EndParensTokenType);
378
385
  buffer = '';
379
386
  break;
380
387
  }
@@ -385,15 +392,15 @@ function* tokenize(iterator) {
385
392
  continue;
386
393
  }
387
394
  if (cp == 0x29) {
388
- yield pushToken(buffer, 'Bad-string');
389
- yield pushToken('', 'End-parens');
395
+ yield pushToken(buffer, EnumToken.BadStringTokenType);
396
+ yield pushToken('', EnumToken.EndParensTokenType);
390
397
  buffer = '';
391
398
  break;
392
399
  }
393
400
  buffer += value;
394
401
  }
395
402
  if (hasNewLine) {
396
- yield pushToken(buffer, 'Bad-string');
403
+ yield pushToken(buffer, EnumToken.BadStringTokenType);
397
404
  buffer = '';
398
405
  }
399
406
  break;
@@ -408,8 +415,8 @@ function* tokenize(iterator) {
408
415
  cp = value.charCodeAt(0);
409
416
  // ')'
410
417
  if (cp == 0x29) {
411
- yield pushToken(buffer, 'Url-token');
412
- yield pushToken('', 'End-parens');
418
+ yield pushToken(buffer, EnumToken.UrlTokenTokenType);
419
+ yield pushToken('', EnumToken.EndParensTokenType);
413
420
  buffer = '';
414
421
  break;
415
422
  }
@@ -445,7 +452,7 @@ function* tokenize(iterator) {
445
452
  }
446
453
  buffer += next();
447
454
  }
448
- yield pushToken(buffer, 'Bad-url-token');
455
+ yield pushToken(buffer, EnumToken.BadUrlTokenType);
449
456
  buffer = '';
450
457
  break;
451
458
  }
@@ -453,7 +460,7 @@ function* tokenize(iterator) {
453
460
  }
454
461
  }
455
462
  if (buffer !== '') {
456
- yield pushToken(buffer, 'Url-token');
463
+ yield pushToken(buffer, EnumToken.UrlTokenTokenType);
457
464
  buffer = '';
458
465
  break;
459
466
  }
@@ -479,7 +486,7 @@ function* tokenize(iterator) {
479
486
  buffer = '';
480
487
  }
481
488
  if (peek(9) == 'important') {
482
- yield pushToken('', 'Important');
489
+ yield pushToken('', EnumToken.ImportantTokenType);
483
490
  next(9);
484
491
  buffer = '';
485
492
  break;
@@ -1,5 +1,8 @@
1
1
  import { colorsFunc } from '../../renderer/render.js';
2
2
  import { COLORS_NAMES } from '../../renderer/utils/color.js';
3
+ import { EnumToken } from '../../ast/types.js';
4
+ import '../../ast/minify.js';
5
+ import '../parse.js';
3
6
 
4
7
  // https://www.w3.org/TR/CSS21/syndata.html#syntax
5
8
  // https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#typedef-ident-token
@@ -27,17 +30,17 @@ function isFrequency(dimension) {
27
30
  return 'unit' in dimension && ['hz', 'khz'].includes(dimension.unit.toLowerCase());
28
31
  }
29
32
  function isColor(token) {
30
- if (token.typ == 'Color') {
33
+ if (token.typ == EnumToken.ColorTokenType) {
31
34
  return true;
32
35
  }
33
- if (token.typ == 'Iden') {
36
+ if (token.typ == EnumToken.IdenTokenType) {
34
37
  // named color
35
38
  return token.val.toLowerCase() in COLORS_NAMES;
36
39
  }
37
- if (token.typ == 'Func' && token.chi.length > 0 && colorsFunc.includes(token.val)) {
40
+ if (token.typ == EnumToken.FunctionTokenType && token.chi.length > 0 && colorsFunc.includes(token.val)) {
38
41
  // @ts-ignore
39
42
  for (const v of token.chi) {
40
- if (!['Number', 'Angle', 'Perc', 'Comma', 'Whitespace', 'Literal'].includes(v.typ)) {
43
+ if (![EnumToken.NumberTokenType, EnumToken.AngleTokenType, EnumToken.PercentageTokenType, EnumToken.CommaTokenType, EnumToken.WhitespaceTokenType, EnumToken.LiteralTokenType].includes(v.typ)) {
41
44
  return false;
42
45
  }
43
46
  }
@@ -206,29 +209,29 @@ function parseDimension(name) {
206
209
  index++;
207
210
  break;
208
211
  }
209
- const dimension = { typ: 'Dimension', val: name.slice(0, index), unit: name.slice(index) };
212
+ const dimension = { typ: EnumToken.DimensionTokenType, val: name.slice(0, index), unit: name.slice(index) };
210
213
  if (isAngle(dimension)) {
211
214
  // @ts-ignore
212
- dimension.typ = 'Angle';
215
+ dimension.typ = EnumToken.AngleTokenType;
213
216
  }
214
217
  else if (isLength(dimension)) {
215
218
  // @ts-ignore
216
- dimension.typ = 'Length';
219
+ dimension.typ = EnumToken.LengthTokenType;
217
220
  }
218
221
  else if (isTime(dimension)) {
219
222
  // @ts-ignore
220
- dimension.typ = 'Time';
223
+ dimension.typ = EnumToken.TimeTokenType;
221
224
  }
222
225
  else if (isResolution(dimension)) {
223
226
  // @ts-ignore
224
- dimension.typ = 'Resolution';
227
+ dimension.typ = EnumToken.ResolutionTokenType;
225
228
  if (dimension.unit == 'dppx') {
226
229
  dimension.unit = 'x';
227
230
  }
228
231
  }
229
232
  else if (isFrequency(dimension)) {
230
233
  // @ts-ignore
231
- dimension.typ = 'Frequency';
234
+ dimension.typ = EnumToken.FrequencyTokenType;
232
235
  }
233
236
  return dimension;
234
237
  }
@@ -1,14 +1,26 @@
1
+ import { EnumToken } from '../../ast/types.js';
2
+ import '../../ast/minify.js';
3
+ import '../parse.js';
4
+ import '../../renderer/utils/color.js';
5
+ import '../../renderer/sourcemap/lib/encode.js';
6
+
1
7
  const funcList = ['clamp', 'calc'];
2
8
  function matchType(val, properties) {
3
- if (val.typ == 'Iden' && properties.keywords.includes(val.val) ||
4
- (properties.types.includes(val.typ))) {
9
+ if (val.typ == EnumToken.IdenTokenType && properties.keywords.includes(val.val) ||
10
+ // @ts-ignore
11
+ (properties.types.some((t) => EnumToken[t] == val.typ))) {
5
12
  return true;
6
13
  }
7
- if (val.typ == 'Number' && val.val == '0') {
8
- return properties.types.some(type => type == 'Length' || type == 'Angle');
14
+ if (val.typ == EnumToken.NumberTokenType && val.val == '0') {
15
+ // @ts-ignore
16
+ return properties.types.some((type) => {
17
+ // @ts-ignore
18
+ const typ = EnumToken[type];
19
+ return typ == EnumToken.LengthTokenType || typ == EnumToken.AngleTokenType;
20
+ });
9
21
  }
10
- if (val.typ == 'Func' && funcList.includes(val.val)) {
11
- return val.chi.every((t => ['Literal', 'Comma', 'Whitespace', 'Start-parens', 'End-parens'].includes(t.typ) || matchType(t, properties)));
22
+ if (val.typ == EnumToken.FunctionTokenType && funcList.includes(val.val)) {
23
+ return val.chi.every((t => [EnumToken.LiteralTokenType, EnumToken.CommaTokenType, EnumToken.WhitespaceTokenType, EnumToken.StartParensTokenType, EnumToken.EndParensTokenType].includes(t.typ) || matchType(t, properties)));
12
24
  }
13
25
  return false;
14
26
  }