@tbela99/css-parser 1.0.0 → 1.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.
Files changed (86) hide show
  1. package/CHANGELOG.md +265 -0
  2. package/README.md +16 -11
  3. package/dist/index-umd-web.js +3613 -1829
  4. package/dist/index.cjs +3611 -1827
  5. package/dist/index.d.ts +160 -50
  6. package/dist/lib/ast/expand.js +2 -1
  7. package/dist/lib/ast/features/calc.js +12 -1
  8. package/dist/lib/ast/features/inlinecssvariables.js +47 -24
  9. package/dist/lib/ast/features/prefix.js +117 -86
  10. package/dist/lib/ast/features/shorthand.js +29 -6
  11. package/dist/lib/ast/features/transform.js +10 -3
  12. package/dist/lib/ast/features/type.js +7 -0
  13. package/dist/lib/ast/math/expression.js +7 -1
  14. package/dist/lib/ast/math/math.js +6 -0
  15. package/dist/lib/ast/minify.js +165 -77
  16. package/dist/lib/ast/transform/compute.js +1 -0
  17. package/dist/lib/ast/transform/matrix.js +1 -0
  18. package/dist/lib/ast/types.js +17 -15
  19. package/dist/lib/ast/walk.js +33 -7
  20. package/dist/lib/fs/resolve.js +10 -0
  21. package/dist/lib/parser/declaration/list.js +48 -45
  22. package/dist/lib/parser/declaration/map.js +1 -0
  23. package/dist/lib/parser/declaration/set.js +2 -1
  24. package/dist/lib/parser/parse.js +364 -276
  25. package/dist/lib/parser/tokenize.js +147 -72
  26. package/dist/lib/parser/utils/declaration.js +4 -3
  27. package/dist/lib/parser/utils/type.js +2 -1
  28. package/dist/lib/renderer/color/a98rgb.js +2 -1
  29. package/dist/lib/renderer/color/color-mix.js +10 -7
  30. package/dist/lib/renderer/color/color.js +171 -153
  31. package/dist/lib/renderer/color/hex.js +2 -1
  32. package/dist/lib/renderer/color/hsl.js +2 -1
  33. package/dist/lib/renderer/color/hwb.js +2 -1
  34. package/dist/lib/renderer/color/lab.js +2 -1
  35. package/dist/lib/renderer/color/lch.js +2 -1
  36. package/dist/lib/renderer/color/oklab.js +2 -1
  37. package/dist/lib/renderer/color/oklch.js +2 -1
  38. package/dist/lib/renderer/color/p3.js +2 -1
  39. package/dist/lib/renderer/color/rec2020.js +2 -1
  40. package/dist/lib/renderer/color/relativecolor.js +17 -11
  41. package/dist/lib/renderer/color/rgb.js +4 -3
  42. package/dist/lib/renderer/color/srgb.js +18 -17
  43. package/dist/lib/renderer/color/utils/components.js +6 -5
  44. package/dist/lib/renderer/color/utils/constants.js +47 -3
  45. package/dist/lib/renderer/color/xyz.js +2 -1
  46. package/dist/lib/renderer/color/xyzd50.js +2 -1
  47. package/dist/lib/renderer/render.js +48 -20
  48. package/dist/lib/syntax/syntax.js +253 -140
  49. package/dist/lib/validation/at-rules/container.js +75 -97
  50. package/dist/lib/validation/at-rules/counter-style.js +9 -8
  51. package/dist/lib/validation/at-rules/custom-media.js +13 -15
  52. package/dist/lib/validation/at-rules/document.js +22 -27
  53. package/dist/lib/validation/at-rules/font-feature-values.js +8 -8
  54. package/dist/lib/validation/at-rules/import.js +30 -81
  55. package/dist/lib/validation/at-rules/keyframes.js +18 -22
  56. package/dist/lib/validation/at-rules/layer.js +5 -5
  57. package/dist/lib/validation/at-rules/media.js +42 -52
  58. package/dist/lib/validation/at-rules/namespace.js +19 -23
  59. package/dist/lib/validation/at-rules/page-margin-box.js +15 -18
  60. package/dist/lib/validation/at-rules/page.js +8 -7
  61. package/dist/lib/validation/at-rules/supports.js +73 -82
  62. package/dist/lib/validation/at-rules/when.js +32 -36
  63. package/dist/lib/validation/atrule.js +15 -14
  64. package/dist/lib/validation/config.js +24 -1
  65. package/dist/lib/validation/config.json.js +563 -63
  66. package/dist/lib/validation/parser/parse.js +196 -185
  67. package/dist/lib/validation/parser/types.js +1 -1
  68. package/dist/lib/validation/selector.js +3 -3
  69. package/dist/lib/validation/syntax.js +828 -0
  70. package/dist/lib/validation/syntaxes/complex-selector-list.js +10 -11
  71. package/dist/lib/validation/syntaxes/complex-selector.js +10 -11
  72. package/dist/lib/validation/syntaxes/compound-selector.js +40 -50
  73. package/dist/lib/validation/syntaxes/family-name.js +9 -8
  74. package/dist/lib/validation/syntaxes/keyframe-block-list.js +4 -3
  75. package/dist/lib/validation/syntaxes/keyframe-selector.js +15 -18
  76. package/dist/lib/validation/syntaxes/layer-name.js +6 -5
  77. package/dist/lib/validation/syntaxes/relative-selector-list.js +7 -6
  78. package/dist/lib/validation/syntaxes/relative-selector.js +2 -1
  79. package/dist/lib/validation/syntaxes/url.js +18 -22
  80. package/dist/lib/validation/utils/list.js +2 -1
  81. package/dist/lib/validation/utils/whitespace.js +2 -1
  82. package/dist/node/index.js +4 -2
  83. package/dist/node/load.js +5 -0
  84. package/dist/web/index.js +4 -2
  85. package/dist/web/load.js +5 -0
  86. package/package.json +12 -11
package/dist/index.d.ts CHANGED
@@ -1,3 +1,14 @@
1
+ /**
2
+ * validation level enum
3
+ */
4
+ declare enum ValidationLevel {
5
+ None = 0,
6
+ Default = 1,// selectors + at-rules
7
+ All = 2
8
+ }
9
+ /**
10
+ * token types enum
11
+ */
1
12
  declare enum EnumToken {
2
13
  CommentTokenType = 0,
3
14
  CDOCOMMTokenType = 1,
@@ -93,6 +104,7 @@ declare enum EnumToken {
93
104
  PseudoPageTokenType = 91,
94
105
  PseudoElementTokenType = 92,
95
106
  KeyframeAtRuleNodeType = 93,
107
+ InvalidDeclarationNodeType = 94,
96
108
  Time = 25,
97
109
  Iden = 7,
98
110
  EOF = 48,
@@ -131,7 +143,7 @@ declare enum EnumToken {
131
143
  * @param nestingContent
132
144
  * @param context
133
145
  */
134
- declare function minify(ast: AstNode, options?: ParserOptions | MinifyFeatureOptions, recursive?: boolean, errors?: ErrorDescription[], nestingContent?: boolean, context?: {
146
+ declare function minify(ast: AstNode, options?: ParserOptions, recursive?: boolean, errors?: ErrorDescription[], nestingContent?: boolean, context?: {
135
147
  [key: string]: any;
136
148
  }): AstNode;
137
149
 
@@ -152,7 +164,7 @@ declare enum WalkerValueEvent {
152
164
  */
153
165
  declare function walk(node: AstNode, filter?: WalkerFilter): Generator<WalkResult>;
154
166
  /**
155
- * walk ast values
167
+ * walk ast node value tokens
156
168
  * @param values
157
169
  * @param root
158
170
  * @param filter
@@ -182,8 +194,37 @@ declare function renderToken(token: Token, options?: RenderOptions, cache?: {
182
194
  [key: string]: any;
183
195
  }, reducer?: (acc: string, curr: Token) => string, errors?: ErrorDescription[]): string;
184
196
 
197
+ declare enum ColorKind {
198
+ SYS = 0,
199
+ DPSYS = 1,
200
+ LIT = 2,
201
+ HEX = 3,
202
+ RGB = 4,
203
+ RGBA = 5,
204
+ HSL = 6,
205
+ HSLA = 7,
206
+ HWB = 8,
207
+ DEVICE_CMYK = 9,
208
+ OKLAB = 10,
209
+ OKLCH = 11,
210
+ LAB = 12,
211
+ LCH = 13,
212
+ COLOR = 14,
213
+ SRGB = 15,
214
+ PROPHOTO_RGB = 16,
215
+ A98_RGB = 17,
216
+ REC2020 = 18,
217
+ DISPLAY_P3 = 19,
218
+ SRGB_LINEAR = 20,
219
+ XYZ = 21,
220
+ XYZ_D50 = 22,
221
+ XYZ_D65 = 23,
222
+ LIGHT_DARK = 24,
223
+ COLOR_MIX = 25
224
+ }
225
+
185
226
  /**
186
- * parse string
227
+ * parse css string
187
228
  * @param src
188
229
  * @param options
189
230
  */
@@ -191,7 +232,7 @@ declare function parseString(src: string, options?: {
191
232
  location: boolean;
192
233
  }): Token[];
193
234
  /**
194
- * parse token list
235
+ * parse token array into a tree structure
195
236
  * @param tokens
196
237
  * @param options
197
238
  */
@@ -571,26 +612,6 @@ export declare interface ImportantToken extends BaseToken {
571
612
  typ: EnumToken.ImportantTokenType;
572
613
  }
573
614
 
574
- export declare type ColorKind =
575
- 'sys'
576
- | 'dpsys'
577
- | 'lit'
578
- | 'hex'
579
- | 'rgb'
580
- | 'rgba'
581
- | 'hsl'
582
- | 'hsla'
583
- | 'hwb'
584
- | 'device-cmyk'
585
- | 'oklab'
586
- | 'oklch'
587
- | 'lab'
588
- | 'lch'
589
- | 'color'
590
- | 'light-dark';
591
-
592
- // export declare type HueInterpolationMethod = 'shorter' | 'longer' | 'increasing' | 'decreasing';
593
-
594
615
  export declare interface ColorToken extends BaseToken {
595
616
 
596
617
  typ: EnumToken.ColorTokenType;
@@ -845,6 +866,69 @@ export declare type Token =
845
866
  | AttrToken
846
867
  | EOFToken;
847
868
 
869
+ declare enum ValidationTokenEnum {
870
+ Root = 0,
871
+ Keyword = 1,
872
+ PropertyType = 2,
873
+ DeclarationType = 3,
874
+ AtRule = 4,
875
+ ValidationFunctionDefinition = 5,
876
+ OpenBracket = 6,
877
+ CloseBracket = 7,
878
+ OpenParenthesis = 8,
879
+ CloseParenthesis = 9,
880
+ Comma = 10,
881
+ Pipe = 11,
882
+ Column = 12,
883
+ Star = 13,
884
+ OpenCurlyBrace = 14,
885
+ CloseCurlyBrace = 15,
886
+ HashMark = 16,
887
+ QuestionMark = 17,
888
+ Function = 18,
889
+ Number = 19,
890
+ Whitespace = 20,
891
+ Parenthesis = 21,
892
+ Bracket = 22,
893
+ Block = 23,
894
+ AtLeastOnce = 24,
895
+ Separator = 25,
896
+ Exclamation = 26,
897
+ Ampersand = 27,
898
+ PipeToken = 28,
899
+ ColumnToken = 29,
900
+ AmpersandToken = 30,
901
+ Parens = 31,
902
+ PseudoClassToken = 32,
903
+ PseudoClassFunctionToken = 33,
904
+ StringToken = 34,
905
+ AtRuleDefinition = 35,
906
+ DeclarationNameToken = 36,
907
+ DeclarationDefinitionToken = 37,
908
+ SemiColon = 38,
909
+ Character = 39,
910
+ InfinityToken = 40
911
+ }
912
+ interface Position$1 {
913
+ ind: number;
914
+ lin: number;
915
+ col: number;
916
+ }
917
+ interface ValidationToken {
918
+ typ: ValidationTokenEnum;
919
+ pos: Position$1;
920
+ isList?: boolean;
921
+ text?: string;
922
+ isRepeatable?: boolean;
923
+ atLeastOnce?: boolean;
924
+ isOptional?: boolean;
925
+ isRepeatableGroup?: boolean;
926
+ occurence?: {
927
+ min: number;
928
+ max: number | null;
929
+ };
930
+ }
931
+
848
932
  export declare interface Position {
849
933
 
850
934
  ind: number;
@@ -855,7 +939,7 @@ export declare interface Position {
855
939
  export declare interface Location {
856
940
 
857
941
  sta: Position;
858
- // end: Position;
942
+ end: Position;
859
943
  src: string;
860
944
  }
861
945
 
@@ -865,6 +949,7 @@ export declare interface BaseToken {
865
949
  loc?: Location;
866
950
  tokens?: Token[];
867
951
  parent?: AstRuleList;
952
+ validSyntax?: boolean;
868
953
  }
869
954
 
870
955
  export declare interface AstComment extends BaseToken {
@@ -885,8 +970,8 @@ export declare interface AstRule extends BaseToken {
885
970
  typ: EnumToken.RuleNodeType;
886
971
  sel: string;
887
972
  chi: Array<AstDeclaration | AstComment | AstRuleList>;
888
- optimized?: OptimizedSelector;
889
- raw?: RawSelectorTokens;
973
+ optimized?: OptimizedSelector | null;
974
+ raw?: RawSelectorTokens | null;
890
975
  }
891
976
 
892
977
  export declare interface AstInvalidRule extends BaseToken {
@@ -896,11 +981,10 @@ export declare interface AstInvalidRule extends BaseToken {
896
981
  chi: Array<AstNode>;
897
982
  }
898
983
 
899
- export declare interface AstInvalidAtRule extends BaseToken {
984
+ export declare interface AstInvalidDeclaration extends BaseToken {
900
985
 
901
- typ: EnumToken.InvalidAtRuleTokenType;
902
- val: string;
903
- chi?: Array<AstNode>;
986
+ typ: EnumToken.InvalidDeclarationNodeType;
987
+ val: Array<AstNode>;
904
988
  }
905
989
 
906
990
  export declare interface AstKeyFrameRule extends BaseToken {
@@ -927,14 +1011,14 @@ export declare interface AstAtRule extends BaseToken {
927
1011
  typ: EnumToken.AtRuleNodeType,
928
1012
  nam: string;
929
1013
  val: string;
930
- chi?: Array<AstDeclaration | AstComment> | Array<AstRule | AstComment>
1014
+ chi?: Array<AstDeclaration | AstInvalidDeclaration | AstComment> | Array<AstRule | AstComment>
931
1015
  }
932
1016
 
933
1017
  export declare interface AstKeyframeRule extends BaseToken {
934
1018
 
935
1019
  typ: EnumToken.KeyFrameRuleNodeType;
936
1020
  sel: string;
937
- chi: Array<AstDeclaration | AstComment | AstRuleList>;
1021
+ chi: Array<AstDeclaration | AstInvalidDeclaration | AstComment | AstRuleList>;
938
1022
  optimized?: OptimizedSelector;
939
1023
  raw?: RawSelectorTokens;
940
1024
  }
@@ -949,7 +1033,7 @@ export declare interface AstKeyframAtRule extends BaseToken {
949
1033
 
950
1034
  export declare interface AstRuleList extends BaseToken {
951
1035
 
952
- typ: EnumToken.StyleSheetNodeType | EnumToken.RuleNodeType | EnumToken.AtRuleNodeType | EnumToken.KeyframeAtRuleNodeType | EnumToken.KeyFrameRuleNodeType,
1036
+ typ: EnumToken.StyleSheetNodeType | EnumToken.RuleNodeType | EnumToken.AtRuleNodeType | EnumToken.KeyframeAtRuleNodeType | EnumToken.KeyFrameRuleNodeType | EnumToken.InvalidRuleTokenType | EnumToken.InvalidAtRuleTokenType,
953
1037
  chi: Array<BaseToken | AstComment>;
954
1038
  }
955
1039
 
@@ -968,7 +1052,7 @@ export declare type AstNode =
968
1052
  | AstKeyframAtRule
969
1053
  | AstKeyFrameRule
970
1054
  | AstInvalidRule
971
- | AstInvalidAtRule;
1055
+ | AstInvalidDeclaration;
972
1056
 
973
1057
  /**
974
1058
  * Declaration visitor handler
@@ -1012,6 +1096,11 @@ export declare interface PropertyListOptions {
1012
1096
  computeShorthand?: boolean;
1013
1097
  }
1014
1098
 
1099
+ declare enum FeatureWalkMode {
1100
+ Pre = 0,
1101
+ Post = 1
1102
+ }
1103
+
1015
1104
  export declare type WalkerOption = WalkerOptionEnum | Token | null;
1016
1105
  /**
1017
1106
  * returned value:
@@ -1042,7 +1131,7 @@ export declare interface WalkAttributesResult {
1042
1131
  previousValue: Token | null;
1043
1132
  nextValue: Token | null;
1044
1133
  root?: AstNode;
1045
- parent: FunctionToken | ParensToken | BinaryExpressionToken | null;
1134
+ parent: AstNode | Token | null;
1046
1135
  list: Token[] | null;
1047
1136
  }
1048
1137
 
@@ -1051,19 +1140,21 @@ export declare interface ErrorDescription {
1051
1140
  // drop rule or declaration | fix rule or declaration
1052
1141
  action: 'drop' | 'ignore';
1053
1142
  message: string;
1054
- location?: {
1055
- src: string,
1056
- lin: number,
1057
- col: number;
1058
- };
1143
+ syntax?: string;
1144
+ location?: Location;
1059
1145
  error?: Error;
1060
1146
  rawTokens?: TokenizeResult[];
1061
1147
  }
1062
1148
 
1063
1149
  interface ValidationOptions {
1064
1150
 
1065
- validation?: boolean;
1151
+ validation?: boolean | ValidationLevel;
1066
1152
  lenient?: boolean;
1153
+ visited?: WeakMap<Token, Map<string, Set<ValidationToken>>>;
1154
+ isRepeatable?:boolean | null;
1155
+ isList?:boolean | null;
1156
+ occurence?:boolean | null;
1157
+ atLeastOnce?: boolean | null;
1067
1158
  }
1068
1159
 
1069
1160
  interface MinifyOptions {
@@ -1080,10 +1171,10 @@ interface MinifyOptions {
1080
1171
  pass?: number;
1081
1172
  }
1082
1173
 
1083
- export declare interface ParserOptions extends MinifyOptions, ValidationOptions, PropertyListOptions {
1174
+ export declare interface ParserOptions extends MinifyOptions, MinifyFeatureOptions, ValidationOptions, PropertyListOptions {
1084
1175
 
1085
1176
  src?: string;
1086
- sourcemap?: boolean;
1177
+ sourcemap?: boolean | 'inline';
1087
1178
  removeCharset?: boolean;
1088
1179
  resolveUrls?: boolean;
1089
1180
  resolveImport?: boolean;
@@ -1099,11 +1190,12 @@ export declare interface ParserOptions extends MinifyOptions, ValidationOptions,
1099
1190
  visitor?: VisitorNodeMap;
1100
1191
  signal?: AbortSignal;
1101
1192
  setParent?: boolean;
1193
+ cache?: WeakMap<AstNode, string>;
1102
1194
  }
1103
1195
 
1104
- export declare interface MinifyFeatureOptions extends ParserOptions {
1196
+ export declare interface MinifyFeatureOptions {
1105
1197
 
1106
- features: MinifyFeature[];
1198
+ features?: MinifyFeature[];
1107
1199
  }
1108
1200
 
1109
1201
  export declare interface MinifyFeature {
@@ -1120,10 +1212,12 @@ export declare interface MinifyFeature {
1120
1212
  export declare interface MinifyFeature {
1121
1213
 
1122
1214
  ordering: number;
1215
+ preProcess: boolean;
1216
+ postProcess: boolean;
1123
1217
  register: (options: MinifyFeatureOptions | ParserOptions) => void;
1124
1218
  run: (ast: AstRule | AstAtRule, options: ParserOptions, parent: AstRule | AstAtRule | AstRuleStyleSheet, context: {
1125
1219
  [key: string]: any
1126
- }) => void;
1220
+ }, mode: FeatureWalkMode) => void;
1127
1221
  }
1128
1222
 
1129
1223
  export declare interface ResolvedPath {
@@ -1138,7 +1232,7 @@ export declare interface RenderOptions {
1138
1232
  removeEmpty?: boolean;
1139
1233
  expandNestingRules?: boolean;
1140
1234
  preserveLicense?: boolean;
1141
- sourcemap?: boolean;
1235
+ sourcemap?: boolean | 'inline';
1142
1236
  indent?: string;
1143
1237
  newLine?: string;
1144
1238
  removeComments?: boolean;
@@ -1193,7 +1287,8 @@ export declare interface TokenizeResult {
1193
1287
  token: string;
1194
1288
  len: number;
1195
1289
  hint?: EnumToken;
1196
- position: Position;
1290
+ sta: Position;
1291
+ end: Position;
1197
1292
  bytesIn: number;
1198
1293
  }
1199
1294
 
@@ -1207,12 +1302,27 @@ export declare interface SourceMapObject {
1207
1302
  mappings: string;
1208
1303
  }
1209
1304
 
1305
+ /**
1306
+ * return the directory name of a path
1307
+ * @param path
1308
+ */
1210
1309
  declare function dirname(path: string): string;
1310
+ /**
1311
+ * resolve path
1312
+ * @param url
1313
+ * @param currentDirectory
1314
+ * @param cwd
1315
+ */
1211
1316
  declare function resolve(url: string, currentDirectory: string, cwd?: string): {
1212
1317
  absolute: string;
1213
1318
  relative: string;
1214
1319
  };
1215
1320
 
1321
+ /**
1322
+ * load file
1323
+ * @param url
1324
+ * @param currentFile
1325
+ */
1216
1326
  declare function load(url: string, currentFile?: string): Promise<string>;
1217
1327
 
1218
1328
  /**
@@ -1228,4 +1338,4 @@ declare function parse(iterator: string, opt?: ParserOptions): Promise<ParseResu
1228
1338
  */
1229
1339
  declare function transform(css: string, options?: TransformOptions): Promise<TransformResult>;
1230
1340
 
1231
- export { EnumToken, dirname, expand, load, minify, parse, parseString, parseTokens, render, renderToken, resolve, transform, walk, walkValues };
1341
+ export { EnumToken, ValidationLevel, dirname, expand, load, minify, parse, parseString, parseTokens, render, renderToken, resolve, transform, walk, walkValues };
@@ -1,10 +1,11 @@
1
1
  import { splitRule, combinators } from './minify.js';
2
2
  import { parseString } from '../parser/parse.js';
3
+ import '../parser/tokenize.js';
4
+ import '../parser/utils/config.js';
3
5
  import { EnumToken } from './types.js';
4
6
  import { walkValues } from './walk.js';
5
7
  import { renderToken } from '../renderer/render.js';
6
8
  import '../renderer/color/utils/constants.js';
7
- import '../parser/utils/config.js';
8
9
 
9
10
  /**
10
11
  * expand nested css ast
@@ -2,12 +2,23 @@ import { EnumToken } from '../types.js';
2
2
  import { walkValues, WalkerValueEvent, WalkerOptionEnum } from '../walk.js';
3
3
  import { evaluate } from '../math/expression.js';
4
4
  import { renderToken } from '../../renderer/render.js';
5
+ import '../../renderer/color/utils/constants.js';
6
+ import '../minify.js';
7
+ import '../../parser/parse.js';
8
+ import '../../parser/tokenize.js';
9
+ import '../../parser/utils/config.js';
5
10
  import { mathFuncs } from '../../syntax/syntax.js';
6
11
 
7
12
  class ComputeCalcExpressionFeature {
8
- static get ordering() {
13
+ get ordering() {
9
14
  return 1;
10
15
  }
16
+ get preProcess() {
17
+ return false;
18
+ }
19
+ get postProcess() {
20
+ return true;
21
+ }
11
22
  static register(options) {
12
23
  if (options.computeCalcExpression) {
13
24
  // @ts-ignore
@@ -1,10 +1,35 @@
1
1
  import { EnumToken } from '../types.js';
2
2
  import { walkValues } from '../walk.js';
3
3
  import { renderToken } from '../../renderer/render.js';
4
+ import '../../renderer/color/utils/constants.js';
5
+ import { splitRule } from '../minify.js';
6
+ import '../../parser/parse.js';
7
+ import '../../parser/tokenize.js';
8
+ import '../../parser/utils/config.js';
9
+ import { mathFuncs } from '../../syntax/syntax.js';
4
10
 
11
+ function inlineExpression(token) {
12
+ const result = [];
13
+ if (token.typ == EnumToken.BinaryExpressionTokenType) {
14
+ result.push({
15
+ typ: EnumToken.ParensTokenType,
16
+ chi: [...inlineExpression(token.l), { typ: token.op }, ...inlineExpression(token.r)]
17
+ });
18
+ }
19
+ else {
20
+ result.push(token);
21
+ }
22
+ return result;
23
+ }
5
24
  function replace(node, variableScope) {
6
25
  for (const { value, parent: parentValue } of walkValues(node.val)) {
7
- if (value?.typ == EnumToken.FunctionTokenType && value.val == 'var') {
26
+ if (value.typ == EnumToken.BinaryExpressionTokenType && parentValue != null && 'chi' in parentValue) {
27
+ // @ts-ignore
28
+ parentValue.chi.splice(parentValue.chi.indexOf(value), 1, ...inlineExpression(value));
29
+ }
30
+ }
31
+ for (const { value, parent: parentValue } of walkValues(node.val)) {
32
+ if (value.typ == EnumToken.FunctionTokenType && value.val == 'var') {
8
33
  if (value.chi.length == 1 && value.chi[0].typ == EnumToken.DashedIdenTokenType) {
9
34
  const info = variableScope.get(value.chi[0].val);
10
35
  if (info?.replaceable) {
@@ -26,9 +51,15 @@ function replace(node, variableScope) {
26
51
  }
27
52
  }
28
53
  class InlineCssVariablesFeature {
29
- static get ordering() {
54
+ get ordering() {
30
55
  return 0;
31
56
  }
57
+ get preProcess() {
58
+ return true;
59
+ }
60
+ get postProcess() {
61
+ return false;
62
+ }
32
63
  static register(options) {
33
64
  if (options.inlineCssVariables) {
34
65
  // @ts-ignore
@@ -36,18 +67,19 @@ class InlineCssVariablesFeature {
36
67
  }
37
68
  }
38
69
  run(ast, options = {}, parent, context) {
70
+ if (!('chi' in ast)) {
71
+ return;
72
+ }
39
73
  if (!('variableScope' in context)) {
40
74
  context.variableScope = new Map;
41
75
  }
42
- const isRoot = parent.typ == EnumToken.StyleSheetNodeType && ast.typ == EnumToken.RuleNodeType && [':root', 'html'].includes(ast.sel);
76
+ // [':root', 'html']
77
+ const isRoot = parent.typ == EnumToken.StyleSheetNodeType && ast.typ == EnumToken.RuleNodeType && (ast.raw ?? splitRule(ast.sel)).some(segment => segment.some(s => s == ':root' || s == 'html'));
43
78
  const variableScope = context.variableScope;
44
79
  // @ts-ignore
45
80
  for (const node of ast.chi) {
46
- if (node.typ == EnumToken.CDOCOMMNodeType || node.typ == EnumToken.CommentNodeType) {
47
- continue;
48
- }
49
81
  if (node.typ != EnumToken.DeclarationNodeType) {
50
- break;
82
+ continue;
51
83
  }
52
84
  // css variable
53
85
  if (node.nam.startsWith('--')) {
@@ -58,13 +90,14 @@ class InlineCssVariablesFeature {
58
90
  parent: new Set(),
59
91
  declarationCount: 1,
60
92
  replaceable: isRoot,
61
- node: node
93
+ node: node,
94
+ values: structuredClone(node.val)
62
95
  };
63
96
  info.parent.add(ast);
64
97
  variableScope.set(node.nam, info);
65
98
  let recursive = false;
66
- for (const { value, parent: parentValue } of walkValues(node.val)) {
67
- if (value?.typ == EnumToken.FunctionTokenType && value.val == 'var') {
99
+ for (const { value } of walkValues(node.val)) {
100
+ if (value?.typ == EnumToken.FunctionTokenType && (mathFuncs.includes(value.val) || value.val == 'var')) {
68
101
  recursive = true;
69
102
  break;
70
103
  }
@@ -103,23 +136,13 @@ class InlineCssVariablesFeature {
103
136
  for (const parent of info.parent) {
104
137
  i = parent.chi?.length ?? 0;
105
138
  while (i--) {
106
- if (parent.chi[i].typ == EnumToken.DeclarationNodeType && parent.chi[i].nam == info.node.nam) {
139
+ if (parent.chi[i] == info.node) {
107
140
  // @ts-ignore
108
- parent.chi.splice(i++, 1, {
141
+ parent.chi.splice(i, 1, {
109
142
  typ: EnumToken.CommentTokenType,
110
- val: `/* ${info.node.nam}: ${info.node.val.reduce((acc, curr) => acc + renderToken(curr), '')} */`
143
+ val: `/* ${info.node.nam}: ${info.values.reduce((acc, curr) => acc + renderToken(curr), '')} */`
111
144
  });
112
- }
113
- }
114
- if (parent.chi?.length == 0 && 'parent' in parent) {
115
- // @ts-ignore
116
- for (i = 0; i < parent.parent.chi?.length; i++) {
117
- // @ts-ignore
118
- if (parent.parent.chi[i] == parent) {
119
- // @ts-ignore
120
- parent.parent.chi.splice(i, 1);
121
- break;
122
- }
145
+ break;
123
146
  }
124
147
  }
125
148
  }