@tbela99/css-parser 0.4.1 → 0.5.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.
package/dist/index.d.ts CHANGED
@@ -664,7 +664,7 @@ export declare interface AstDeclaration extends Node {
664
664
  typ: EnumToken.DeclarationNodeType
665
665
  }
666
666
 
667
- export declare interface AstRule$1 extends Node {
667
+ export declare interface AstRule extends Node {
668
668
 
669
669
  typ: EnumToken.RuleNodeType;
670
670
  sel: string;
@@ -682,12 +682,12 @@ export declare interface OptimizedSelector {
682
682
  reducible: boolean;
683
683
  }
684
684
 
685
- export declare interface AstAtRule$1 extends Node {
685
+ export declare interface AstAtRule extends Node {
686
686
 
687
687
  typ: AtRuleNodeType,
688
688
  nam: string;
689
689
  val: string;
690
- chi?: Array<AstDeclaration | AstComment> | Array<AstRule$1 | AstComment>
690
+ chi?: Array<AstDeclaration | AstComment> | Array<AstRule | AstComment>
691
691
  }
692
692
 
693
693
  export declare interface AstRuleList extends Node {
@@ -696,17 +696,17 @@ export declare interface AstRuleList extends Node {
696
696
  chi: Array<Node | AstComment>
697
697
  }
698
698
 
699
- export declare interface AstRuleStyleSheet$1 extends AstRuleList {
699
+ export declare interface AstRuleStyleSheet extends AstRuleList {
700
700
  typ: StyleSheetNodeType,
701
701
  chi: Array<AstRuleList | AstComment>
702
702
  }
703
703
 
704
704
  export declare type AstNode =
705
- AstRuleStyleSheet$1
705
+ AstRuleStyleSheet
706
706
  | AstRuleList
707
707
  | AstComment
708
- | AstAtRule$1
709
- | AstRule$1
708
+ | AstAtRule
709
+ | AstRule
710
710
  | AstDeclaration;
711
711
 
712
712
  /**
@@ -716,12 +716,12 @@ export declare type DeclarationVisitorHandler = (node: AstDeclaration) => AstDec
716
716
  /**
717
717
  * Rule visitor handler
718
718
  */
719
- export declare type RuleVisitorHandler = (node: AstRule$1) => AstRule$1 | AstRule$1[] | null | Promise<AstRule$1 | AstRule$1[] | null>;
719
+ export declare type RuleVisitorHandler = (node: AstRule) => AstRule | AstRule[] | null | Promise<AstRule | AstRule[] | null>;
720
720
 
721
721
  /**
722
722
  * AtRule visitor handler
723
723
  */
724
- export declare type AtRuleVisitorHandler = (node: AstAtRule$1) => AstAtRule$1 | AstAtRule$1[] | null | Promise<AstAtRule$1 | AstAtRule$1[] | null>;
724
+ export declare type AtRuleVisitorHandler = (node: AstAtRule) => AstAtRule | AstAtRule[] | null | Promise<AstAtRule | AstAtRule[] | null>;
725
725
 
726
726
  /**
727
727
  * Value visitor handler
@@ -781,17 +781,6 @@ export declare interface ErrorDescription {
781
781
  error?: Error;
782
782
  }
783
783
 
784
- export declare interface MinifyFeature {
785
-
786
- ordering: number;
787
-
788
- register: (options: MinifyOptions | ParserOptions) => void;
789
- run: (ast: AstRule | AstAtRule, options: ParserOptions = {}, parent: AstRule | AstAtRule | AstRuleStyleSheet, context: {
790
- [key: string]: any
791
- }) => void;
792
- cleanup?: (ast: AstRuleStyleSheet, options: ParserOptions = {}, context: { [key: string]: any }) => void;
793
- }
794
-
795
784
  export declare interface ParserOptions extends PropertyListOptions {
796
785
 
797
786
  minify?: boolean;
@@ -817,6 +806,7 @@ export declare interface ParserOptions extends PropertyListOptions {
817
806
  };
818
807
  visitor?: VisitorNodeMap;
819
808
  signal?: AbortSignal;
809
+ setParent?: boolean;
820
810
  }
821
811
 
822
812
  export declare interface MinifyOptions extends ParserOptions {
@@ -824,6 +814,28 @@ export declare interface MinifyOptions extends ParserOptions {
824
814
  features: MinifyFeature[];
825
815
  }
826
816
 
817
+ export declare interface MinifyFeature {
818
+
819
+ ordering: number;
820
+
821
+ register: (options: MinifyOptions | ParserOptions) => void;
822
+ run: (ast: AstRule | AstAtRule, options: ParserOptions = {}, parent: AstRule | AstAtRule | AstRuleStyleSheet, context: {
823
+ [key: string]: any
824
+ }) => void;
825
+ cleanup?: (ast: AstRuleStyleSheet, options: ParserOptions = {}, context: { [key: string]: any }) => void;
826
+ }
827
+
828
+ export declare interface MinifyFeature {
829
+
830
+ ordering: number;
831
+
832
+ register: (options: MinifyOptions | ParserOptions) => void;
833
+
834
+ run: (ast: AstRule | AstAtRule, options: ParserOptions, parent: AstRule | AstAtRule | AstRuleStyleSheet, context: {
835
+ [key: string]: any
836
+ }) => void;
837
+ }
838
+
827
839
  export declare interface ResolvedPath {
828
840
  absolute: string;
829
841
  relative: string;
@@ -839,6 +851,7 @@ export declare interface RenderOptions {
839
851
  newLine?: string;
840
852
  removeComments?: boolean;
841
853
  convertColor?: boolean;
854
+ withParents?: boolean;
842
855
  output?: string;
843
856
  cwd?: string;
844
857
  load?: (url: string, currentUrl: string) => Promise<string>;
@@ -1,10 +1,8 @@
1
1
  import { EnumToken } from '../types.js';
2
2
  import { walkValues } from '../walk.js';
3
- import { MinifyFeature } from '../utils/minifyfeature.js';
4
- import { IterableWeakSet } from '../../iterable/weakset.js';
5
3
  import { evaluate } from '../math/expression.js';
6
4
 
7
- class ComputeCalcExpressionFeature extends MinifyFeature {
5
+ class ComputeCalcExpressionFeature {
8
6
  static get ordering() {
9
7
  return 1;
10
8
  }
@@ -28,7 +26,7 @@ class ComputeCalcExpressionFeature extends MinifyFeature {
28
26
  if (node.typ != EnumToken.DeclarationNodeType) {
29
27
  continue;
30
28
  }
31
- const set = new IterableWeakSet;
29
+ const set = new Set;
32
30
  for (const { value, parent } of walkValues(node.val)) {
33
31
  if (value != null && value.typ == EnumToken.FunctionTokenType && value.val == 'calc') {
34
32
  if (!set.has(parent)) {
@@ -1,7 +1,5 @@
1
1
  import { EnumToken } from '../types.js';
2
2
  import { walkValues } from '../walk.js';
3
- import { MinifyFeature } from '../utils/minifyfeature.js';
4
- import { IterableWeakSet } from '../../iterable/weakset.js';
5
3
 
6
4
  function replace(node, variableScope) {
7
5
  for (const { value, parent: parentValue } of walkValues(node.val)) {
@@ -26,7 +24,7 @@ function replace(node, variableScope) {
26
24
  }
27
25
  }
28
26
  }
29
- class InlineCssVariablesFeature extends MinifyFeature {
27
+ class InlineCssVariablesFeature {
30
28
  static get ordering() {
31
29
  return 0;
32
30
  }
@@ -61,7 +59,7 @@ class InlineCssVariablesFeature extends MinifyFeature {
61
59
  const info = {
62
60
  globalScope: isRoot,
63
61
  // @ts-ignore
64
- parent: new IterableWeakSet(),
62
+ parent: new Set(),
65
63
  declarationCount: 1,
66
64
  replaceable: isRoot,
67
65
  node: node
@@ -99,6 +97,9 @@ class InlineCssVariablesFeature extends MinifyFeature {
99
97
  }
100
98
  cleanup(ast, options = {}, context) {
101
99
  const variableScope = context.variableScope;
100
+ if (variableScope == null) {
101
+ return;
102
+ }
102
103
  for (const info of variableScope.values()) {
103
104
  if (info.replaceable) {
104
105
  let i;
@@ -4,9 +4,8 @@ import '../minify.js';
4
4
  import '../../parser/parse.js';
5
5
  import '../../renderer/color/utils/constants.js';
6
6
  import '../../renderer/sourcemap/lib/encode.js';
7
- import { MinifyFeature } from '../utils/minifyfeature.js';
8
7
 
9
- class ComputeShorthandFeature extends MinifyFeature {
8
+ class ComputeShorthandFeature {
10
9
  static get ordering() {
11
10
  return 2;
12
11
  }
@@ -18,7 +17,7 @@ class ComputeShorthandFeature extends MinifyFeature {
18
17
  }
19
18
  }
20
19
  // @ts-ignore
21
- options.features.push(new ComputeShorthandFeature());
20
+ options.features.push(new ComputeShorthandFeature(options));
22
21
  }
23
22
  }
24
23
  run(ast, options = {}, parent, context) {
@@ -8,8 +8,8 @@ import { renderToken, doRender } from '../renderer/render.js';
8
8
  import * as index from './features/index.js';
9
9
 
10
10
  const combinators = ['+', '>', '~', '||'];
11
- const notEndingWith = ['(', '['].concat(combinators);
12
11
  const definedPropertySettings = { configurable: true, enumerable: false, writable: true };
12
+ const notEndingWith = ['(', '['].concat(combinators);
13
13
  // @ts-ignore
14
14
  const features = Object.values(index).sort((a, b) => a.ordering - b.ordering);
15
15
  function minify(ast, options = {}, recursive = false, errors, nestingContent, context = {}) {
@@ -892,4 +892,4 @@ function reduceRuleSelector(node) {
892
892
  }
893
893
  }
894
894
 
895
- export { combinators, hasDeclaration, minify, reduceSelector, splitRule };
895
+ export { combinators, definedPropertySettings, hasDeclaration, minify, reduceSelector, splitRule };
@@ -3,9 +3,8 @@ import { EnumToken } from './types.js';
3
3
  function* walk(node, filter) {
4
4
  const parents = [node];
5
5
  const root = node;
6
- const weakMap = new WeakMap;
7
- while (parents.length > 0) {
8
- node = parents.shift();
6
+ const map = new Map;
7
+ while ((node = parents.shift())) {
9
8
  let option = null;
10
9
  if (filter != null) {
11
10
  option = filter(node);
@@ -19,22 +18,21 @@ function* walk(node, filter) {
19
18
  // @ts-ignore
20
19
  if (option !== 'children') {
21
20
  // @ts-ignore
22
- yield { node, parent: weakMap.get(node), root };
21
+ yield { node, parent: map.get(node), root };
23
22
  }
24
23
  if (option !== 'ignore-children' && 'chi' in node) {
25
24
  parents.unshift(...node.chi);
26
25
  for (const child of node.chi.slice()) {
27
- weakMap.set(child, node);
26
+ map.set(child, node);
28
27
  }
29
28
  }
30
29
  }
31
30
  }
32
31
  function* walkValues(values, root = null, filter) {
33
32
  const stack = values.slice();
34
- const weakMap = new WeakMap;
33
+ const map = new Map;
35
34
  let value;
36
- while (stack.length > 0) {
37
- value = stack.shift();
35
+ while ((value = stack.shift())) {
38
36
  let option = null;
39
37
  if (filter != null) {
40
38
  option = filter(value);
@@ -48,17 +46,17 @@ function* walkValues(values, root = null, filter) {
48
46
  // @ts-ignore
49
47
  if (option !== 'children') {
50
48
  // @ts-ignore
51
- yield { value, parent: weakMap.get(value), root };
49
+ yield { value, parent: map.get(value), root };
52
50
  }
53
51
  if (option !== 'ignore-children' && 'chi' in value) {
54
52
  for (const child of value.chi.slice()) {
55
- weakMap.set(child, value);
53
+ map.set(child, value);
56
54
  }
57
55
  stack.unshift(...value.chi);
58
56
  }
59
57
  else if (value.typ == EnumToken.BinaryExpressionTokenType) {
60
- weakMap.set(value.l, value);
61
- weakMap.set(value.r, value);
58
+ map.set(value.l, value);
59
+ map.set(value.r, value);
62
60
  stack.unshift(value.l, value.r);
63
61
  }
64
62
  }
@@ -1,30 +1,40 @@
1
1
  class IterableWeakSet {
2
2
  #weakset = new WeakSet;
3
3
  #set = new Set;
4
- constructor(iterable) {
5
- if (iterable) {
6
- for (const value of iterable) {
7
- const ref = new WeakRef(value);
8
- this.#weakset.add(value);
9
- this.#set.add(ref);
10
- }
11
- }
12
- }
4
+ // constructor(iterable?: Iterable<any>) {
5
+ //
6
+ // if (iterable) {
7
+ //
8
+ // for (const value of iterable) {
9
+ //
10
+ // const ref: WeakRef<any> = new WeakRef(value);
11
+ //
12
+ // this.#weakset.add(value);
13
+ // this.#set.add(ref);
14
+ // }
15
+ // }
16
+ // }
13
17
  has(value) {
14
18
  return this.#weakset.has(value);
15
19
  }
16
- delete(value) {
17
- if (this.#weakset.has(value)) {
18
- for (const ref of this.#set) {
19
- if (ref.deref() === value) {
20
- this.#set.delete(ref);
21
- break;
22
- }
23
- }
24
- return this.#weakset.delete(value);
25
- }
26
- return false;
27
- }
20
+ // delete(value: any): boolean {
21
+ //
22
+ // if (this.#weakset.has(value)) {
23
+ //
24
+ // for (const ref of this.#set) {
25
+ //
26
+ // if (ref.deref() === value) {
27
+ //
28
+ // this.#set.delete(ref);
29
+ // break;
30
+ // }
31
+ // }
32
+ //
33
+ // return this.#weakset.delete(value);
34
+ // }
35
+ //
36
+ // return false;
37
+ // }
28
38
  add(value) {
29
39
  if (!this.#weakset.has(value)) {
30
40
  this.#weakset.add(value);
@@ -12,12 +12,7 @@ class PropertyList {
12
12
  options = { removeDuplicateDeclarations: true, computeShorthand: true };
13
13
  declarations;
14
14
  constructor(options = {}) {
15
- for (const key of Object.keys(this.options)) {
16
- if (key in options) {
17
- // @ts-ignore
18
- this.options[key] = options[key];
19
- }
20
- }
15
+ this.options = options;
21
16
  this.declarations = new Map;
22
17
  }
23
18
  set(nam, value) {
@@ -30,6 +30,7 @@ class PropertyMap {
30
30
  else {
31
31
  const separator = this.config.separator != null ? {
32
32
  ...this.config.separator,
33
+ // @ts-ignore
33
34
  typ: EnumToken[this.config.separator.typ]
34
35
  } : null;
35
36
  // expand shorthand
@@ -39,7 +40,7 @@ class PropertyMap {
39
40
  // @ts-ignore
40
41
  this.declarations.get(this.config.shorthand).val.slice().reduce((acc, curr) => {
41
42
  // @ts-ignore
42
- if (separator != null && separator.typ == curr.typ && eq(separator, curr)) {
43
+ if (separator != null && separator.typ == curr.typ && separator.val == curr.val) {
43
44
  acc.push([]);
44
45
  return acc;
45
46
  }
@@ -78,11 +79,8 @@ class PropertyMap {
78
79
  i--;
79
80
  // @ts-ignore
80
81
  if ('prefix' in props && acc[i]?.typ == EnumToken[props.prefix.typ]) {
81
- if (eq(acc[i], {
82
- ...this.config.properties[property].prefix,
83
- // @ts-ignore
84
- typ: EnumToken[props.prefix.typ]
85
- })) {
82
+ // @ts-ignore
83
+ if (acc[i].typ == EnumToken[props.prefix.typ] && acc[i].val == this.config.properties[property].prefix.val) {
86
84
  acc.splice(i, 1);
87
85
  i--;
88
86
  }
@@ -249,6 +247,7 @@ class PropertyMap {
249
247
  let values = [];
250
248
  // @ts-ignore
251
249
  let typ = (EnumToken[this.config.separator?.typ] ?? EnumToken.CommaTokenType);
250
+ // @ts-ignore
252
251
  let separator = this.config.separator ? renderToken(this.config.separator) : ',';
253
252
  this.matchTypes(declaration);
254
253
  values.push(value);
@@ -328,6 +327,26 @@ class PropertyMap {
328
327
  }
329
328
  return (filtered.length > 0 ? filtered : values)[Symbol.iterator]();
330
329
  }
330
+ for (const declaration of this.declarations.values()) {
331
+ if (declaration instanceof PropertySet) {
332
+ continue;
333
+ }
334
+ const config = declaration.nam == this.config.shorthand ? this.config : this.config.properties[declaration.nam] ?? this.config;
335
+ if (!('mapping' in config)) {
336
+ continue;
337
+ }
338
+ // @ts-ignore
339
+ for (const [key, val] of Object.entries(config.mapping)) {
340
+ const keys = parseString(key);
341
+ if (keys.length != declaration.val.length) {
342
+ continue;
343
+ }
344
+ if (eq(declaration.val, keys)) {
345
+ declaration.val = parseString(val);
346
+ break;
347
+ }
348
+ }
349
+ }
331
350
  // @ts-ignore
332
351
  iterable = this.declarations.values();
333
352
  }
@@ -336,6 +355,7 @@ class PropertyMap {
336
355
  let match;
337
356
  const separator = this.config.separator != null ? {
338
357
  ...this.config.separator,
358
+ // @ts-ignore
339
359
  typ: EnumToken[this.config.separator.typ]
340
360
  } : null;
341
361
  const tokens = {};
@@ -353,7 +373,8 @@ class PropertyMap {
353
373
  for (const declaration of [(properties instanceof PropertySet ? [...properties][0] : properties)]) {
354
374
  // @ts-ignore
355
375
  for (const val of declaration.val) {
356
- if (separator != null && separator.typ == val.typ && eq(separator, val)) {
376
+ // @ts-ignore
377
+ if (separator != null && separator.typ == val.typ && separator.val == val.val) {
357
378
  current++;
358
379
  if (tokens[curr[0]].length == current) {
359
380
  tokens[curr[0]].push([]);
@@ -364,10 +385,11 @@ class PropertyMap {
364
385
  continue;
365
386
  }
366
387
  // @ts-ignore
367
- if (props.multiple && props.separator != null && EnumToken[props.separator.typ] == val.typ && eq({
368
- ...props.separator,
369
- typ: EnumToken[props.separator.typ]
370
- }, val)) {
388
+ if (props.multiple && props.separator != null &&
389
+ // @ts-ignore
390
+ EnumToken[props.separator.typ] == val.typ &&
391
+ // @ts-ignore
392
+ props.separator.val == val.val) {
371
393
  continue;
372
394
  }
373
395
  // @ts-ignore
@@ -472,6 +494,7 @@ class PropertyMap {
472
494
  acc.push({
473
495
  ...((props.separator && {
474
496
  ...props.separator,
497
+ // @ts-ignore
475
498
  typ: EnumToken[props.separator.typ]
476
499
  }) ?? { typ: EnumToken.WhitespaceTokenType })
477
500
  });
@@ -572,6 +595,7 @@ class PropertyMap {
572
595
  if (value[index].typ == EnumToken.WhitespaceTokenType) {
573
596
  continue;
574
597
  }
598
+ // @ts-ignore@
575
599
  if (value[index].typ == EnumToken[config.prefix.typ] &&
576
600
  // @ts-ignore
577
601
  value[index].val == config.prefix.val) {
@@ -68,6 +68,7 @@ class PropertySet {
68
68
  }
69
69
  tokens[current].push(token);
70
70
  }
71
+ // @ts-ignore
71
72
  if (token.typ == EnumToken.LiteralTokenType && token.val == this.config.separator) {
72
73
  tokens.push([]);
73
74
  current++;
@@ -171,6 +172,7 @@ class PropertySet {
171
172
  }
172
173
  }
173
174
  if (acc.length > 0) {
175
+ // @ts-ignore
174
176
  acc.push({ typ: EnumToken.LiteralTokenType, val: this.config.separator });
175
177
  }
176
178
  acc.push(...curr);