@tbela99/css-parser 1.0.0 → 1.1.1-alpha4

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 (87) hide show
  1. package/CHANGELOG.md +269 -0
  2. package/README.md +16 -11
  3. package/dist/index-umd-web.js +3805 -1894
  4. package/dist/index.cjs +3806 -1895
  5. package/dist/index.d.ts +184 -57
  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 +26 -15
  19. package/dist/lib/ast/utils/utils.js +104 -0
  20. package/dist/lib/ast/walk.js +33 -7
  21. package/dist/lib/fs/resolve.js +10 -0
  22. package/dist/lib/parser/declaration/list.js +48 -45
  23. package/dist/lib/parser/declaration/map.js +1 -0
  24. package/dist/lib/parser/declaration/set.js +2 -1
  25. package/dist/lib/parser/parse.js +350 -279
  26. package/dist/lib/parser/tokenize.js +147 -72
  27. package/dist/lib/parser/utils/declaration.js +4 -3
  28. package/dist/lib/parser/utils/type.js +2 -1
  29. package/dist/lib/renderer/color/a98rgb.js +2 -1
  30. package/dist/lib/renderer/color/color-mix.js +10 -7
  31. package/dist/lib/renderer/color/color.js +171 -153
  32. package/dist/lib/renderer/color/hex.js +2 -1
  33. package/dist/lib/renderer/color/hsl.js +2 -1
  34. package/dist/lib/renderer/color/hwb.js +2 -1
  35. package/dist/lib/renderer/color/lab.js +2 -1
  36. package/dist/lib/renderer/color/lch.js +2 -1
  37. package/dist/lib/renderer/color/oklab.js +2 -1
  38. package/dist/lib/renderer/color/oklch.js +2 -1
  39. package/dist/lib/renderer/color/p3.js +2 -1
  40. package/dist/lib/renderer/color/rec2020.js +2 -1
  41. package/dist/lib/renderer/color/relativecolor.js +17 -11
  42. package/dist/lib/renderer/color/rgb.js +4 -3
  43. package/dist/lib/renderer/color/srgb.js +18 -17
  44. package/dist/lib/renderer/color/utils/components.js +6 -5
  45. package/dist/lib/renderer/color/utils/constants.js +47 -3
  46. package/dist/lib/renderer/color/xyz.js +2 -1
  47. package/dist/lib/renderer/color/xyzd50.js +2 -1
  48. package/dist/lib/renderer/render.js +48 -20
  49. package/dist/lib/syntax/syntax.js +257 -140
  50. package/dist/lib/validation/at-rules/container.js +75 -97
  51. package/dist/lib/validation/at-rules/counter-style.js +9 -8
  52. package/dist/lib/validation/at-rules/custom-media.js +13 -15
  53. package/dist/lib/validation/at-rules/document.js +22 -27
  54. package/dist/lib/validation/at-rules/font-feature-values.js +8 -8
  55. package/dist/lib/validation/at-rules/import.js +30 -81
  56. package/dist/lib/validation/at-rules/keyframes.js +18 -22
  57. package/dist/lib/validation/at-rules/layer.js +5 -5
  58. package/dist/lib/validation/at-rules/media.js +42 -52
  59. package/dist/lib/validation/at-rules/namespace.js +19 -23
  60. package/dist/lib/validation/at-rules/page-margin-box.js +15 -18
  61. package/dist/lib/validation/at-rules/page.js +8 -7
  62. package/dist/lib/validation/at-rules/supports.js +73 -82
  63. package/dist/lib/validation/at-rules/when.js +32 -36
  64. package/dist/lib/validation/atrule.js +15 -14
  65. package/dist/lib/validation/config.js +24 -1
  66. package/dist/lib/validation/config.json.js +611 -111
  67. package/dist/lib/validation/parser/parse.js +206 -212
  68. package/dist/lib/validation/parser/types.js +1 -1
  69. package/dist/lib/validation/selector.js +3 -3
  70. package/dist/lib/validation/syntax.js +984 -0
  71. package/dist/lib/validation/syntaxes/complex-selector-list.js +10 -11
  72. package/dist/lib/validation/syntaxes/complex-selector.js +10 -11
  73. package/dist/lib/validation/syntaxes/compound-selector.js +40 -50
  74. package/dist/lib/validation/syntaxes/family-name.js +9 -8
  75. package/dist/lib/validation/syntaxes/keyframe-block-list.js +4 -3
  76. package/dist/lib/validation/syntaxes/keyframe-selector.js +15 -18
  77. package/dist/lib/validation/syntaxes/layer-name.js +6 -5
  78. package/dist/lib/validation/syntaxes/relative-selector-list.js +7 -6
  79. package/dist/lib/validation/syntaxes/relative-selector.js +2 -1
  80. package/dist/lib/validation/syntaxes/url.js +18 -22
  81. package/dist/lib/validation/utils/list.js +2 -1
  82. package/dist/lib/validation/utils/whitespace.js +2 -1
  83. package/dist/node/index.js +4 -2
  84. package/dist/node/load.js +5 -0
  85. package/dist/web/index.js +4 -2
  86. package/dist/web/load.js +5 -0
  87. package/package.json +14 -13
package/dist/index.d.ts CHANGED
@@ -1,3 +1,23 @@
1
+ /**
2
+ * validation level enum
3
+ */
4
+ declare enum ValidationLevel {
5
+ /**
6
+ * disable validation
7
+ */
8
+ None = 0,
9
+ /**
10
+ * validate selectors and at-rules
11
+ */
12
+ Default = 1,// selectors + at-rules
13
+ /**
14
+ * validate selectors, at-rules and declarations
15
+ */
16
+ All = 2
17
+ }
18
+ /**
19
+ * token types enum
20
+ */
1
21
  declare enum EnumToken {
2
22
  CommentTokenType = 0,
3
23
  CDOCOMMTokenType = 1,
@@ -93,6 +113,7 @@ declare enum EnumToken {
93
113
  PseudoPageTokenType = 91,
94
114
  PseudoElementTokenType = 92,
95
115
  KeyframeAtRuleNodeType = 93,
116
+ InvalidDeclarationNodeType = 94,
96
117
  Time = 25,
97
118
  Iden = 7,
98
119
  EOF = 48,
@@ -131,7 +152,7 @@ declare enum EnumToken {
131
152
  * @param nestingContent
132
153
  * @param context
133
154
  */
134
- declare function minify(ast: AstNode, options?: ParserOptions | MinifyFeatureOptions, recursive?: boolean, errors?: ErrorDescription[], nestingContent?: boolean, context?: {
155
+ declare function minify(ast: AstNode, options?: ParserOptions, recursive?: boolean, errors?: ErrorDescription[], nestingContent?: boolean, context?: {
135
156
  [key: string]: any;
136
157
  }): AstNode;
137
158
 
@@ -152,7 +173,7 @@ declare enum WalkerValueEvent {
152
173
  */
153
174
  declare function walk(node: AstNode, filter?: WalkerFilter): Generator<WalkResult>;
154
175
  /**
155
- * walk ast values
176
+ * walk ast node value tokens
156
177
  * @param values
157
178
  * @param root
158
179
  * @param filter
@@ -182,8 +203,37 @@ declare function renderToken(token: Token, options?: RenderOptions, cache?: {
182
203
  [key: string]: any;
183
204
  }, reducer?: (acc: string, curr: Token) => string, errors?: ErrorDescription[]): string;
184
205
 
206
+ declare enum ColorKind {
207
+ SYS = 0,
208
+ DPSYS = 1,
209
+ LIT = 2,
210
+ HEX = 3,
211
+ RGB = 4,
212
+ RGBA = 5,
213
+ HSL = 6,
214
+ HSLA = 7,
215
+ HWB = 8,
216
+ DEVICE_CMYK = 9,
217
+ OKLAB = 10,
218
+ OKLCH = 11,
219
+ LAB = 12,
220
+ LCH = 13,
221
+ COLOR = 14,
222
+ SRGB = 15,
223
+ PROPHOTO_RGB = 16,
224
+ A98_RGB = 17,
225
+ REC2020 = 18,
226
+ DISPLAY_P3 = 19,
227
+ SRGB_LINEAR = 20,
228
+ XYZ = 21,
229
+ XYZ_D50 = 22,
230
+ XYZ_D65 = 23,
231
+ LIGHT_DARK = 24,
232
+ COLOR_MIX = 25
233
+ }
234
+
185
235
  /**
186
- * parse string
236
+ * parse css string
187
237
  * @param src
188
238
  * @param options
189
239
  */
@@ -191,7 +241,7 @@ declare function parseString(src: string, options?: {
191
241
  location: boolean;
192
242
  }): Token[];
193
243
  /**
194
- * parse token list
244
+ * parse token array into a tree structure
195
245
  * @param tokens
196
246
  * @param options
197
247
  */
@@ -571,26 +621,6 @@ export declare interface ImportantToken extends BaseToken {
571
621
  typ: EnumToken.ImportantTokenType;
572
622
  }
573
623
 
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
624
  export declare interface ColorToken extends BaseToken {
595
625
 
596
626
  typ: EnumToken.ColorTokenType;
@@ -845,6 +875,69 @@ export declare type Token =
845
875
  | AttrToken
846
876
  | EOFToken;
847
877
 
878
+ declare enum ValidationTokenEnum {
879
+ Root = 0,
880
+ Keyword = 1,
881
+ PropertyType = 2,
882
+ DeclarationType = 3,
883
+ AtRule = 4,
884
+ ValidationFunctionDefinition = 5,
885
+ OpenBracket = 6,
886
+ CloseBracket = 7,
887
+ OpenParenthesis = 8,
888
+ CloseParenthesis = 9,
889
+ Comma = 10,
890
+ Pipe = 11,
891
+ Column = 12,
892
+ Star = 13,
893
+ OpenCurlyBrace = 14,
894
+ CloseCurlyBrace = 15,
895
+ HashMark = 16,
896
+ QuestionMark = 17,
897
+ Function = 18,
898
+ Number = 19,
899
+ Whitespace = 20,
900
+ Parenthesis = 21,
901
+ Bracket = 22,
902
+ Block = 23,
903
+ AtLeastOnce = 24,
904
+ Separator = 25,
905
+ Exclamation = 26,
906
+ Ampersand = 27,
907
+ PipeToken = 28,
908
+ ColumnToken = 29,
909
+ AmpersandToken = 30,
910
+ Parens = 31,
911
+ PseudoClassToken = 32,
912
+ PseudoClassFunctionToken = 33,
913
+ StringToken = 34,
914
+ AtRuleDefinition = 35,
915
+ DeclarationNameToken = 36,
916
+ DeclarationDefinitionToken = 37,
917
+ SemiColon = 38,
918
+ Character = 39,
919
+ InfinityToken = 40
920
+ }
921
+ interface Position$1 {
922
+ ind: number;
923
+ lin: number;
924
+ col: number;
925
+ }
926
+ interface ValidationToken {
927
+ typ: ValidationTokenEnum;
928
+ pos: Position$1;
929
+ isList?: boolean;
930
+ text?: string;
931
+ isRepeatable?: boolean;
932
+ atLeastOnce?: boolean;
933
+ isOptional?: boolean;
934
+ isRepeatableGroup?: boolean;
935
+ occurence?: {
936
+ min: number;
937
+ max: number | null;
938
+ };
939
+ }
940
+
848
941
  export declare interface Position {
849
942
 
850
943
  ind: number;
@@ -855,7 +948,7 @@ export declare interface Position {
855
948
  export declare interface Location {
856
949
 
857
950
  sta: Position;
858
- // end: Position;
951
+ end: Position;
859
952
  src: string;
860
953
  }
861
954
 
@@ -865,6 +958,7 @@ export declare interface BaseToken {
865
958
  loc?: Location;
866
959
  tokens?: Token[];
867
960
  parent?: AstRuleList;
961
+ validSyntax?: boolean;
868
962
  }
869
963
 
870
964
  export declare interface AstComment extends BaseToken {
@@ -885,8 +979,8 @@ export declare interface AstRule extends BaseToken {
885
979
  typ: EnumToken.RuleNodeType;
886
980
  sel: string;
887
981
  chi: Array<AstDeclaration | AstComment | AstRuleList>;
888
- optimized?: OptimizedSelector;
889
- raw?: RawSelectorTokens;
982
+ optimized?: OptimizedSelector | null;
983
+ raw?: RawSelectorTokens | null;
890
984
  }
891
985
 
892
986
  export declare interface AstInvalidRule extends BaseToken {
@@ -896,11 +990,10 @@ export declare interface AstInvalidRule extends BaseToken {
896
990
  chi: Array<AstNode>;
897
991
  }
898
992
 
899
- export declare interface AstInvalidAtRule extends BaseToken {
993
+ export declare interface AstInvalidDeclaration extends BaseToken {
900
994
 
901
- typ: EnumToken.InvalidAtRuleTokenType;
902
- val: string;
903
- chi?: Array<AstNode>;
995
+ typ: EnumToken.InvalidDeclarationNodeType;
996
+ val: Array<AstNode>;
904
997
  }
905
998
 
906
999
  export declare interface AstKeyFrameRule extends BaseToken {
@@ -927,14 +1020,14 @@ export declare interface AstAtRule extends BaseToken {
927
1020
  typ: EnumToken.AtRuleNodeType,
928
1021
  nam: string;
929
1022
  val: string;
930
- chi?: Array<AstDeclaration | AstComment> | Array<AstRule | AstComment>
1023
+ chi?: Array<AstDeclaration | AstInvalidDeclaration | AstComment> | Array<AstRule | AstComment>
931
1024
  }
932
1025
 
933
1026
  export declare interface AstKeyframeRule extends BaseToken {
934
1027
 
935
1028
  typ: EnumToken.KeyFrameRuleNodeType;
936
1029
  sel: string;
937
- chi: Array<AstDeclaration | AstComment | AstRuleList>;
1030
+ chi: Array<AstDeclaration | AstInvalidDeclaration | AstComment | AstRuleList>;
938
1031
  optimized?: OptimizedSelector;
939
1032
  raw?: RawSelectorTokens;
940
1033
  }
@@ -949,7 +1042,7 @@ export declare interface AstKeyframAtRule extends BaseToken {
949
1042
 
950
1043
  export declare interface AstRuleList extends BaseToken {
951
1044
 
952
- typ: EnumToken.StyleSheetNodeType | EnumToken.RuleNodeType | EnumToken.AtRuleNodeType | EnumToken.KeyframeAtRuleNodeType | EnumToken.KeyFrameRuleNodeType,
1045
+ typ: EnumToken.StyleSheetNodeType | EnumToken.RuleNodeType | EnumToken.AtRuleNodeType | EnumToken.KeyframeAtRuleNodeType | EnumToken.KeyFrameRuleNodeType | EnumToken.InvalidRuleTokenType | EnumToken.InvalidAtRuleTokenType,
953
1046
  chi: Array<BaseToken | AstComment>;
954
1047
  }
955
1048
 
@@ -968,7 +1061,7 @@ export declare type AstNode =
968
1061
  | AstKeyframAtRule
969
1062
  | AstKeyFrameRule
970
1063
  | AstInvalidRule
971
- | AstInvalidAtRule;
1064
+ | AstInvalidDeclaration;
972
1065
 
973
1066
  /**
974
1067
  * Declaration visitor handler
@@ -1012,6 +1105,11 @@ export declare interface PropertyListOptions {
1012
1105
  computeShorthand?: boolean;
1013
1106
  }
1014
1107
 
1108
+ declare enum FeatureWalkMode {
1109
+ Pre = 0,
1110
+ Post = 1
1111
+ }
1112
+
1015
1113
  export declare type WalkerOption = WalkerOptionEnum | Token | null;
1016
1114
  /**
1017
1115
  * returned value:
@@ -1042,7 +1140,7 @@ export declare interface WalkAttributesResult {
1042
1140
  previousValue: Token | null;
1043
1141
  nextValue: Token | null;
1044
1142
  root?: AstNode;
1045
- parent: FunctionToken | ParensToken | BinaryExpressionToken | null;
1143
+ parent: AstNode | Token | null;
1046
1144
  list: Token[] | null;
1047
1145
  }
1048
1146
 
@@ -1051,24 +1149,27 @@ export declare interface ErrorDescription {
1051
1149
  // drop rule or declaration | fix rule or declaration
1052
1150
  action: 'drop' | 'ignore';
1053
1151
  message: string;
1054
- location?: {
1055
- src: string,
1056
- lin: number,
1057
- col: number;
1058
- };
1152
+ syntax?: string;
1153
+ location?: Location;
1059
1154
  error?: Error;
1060
1155
  rawTokens?: TokenizeResult[];
1061
1156
  }
1062
1157
 
1063
1158
  interface ValidationOptions {
1064
1159
 
1065
- validation?: boolean;
1160
+ validation?: boolean | ValidationLevel;
1066
1161
  lenient?: boolean;
1162
+ visited?: WeakMap<Token, Map<string, Set<ValidationToken>>>;
1163
+ isRepeatable?:boolean | null;
1164
+ isList?:boolean | null;
1165
+ occurence?:boolean | null;
1166
+ atLeastOnce?: boolean | null;
1067
1167
  }
1068
1168
 
1069
1169
  interface MinifyOptions {
1070
1170
 
1071
1171
  minify?: boolean;
1172
+ parseColor?: boolean;
1072
1173
  nestingRules?: boolean;
1073
1174
  expandNestingRules?: boolean;
1074
1175
  removeDuplicateDeclarations?: boolean;
@@ -1080,15 +1181,14 @@ interface MinifyOptions {
1080
1181
  pass?: number;
1081
1182
  }
1082
1183
 
1083
- export declare interface ParserOptions extends MinifyOptions, ValidationOptions, PropertyListOptions {
1184
+ export declare interface ParserOptions extends MinifyOptions, MinifyFeatureOptions, ValidationOptions, PropertyListOptions {
1084
1185
 
1085
1186
  src?: string;
1086
- sourcemap?: boolean;
1187
+ sourcemap?: boolean | 'inline';
1087
1188
  removeCharset?: boolean;
1088
1189
  resolveUrls?: boolean;
1089
1190
  resolveImport?: boolean;
1090
1191
  cwd?: string;
1091
- parseColor?: boolean;
1092
1192
  removePrefix?: boolean;
1093
1193
  load?: (url: string, currentUrl: string) => Promise<string>;
1094
1194
  dirname?: (path: string) => string;
@@ -1099,11 +1199,12 @@ export declare interface ParserOptions extends MinifyOptions, ValidationOptions,
1099
1199
  visitor?: VisitorNodeMap;
1100
1200
  signal?: AbortSignal;
1101
1201
  setParent?: boolean;
1202
+ cache?: WeakMap<AstNode, string>;
1102
1203
  }
1103
1204
 
1104
- export declare interface MinifyFeatureOptions extends ParserOptions {
1205
+ export declare interface MinifyFeatureOptions {
1105
1206
 
1106
- features: MinifyFeature[];
1207
+ features?: MinifyFeature[];
1107
1208
  }
1108
1209
 
1109
1210
  export declare interface MinifyFeature {
@@ -1120,10 +1221,12 @@ export declare interface MinifyFeature {
1120
1221
  export declare interface MinifyFeature {
1121
1222
 
1122
1223
  ordering: number;
1224
+ preProcess: boolean;
1225
+ postProcess: boolean;
1123
1226
  register: (options: MinifyFeatureOptions | ParserOptions) => void;
1124
1227
  run: (ast: AstRule | AstAtRule, options: ParserOptions, parent: AstRule | AstAtRule | AstRuleStyleSheet, context: {
1125
1228
  [key: string]: any
1126
- }) => void;
1229
+ }, mode: FeatureWalkMode) => void;
1127
1230
  }
1128
1231
 
1129
1232
  export declare interface ResolvedPath {
@@ -1138,7 +1241,7 @@ export declare interface RenderOptions {
1138
1241
  removeEmpty?: boolean;
1139
1242
  expandNestingRules?: boolean;
1140
1243
  preserveLicense?: boolean;
1141
- sourcemap?: boolean;
1244
+ sourcemap?: boolean | 'inline';
1142
1245
  indent?: string;
1143
1246
  newLine?: string;
1144
1247
  removeComments?: boolean;
@@ -1154,15 +1257,20 @@ export declare interface TransformOptions extends ParserOptions, RenderOptions {
1154
1257
 
1155
1258
  }
1156
1259
 
1260
+ export declare interface ParseResultStats {
1261
+ src: string;
1262
+ bytesIn: number;
1263
+ importedBytesIn: number;
1264
+ parse: string;
1265
+ minify: string;
1266
+ total: string;
1267
+ imports: ParseResultStats[]
1268
+ }
1269
+
1157
1270
  export declare interface ParseResult {
1158
1271
  ast: AstRuleStyleSheet;
1159
1272
  errors: ErrorDescription[];
1160
- stats: {
1161
- bytesIn: number;
1162
- parse: string;
1163
- minify: string;
1164
- total: string;
1165
- }
1273
+ stats: ParseResultStats
1166
1274
  }
1167
1275
 
1168
1276
  export declare interface RenderResult {
@@ -1177,12 +1285,15 @@ export declare interface RenderResult {
1177
1285
  export declare interface TransformResult extends ParseResult, RenderResult {
1178
1286
 
1179
1287
  stats: {
1288
+ src: string;
1180
1289
  bytesIn: number;
1181
1290
  bytesOut: number;
1291
+ importedBytesIn: number;
1182
1292
  parse: string;
1183
1293
  minify: string;
1184
1294
  render: string;
1185
1295
  total: string;
1296
+ imports: ParseResultStats[];
1186
1297
  }
1187
1298
  }
1188
1299
 
@@ -1193,7 +1304,8 @@ export declare interface TokenizeResult {
1193
1304
  token: string;
1194
1305
  len: number;
1195
1306
  hint?: EnumToken;
1196
- position: Position;
1307
+ sta: Position;
1308
+ end: Position;
1197
1309
  bytesIn: number;
1198
1310
  }
1199
1311
 
@@ -1207,12 +1319,27 @@ export declare interface SourceMapObject {
1207
1319
  mappings: string;
1208
1320
  }
1209
1321
 
1322
+ /**
1323
+ * return the directory name of a path
1324
+ * @param path
1325
+ */
1210
1326
  declare function dirname(path: string): string;
1327
+ /**
1328
+ * resolve path
1329
+ * @param url
1330
+ * @param currentDirectory
1331
+ * @param cwd
1332
+ */
1211
1333
  declare function resolve(url: string, currentDirectory: string, cwd?: string): {
1212
1334
  absolute: string;
1213
1335
  relative: string;
1214
1336
  };
1215
1337
 
1338
+ /**
1339
+ * load file
1340
+ * @param url
1341
+ * @param currentFile
1342
+ */
1216
1343
  declare function load(url: string, currentFile?: string): Promise<string>;
1217
1344
 
1218
1345
  /**
@@ -1228,4 +1355,4 @@ declare function parse(iterator: string, opt?: ParserOptions): Promise<ParseResu
1228
1355
  */
1229
1356
  declare function transform(css: string, options?: TransformOptions): Promise<TransformResult>;
1230
1357
 
1231
- export { EnumToken, dirname, expand, load, minify, parse, parseString, parseTokens, render, renderToken, resolve, transform, walk, walkValues };
1358
+ 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
  }