@tbela99/css-parser 0.0.1-rc6 → 0.0.1

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,13 +1,15 @@
1
1
  import { isIdentStart, isWhiteSpace, isIdent, isFunction } from '../parser/utils/syntax.js';
2
2
  import { PropertyList } from '../parser/declaration/list.js';
3
3
  import { eq } from '../parser/utils/eq.js';
4
- import { render } from '../renderer/render.js';
5
- import '../renderer/utils/color.js';
4
+ import { render, renderToken } from '../renderer/render.js';
5
+ import { parseString } from '../parser/parse.js';
6
+ import { replaceCompound } from './expand.js';
7
+ import { walkValues } from './walk.js';
6
8
 
7
9
  const combinators = ['+', '>', '~'];
8
10
  const notEndingWith = ['(', '['].concat(combinators);
9
11
  const definedPropertySettings = { configurable: true, enumerable: false, writable: true };
10
- function minify(ast, options = {}, recursive = false, errors) {
12
+ function minify(ast, options = {}, recursive = false, errors, nestingContent) {
11
13
  function wrapNodes(previous, node, match, ast, i, nodeIndex) {
12
14
  // @ts-ignore
13
15
  let pSel = match.selector1.reduce(reducer, []).join(',');
@@ -296,8 +298,28 @@ function minify(ast, options = {}, recursive = false, errors) {
296
298
  selector2
297
299
  };
298
300
  }
301
+ function fixSelector(node) {
302
+ // @ts-ignore
303
+ if (node.sel.includes('&')) {
304
+ const attributes = parseString(node.sel);
305
+ for (const attr of walkValues(attributes)) {
306
+ if (attr.value.typ == 'Pseudo-class-func' && attr.value.val == ':is') {
307
+ let i = attr.value.chi.length;
308
+ while (i--) {
309
+ if (attr.value.chi[i].typ == 'Literal' && attr.value.chi[i].val == '&') {
310
+ attr.value.chi.splice(i, 1);
311
+ }
312
+ }
313
+ }
314
+ }
315
+ node.sel = attributes.reduce((acc, curr) => acc + renderToken(curr), '');
316
+ }
317
+ }
299
318
  // @ts-ignore
300
319
  if (('chi' in ast) && ast.chi?.length > 0) {
320
+ if (!nestingContent) {
321
+ nestingContent = options.nestingRules && ast.typ == 'Rule';
322
+ }
301
323
  let i = 0;
302
324
  let previous;
303
325
  let node;
@@ -312,7 +334,6 @@ function minify(ast, options = {}, recursive = false, errors) {
312
334
  node = ast.chi[i];
313
335
  // @ts-ignore
314
336
  if (previous == node) {
315
- // console.error('idem!');
316
337
  // @ts-ignore
317
338
  ast.chi.splice(i, 1);
318
339
  i--;
@@ -328,7 +349,6 @@ function minify(ast, options = {}, recursive = false, errors) {
328
349
  i--;
329
350
  continue;
330
351
  }
331
- // console.debug({previous, node});
332
352
  // @ts-ignore
333
353
  if (previous?.typ == 'AtRule' &&
334
354
  previous.nam == node.nam &&
@@ -348,7 +368,7 @@ function minify(ast, options = {}, recursive = false, errors) {
348
368
  minifyRule(node);
349
369
  }
350
370
  else {
351
- minify(node, options, recursive, errors);
371
+ minify(node, options, recursive, errors, nestingContent);
352
372
  }
353
373
  previous = node;
354
374
  nodeIndex = i;
@@ -402,7 +422,7 @@ function minify(ast, options = {}, recursive = false, errors) {
402
422
  nodeIndex = --i;
403
423
  // @ts-ignore
404
424
  previous = ast.chi[nodeIndex];
405
- minify(wrapper, options, recursive, errors);
425
+ minify(wrapper, options, recursive, errors, nestingContent);
406
426
  continue;
407
427
  }
408
428
  // @ts-ignore
@@ -435,7 +455,7 @@ function minify(ast, options = {}, recursive = false, errors) {
435
455
  let wrap = true;
436
456
  // @ts-ignore
437
457
  const selector = node.optimized.selector.reduce((acc, curr) => {
438
- if (curr[0] == '&') {
458
+ if (curr[0] == '&' && curr.length > 1) {
439
459
  if (curr[1] == ' ') {
440
460
  curr.splice(0, 2);
441
461
  }
@@ -459,7 +479,7 @@ function minify(ast, options = {}, recursive = false, errors) {
459
479
  if (!wrap) {
460
480
  wrap = selector.some(s => s[0] != '&');
461
481
  }
462
- const rule = selector.map(s => {
482
+ let rule = selector.map(s => {
463
483
  if (s[0] == '&') {
464
484
  // @ts-ignore
465
485
  s[0] = node.optimized.optimized[0];
@@ -467,7 +487,14 @@ function minify(ast, options = {}, recursive = false, errors) {
467
487
  return s.join('');
468
488
  }).join(',');
469
489
  // @ts-ignore
470
- node.sel = wrap ? node.optimized.optimized[0] + `:is(${rule})` : rule;
490
+ let sel = wrap ? node.optimized.optimized[0] + `:is(${rule})` : rule;
491
+ if (rule.includes('&')) {
492
+ // @ts-ignore
493
+ rule = replaceCompound(rule, node.optimized.optimized[0]);
494
+ }
495
+ if (sel.length < node.sel.length) {
496
+ node.sel = sel;
497
+ }
471
498
  }
472
499
  }
473
500
  // @ts-ignore
@@ -503,7 +530,7 @@ function minify(ast, options = {}, recursive = false, errors) {
503
530
  minifyRule(node);
504
531
  }
505
532
  else {
506
- minify(node, options, recursive, errors);
533
+ minify(node, options, recursive, errors, nestingContent);
507
534
  }
508
535
  i--;
509
536
  previous = node;
@@ -548,7 +575,7 @@ function minify(ast, options = {}, recursive = false, errors) {
548
575
  minifyRule(previous);
549
576
  }
550
577
  else {
551
- minify(previous, options, recursive, errors);
578
+ minify(previous, options, recursive, errors, nestingContent);
552
579
  }
553
580
  }
554
581
  }
@@ -560,11 +587,19 @@ function minify(ast, options = {}, recursive = false, errors) {
560
587
  minifyRule(previous);
561
588
  }
562
589
  else {
563
- minify(previous, options, recursive, errors);
590
+ minify(previous, options, recursive, errors, nestingContent);
564
591
  }
565
592
  }
566
593
  }
567
594
  }
595
+ if (!nestingContent &&
596
+ // @ts-ignore
597
+ previous != null &&
598
+ // previous.optimized != null &&
599
+ previous.typ == 'Rule' &&
600
+ previous.sel.includes('&')) {
601
+ fixSelector(previous);
602
+ }
568
603
  previous = node;
569
604
  nodeIndex = i;
570
605
  }
@@ -577,10 +612,18 @@ function minify(ast, options = {}, recursive = false, errors) {
577
612
  else {
578
613
  // @ts-ignore
579
614
  if (!(node.typ == 'AtRule' && node.nam != 'font-face')) {
580
- minify(node, options, recursive, errors);
615
+ minify(node, options, recursive, errors, nestingContent);
581
616
  }
582
617
  }
583
618
  }
619
+ if (!nestingContent &&
620
+ // @ts-ignore
621
+ node != null &&
622
+ // previous.optimized != null &&
623
+ node.typ == 'Rule' &&
624
+ node.sel.includes('&')) {
625
+ fixSelector(node);
626
+ }
584
627
  }
585
628
  return ast;
586
629
  }
@@ -819,7 +862,6 @@ function reduceRuleSelector(node) {
819
862
  Object.defineProperty(node, 'raw', { ...definedPropertySettings, value: raw });
820
863
  }
821
864
  }
822
- // }
823
865
  }
824
866
 
825
867
  export { combinators, hasDeclaration, minify, minifyRule, reduceSelector, splitRule };
@@ -1,12 +1,8 @@
1
- function* walk(node) {
2
- // @ts-ignore
3
- yield* doWalk(node, null, null);
4
- }
5
- function* doWalk(node, parent, root) {
1
+ function* walk(node, parent, root) {
6
2
  yield { node, parent, root };
7
3
  if ('chi' in node) {
8
4
  for (const child of node.chi) {
9
- yield* doWalk(child, node, (root ?? node));
5
+ yield* walk(child, node, (root ?? node));
10
6
  }
11
7
  }
12
8
  }
@@ -204,7 +204,7 @@ async function parse(iterator, opt = {}) {
204
204
  if (delim.typ == 'Block-start') {
205
205
  const position = map.get(tokens[0]);
206
206
  const uniq = new Map;
207
- parseTokens(tokens, { minify: options.minify }).reduce((acc, curr, index, array) => {
207
+ parseTokens(tokens, { minify: true }).reduce((acc, curr, index, array) => {
208
208
  if (curr.typ == 'Whitespace') {
209
209
  if (trimWhiteSpace.includes(array[index - 1]?.typ) ||
210
210
  trimWhiteSpace.includes(array[index + 1]?.typ) ||
@@ -213,7 +213,7 @@ async function parse(iterator, opt = {}) {
213
213
  return acc;
214
214
  }
215
215
  }
216
- let t = renderToken(curr, { minify: true });
216
+ let t = renderToken(curr, { minify: false });
217
217
  if (t == ',') {
218
218
  acc.push([]);
219
219
  }
@@ -121,12 +121,16 @@ function* tokenize(iterator) {
121
121
  }
122
122
  function next(count = 1) {
123
123
  let char = '';
124
- while (count-- > 0 && ind < iterator.length) {
124
+ let chr = '';
125
+ if (count < 0) {
126
+ return '';
127
+ }
128
+ while (count-- && (chr = iterator.charAt(ind + 1))) {
129
+ char += chr;
125
130
  const codepoint = iterator.charCodeAt(++ind);
126
131
  if (isNaN(codepoint)) {
127
132
  return char;
128
133
  }
129
- char += iterator.charAt(ind);
130
134
  if (isNewLine(codepoint)) {
131
135
  lin++;
132
136
  col = 0;
@@ -27,7 +27,7 @@ function eq(a, b) {
27
27
  }
28
28
  let key;
29
29
  for (key of k1) {
30
- if (!eq(a[key], b[key])) {
30
+ if (!(key in b) || !eq(a[key], b[key])) {
31
31
  return false;
32
32
  }
33
33
  }
@@ -37,7 +37,7 @@ function isColor(token) {
37
37
  if (token.typ == 'Func' && token.chi.length > 0 && colorsFunc.includes(token.val)) {
38
38
  // @ts-ignore
39
39
  for (const v of token.chi) {
40
- if (!['Number', 'Perc', 'Comma', 'Whitespace'].includes(v.typ)) {
40
+ if (!['Number', 'Angle', 'Perc', 'Comma', 'Whitespace', 'Literal'].includes(v.typ)) {
41
41
  return false;
42
42
  }
43
43
  }
@@ -276,7 +276,8 @@ function renderToken(token, options = {}, reducer, errors) {
276
276
  }
277
277
  return val + unit;
278
278
  case 'Perc':
279
- return token.val + '%';
279
+ const perc = reduceNumber(token.val);
280
+ return options.minify && perc == '0' ? '0' : perc + '%';
280
281
  case 'Number':
281
282
  return reduceNumber(token.val);
282
283
  case 'Comment':
@@ -1,6 +1,6 @@
1
1
  export { combinators, hasDeclaration, minify, minifyRule, reduceSelector, splitRule } from '../lib/ast/minify.js';
2
2
  export { walk, walkValues } from '../lib/ast/walk.js';
3
- export { expand } from '../lib/ast/expand.js';
3
+ export { expand, replaceCompound } from '../lib/ast/expand.js';
4
4
  export { colorsFunc, render, renderToken } from '../lib/renderer/render.js';
5
5
  import { parse as parse$1 } from '../lib/parser/parse.js';
6
6
  export { parseString, urlTokenMatcher } from '../lib/parser/parse.js';
package/dist/web/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  export { combinators, hasDeclaration, minify, minifyRule, reduceSelector, splitRule } from '../lib/ast/minify.js';
2
2
  export { walk, walkValues } from '../lib/ast/walk.js';
3
- export { expand } from '../lib/ast/expand.js';
3
+ export { expand, replaceCompound } from '../lib/ast/expand.js';
4
4
  export { colorsFunc, render, renderToken } from '../lib/renderer/render.js';
5
5
  import { parse as parse$1 } from '../lib/parser/parse.js';
6
6
  export { parseString, urlTokenMatcher } from '../lib/parser/parse.js';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tbela99/css-parser",
3
3
  "description": "CSS parser for node and the browser",
4
- "version": "0.0.1-rc6",
4
+ "version": "0.0.1",
5
5
  "exports": {
6
6
  ".": "./dist/node/index.js",
7
7
  "./umd": "./dist/index-umd-web.js",