@tbela99/css-parser 1.1.1 → 1.3.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/CHANGELOG.md +11 -0
- package/README.md +53 -6
- package/dist/index-umd-web.js +5503 -5037
- package/dist/index.cjs +5510 -5044
- package/dist/index.d.ts +148 -41
- package/dist/lib/ast/expand.js +81 -65
- package/dist/lib/ast/features/calc.js +14 -36
- package/dist/lib/ast/features/inlinecssvariables.js +6 -17
- package/dist/lib/ast/features/prefix.js +22 -19
- package/dist/lib/ast/features/shorthand.js +1 -1
- package/dist/lib/ast/features/transform.js +17 -2
- package/dist/lib/ast/math/expression.js +74 -172
- package/dist/lib/ast/math/math.js +24 -22
- package/dist/lib/ast/minify.js +249 -199
- package/dist/lib/ast/transform/compute.js +18 -41
- package/dist/lib/ast/transform/matrix.js +36 -36
- package/dist/lib/ast/transform/minify.js +37 -59
- package/dist/lib/ast/transform/perspective.js +1 -1
- package/dist/lib/ast/transform/rotate.js +13 -13
- package/dist/lib/ast/transform/scale.js +8 -8
- package/dist/lib/ast/transform/skew.js +4 -4
- package/dist/lib/ast/transform/translate.js +8 -8
- package/dist/lib/ast/transform/utils.js +80 -28
- package/dist/lib/ast/types.js +122 -2
- package/dist/lib/fs/resolve.js +1 -14
- package/dist/lib/parser/declaration/list.js +1 -1
- package/dist/lib/parser/declaration/map.js +1 -1
- package/dist/lib/parser/declaration/set.js +3 -3
- package/dist/lib/parser/parse.js +53 -107
- package/dist/lib/parser/tokenize.js +29 -53
- package/dist/lib/parser/utils/declaration.js +1 -1
- package/dist/lib/parser/utils/type.js +2 -2
- package/dist/lib/renderer/render.js +79 -194
- package/dist/lib/{renderer → syntax}/color/a98rgb.js +2 -2
- package/dist/lib/syntax/color/cmyk.js +104 -0
- package/dist/lib/{renderer → syntax}/color/color-mix.js +31 -33
- package/dist/lib/syntax/color/color.js +581 -0
- package/dist/lib/syntax/color/hex.js +179 -0
- package/dist/lib/syntax/color/hsl.js +201 -0
- package/dist/lib/syntax/color/hwb.js +204 -0
- package/dist/lib/syntax/color/lab.js +262 -0
- package/dist/lib/syntax/color/lch.js +194 -0
- package/dist/lib/syntax/color/oklab.js +237 -0
- package/dist/lib/syntax/color/oklch.js +166 -0
- package/dist/lib/{renderer → syntax}/color/p3.js +3 -3
- package/dist/lib/{renderer → syntax}/color/rec2020.js +11 -11
- package/dist/lib/{renderer → syntax}/color/relativecolor.js +54 -41
- package/dist/lib/syntax/color/rgb.js +140 -0
- package/dist/lib/{renderer → syntax}/color/srgb.js +58 -46
- package/dist/lib/syntax/color/utils/components.js +46 -0
- package/dist/lib/{renderer → syntax}/color/utils/constants.js +6 -33
- package/dist/lib/syntax/color/utils/distance.js +40 -0
- package/dist/lib/{renderer → syntax}/color/xyz.js +27 -14
- package/dist/lib/{renderer → syntax}/color/xyzd50.js +8 -8
- package/dist/lib/syntax/syntax.js +78 -77
- package/dist/lib/syntax/utils.js +70 -0
- package/dist/lib/validation/at-rules/container.js +1 -1
- package/dist/lib/validation/at-rules/counter-style.js +1 -1
- package/dist/lib/validation/at-rules/custom-media.js +1 -1
- package/dist/lib/validation/at-rules/document.js +2 -2
- package/dist/lib/validation/at-rules/font-feature-values.js +2 -2
- package/dist/lib/validation/at-rules/import.js +5 -5
- package/dist/lib/validation/at-rules/keyframes.js +3 -13
- package/dist/lib/validation/at-rules/layer.js +1 -1
- package/dist/lib/validation/at-rules/media.js +1 -1
- package/dist/lib/validation/at-rules/namespace.js +1 -1
- package/dist/lib/validation/at-rules/page-margin-box.js +1 -1
- package/dist/lib/validation/at-rules/page.js +1 -1
- package/dist/lib/validation/at-rules/supports.js +7 -7
- package/dist/lib/validation/at-rules/when.js +1 -1
- package/dist/lib/validation/atrule.js +2 -2
- package/dist/lib/validation/config.js +0 -3
- package/dist/lib/validation/config.json.js +1 -1
- package/dist/lib/validation/parser/parse.js +8 -11
- package/dist/lib/validation/selector.js +1 -9
- package/dist/lib/validation/syntax.js +67 -137
- package/dist/lib/validation/syntaxes/complex-selector-list.js +2 -19
- package/dist/lib/validation/syntaxes/complex-selector.js +1 -1
- package/dist/lib/validation/syntaxes/compound-selector.js +5 -24
- package/dist/lib/validation/syntaxes/family-name.js +5 -40
- package/dist/lib/validation/syntaxes/keyframe-selector.js +3 -22
- package/dist/lib/validation/syntaxes/layer-name.js +1 -1
- package/dist/lib/validation/syntaxes/relative-selector-list.js +1 -25
- package/dist/lib/validation/syntaxes/relative-selector.js +1 -1
- package/dist/lib/validation/syntaxes/url.js +3 -34
- package/dist/lib/validation/utils/list.js +2 -9
- package/dist/lib/validation/utils/whitespace.js +1 -1
- package/dist/node/index.js +4 -2
- package/dist/web/index.js +4 -2
- package/package.json +4 -4
- package/.editorconfig +0 -484
- package/dist/lib/ast/transform/convert.js +0 -33
- package/dist/lib/ast/utils/utils.js +0 -104
- package/dist/lib/renderer/color/color.js +0 -654
- package/dist/lib/renderer/color/hex.js +0 -105
- package/dist/lib/renderer/color/hsl.js +0 -125
- package/dist/lib/renderer/color/hwb.js +0 -103
- package/dist/lib/renderer/color/lab.js +0 -148
- package/dist/lib/renderer/color/lch.js +0 -90
- package/dist/lib/renderer/color/oklab.js +0 -131
- package/dist/lib/renderer/color/oklch.js +0 -75
- package/dist/lib/renderer/color/rgb.js +0 -50
- package/dist/lib/renderer/color/utils/components.js +0 -34
- package/dist/lib/validation/syntaxes/keyframe-block-list.js +0 -28
- package/dist/lib/{renderer → syntax}/color/hsv.js +0 -0
- package/dist/lib/{renderer → syntax}/color/prophotorgb.js +1 -1
- /package/dist/lib/{renderer → syntax}/color/utils/matrix.js +0 -0
package/dist/index.d.ts
CHANGED
|
@@ -142,6 +142,119 @@ declare enum EnumToken {
|
|
|
142
142
|
TimingFunction = 17,
|
|
143
143
|
TimelineFunction = 16
|
|
144
144
|
}
|
|
145
|
+
/**
|
|
146
|
+
* color types enum
|
|
147
|
+
*/
|
|
148
|
+
declare enum ColorType {
|
|
149
|
+
/**
|
|
150
|
+
* system colors
|
|
151
|
+
*/
|
|
152
|
+
SYS = 0,
|
|
153
|
+
/**
|
|
154
|
+
* deprecated system colors
|
|
155
|
+
*/
|
|
156
|
+
DPSYS = 1,
|
|
157
|
+
/**
|
|
158
|
+
* colors as literals
|
|
159
|
+
*/
|
|
160
|
+
LIT = 2,
|
|
161
|
+
/**
|
|
162
|
+
* colors as hex values
|
|
163
|
+
*/
|
|
164
|
+
HEX = 3,
|
|
165
|
+
/**
|
|
166
|
+
* colors as rgb values
|
|
167
|
+
*/
|
|
168
|
+
RGBA = 4,
|
|
169
|
+
/**
|
|
170
|
+
* colors as hsl values
|
|
171
|
+
*/
|
|
172
|
+
HSLA = 5,
|
|
173
|
+
/**
|
|
174
|
+
* colors as hwb values
|
|
175
|
+
*/
|
|
176
|
+
HWB = 6,
|
|
177
|
+
/**
|
|
178
|
+
* colors as cmyk values
|
|
179
|
+
*/
|
|
180
|
+
CMYK = 7,
|
|
181
|
+
/**
|
|
182
|
+
* colors as oklab values
|
|
183
|
+
* */
|
|
184
|
+
OKLAB = 8,
|
|
185
|
+
/**
|
|
186
|
+
* colors as oklch values
|
|
187
|
+
* */
|
|
188
|
+
OKLCH = 9,
|
|
189
|
+
/**
|
|
190
|
+
* colors as lab values
|
|
191
|
+
*/
|
|
192
|
+
LAB = 10,
|
|
193
|
+
/**
|
|
194
|
+
* colors as lch values
|
|
195
|
+
*/
|
|
196
|
+
LCH = 11,
|
|
197
|
+
/**
|
|
198
|
+
* colors using color() function
|
|
199
|
+
*/
|
|
200
|
+
COLOR = 12,
|
|
201
|
+
/**
|
|
202
|
+
* color using srgb values
|
|
203
|
+
*/
|
|
204
|
+
SRGB = 13,
|
|
205
|
+
/**
|
|
206
|
+
* color using prophoto-rgb values
|
|
207
|
+
*/
|
|
208
|
+
PROPHOTO_RGB = 14,
|
|
209
|
+
/**
|
|
210
|
+
* color using a98-rgb values
|
|
211
|
+
*/
|
|
212
|
+
A98_RGB = 15,
|
|
213
|
+
/**
|
|
214
|
+
* color using rec2020 values
|
|
215
|
+
*/
|
|
216
|
+
REC2020 = 16,
|
|
217
|
+
/**
|
|
218
|
+
* color using display-p3 values
|
|
219
|
+
*/
|
|
220
|
+
DISPLAY_P3 = 17,
|
|
221
|
+
/**
|
|
222
|
+
* color using srgb-linear values
|
|
223
|
+
*/
|
|
224
|
+
SRGB_LINEAR = 18,
|
|
225
|
+
/**
|
|
226
|
+
* color using xyz-d50 values
|
|
227
|
+
*/
|
|
228
|
+
XYZ_D50 = 19,
|
|
229
|
+
/**
|
|
230
|
+
* color using xyz-d65 values
|
|
231
|
+
*/
|
|
232
|
+
XYZ_D65 = 20,
|
|
233
|
+
/**
|
|
234
|
+
* light-dark() color function
|
|
235
|
+
*/
|
|
236
|
+
LIGHT_DARK = 21,
|
|
237
|
+
/**
|
|
238
|
+
* color-mix() color function
|
|
239
|
+
*/
|
|
240
|
+
COLOR_MIX = 22,
|
|
241
|
+
/**
|
|
242
|
+
* alias for rgba
|
|
243
|
+
*/
|
|
244
|
+
RGB = 4,
|
|
245
|
+
/**
|
|
246
|
+
* alias for hsl
|
|
247
|
+
*/
|
|
248
|
+
HSL = 5,
|
|
249
|
+
/**
|
|
250
|
+
* alias for xyz-d65
|
|
251
|
+
*/
|
|
252
|
+
XYZ = 20,
|
|
253
|
+
/**
|
|
254
|
+
* alias for cmyk
|
|
255
|
+
*/
|
|
256
|
+
DEVICE_CMYK = 7
|
|
257
|
+
}
|
|
145
258
|
|
|
146
259
|
/**
|
|
147
260
|
* minify ast
|
|
@@ -203,34 +316,19 @@ declare function renderToken(token: Token, options?: RenderOptions, cache?: {
|
|
|
203
316
|
[key: string]: any;
|
|
204
317
|
}, reducer?: (acc: string, curr: Token) => string, errors?: ErrorDescription[]): string;
|
|
205
318
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
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
|
-
}
|
|
319
|
+
/**
|
|
320
|
+
* Calculate the distance between two okLab colors.
|
|
321
|
+
* @param okLab1
|
|
322
|
+
* @param okLab2
|
|
323
|
+
*/
|
|
324
|
+
declare function okLabDistance(okLab1: [number, number, number], okLab2: [number, number, number]): number;
|
|
325
|
+
/**
|
|
326
|
+
* Check if two colors are close.
|
|
327
|
+
* @param color1
|
|
328
|
+
* @param color2
|
|
329
|
+
* @param threshold
|
|
330
|
+
*/
|
|
331
|
+
declare function isOkLabClose(color1: ColorToken, color2: ColorToken, threshold?: number): boolean;
|
|
234
332
|
|
|
235
333
|
/**
|
|
236
334
|
* parse css string
|
|
@@ -312,7 +410,7 @@ export declare interface NestingSelectorToken extends BaseToken {
|
|
|
312
410
|
export declare interface NumberToken extends BaseToken {
|
|
313
411
|
|
|
314
412
|
typ: EnumToken.NumberTokenType,
|
|
315
|
-
val:
|
|
413
|
+
val: number | FractionToken;
|
|
316
414
|
}
|
|
317
415
|
|
|
318
416
|
export declare interface AtRuleToken extends BaseToken {
|
|
@@ -325,13 +423,13 @@ export declare interface AtRuleToken extends BaseToken {
|
|
|
325
423
|
export declare interface PercentageToken extends BaseToken {
|
|
326
424
|
|
|
327
425
|
typ: EnumToken.PercentageTokenType,
|
|
328
|
-
val:
|
|
426
|
+
val: number | FractionToken;
|
|
329
427
|
}
|
|
330
428
|
|
|
331
429
|
export declare interface FlexToken extends BaseToken {
|
|
332
430
|
|
|
333
431
|
typ: EnumToken.FlexTokenType,
|
|
334
|
-
val:
|
|
432
|
+
val: number | FractionToken;
|
|
335
433
|
}
|
|
336
434
|
|
|
337
435
|
export declare interface FunctionToken extends BaseToken {
|
|
@@ -397,42 +495,42 @@ export declare interface UnclosedStringToken extends BaseToken {
|
|
|
397
495
|
export declare interface DimensionToken extends BaseToken {
|
|
398
496
|
|
|
399
497
|
typ: EnumToken.DimensionTokenType;
|
|
400
|
-
val:
|
|
498
|
+
val: number | FractionToken;
|
|
401
499
|
unit: string;
|
|
402
500
|
}
|
|
403
501
|
|
|
404
502
|
export declare interface LengthToken extends BaseToken {
|
|
405
503
|
|
|
406
504
|
typ: EnumToken.LengthTokenType;
|
|
407
|
-
val:
|
|
505
|
+
val: number | FractionToken;
|
|
408
506
|
unit: string;
|
|
409
507
|
}
|
|
410
508
|
|
|
411
509
|
export declare interface AngleToken extends BaseToken {
|
|
412
510
|
|
|
413
511
|
typ: EnumToken.AngleTokenType;
|
|
414
|
-
val:
|
|
512
|
+
val: number | FractionToken;
|
|
415
513
|
unit: string;
|
|
416
514
|
}
|
|
417
515
|
|
|
418
516
|
export declare interface TimeToken extends BaseToken {
|
|
419
517
|
|
|
420
518
|
typ: EnumToken.TimeTokenType;
|
|
421
|
-
val:
|
|
519
|
+
val: number | FractionToken;
|
|
422
520
|
unit: 'ms' | 's';
|
|
423
521
|
}
|
|
424
522
|
|
|
425
523
|
export declare interface FrequencyToken extends BaseToken {
|
|
426
524
|
|
|
427
525
|
typ: EnumToken.FrequencyTokenType;
|
|
428
|
-
val:
|
|
526
|
+
val: number | FractionToken;
|
|
429
527
|
unit: 'Hz' | 'Khz';
|
|
430
528
|
}
|
|
431
529
|
|
|
432
530
|
export declare interface ResolutionToken extends BaseToken {
|
|
433
531
|
|
|
434
532
|
typ: EnumToken.ResolutionTokenType;
|
|
435
|
-
val:
|
|
533
|
+
val: number | FractionToken;
|
|
436
534
|
unit: 'dpi' | 'dpcm' | 'dppx' | 'x';
|
|
437
535
|
}
|
|
438
536
|
|
|
@@ -625,7 +723,7 @@ export declare interface ColorToken extends BaseToken {
|
|
|
625
723
|
|
|
626
724
|
typ: EnumToken.ColorTokenType;
|
|
627
725
|
val: string;
|
|
628
|
-
kin:
|
|
726
|
+
kin: ColorType;
|
|
629
727
|
chi?: Token[];
|
|
630
728
|
/* calculated */
|
|
631
729
|
cal?: 'rel' | 'mix';
|
|
@@ -938,6 +1036,13 @@ interface ValidationToken {
|
|
|
938
1036
|
};
|
|
939
1037
|
}
|
|
940
1038
|
|
|
1039
|
+
/**
|
|
1040
|
+
* Converts a color to another color space
|
|
1041
|
+
* @param token
|
|
1042
|
+
* @param to
|
|
1043
|
+
*/
|
|
1044
|
+
declare function convertColor(token: ColorToken, to: ColorType): ColorToken | null;
|
|
1045
|
+
|
|
941
1046
|
export declare interface Position {
|
|
942
1047
|
|
|
943
1048
|
ind: number;
|
|
@@ -1149,7 +1254,8 @@ export declare interface ErrorDescription {
|
|
|
1149
1254
|
// drop rule or declaration | fix rule or declaration
|
|
1150
1255
|
action: 'drop' | 'ignore';
|
|
1151
1256
|
message: string;
|
|
1152
|
-
syntax?: string;
|
|
1257
|
+
syntax?: string | null;
|
|
1258
|
+
node?: Token | AstNode | null;
|
|
1153
1259
|
location?: Location;
|
|
1154
1260
|
error?: Error;
|
|
1155
1261
|
rawTokens?: TokenizeResult[];
|
|
@@ -1160,6 +1266,7 @@ interface ValidationOptions {
|
|
|
1160
1266
|
validation?: boolean | ValidationLevel;
|
|
1161
1267
|
lenient?: boolean;
|
|
1162
1268
|
visited?: WeakMap<Token, Map<string, Set<ValidationToken>>>;
|
|
1269
|
+
isOptional?:boolean | null;
|
|
1163
1270
|
isRepeatable?:boolean | null;
|
|
1164
1271
|
isList?:boolean | null;
|
|
1165
1272
|
occurence?:boolean | null;
|
|
@@ -1245,7 +1352,7 @@ export declare interface RenderOptions {
|
|
|
1245
1352
|
indent?: string;
|
|
1246
1353
|
newLine?: string;
|
|
1247
1354
|
removeComments?: boolean;
|
|
1248
|
-
convertColor?: boolean;
|
|
1355
|
+
convertColor?: boolean | ColorType;
|
|
1249
1356
|
withParents?: boolean;
|
|
1250
1357
|
output?: string;
|
|
1251
1358
|
cwd?: string;
|
|
@@ -1355,4 +1462,4 @@ declare function parse(iterator: string, opt?: ParserOptions): Promise<ParseResu
|
|
|
1355
1462
|
*/
|
|
1356
1463
|
declare function transform(css: string, options?: TransformOptions): Promise<TransformResult>;
|
|
1357
1464
|
|
|
1358
|
-
export { EnumToken, ValidationLevel, dirname, expand, load, minify, parse, parseString, parseTokens, render, renderToken, resolve, transform, walk, walkValues };
|
|
1465
|
+
export { ColorType, EnumToken, ValidationLevel, convertColor, dirname, expand, isOkLabClose, load, minify, okLabDistance, parse, parseString, parseTokens, render, renderToken, resolve, transform, walk, walkValues };
|
package/dist/lib/ast/expand.js
CHANGED
|
@@ -5,7 +5,7 @@ import '../parser/utils/config.js';
|
|
|
5
5
|
import { EnumToken } from './types.js';
|
|
6
6
|
import { walkValues } from './walk.js';
|
|
7
7
|
import { renderToken } from '../renderer/render.js';
|
|
8
|
-
import '../
|
|
8
|
+
import '../syntax/color/utils/constants.js';
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* expand nested css ast
|
|
@@ -13,18 +13,22 @@ import '../renderer/color/utils/constants.js';
|
|
|
13
13
|
*/
|
|
14
14
|
function expand(ast) {
|
|
15
15
|
//
|
|
16
|
-
if (![EnumToken.RuleNodeType, EnumToken.StyleSheetNodeType, EnumToken.AtRuleNodeType].includes(ast.typ)) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
16
|
+
// if (![EnumToken.RuleNodeType, EnumToken.StyleSheetNodeType, EnumToken.AtRuleNodeType].includes(ast.typ)) {
|
|
17
|
+
//
|
|
18
|
+
// return ast;
|
|
19
|
+
// }
|
|
20
|
+
// if (EnumToken.RuleNodeType == ast.typ) {
|
|
21
|
+
//
|
|
22
|
+
// return <AstRuleStyleSheet>{
|
|
23
|
+
// typ: EnumToken.StyleSheetNodeType,
|
|
24
|
+
// chi: expandRule(<AstRule>ast)
|
|
25
|
+
// }
|
|
26
|
+
// }
|
|
27
|
+
//
|
|
28
|
+
// if (!('chi' in ast)) {
|
|
29
|
+
//
|
|
30
|
+
// return ast;
|
|
31
|
+
// }
|
|
28
32
|
const result = { ...ast, chi: [] };
|
|
29
33
|
// @ts-ignore
|
|
30
34
|
for (let i = 0; i < ast.chi.length; i++) {
|
|
@@ -48,10 +52,11 @@ function expand(ast) {
|
|
|
48
52
|
// @ts-ignore
|
|
49
53
|
result.chi.push({ ...(hasRule ? expand(node) : node) });
|
|
50
54
|
}
|
|
51
|
-
else {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
+
// else {
|
|
56
|
+
//
|
|
57
|
+
// // @ts-ignore
|
|
58
|
+
// result.chi.push(node);
|
|
59
|
+
// }
|
|
55
60
|
}
|
|
56
61
|
return result;
|
|
57
62
|
}
|
|
@@ -65,24 +70,26 @@ function expandRule(node) {
|
|
|
65
70
|
const rule = ast.chi[i];
|
|
66
71
|
if (!rule.sel.includes('&')) {
|
|
67
72
|
const selRule = splitRule(rule.sel);
|
|
68
|
-
if (selRule.length > 1) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
selRule.forEach(arr => combinators.includes(arr[0].charAt(0)) ? arr.unshift(arSelf) : arr.unshift(arSelf, ' '));
|
|
81
|
-
rule.sel = selRule.reduce((acc, curr) => {
|
|
82
|
-
acc.push(curr.join(''));
|
|
83
|
-
return acc;
|
|
84
|
-
}, []).join(',');
|
|
73
|
+
// if (selRule.length > 1) {
|
|
74
|
+
//
|
|
75
|
+
// const r: string = ':is(' + selRule.map(a => a.join('')).join(',') + ')';
|
|
76
|
+
// rule.sel = splitRule(ast.sel).reduce((a: string[], b: string[]): string[] => a.concat([b.join('') + r]), <string[]>[]).join(',');
|
|
77
|
+
//
|
|
78
|
+
// }
|
|
79
|
+
// else {
|
|
80
|
+
// selRule = splitRule(selRule.reduce((acc, curr) => acc + (acc.length > 0 ? ',' : '') + curr.join(''), ''));
|
|
81
|
+
const arSelf = splitRule(ast.sel).filter((r) => r.every((t) => t != ':before' && t != ':after' && !t.startsWith('::'))).reduce((acc, curr) => acc.concat([curr.join('')]), []).join(',');
|
|
82
|
+
if (arSelf.length == 0) {
|
|
83
|
+
ast.chi.splice(i--, 1);
|
|
84
|
+
continue;
|
|
85
85
|
}
|
|
86
|
+
//
|
|
87
|
+
selRule.forEach(arr => combinators.includes(arr[0].charAt(0)) ? arr.unshift(arSelf) : arr.unshift(arSelf, ' '));
|
|
88
|
+
rule.sel = selRule.reduce((acc, curr) => {
|
|
89
|
+
acc.push(curr.join(''));
|
|
90
|
+
return acc;
|
|
91
|
+
}, []).join(',');
|
|
92
|
+
// }
|
|
86
93
|
}
|
|
87
94
|
else {
|
|
88
95
|
let childSelectorCompound = [];
|
|
@@ -108,9 +115,10 @@ function expandRule(node) {
|
|
|
108
115
|
if (s == '&' || parentSelector) {
|
|
109
116
|
withCompound.push(s);
|
|
110
117
|
}
|
|
111
|
-
else {
|
|
112
|
-
|
|
113
|
-
|
|
118
|
+
// else {
|
|
119
|
+
//
|
|
120
|
+
// withoutCompound.push(s.slice(1));
|
|
121
|
+
// }
|
|
114
122
|
}
|
|
115
123
|
}
|
|
116
124
|
else {
|
|
@@ -121,9 +129,10 @@ function expandRule(node) {
|
|
|
121
129
|
withCompound.push(s);
|
|
122
130
|
}
|
|
123
131
|
}
|
|
124
|
-
else {
|
|
125
|
-
|
|
126
|
-
|
|
132
|
+
// else {
|
|
133
|
+
//
|
|
134
|
+
// withoutCompound.push(s);
|
|
135
|
+
// }
|
|
127
136
|
}
|
|
128
137
|
const selectors = [];
|
|
129
138
|
const selector = rules.length > 1 ? ':is(' + rules.map(a => a.join('')).join(',') + ')' : rules[0].join('');
|
|
@@ -155,11 +164,13 @@ function expandRule(node) {
|
|
|
155
164
|
if (withCompound.length == 1) {
|
|
156
165
|
selectors.push(replaceCompound(withCompound[0], selector));
|
|
157
166
|
}
|
|
158
|
-
else {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
167
|
+
// else {
|
|
168
|
+
//
|
|
169
|
+
// for (const w of withCompound) {
|
|
170
|
+
//
|
|
171
|
+
// selectors.push(replaceCompound(w, selector));
|
|
172
|
+
// }
|
|
173
|
+
// }
|
|
163
174
|
}
|
|
164
175
|
rule.sel = selectors.reduce((acc, curr) => curr.length == 0 ? acc : acc + (acc.length > 0 ? ',' : '') + curr, '');
|
|
165
176
|
}
|
|
@@ -200,10 +211,11 @@ function expandRule(node) {
|
|
|
200
211
|
// @ts-ignore
|
|
201
212
|
astAtRule.chi.push(...expandRule(r));
|
|
202
213
|
}
|
|
203
|
-
else {
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
214
|
+
// else {
|
|
215
|
+
//
|
|
216
|
+
// // @ts-ignore
|
|
217
|
+
// astAtRule.chi.push(r);
|
|
218
|
+
// }
|
|
207
219
|
}
|
|
208
220
|
}
|
|
209
221
|
// @ts-ignore
|
|
@@ -230,21 +242,23 @@ function replaceCompound(input, replace) {
|
|
|
230
242
|
if (replacement == null) {
|
|
231
243
|
replacement = parseString(replace);
|
|
232
244
|
}
|
|
233
|
-
if (tokens[1].typ == EnumToken.IdenTokenType) {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
245
|
+
// if (tokens[1].typ == EnumToken.IdenTokenType) {
|
|
246
|
+
//
|
|
247
|
+
//
|
|
248
|
+
// (t.value as LiteralToken).val = (replacement as Token[]).length == 1 || (!replace.includes(' ') && replace.charAt(0).match(/[:.]/)) ? (tokens[1] as IdentToken).val + replace : replaceCompoundLiteral((tokens[1] as IdentToken).val + '&', replace);
|
|
249
|
+
// tokens.splice(1, 1);
|
|
250
|
+
// } else {
|
|
251
|
+
t.value.val = replaceCompoundLiteral(t.value.val, replace);
|
|
252
|
+
// }
|
|
240
253
|
continue;
|
|
241
254
|
}
|
|
242
255
|
const rule = splitRule(replace);
|
|
243
256
|
t.value.val = rule.length > 1 ? ':is(' + replace + ')' : replace;
|
|
244
257
|
}
|
|
245
|
-
else if (t.value.val.length > 1 && t.value.val.charAt(0) == '&') {
|
|
246
|
-
|
|
247
|
-
|
|
258
|
+
// else if ((t.value as LiteralToken).val.length > 1 && (t.value as LiteralToken).val.charAt(0) == '&') {
|
|
259
|
+
//
|
|
260
|
+
// (t.value as LiteralToken).val = replaceCompoundLiteral((t.value as LiteralToken).val, replace);
|
|
261
|
+
// }
|
|
248
262
|
}
|
|
249
263
|
}
|
|
250
264
|
return tokens.reduce((acc, curr) => acc + renderToken(curr), '');
|
|
@@ -257,9 +271,10 @@ function replaceCompoundLiteral(selector, replace) {
|
|
|
257
271
|
tokens.push('&');
|
|
258
272
|
tokens.push('');
|
|
259
273
|
}
|
|
260
|
-
else {
|
|
261
|
-
|
|
262
|
-
|
|
274
|
+
// else {
|
|
275
|
+
//
|
|
276
|
+
// tokens[tokens.length - 1] += selector.charAt(i);
|
|
277
|
+
// }
|
|
263
278
|
}
|
|
264
279
|
return tokens.sort((a, b) => {
|
|
265
280
|
if (a == '&') {
|
|
@@ -267,9 +282,10 @@ function replaceCompoundLiteral(selector, replace) {
|
|
|
267
282
|
}
|
|
268
283
|
return b == '&' ? -1 : 0;
|
|
269
284
|
}).reduce((acc, curr) => {
|
|
270
|
-
if (acc.length > 0 && curr == '&' && (replace.charAt(0) != '.' || replace.includes(' '))) {
|
|
271
|
-
|
|
272
|
-
|
|
285
|
+
// if (acc.length > 0 && curr == '&' && (replace.charAt(0) != '.' || replace.includes(' '))) {
|
|
286
|
+
//
|
|
287
|
+
// return acc + ':is(' + replace + ')';
|
|
288
|
+
// }
|
|
273
289
|
return acc + (curr == '&' ? replace : curr);
|
|
274
290
|
}, '');
|
|
275
291
|
}
|
|
@@ -2,7 +2,7 @@ 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 '../../
|
|
5
|
+
import '../../syntax/color/utils/constants.js';
|
|
6
6
|
import '../minify.js';
|
|
7
7
|
import '../../parser/parse.js';
|
|
8
8
|
import '../../parser/tokenize.js';
|
|
@@ -56,8 +56,6 @@ class ComputeCalcExpressionFeature {
|
|
|
56
56
|
const slice = (node.typ == EnumToken.FunctionTokenType ? node.chi : (node.typ == EnumToken.DeclarationNodeType ? node.val : node.chi))?.slice();
|
|
57
57
|
if (slice != null && node.typ == EnumToken.FunctionTokenType && mathFuncs.includes(node.val)) {
|
|
58
58
|
// @ts-ignore
|
|
59
|
-
const cp = (node.typ == EnumToken.FunctionTokenType && mathFuncs.includes(node.val) && node.val != 'calc' ? [node] : (node.typ == EnumToken.DeclarationNodeType ? node.val : node.chi)).slice();
|
|
60
|
-
const values = evaluate(cp);
|
|
61
59
|
const key = 'chi' in node ? 'chi' : 'val';
|
|
62
60
|
const str1 = renderToken({ ...node, [key]: slice });
|
|
63
61
|
const str2 = renderToken(node); // values.reduce((acc: string, curr: Token): string => acc + renderToken(curr), '');
|
|
@@ -65,10 +63,6 @@ class ComputeCalcExpressionFeature {
|
|
|
65
63
|
// @ts-ignore
|
|
66
64
|
node[key] = slice;
|
|
67
65
|
}
|
|
68
|
-
else {
|
|
69
|
-
// @ts-ignore
|
|
70
|
-
node[key] = values;
|
|
71
|
-
}
|
|
72
66
|
return WalkerOptionEnum.Ignore;
|
|
73
67
|
}
|
|
74
68
|
return null;
|
|
@@ -84,41 +78,25 @@ class ComputeCalcExpressionFeature {
|
|
|
84
78
|
// @ts-ignore
|
|
85
79
|
const children = parent.typ == EnumToken.DeclarationNodeType ? parent.val : parent.chi;
|
|
86
80
|
if (values.length == 1 && values[0].typ != EnumToken.BinaryExpressionTokenType) {
|
|
87
|
-
|
|
88
|
-
if (
|
|
89
|
-
parent.
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
else {
|
|
96
|
-
for (let i = 0; i < children.length; i++) {
|
|
97
|
-
if (children[i] == value) {
|
|
98
|
-
// @ts-ignore
|
|
99
|
-
children.splice(i, 1, !(parent.typ == EnumToken.FunctionTokenType && parent.val == 'calc') && typeof values[0].val != 'string' ? {
|
|
100
|
-
typ: EnumToken.FunctionTokenType,
|
|
101
|
-
val: 'calc',
|
|
102
|
-
chi: values
|
|
103
|
-
} : values[0]);
|
|
104
|
-
break;
|
|
105
|
-
}
|
|
81
|
+
for (let i = 0; i < children.length; i++) {
|
|
82
|
+
if (children[i] == value) {
|
|
83
|
+
children.splice(i, 1, !(parent.typ == EnumToken.FunctionTokenType && parent.val == 'calc') && (typeof values[0].val != 'number' && !(values[0].typ == EnumToken.FunctionTokenType && mathFuncs.includes(values[0].val))) ? {
|
|
84
|
+
typ: EnumToken.FunctionTokenType,
|
|
85
|
+
val: 'calc',
|
|
86
|
+
chi: values
|
|
87
|
+
} : values[0]);
|
|
88
|
+
break;
|
|
106
89
|
}
|
|
107
90
|
}
|
|
108
91
|
}
|
|
109
92
|
else {
|
|
110
93
|
for (let i = 0; i < children.length; i++) {
|
|
111
94
|
if (children[i] == value) {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
typ: EnumToken.FunctionTokenType,
|
|
118
|
-
val: 'calc',
|
|
119
|
-
chi: values
|
|
120
|
-
});
|
|
121
|
-
}
|
|
95
|
+
children.splice(i, 1, {
|
|
96
|
+
typ: EnumToken.FunctionTokenType,
|
|
97
|
+
val: 'calc',
|
|
98
|
+
chi: values
|
|
99
|
+
});
|
|
122
100
|
break;
|
|
123
101
|
}
|
|
124
102
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { EnumToken } from '../types.js';
|
|
2
2
|
import { walkValues } from '../walk.js';
|
|
3
3
|
import { renderToken } from '../../renderer/render.js';
|
|
4
|
-
import '../../
|
|
4
|
+
import '../../syntax/color/utils/constants.js';
|
|
5
5
|
import { splitRule } from '../minify.js';
|
|
6
6
|
import '../../parser/parse.js';
|
|
7
7
|
import '../../parser/tokenize.js';
|
|
@@ -106,18 +106,6 @@ class InlineCssVariablesFeature {
|
|
|
106
106
|
replace(node, variableScope);
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
|
-
else {
|
|
110
|
-
const info = variableScope.get(node.nam);
|
|
111
|
-
info.globalScope = isRoot;
|
|
112
|
-
if (!isRoot) {
|
|
113
|
-
++info.declarationCount;
|
|
114
|
-
}
|
|
115
|
-
if (info.replaceable) {
|
|
116
|
-
info.replaceable = isRoot && info.declarationCount == 1;
|
|
117
|
-
}
|
|
118
|
-
info.parent.add(ast);
|
|
119
|
-
info.node = node;
|
|
120
|
-
}
|
|
121
109
|
}
|
|
122
110
|
else {
|
|
123
111
|
replace(node, variableScope);
|
|
@@ -126,9 +114,10 @@ class InlineCssVariablesFeature {
|
|
|
126
114
|
}
|
|
127
115
|
cleanup(ast, options = {}, context) {
|
|
128
116
|
const variableScope = context.variableScope;
|
|
129
|
-
if (variableScope == null) {
|
|
130
|
-
|
|
131
|
-
|
|
117
|
+
// if (variableScope == null) {
|
|
118
|
+
//
|
|
119
|
+
// return;
|
|
120
|
+
// }
|
|
132
121
|
for (const info of variableScope.values()) {
|
|
133
122
|
if (info.replaceable) {
|
|
134
123
|
let i;
|
|
@@ -140,7 +129,7 @@ class InlineCssVariablesFeature {
|
|
|
140
129
|
// @ts-ignore
|
|
141
130
|
parent.chi.splice(i, 1, {
|
|
142
131
|
typ: EnumToken.CommentTokenType,
|
|
143
|
-
val: `/* ${info.node.nam}: ${info.values.reduce((acc, curr) => acc + renderToken(curr), '')} */`
|
|
132
|
+
val: `/* ${info.node.nam}: ${info.values.reduce((acc, curr) => acc + renderToken(curr, { convertColor: false }), '')} */`
|
|
144
133
|
});
|
|
145
134
|
break;
|
|
146
135
|
}
|