@tbela99/css-parser 1.4.2 → 1.4.3
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/.nyc_output/4874b20e-6f53-4d7a-be5a-cf68316925f2.json +1 -0
- package/.nyc_output/6193bc4c-6f5f-4898-8950-c628825e6342.json +1 -0
- package/.nyc_output/processinfo/4874b20e-6f53-4d7a-be5a-cf68316925f2.json +1 -0
- package/.nyc_output/processinfo/6193bc4c-6f5f-4898-8950-c628825e6342.json +1 -0
- package/.repl_history +4 -0
- package/CHANGELOG.md +164 -4
- package/README.md +43 -0
- package/badges/coverage.svg +20 -0
- package/deno.lock +2861 -0
- package/dist/config.json.js +33 -1
- package/dist/index-umd-web.js +24382 -18476
- package/dist/index.cjs +24522 -18618
- package/dist/index.d.ts +1396 -929
- package/dist/lib/ast/clone.d.ts +10 -0
- package/dist/lib/ast/clone.js +45 -0
- package/dist/lib/ast/expand.d.ts +14 -0
- package/dist/lib/ast/expand.js +89 -64
- package/dist/lib/ast/features/calc.d.ts +10 -0
- package/dist/lib/ast/features/calc.js +62 -24
- package/dist/lib/ast/features/if.d.ts +10 -0
- package/dist/lib/ast/features/if.js +215 -0
- package/dist/lib/ast/features/index.d.ts +6 -0
- package/dist/lib/ast/features/index.js +1 -0
- package/dist/lib/ast/features/inlinecssvariables.d.ts +15 -0
- package/dist/lib/ast/features/inlinecssvariables.js +32 -27
- package/dist/lib/ast/features/prefix.d.ts +8 -0
- package/dist/lib/ast/features/prefix.js +68 -43
- package/dist/lib/ast/features/shorthand.d.ts +12 -0
- package/dist/lib/ast/features/shorthand.js +6 -9
- package/dist/lib/ast/features/transform.d.ts +10 -0
- package/dist/lib/ast/features/transform.js +9 -13
- package/dist/lib/ast/features/type.d.ts +15 -0
- package/dist/lib/ast/find.d.ts +165 -0
- package/dist/lib/ast/find.js +175 -0
- package/dist/lib/ast/math/expression.d.ts +18 -0
- package/dist/lib/ast/math/expression.js +140 -98
- package/dist/lib/ast/math/math.d.ts +6 -0
- package/dist/lib/ast/math/math.js +30 -41
- package/dist/lib/ast/minify.d.ts +19 -0
- package/dist/lib/ast/minify.js +541 -217
- package/dist/lib/ast/transform/compute.d.ts +8 -0
- package/dist/lib/ast/transform/compute.js +82 -69
- package/dist/lib/ast/transform/matrix.d.ts +22 -0
- package/dist/lib/ast/transform/matrix.js +12 -26
- package/dist/lib/ast/transform/minify.d.ts +5 -0
- package/dist/lib/ast/transform/minify.js +20 -20
- package/dist/lib/ast/transform/perspective.d.ts +3 -0
- package/dist/lib/ast/transform/perspective.js +1 -1
- package/dist/lib/ast/transform/rotate.d.ts +12 -0
- package/dist/lib/ast/transform/rotate.js +1 -1
- package/dist/lib/ast/transform/scale.d.ts +6 -0
- package/dist/lib/ast/transform/scale.js +1 -1
- package/dist/lib/ast/transform/skew.d.ts +4 -0
- package/dist/lib/ast/transform/skew.js +1 -1
- package/dist/lib/ast/transform/translate.d.ts +6 -0
- package/dist/lib/ast/transform/translate.js +1 -1
- package/dist/lib/ast/transform/utils.d.ts +9 -0
- package/dist/lib/ast/types.d.ts +903 -0
- package/dist/lib/ast/types.js +277 -23
- package/dist/lib/ast/walk.d.ts +162 -0
- package/dist/lib/ast/walk.js +116 -60
- package/dist/lib/fs/resolve.d.ts +20 -0
- package/dist/lib/fs/resolve.js +37 -45
- package/dist/lib/parser/declaration/list.d.ts +16 -0
- package/dist/lib/parser/declaration/list.js +26 -24
- package/dist/lib/parser/declaration/map.d.ts +15 -0
- package/dist/lib/parser/declaration/map.js +140 -95
- package/dist/lib/parser/declaration/set.d.ts +9 -0
- package/dist/lib/parser/declaration/set.js +30 -25
- package/dist/lib/parser/node.d.ts +7 -0
- package/dist/lib/parser/parse.d.ts +107 -0
- package/dist/lib/parser/parse.js +1454 -1445
- package/dist/lib/parser/tokenize.d.ts +57 -0
- package/dist/lib/parser/tokenize.js +557 -404
- package/dist/lib/parser/utils/at-rule-container.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-container.js +486 -0
- package/dist/lib/parser/utils/at-rule-font-feature-values.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-font-feature-values.js +13 -0
- package/dist/lib/parser/utils/at-rule-generic.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-generic.js +118 -0
- package/dist/lib/parser/utils/at-rule-import.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-import.js +393 -0
- package/dist/lib/parser/utils/at-rule-media.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-media.js +603 -0
- package/dist/lib/parser/utils/at-rule-page.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-page.js +28 -0
- package/dist/lib/parser/utils/at-rule-support.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-support.js +366 -0
- package/dist/lib/parser/utils/at-rule-token.d.ts +1 -0
- package/dist/lib/parser/utils/at-rule-when-else.d.ts +5 -0
- package/dist/lib/parser/utils/at-rule-when-else.js +363 -0
- package/dist/lib/parser/utils/at-rule.d.ts +13 -0
- package/dist/lib/parser/utils/at-rule.js +37 -0
- package/dist/lib/parser/utils/cache.d.ts +6 -0
- package/dist/lib/parser/utils/cache.js +19 -0
- package/dist/lib/parser/utils/config.d.ts +2 -0
- package/dist/lib/parser/utils/config.js +1 -0
- package/dist/lib/parser/utils/declaration-list.d.ts +5 -0
- package/dist/lib/parser/utils/declaration.d.ts +18 -0
- package/dist/lib/parser/utils/declaration.js +569 -91
- package/dist/lib/parser/utils/eq.d.ts +1 -0
- package/dist/lib/parser/utils/hash.d.ts +21 -0
- package/dist/lib/parser/utils/hash.js +1 -1
- package/dist/lib/parser/utils/selector.d.ts +5 -0
- package/dist/lib/parser/utils/selector.js +476 -0
- package/dist/lib/parser/utils/text.d.ts +3 -0
- package/dist/lib/parser/utils/text.js +17 -1
- package/dist/lib/parser/utils/token.d.ts +14 -0
- package/dist/lib/parser/utils/token.js +102 -0
- package/dist/lib/parser/utils/type.d.ts +2 -0
- package/dist/lib/parser/utils/type.js +29 -18
- package/dist/lib/renderer/render.d.ts +28 -0
- package/dist/lib/renderer/render.js +421 -262
- package/dist/lib/renderer/sourcemap/lib/encode.d.ts +1 -0
- package/dist/lib/renderer/sourcemap/sourcemap.d.ts +26 -0
- package/dist/lib/renderer/sourcemap/sourcemap.js +17 -7
- package/dist/lib/syntax/color/a98rgb.d.ts +2 -0
- package/dist/lib/syntax/color/a98rgb.js +8 -12
- package/dist/lib/syntax/color/cmyk.d.ts +10 -0
- package/dist/lib/syntax/color/cmyk.js +23 -21
- package/dist/lib/syntax/color/color-mix.d.ts +2 -0
- package/dist/lib/syntax/color/color-mix.js +88 -77
- package/dist/lib/syntax/color/color.d.ts +42 -0
- package/dist/lib/syntax/color/color.js +65 -68
- package/dist/lib/syntax/color/hex.d.ts +16 -0
- package/dist/lib/syntax/color/hex.js +27 -31
- package/dist/lib/syntax/color/hsl.d.ts +20 -0
- package/dist/lib/syntax/color/hsl.js +5 -12
- package/dist/lib/syntax/color/hsv.d.ts +2 -0
- package/dist/lib/syntax/color/hwb.d.ts +21 -0
- package/dist/lib/syntax/color/hwb.js +8 -21
- package/dist/lib/syntax/color/lab.d.ts +25 -0
- package/dist/lib/syntax/color/lab.js +20 -21
- package/dist/lib/syntax/color/lch.d.ts +23 -0
- package/dist/lib/syntax/color/lch.js +13 -15
- package/dist/lib/syntax/color/oklab.d.ts +22 -0
- package/dist/lib/syntax/color/oklab.js +20 -39
- package/dist/lib/syntax/color/oklch.d.ts +20 -0
- package/dist/lib/syntax/color/oklch.js +14 -16
- package/dist/lib/syntax/color/p3.d.ts +6 -0
- package/dist/lib/syntax/color/p3.js +0 -8
- package/dist/lib/syntax/color/prophotorgb.d.ts +2 -0
- package/dist/lib/syntax/color/rec2020.d.ts +2 -0
- package/dist/lib/syntax/color/rec2020.js +9 -13
- package/dist/lib/syntax/color/relativecolor.d.ts +13 -0
- package/dist/lib/syntax/color/relativecolor.js +68 -41
- package/dist/lib/syntax/color/rgb.d.ts +20 -0
- package/dist/lib/syntax/color/rgb.js +14 -18
- package/dist/lib/syntax/color/srgb.d.ts +23 -0
- package/dist/lib/syntax/color/srgb.js +27 -26
- package/dist/lib/syntax/color/utils/components.d.ts +2 -0
- package/dist/lib/syntax/color/utils/components.js +30 -14
- package/dist/lib/syntax/color/utils/distance.d.ts +18 -0
- package/dist/lib/syntax/color/utils/distance.js +1 -8
- package/dist/lib/syntax/color/utils/matrix.d.ts +6 -0
- package/dist/lib/syntax/color/xyz.d.ts +5 -0
- package/dist/lib/syntax/color/xyz.js +8 -20
- package/dist/lib/syntax/color/xyzd50.d.ts +4 -0
- package/dist/lib/syntax/color/xyzd50.js +6 -20
- package/dist/lib/syntax/constants.d.ts +67 -0
- package/dist/lib/syntax/constants.js +436 -0
- package/dist/lib/syntax/syntax.d.ts +38 -0
- package/dist/lib/syntax/syntax.js +533 -568
- package/dist/lib/validation/config.d.ts +14 -0
- package/dist/lib/validation/config.js +72 -33
- package/dist/lib/validation/config.json.js +1159 -74
- package/dist/lib/validation/json.d.ts +2 -0
- package/dist/lib/validation/match.d.ts +38 -0
- package/dist/lib/validation/match.js +2985 -0
- package/dist/lib/validation/parser/parse.d.ts +8 -0
- package/dist/lib/validation/parser/parse.js +684 -935
- package/dist/lib/validation/parser/typedef.d.ts +95 -0
- package/dist/lib/validation/parser/typedef.js +100 -0
- package/dist/lib/validation/utils/list.d.ts +4 -0
- package/dist/lib/validation/utils/list.js +4 -11
- package/dist/lib/validation/utils/whitespace.d.ts +2 -0
- package/dist/lib/validation/utils/whitespace.js +2 -8
- package/dist/node.d.ts +207 -0
- package/dist/node.js +42 -39
- package/dist/web.d.ts +169 -0
- package/dist/web.js +38 -33
- package/package.json +15 -12
- package/playground/index.html +1328 -0
- package/playground/sw.js +55 -0
- package/playground/tree.js +176 -0
- package/dist/lib/syntax/color/utils/constants.js +0 -214
- package/dist/lib/syntax/utils.js +0 -70
- package/dist/lib/validation/at-rules/container.js +0 -342
- package/dist/lib/validation/at-rules/counter-style.js +0 -90
- package/dist/lib/validation/at-rules/custom-media.js +0 -50
- package/dist/lib/validation/at-rules/document.js +0 -89
- package/dist/lib/validation/at-rules/else.js +0 -5
- package/dist/lib/validation/at-rules/font-feature-values.js +0 -63
- package/dist/lib/validation/at-rules/import.js +0 -150
- package/dist/lib/validation/at-rules/keyframes.js +0 -67
- package/dist/lib/validation/at-rules/layer.js +0 -41
- package/dist/lib/validation/at-rules/media.js +0 -255
- package/dist/lib/validation/at-rules/namespace.js +0 -81
- package/dist/lib/validation/at-rules/page-margin-box.js +0 -64
- package/dist/lib/validation/at-rules/page.js +0 -100
- package/dist/lib/validation/at-rules/supports.js +0 -295
- package/dist/lib/validation/at-rules/when.js +0 -185
- package/dist/lib/validation/atrule.js +0 -184
- package/dist/lib/validation/selector.js +0 -36
- package/dist/lib/validation/syntax.js +0 -1073
- package/dist/lib/validation/syntaxes/complex-selector-list.js +0 -27
- package/dist/lib/validation/syntaxes/complex-selector.js +0 -52
- package/dist/lib/validation/syntaxes/compound-selector.js +0 -196
- package/dist/lib/validation/syntaxes/family-name.js +0 -57
- package/dist/lib/validation/syntaxes/keyframe-selector.js +0 -36
- package/dist/lib/validation/syntaxes/layer-name.js +0 -57
- package/dist/lib/validation/syntaxes/relative-selector-list.js +0 -31
- package/dist/lib/validation/syntaxes/relative-selector.js +0 -38
- package/dist/lib/validation/syntaxes/selector-list.js +0 -5
- package/dist/lib/validation/syntaxes/selector.js +0 -5
- package/dist/lib/validation/syntaxes/url.js +0 -40
|
@@ -1,30 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { EnumToken, ColorType } from '../ast/types.js';
|
|
4
|
-
import '../ast/minify.js';
|
|
5
|
-
import '../ast/walk.js';
|
|
1
|
+
import { convertColor, getAngle } from '../syntax/color/color.js';
|
|
2
|
+
import { reduceHexValue } from '../syntax/color/hex.js';
|
|
3
|
+
import { EnumToken, minifyNumber, ColorType } from '../ast/types.js';
|
|
6
4
|
import { expand } from '../ast/expand.js';
|
|
7
|
-
import { isColor, pseudoElements, mathFuncs, isNewLine } from '../syntax/syntax.js';
|
|
8
|
-
import { minifyNumber } from '../syntax/utils.js';
|
|
9
5
|
import { SourceMap } from './sourcemap/sourcemap.js';
|
|
6
|
+
import { colorsFunc, urlTokenMatcher, tokensfuncSet } from '../syntax/constants.js';
|
|
7
|
+
import { isColor, pseudoElements, parseColor } from '../syntax/syntax.js';
|
|
8
|
+
import { move } from '../parser/tokenize.js';
|
|
10
9
|
|
|
11
|
-
/**
|
|
12
|
-
* Update position
|
|
13
|
-
* @param position
|
|
14
|
-
* @param str
|
|
15
|
-
*/
|
|
16
|
-
function update(position, str) {
|
|
17
|
-
let i = 0;
|
|
18
|
-
for (; i < str.length; i++) {
|
|
19
|
-
if (isNewLine(str[i].charCodeAt(0))) {
|
|
20
|
-
position.lin++;
|
|
21
|
-
position.col = 0;
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
24
|
-
position.col++;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
10
|
/**
|
|
29
11
|
* render ast
|
|
30
12
|
* @param data
|
|
@@ -36,21 +18,30 @@ function doRender(data, options = {}, mapping) {
|
|
|
36
18
|
const minify = options.minify ?? true;
|
|
37
19
|
const beautify = options.beautify ?? !minify;
|
|
38
20
|
options = {
|
|
39
|
-
...(beautify
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
21
|
+
...(beautify
|
|
22
|
+
? {
|
|
23
|
+
indent: " ",
|
|
24
|
+
newLine: "\n",
|
|
25
|
+
}
|
|
26
|
+
: {
|
|
27
|
+
indent: "",
|
|
28
|
+
newLine: "",
|
|
29
|
+
}),
|
|
30
|
+
...(minify
|
|
31
|
+
? {
|
|
32
|
+
removeEmpty: true,
|
|
33
|
+
removeComments: true,
|
|
34
|
+
minify: true,
|
|
35
|
+
}
|
|
36
|
+
: {
|
|
37
|
+
removeEmpty: false,
|
|
38
|
+
removeComments: false,
|
|
39
|
+
}),
|
|
40
|
+
sourcemap: false,
|
|
41
|
+
convertColor: true,
|
|
42
|
+
expandNestingRules: false,
|
|
43
|
+
preserveLicense: false,
|
|
44
|
+
...options,
|
|
54
45
|
};
|
|
55
46
|
if (options.withParents) {
|
|
56
47
|
// @ts-ignore
|
|
@@ -67,35 +58,42 @@ function doRender(data, options = {}, mapping) {
|
|
|
67
58
|
}
|
|
68
59
|
const startTime = performance.now();
|
|
69
60
|
const errors = [];
|
|
70
|
-
const sourcemap = options.sourcemap ? new SourceMap : null;
|
|
61
|
+
const sourcemap = options.sourcemap ? new SourceMap() : null;
|
|
71
62
|
const cache = Object.create(null);
|
|
72
63
|
const position = {
|
|
73
64
|
ind: 0,
|
|
74
65
|
lin: 1,
|
|
75
|
-
col: 1
|
|
66
|
+
col: 1,
|
|
76
67
|
};
|
|
77
|
-
let code =
|
|
68
|
+
let code = "";
|
|
78
69
|
if (mapping != null) {
|
|
79
70
|
if (mapping.importMapping != null) {
|
|
80
71
|
for (const [key, value] of Object.entries(mapping.importMapping)) {
|
|
81
|
-
code += `:import("${key}")${options.indent}{${options.newLine}${Object.entries(value).reduce((acc, [k, v]) => acc + (acc.length > 0 ? options.newLine :
|
|
72
|
+
code += `:import("${key}")${options.indent}{${options.newLine}${Object.entries(value).reduce((acc, [k, v]) => acc + (acc.length > 0 ? options.newLine : "") + `${options.indent}${v}:${options.indent}${k};`, "")}${options.newLine}}${options.newLine}`;
|
|
82
73
|
}
|
|
83
74
|
}
|
|
84
|
-
code += `:export${options.indent}{${options.newLine}${Object.entries(mapping.mapping).reduce((acc, [k, v]) => acc + (acc.length > 0 ? options.newLine :
|
|
85
|
-
|
|
75
|
+
code += `:export${options.indent}{${options.newLine}${Object.entries(mapping.mapping).reduce((acc, [k, v]) => acc + (acc.length > 0 ? options.newLine : "") + `${options.indent}${k}:${options.indent}${v};`, "")}${options.newLine}}${options.newLine}`;
|
|
76
|
+
move(position, code);
|
|
86
77
|
}
|
|
87
78
|
const result = {
|
|
88
|
-
code: code +
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
79
|
+
code: code +
|
|
80
|
+
renderAstNode(options.expandNestingRules &&
|
|
81
|
+
[EnumToken.StyleSheetNodeType, EnumToken.AtRuleNodeType, EnumToken.RuleNodeType].includes(data.typ) &&
|
|
82
|
+
"chi" in data
|
|
83
|
+
? expand(data)
|
|
84
|
+
: data, options, sourcemap, position, errors, function reducer(acc, curr) {
|
|
85
|
+
if (curr.typ == EnumToken.CommentTokenType && options.removeComments) {
|
|
86
|
+
if (!options.preserveLicense || !curr.val.startsWith("/*!")) {
|
|
87
|
+
return acc;
|
|
88
|
+
}
|
|
89
|
+
return acc + curr.val;
|
|
92
90
|
}
|
|
93
|
-
return acc + curr
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
total: `${(performance.now() - startTime).toFixed(2)}ms
|
|
98
|
-
}
|
|
91
|
+
return acc + renderToken(curr, options, cache, reducer, errors);
|
|
92
|
+
}, cache),
|
|
93
|
+
errors,
|
|
94
|
+
stats: {
|
|
95
|
+
total: `${(performance.now() - startTime).toFixed(2)}ms`,
|
|
96
|
+
},
|
|
99
97
|
};
|
|
100
98
|
if (options.output != null) {
|
|
101
99
|
// @ts-ignore
|
|
@@ -103,7 +101,7 @@ function doRender(data, options = {}, mapping) {
|
|
|
103
101
|
}
|
|
104
102
|
if (sourcemap != null) {
|
|
105
103
|
result.map = sourcemap;
|
|
106
|
-
if (options.sourcemap ===
|
|
104
|
+
if (options.sourcemap === "inline") {
|
|
107
105
|
result.code += `\n/*# sourceMappingURL=data:application/json,${encodeURIComponent(JSON.stringify(result.map))} */`;
|
|
108
106
|
}
|
|
109
107
|
}
|
|
@@ -122,27 +120,21 @@ function doRender(data, options = {}, mapping) {
|
|
|
122
120
|
*/
|
|
123
121
|
function updateSourceMap(node, options, cache, sourcemap, position, str) {
|
|
124
122
|
if ([
|
|
125
|
-
EnumToken.RuleNodeType,
|
|
126
|
-
EnumToken.
|
|
123
|
+
EnumToken.RuleNodeType,
|
|
124
|
+
EnumToken.AtRuleNodeType,
|
|
125
|
+
EnumToken.KeyFramesRuleNodeType,
|
|
126
|
+
EnumToken.KeyframesAtRuleNodeType,
|
|
127
127
|
].includes(node.typ)) {
|
|
128
|
-
let src = node.loc?.src ??
|
|
129
|
-
|
|
130
|
-
if (!(src in cache)) {
|
|
131
|
-
// @ts-ignore
|
|
132
|
-
cache[src] = options.resolve(src, options.cwd ?? '').relative;
|
|
133
|
-
}
|
|
134
|
-
if (!(output in cache)) {
|
|
135
|
-
// @ts-ignore
|
|
136
|
-
cache[output] = options.resolve(output, options.cwd).relative;
|
|
137
|
-
}
|
|
128
|
+
let src = node.loc?.src ?? "";
|
|
129
|
+
sourcemap.add(
|
|
138
130
|
// @ts-ignore
|
|
139
|
-
|
|
131
|
+
{ src, sta: { ...position } }, {
|
|
140
132
|
...node.loc,
|
|
141
133
|
// @ts-ignore
|
|
142
|
-
src
|
|
134
|
+
src
|
|
143
135
|
});
|
|
144
136
|
}
|
|
145
|
-
|
|
137
|
+
move(position, str);
|
|
146
138
|
}
|
|
147
139
|
/**
|
|
148
140
|
* render ast node
|
|
@@ -169,103 +161,131 @@ function renderAstNode(data, options, sourcemap, position, errors, reducer, cach
|
|
|
169
161
|
const indentSub = indents[level + 1];
|
|
170
162
|
switch (data.typ) {
|
|
171
163
|
case EnumToken.DeclarationNodeType:
|
|
172
|
-
return `${data.nam}:${options.indent}${(options.minify
|
|
164
|
+
return `${data.nam}:${options.indent}${(options.minify
|
|
165
|
+
? filterValues(data.val)
|
|
166
|
+
: data.val).reduce(reducer, "")}`;
|
|
173
167
|
case EnumToken.CommentNodeType:
|
|
174
168
|
case EnumToken.CDOCOMMNodeType:
|
|
175
|
-
if (data.val.startsWith(
|
|
169
|
+
if (data.val.startsWith("/*# sourceMappingURL=")) {
|
|
176
170
|
// ignore sourcemap
|
|
177
|
-
return
|
|
171
|
+
return "";
|
|
178
172
|
}
|
|
179
|
-
return !options.removeComments || (options.preserveLicense && data.val.startsWith(
|
|
173
|
+
return !options.removeComments || (options.preserveLicense && data.val.startsWith("/*!"))
|
|
174
|
+
? data.val
|
|
175
|
+
: "";
|
|
180
176
|
case EnumToken.StyleSheetNodeType:
|
|
181
177
|
return data.chi.reduce((css, node) => {
|
|
182
178
|
const str = renderAstNode(node, options, sourcemap, { ...position }, errors, reducer, cache, level, indents);
|
|
183
|
-
if (str ===
|
|
179
|
+
if (str === "") {
|
|
184
180
|
return css;
|
|
185
181
|
}
|
|
186
|
-
if (css ===
|
|
182
|
+
if (css === "") {
|
|
187
183
|
if (sourcemap != null && node.loc != null) {
|
|
188
184
|
updateSourceMap(node, options, cache, sourcemap, position, str);
|
|
189
185
|
}
|
|
190
186
|
return str;
|
|
191
187
|
}
|
|
192
188
|
if (sourcemap != null && node.loc != null) {
|
|
193
|
-
|
|
189
|
+
move(position, options.newLine);
|
|
194
190
|
updateSourceMap(node, options, cache, sourcemap, position, str);
|
|
195
191
|
}
|
|
196
192
|
return `${css}${options.newLine}${str}`;
|
|
197
|
-
},
|
|
193
|
+
}, "");
|
|
198
194
|
case EnumToken.AtRuleNodeType:
|
|
199
195
|
case EnumToken.RuleNodeType:
|
|
200
196
|
case EnumToken.KeyFramesRuleNodeType:
|
|
201
197
|
case EnumToken.KeyframesAtRuleNodeType:
|
|
202
|
-
if ([EnumToken.AtRuleNodeType, EnumToken.KeyframesAtRuleNodeType].includes(data.typ) && !(
|
|
203
|
-
return `${indent}@${data.nam}${data.val ===
|
|
198
|
+
if ([EnumToken.AtRuleNodeType, EnumToken.KeyframesAtRuleNodeType].includes(data.typ) && !("chi" in data)) {
|
|
199
|
+
return `${indent}@${data.nam}${data.val === "" ? "" : options.indent || " "}${data.val};`;
|
|
204
200
|
}
|
|
205
201
|
// @ts-ignore
|
|
206
202
|
let children = data.chi.reduce((css, node) => {
|
|
207
203
|
let str;
|
|
208
204
|
if (node.typ == EnumToken.CommentNodeType) {
|
|
209
|
-
str =
|
|
205
|
+
str =
|
|
206
|
+
options.removeComments &&
|
|
207
|
+
(!options.preserveLicense || !node.val.startsWith("/*!"))
|
|
208
|
+
? ""
|
|
209
|
+
: node.val;
|
|
210
210
|
}
|
|
211
211
|
else if (node.typ == EnumToken.DeclarationNodeType) {
|
|
212
|
-
if (!node.nam.startsWith(
|
|
212
|
+
if (!node.nam.startsWith("--") && node.val.length === 0) {
|
|
213
213
|
// @ts-ignore
|
|
214
214
|
errors.push({
|
|
215
|
-
action:
|
|
215
|
+
action: "ignore",
|
|
216
216
|
message: `render: invalid declaration ${JSON.stringify(node)}`,
|
|
217
|
-
location: node.loc
|
|
217
|
+
location: node.loc,
|
|
218
218
|
});
|
|
219
|
-
return
|
|
219
|
+
return "";
|
|
220
220
|
}
|
|
221
|
-
str = `${node.nam}:${options.indent}${(options.minify
|
|
221
|
+
str = `${node.nam}:${options.indent}${(options.minify
|
|
222
|
+
? filterValues(node.val)
|
|
223
|
+
: node.val)
|
|
224
|
+
.reduce(reducer, "")
|
|
225
|
+
.trimEnd()};`;
|
|
222
226
|
}
|
|
223
|
-
else if (node.typ == EnumToken.AtRuleNodeType && !(
|
|
224
|
-
str = `${node.val ===
|
|
227
|
+
else if (node.typ == EnumToken.AtRuleNodeType && !("chi" in node)) {
|
|
228
|
+
str = `${node.val === "" ? "" : options.indent || " "}${node.val};`;
|
|
225
229
|
}
|
|
226
230
|
else {
|
|
227
231
|
str = renderAstNode(node, options, sourcemap, { ...position }, errors, reducer, cache, level + 1, indents);
|
|
228
232
|
}
|
|
229
|
-
if (css ===
|
|
233
|
+
if (css === "") {
|
|
230
234
|
return str;
|
|
231
235
|
}
|
|
232
|
-
if (str ===
|
|
236
|
+
if (str === "") {
|
|
233
237
|
return css;
|
|
234
238
|
}
|
|
235
239
|
return `${css}${options.newLine}${indentSub}${str}`;
|
|
236
|
-
},
|
|
237
|
-
if (options.removeEmpty && children ===
|
|
238
|
-
return
|
|
240
|
+
}, "");
|
|
241
|
+
if (options.removeEmpty && children === "") {
|
|
242
|
+
return "";
|
|
239
243
|
}
|
|
240
|
-
if (children.endsWith(
|
|
244
|
+
if (children.endsWith(";")) {
|
|
241
245
|
children = children.slice(0, -1);
|
|
242
246
|
}
|
|
243
247
|
if ([EnumToken.AtRuleNodeType, EnumToken.KeyframesAtRuleNodeType].includes(data.typ)) {
|
|
244
|
-
return `@${data.nam}${data.val ===
|
|
248
|
+
return (`@${data.nam}${data.val === "" ? "" : options.indent || " "}${data.val}${options.indent}{${options.newLine}` +
|
|
249
|
+
(children === "" ? "" : indentSub + children + options.newLine) +
|
|
250
|
+
indent +
|
|
251
|
+
`}`);
|
|
245
252
|
}
|
|
246
|
-
return data.sel +
|
|
253
|
+
return (data.sel +
|
|
254
|
+
`${options.indent}{${options.newLine}` +
|
|
255
|
+
(children === "" ? "" : indentSub + children + options.newLine) +
|
|
256
|
+
indent +
|
|
257
|
+
`}`);
|
|
247
258
|
case EnumToken.CssVariableTokenType:
|
|
248
259
|
case EnumToken.CssVariableImportTokenType:
|
|
249
|
-
return `@value ${data.
|
|
260
|
+
return `@value ${data.val}:${options.indent}${filterValues(options.minify
|
|
261
|
+
? data.val
|
|
262
|
+
: data.val)
|
|
263
|
+
.reduce(reducer, "")
|
|
264
|
+
.trim()};`;
|
|
250
265
|
case EnumToken.CssVariableDeclarationMapTokenType:
|
|
251
|
-
return `@value ${filterValues(data.vars)
|
|
266
|
+
return `@value ${filterValues(data.vars)
|
|
267
|
+
.reduce((acc, curr) => acc + renderToken(curr), "")
|
|
268
|
+
.trim()} from ${filterValues(data.from)
|
|
269
|
+
.reduce((acc, curr) => acc + renderToken(curr), "")
|
|
270
|
+
.trim()};`;
|
|
252
271
|
case EnumToken.InvalidDeclarationNodeType:
|
|
253
|
-
case EnumToken.
|
|
254
|
-
case EnumToken.
|
|
272
|
+
case EnumToken.InvalidRuleNodeType:
|
|
273
|
+
case EnumToken.InvalidAtRuleNodeType:
|
|
255
274
|
default:
|
|
256
|
-
return
|
|
275
|
+
return "";
|
|
257
276
|
}
|
|
258
277
|
}
|
|
259
278
|
/**
|
|
260
279
|
* render ast token
|
|
261
280
|
* @param token
|
|
262
281
|
* @param options
|
|
282
|
+
* @private
|
|
263
283
|
*/
|
|
264
284
|
function renderToken(token, options = {}, cache = Object.create(null), reducer, errors) {
|
|
265
285
|
if (reducer == null) {
|
|
266
286
|
reducer = function (acc, curr) {
|
|
267
287
|
if (curr.typ == EnumToken.CommentTokenType && options.removeComments) {
|
|
268
|
-
if (!options.preserveLicense || !curr.val.startsWith(
|
|
288
|
+
if (!options.preserveLicense || !curr.val.startsWith("/*!")) {
|
|
269
289
|
return acc;
|
|
270
290
|
}
|
|
271
291
|
return acc + curr.val;
|
|
@@ -277,54 +297,84 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
|
|
|
277
297
|
if (isColor(token)) {
|
|
278
298
|
// @ts-ignore
|
|
279
299
|
token.typ = EnumToken.ColorTokenType;
|
|
300
|
+
if (
|
|
280
301
|
// @ts-ignore
|
|
281
|
-
|
|
302
|
+
token.chi[0].typ == EnumToken.IdenTokenType &&
|
|
303
|
+
// @ts-ignore
|
|
304
|
+
token.chi[0].val == "from") {
|
|
282
305
|
// @ts-ignore
|
|
283
|
-
token.cal =
|
|
306
|
+
token.cal = "rel";
|
|
284
307
|
}
|
|
285
|
-
else {
|
|
286
|
-
|
|
308
|
+
else {
|
|
309
|
+
// @ts-ignore
|
|
310
|
+
if (token.val == "color-mix" &&
|
|
311
|
+
token.chi?.[0]?.typ == EnumToken.IdenTokenType &&
|
|
312
|
+
(token.chi?.[0]).val == "in") {
|
|
287
313
|
// @ts-ignore
|
|
288
|
-
token.cal =
|
|
314
|
+
token.cal = "mix";
|
|
289
315
|
}
|
|
290
316
|
else {
|
|
291
317
|
// @ts-ignore
|
|
292
|
-
if (token.val ==
|
|
318
|
+
if (token.val == "color") {
|
|
293
319
|
// @ts-ignore
|
|
294
|
-
token.cal =
|
|
320
|
+
token.cal = "col";
|
|
295
321
|
}
|
|
296
322
|
// @ts-ignore
|
|
297
|
-
token.chi = token.chi.filter((t) => ![
|
|
323
|
+
token.chi = token.chi.filter((t) => ![
|
|
324
|
+
EnumToken.WhitespaceTokenType,
|
|
325
|
+
EnumToken.CommaTokenType,
|
|
326
|
+
EnumToken.CommentTokenType,
|
|
327
|
+
].includes(t.typ));
|
|
298
328
|
}
|
|
299
329
|
}
|
|
300
330
|
}
|
|
301
331
|
}
|
|
302
332
|
switch (token.typ) {
|
|
333
|
+
case EnumToken.FunctionTokenDefType:
|
|
334
|
+
case EnumToken.UrlFunctionTokenDefType:
|
|
335
|
+
case EnumToken.MathFunctionTokenDefType:
|
|
336
|
+
case EnumToken.ImageFunctionTokenDefType:
|
|
337
|
+
case EnumToken.ColorFunctionTokenDefType:
|
|
338
|
+
case EnumToken.TimingFunctionTokenDefType:
|
|
339
|
+
case EnumToken.WhenElseFunctionTokenDefType:
|
|
340
|
+
case EnumToken.SupportsFunctionTokenDefType:
|
|
341
|
+
case EnumToken.TimelineFunctionTokenDefType:
|
|
342
|
+
case EnumToken.GridTemplateFuncTokenDefType:
|
|
343
|
+
case EnumToken.TransformFunctionTokenDefType:
|
|
344
|
+
case EnumToken.ContainerFunctionTokenDefType:
|
|
345
|
+
case EnumToken.PseudoClassFunctionTokenDefType:
|
|
346
|
+
case EnumToken.GeneralEnclosedFunctionTokenDefType:
|
|
347
|
+
case EnumToken.CustomFunctionTokenDefType:
|
|
348
|
+
case EnumToken.WildCardFunctionTokenDefType:
|
|
349
|
+
return token.val + "(";
|
|
303
350
|
case EnumToken.ListToken:
|
|
304
|
-
return token.chi.reduce((acc, curr) => acc + renderToken(curr, options, cache),
|
|
351
|
+
return token.chi.reduce((acc, curr) => acc + renderToken(curr, options, cache), "");
|
|
305
352
|
case EnumToken.BinaryExpressionTokenType:
|
|
306
|
-
if (
|
|
307
|
-
|
|
353
|
+
if (EnumToken.Mul === token.op ||
|
|
354
|
+
EnumToken.Div === token.op) {
|
|
355
|
+
let result = "";
|
|
308
356
|
if (token.l.typ == EnumToken.BinaryExpressionTokenType &&
|
|
309
357
|
[EnumToken.Add, EnumToken.Sub].includes(token.l.op)) {
|
|
310
|
-
result =
|
|
358
|
+
result = "(" + renderToken(token.l, options, cache) + ")";
|
|
311
359
|
}
|
|
312
360
|
else {
|
|
313
361
|
result = renderToken(token.l, options, cache);
|
|
314
362
|
}
|
|
315
|
-
result += token.op == EnumToken.Mul ?
|
|
363
|
+
result += token.op == EnumToken.Mul ? "*" : "/";
|
|
316
364
|
if (token.r.typ == EnumToken.BinaryExpressionTokenType &&
|
|
317
365
|
[EnumToken.Add, EnumToken.Sub].includes(token.r.op)) {
|
|
318
|
-
result +=
|
|
366
|
+
result += "(" + renderToken(token.r, options, cache) + ")";
|
|
319
367
|
}
|
|
320
368
|
else {
|
|
321
369
|
result += renderToken(token.r, options, cache);
|
|
322
370
|
}
|
|
323
371
|
return result;
|
|
324
372
|
}
|
|
325
|
-
return renderToken(token.l, options, cache) +
|
|
373
|
+
return (renderToken(token.l, options, cache) +
|
|
374
|
+
(token.op == EnumToken.Add ? " + " : " - ") +
|
|
375
|
+
renderToken(token.r, options, cache));
|
|
326
376
|
case EnumToken.FractionTokenType:
|
|
327
|
-
const fraction = renderToken(token.l) +
|
|
377
|
+
const fraction = renderToken(token.l) + "/" + renderToken(token.r);
|
|
328
378
|
if (+token.r.val != 0) {
|
|
329
379
|
const value = minifyNumber(+token.l.val / +token.r.val);
|
|
330
380
|
if (value.length <= fraction.length) {
|
|
@@ -333,150 +383,227 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
|
|
|
333
383
|
}
|
|
334
384
|
return fraction;
|
|
335
385
|
case EnumToken.Add:
|
|
336
|
-
return
|
|
386
|
+
return " + ";
|
|
337
387
|
case EnumToken.Sub:
|
|
338
|
-
return
|
|
388
|
+
return " - ";
|
|
389
|
+
case EnumToken.Star:
|
|
339
390
|
case EnumToken.UniversalSelectorTokenType:
|
|
340
391
|
case EnumToken.Mul:
|
|
341
|
-
return
|
|
392
|
+
return "*";
|
|
342
393
|
case EnumToken.Div:
|
|
343
|
-
return
|
|
394
|
+
return "/";
|
|
344
395
|
case EnumToken.ColorTokenType:
|
|
345
|
-
if (token.kin == ColorType.LIGHT_DARK ||
|
|
346
|
-
|
|
396
|
+
if (token.kin == ColorType.LIGHT_DARK ||
|
|
397
|
+
("chi" in token && options.convertColor === false && token.chi.length == 2)) {
|
|
398
|
+
return (token.val +
|
|
399
|
+
"(" +
|
|
400
|
+
token.chi.reduce((acc, curr) => {
|
|
401
|
+
if (curr.typ === EnumToken.IdenTokenType) {
|
|
402
|
+
parseColor(curr);
|
|
403
|
+
}
|
|
404
|
+
return acc + renderToken(curr, options, cache);
|
|
405
|
+
}, "") +
|
|
406
|
+
")");
|
|
347
407
|
}
|
|
348
408
|
if (options.convertColor !== false) {
|
|
349
|
-
const value = convertColor(token, typeof options.convertColor ==
|
|
350
|
-
|
|
409
|
+
const value = convertColor(token, typeof options.convertColor == "boolean"
|
|
410
|
+
? ColorType.HEX
|
|
411
|
+
: (ColorType[ColorType[options.convertColor ?? "HEX"]
|
|
412
|
+
?.toUpperCase?.()
|
|
413
|
+
.replaceAll?.("-", "_")] ?? ColorType.HEX));
|
|
351
414
|
if (value != null) {
|
|
352
415
|
token = value;
|
|
353
416
|
}
|
|
417
|
+
if (token.kin == ColorType.HEX || token.kin == ColorType.LIT) {
|
|
418
|
+
return reduceHexValue(token.val);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
if (token.kin === ColorType.SYS ||
|
|
422
|
+
token.kin === ColorType.DPSYS ||
|
|
423
|
+
token.kin === ColorType.NON_STD) {
|
|
424
|
+
return token.val;
|
|
354
425
|
}
|
|
355
|
-
if (
|
|
426
|
+
if (token.kin == ColorType.HEX || token.kin == ColorType.LIT) {
|
|
356
427
|
return token.val;
|
|
357
428
|
}
|
|
358
429
|
if (Array.isArray(token.chi)) {
|
|
359
|
-
const isLegacy = [
|
|
360
|
-
const useAlpha = ([
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
430
|
+
const isLegacy = ["rgb", "rgba", "hsl", "hsla"].includes(token.val.toLowerCase());
|
|
431
|
+
const useAlpha = (["rgb", "rgba", "hsl", "hsla", "hwb", "oklab", "oklch", "lab", "lch"].includes(token.val.toLowerCase()) &&
|
|
432
|
+
token.chi.length == 4) ||
|
|
433
|
+
("color" == token.val.toLowerCase() && token.chi.length == 5);
|
|
434
|
+
return ((token.val.endsWith("a")
|
|
435
|
+
? token.val.slice(0, -1)
|
|
436
|
+
: token.val) +
|
|
437
|
+
"(" +
|
|
438
|
+
token
|
|
439
|
+
.chi.reduce((acc, curr, index, array) => {
|
|
440
|
+
if (/[,/]\s*$/.test(acc)) {
|
|
441
|
+
if (curr.typ == EnumToken.WhitespaceTokenType) {
|
|
442
|
+
return acc.trimEnd();
|
|
443
|
+
}
|
|
444
|
+
return acc.trimStart() + renderToken(curr, options, cache);
|
|
445
|
+
}
|
|
446
|
+
if (isLegacy && curr.typ == EnumToken.CommaTokenType) {
|
|
447
|
+
return acc.trimEnd() + " ";
|
|
448
|
+
}
|
|
364
449
|
if (curr.typ == EnumToken.WhitespaceTokenType) {
|
|
365
|
-
return acc.trimEnd();
|
|
450
|
+
return acc.trimEnd() + " ";
|
|
366
451
|
}
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
|
|
452
|
+
if (curr.typ == EnumToken.CommaTokenType ||
|
|
453
|
+
(curr.typ == EnumToken.LiteralTokenType && curr.val == "/")) {
|
|
454
|
+
return acc.trimEnd() + (curr.typ == EnumToken.CommaTokenType ? "," : "/");
|
|
455
|
+
}
|
|
456
|
+
return (acc.trimEnd() +
|
|
457
|
+
(useAlpha && index == array.length - 1 ? "/" : " ") +
|
|
458
|
+
renderToken(curr, options, cache));
|
|
459
|
+
}, "")
|
|
460
|
+
.trimStart() +
|
|
461
|
+
")");
|
|
462
|
+
}
|
|
463
|
+
case EnumToken.UrlFunctionTokenType:
|
|
464
|
+
if (options.minify && token.typ === EnumToken.UrlFunctionTokenType) {
|
|
465
|
+
for (const child of token.chi) {
|
|
466
|
+
if (child.typ === EnumToken.StringTokenType) {
|
|
467
|
+
if (child.val.slice(1, 5) !== "data:" &&
|
|
468
|
+
urlTokenMatcher.test(child.val)) {
|
|
469
|
+
Object.assign(child, {
|
|
470
|
+
typ: EnumToken.UrlTokenTokenType,
|
|
471
|
+
val: child.val.slice(1, -1),
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
break;
|
|
377
475
|
}
|
|
378
|
-
|
|
379
|
-
}, '').trimStart() + ')';
|
|
476
|
+
}
|
|
380
477
|
}
|
|
381
478
|
case EnumToken.ParensTokenType:
|
|
382
479
|
case EnumToken.FunctionTokenType:
|
|
383
|
-
case EnumToken.
|
|
480
|
+
case EnumToken.MathFunctionTokenType:
|
|
384
481
|
case EnumToken.ImageFunctionTokenType:
|
|
385
482
|
case EnumToken.TimingFunctionTokenType:
|
|
386
483
|
case EnumToken.PseudoClassFuncTokenType:
|
|
484
|
+
case EnumToken.WhenElseFunctionTokenType:
|
|
387
485
|
case EnumToken.TimelineFunctionTokenType:
|
|
388
486
|
case EnumToken.GridTemplateFuncTokenType:
|
|
389
|
-
|
|
390
|
-
|
|
487
|
+
case EnumToken.SupportsFunctionTokenType:
|
|
488
|
+
case EnumToken.ContainerFunctionTokenType:
|
|
489
|
+
case EnumToken.TransformFunctionTokenType:
|
|
490
|
+
case EnumToken.GeneralEnclosedFunctionTokenType:
|
|
491
|
+
case EnumToken.CustomFunctionTokenType:
|
|
492
|
+
case EnumToken.WildCardFunctionTokenType:
|
|
493
|
+
if (token.typ == EnumToken.MathFunctionTokenType &&
|
|
391
494
|
token.chi.length == 1 &&
|
|
392
495
|
![EnumToken.BinaryExpressionTokenType, EnumToken.FractionTokenType, EnumToken.IdenTokenType].includes(token.chi[0].typ) &&
|
|
393
496
|
// @ts-ignore
|
|
394
|
-
token.chi[0].val
|
|
395
|
-
|
|
497
|
+
token.chi[0].val
|
|
498
|
+
?.typ != EnumToken.FractionTokenType) {
|
|
499
|
+
return (token.val +
|
|
500
|
+
"(" +
|
|
501
|
+
token.chi.reduce((acc, curr) => acc +
|
|
502
|
+
renderToken(curr, token.typ == EnumToken.FunctionTokenType ? { minify: false } : options, cache, reducer), "") +
|
|
503
|
+
")");
|
|
396
504
|
}
|
|
397
|
-
return (
|
|
505
|
+
return (
|
|
506
|
+
/* options.minify && 'Pseudo-class-func' == token.typ && token.val.slice(0, 2) == '::' ? token.val.slice(1) :*/ (token.val ?? "") +
|
|
507
|
+
"(" +
|
|
508
|
+
token.chi.reduce(reducer, "") +
|
|
509
|
+
")");
|
|
398
510
|
case EnumToken.MatchExpressionTokenType:
|
|
399
|
-
return renderToken(token.l, options, cache, reducer, errors) +
|
|
511
|
+
return (renderToken(token.l, options, cache, reducer, errors) +
|
|
400
512
|
renderToken(token.op, options, cache, reducer, errors) +
|
|
401
513
|
renderToken(token.r, options, cache, reducer, errors) +
|
|
402
|
-
(token.attr ?
|
|
514
|
+
(token.attr ? " " + token.attr : ""));
|
|
403
515
|
case EnumToken.NameSpaceAttributeTokenType:
|
|
404
|
-
return (token.l == null
|
|
405
|
-
|
|
516
|
+
return ((token.l == null
|
|
517
|
+
? ""
|
|
518
|
+
: renderToken(token.l, options, cache, reducer, errors)) +
|
|
519
|
+
"|" +
|
|
520
|
+
renderToken(token.r, options, cache, reducer, errors));
|
|
406
521
|
case EnumToken.ComposesSelectorNodeType:
|
|
407
|
-
return token.l.reduce((acc, curr) => acc + renderToken(curr, options, cache),
|
|
522
|
+
return (token.l.reduce((acc, curr) => acc + renderToken(curr, options, cache), "") +
|
|
523
|
+
(token.r == null
|
|
524
|
+
? ""
|
|
525
|
+
: " from " +
|
|
526
|
+
renderToken(token.r, options, cache, reducer, errors)));
|
|
408
527
|
case EnumToken.BlockStartTokenType:
|
|
409
|
-
return
|
|
528
|
+
return "{";
|
|
410
529
|
case EnumToken.BlockEndTokenType:
|
|
411
|
-
return
|
|
530
|
+
return "}";
|
|
412
531
|
case EnumToken.StartParensTokenType:
|
|
413
|
-
return
|
|
532
|
+
return "(";
|
|
414
533
|
case EnumToken.DelimTokenType:
|
|
415
534
|
case EnumToken.EqualMatchTokenType:
|
|
416
|
-
return
|
|
535
|
+
return "=";
|
|
417
536
|
case EnumToken.IncludeMatchTokenType:
|
|
418
|
-
return
|
|
537
|
+
return "~=";
|
|
419
538
|
case EnumToken.DashMatchTokenType:
|
|
420
|
-
return
|
|
539
|
+
return "|=";
|
|
421
540
|
case EnumToken.StartMatchTokenType:
|
|
422
|
-
return
|
|
541
|
+
return "^=";
|
|
423
542
|
case EnumToken.EndMatchTokenType:
|
|
424
|
-
return
|
|
543
|
+
return "$=";
|
|
425
544
|
case EnumToken.ContainMatchTokenType:
|
|
426
|
-
return
|
|
545
|
+
return "*=";
|
|
427
546
|
case EnumToken.LtTokenType:
|
|
428
|
-
return
|
|
547
|
+
return "<";
|
|
429
548
|
case EnumToken.LteTokenType:
|
|
430
|
-
return
|
|
549
|
+
return "<=";
|
|
550
|
+
case EnumToken.Tilda:
|
|
431
551
|
case EnumToken.SubsequentSiblingCombinatorTokenType:
|
|
432
|
-
return
|
|
552
|
+
return "~";
|
|
553
|
+
case EnumToken.Plus:
|
|
433
554
|
case EnumToken.NextSiblingCombinatorTokenType:
|
|
434
|
-
return
|
|
555
|
+
return "+";
|
|
435
556
|
case EnumToken.GtTokenType:
|
|
436
557
|
case EnumToken.ChildCombinatorTokenType:
|
|
437
|
-
return
|
|
558
|
+
return ">";
|
|
438
559
|
case EnumToken.GteTokenType:
|
|
439
|
-
return
|
|
560
|
+
return ">=";
|
|
440
561
|
case EnumToken.ColumnCombinatorTokenType:
|
|
441
|
-
return
|
|
562
|
+
return "||";
|
|
442
563
|
case EnumToken.EndParensTokenType:
|
|
443
|
-
return
|
|
564
|
+
return ")";
|
|
444
565
|
case EnumToken.AttrStartTokenType:
|
|
445
|
-
return
|
|
566
|
+
return "[";
|
|
446
567
|
case EnumToken.AttrEndTokenType:
|
|
447
|
-
return
|
|
568
|
+
return "]";
|
|
448
569
|
case EnumToken.DescendantCombinatorTokenType:
|
|
449
570
|
case EnumToken.WhitespaceTokenType:
|
|
450
|
-
return
|
|
571
|
+
return " ";
|
|
451
572
|
case EnumToken.ColonTokenType:
|
|
452
|
-
return
|
|
573
|
+
return ":";
|
|
574
|
+
case EnumToken.DoubleColonTokenType:
|
|
575
|
+
return "::";
|
|
453
576
|
case EnumToken.SemiColonTokenType:
|
|
454
|
-
return
|
|
577
|
+
return ";";
|
|
455
578
|
case EnumToken.CommaTokenType:
|
|
456
|
-
return
|
|
579
|
+
return ",";
|
|
457
580
|
case EnumToken.ImportantTokenType:
|
|
458
|
-
return
|
|
581
|
+
return "!important";
|
|
582
|
+
case EnumToken.Pipe:
|
|
583
|
+
return "|";
|
|
459
584
|
case EnumToken.AttrTokenType:
|
|
460
585
|
case EnumToken.IdenListTokenType:
|
|
461
|
-
return
|
|
586
|
+
return "[" + token.chi.reduce(reducer, "") + "]";
|
|
462
587
|
case EnumToken.TimeTokenType:
|
|
463
588
|
case EnumToken.AngleTokenType:
|
|
464
589
|
case EnumToken.LengthTokenType:
|
|
465
590
|
case EnumToken.DimensionTokenType:
|
|
466
591
|
case EnumToken.FrequencyTokenType:
|
|
467
592
|
case EnumToken.ResolutionTokenType:
|
|
468
|
-
let val = token.val.typ == EnumToken.FractionTokenType
|
|
593
|
+
let val = token.val.typ == EnumToken.FractionTokenType
|
|
594
|
+
? renderToken(token.val, options, cache)
|
|
595
|
+
: minifyNumber(token.val);
|
|
469
596
|
let unit = token.unit;
|
|
470
|
-
if (token.typ == EnumToken.AngleTokenType && !val.includes(
|
|
597
|
+
if (token.typ == EnumToken.AngleTokenType && !val.includes("/")) {
|
|
471
598
|
const angle = getAngle(token);
|
|
472
599
|
let v;
|
|
473
600
|
let value = val + unit;
|
|
474
|
-
for (const u of [
|
|
601
|
+
for (const u of ["turn", "deg", "rad", "grad"]) {
|
|
475
602
|
if (token.unit == u) {
|
|
476
603
|
continue;
|
|
477
604
|
}
|
|
478
605
|
switch (u) {
|
|
479
|
-
case
|
|
606
|
+
case "turn":
|
|
480
607
|
v = minifyNumber(angle);
|
|
481
608
|
if (v.length + 4 < value.length) {
|
|
482
609
|
val = v;
|
|
@@ -484,7 +611,7 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
|
|
|
484
611
|
value = v + u;
|
|
485
612
|
}
|
|
486
613
|
break;
|
|
487
|
-
case
|
|
614
|
+
case "deg":
|
|
488
615
|
v = minifyNumber(angle * 360);
|
|
489
616
|
if (v.length + 3 < value.length) {
|
|
490
617
|
val = v;
|
|
@@ -492,7 +619,7 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
|
|
|
492
619
|
value = v + u;
|
|
493
620
|
}
|
|
494
621
|
break;
|
|
495
|
-
case
|
|
622
|
+
case "rad":
|
|
496
623
|
v = minifyNumber(angle * (2 * Math.PI));
|
|
497
624
|
if (v.length + 3 < value.length) {
|
|
498
625
|
val = v;
|
|
@@ -500,7 +627,7 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
|
|
|
500
627
|
value = v + u;
|
|
501
628
|
}
|
|
502
629
|
break;
|
|
503
|
-
case
|
|
630
|
+
case "grad":
|
|
504
631
|
v = minifyNumber(angle * 400);
|
|
505
632
|
if (v.length + 4 < value.length) {
|
|
506
633
|
val = v;
|
|
@@ -511,119 +638,148 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
|
|
|
511
638
|
}
|
|
512
639
|
}
|
|
513
640
|
}
|
|
514
|
-
if (val ===
|
|
641
|
+
if (val === "0") {
|
|
515
642
|
if (token.typ == EnumToken.TimeTokenType) {
|
|
516
|
-
return
|
|
643
|
+
return "0s";
|
|
517
644
|
}
|
|
518
645
|
if (token.typ == EnumToken.FrequencyTokenType) {
|
|
519
|
-
return
|
|
646
|
+
return "0Hz";
|
|
520
647
|
}
|
|
521
648
|
// @ts-ignore
|
|
522
649
|
if (token.typ == EnumToken.ResolutionTokenType) {
|
|
523
|
-
return
|
|
650
|
+
return "0x";
|
|
524
651
|
}
|
|
525
|
-
return
|
|
652
|
+
return "0";
|
|
526
653
|
}
|
|
527
654
|
if (token.typ == EnumToken.TimeTokenType) {
|
|
528
|
-
if (unit ==
|
|
655
|
+
if (unit == "ms") {
|
|
529
656
|
// @ts-ignore
|
|
530
657
|
const v = minifyNumber(val / 1000);
|
|
531
658
|
if (v.length + 1 <= val.length) {
|
|
532
|
-
return v +
|
|
659
|
+
return v + "s";
|
|
533
660
|
}
|
|
534
|
-
return val +
|
|
661
|
+
return val + "ms";
|
|
535
662
|
}
|
|
536
|
-
return val +
|
|
663
|
+
return val + "s";
|
|
537
664
|
}
|
|
538
|
-
if (token.typ == EnumToken.ResolutionTokenType && unit ==
|
|
539
|
-
unit =
|
|
665
|
+
if (token.typ == EnumToken.ResolutionTokenType && unit == "dppx") {
|
|
666
|
+
unit = "x";
|
|
540
667
|
}
|
|
541
|
-
return val.includes(
|
|
668
|
+
return val.includes("/") ? val.replace("/", unit + "/") : val + unit;
|
|
542
669
|
case EnumToken.FlexTokenType:
|
|
543
670
|
case EnumToken.PercentageTokenType:
|
|
544
|
-
const uni = token.typ == EnumToken.PercentageTokenType ?
|
|
545
|
-
const perc = token.val.typ == EnumToken.FractionTokenType
|
|
546
|
-
|
|
671
|
+
const uni = token.typ == EnumToken.PercentageTokenType ? "%" : "fr";
|
|
672
|
+
const perc = token.val.typ == EnumToken.FractionTokenType
|
|
673
|
+
? renderToken(token.val, options, cache)
|
|
674
|
+
: minifyNumber(token.val);
|
|
675
|
+
return options.minify && perc == "0" ? "0" : perc.includes("/") ? perc.replace("/", uni + "/") : perc + uni;
|
|
547
676
|
case EnumToken.NumberTokenType:
|
|
548
|
-
return token.val.typ == EnumToken.FractionTokenType
|
|
677
|
+
return token.val.typ == EnumToken.FractionTokenType
|
|
678
|
+
? renderToken(token.val, options, cache)
|
|
679
|
+
: minifyNumber(token.val);
|
|
680
|
+
case EnumToken.AtRuleTokenType:
|
|
681
|
+
return "@" + token.nam;
|
|
549
682
|
case EnumToken.CommentTokenType:
|
|
550
|
-
|
|
551
|
-
|
|
683
|
+
case EnumToken.CDOCOMMNodeType:
|
|
684
|
+
if (options.removeComments &&
|
|
685
|
+
(!options.preserveLicense || !token.val.startsWith("/*!"))) {
|
|
686
|
+
return "";
|
|
552
687
|
}
|
|
553
688
|
case EnumToken.PseudoClassTokenType:
|
|
554
689
|
case EnumToken.PseudoElementTokenType:
|
|
555
690
|
// https://www.w3.org/TR/selectors-4/#single-colon-pseudos
|
|
556
|
-
if (token.typ == EnumToken.PseudoElementTokenType &&
|
|
691
|
+
if (token.typ == EnumToken.PseudoElementTokenType &&
|
|
692
|
+
pseudoElements.includes(token.val.slice(1))) {
|
|
557
693
|
return token.val.slice(1);
|
|
558
694
|
}
|
|
559
695
|
case EnumToken.UrlTokenTokenType:
|
|
560
|
-
// if (token.typ == EnumToken.UrlTokenTokenType) {
|
|
561
|
-
//
|
|
562
|
-
// if (options.output != null) {
|
|
563
|
-
//
|
|
564
|
-
// if (!('original' in token)) {
|
|
565
|
-
//
|
|
566
|
-
// // do not modify original token
|
|
567
|
-
// token = {...token};
|
|
568
|
-
// Object.defineProperty(token, 'original', {
|
|
569
|
-
// enumerable: false,
|
|
570
|
-
// writable: false,
|
|
571
|
-
// value: (token as UrlToken).val
|
|
572
|
-
// })
|
|
573
|
-
// }
|
|
574
|
-
//
|
|
575
|
-
// // @ts-ignore
|
|
576
|
-
// if (!(token.original in cache)) {
|
|
577
|
-
//
|
|
578
|
-
// let output: string = <string>options.output ?? '';
|
|
579
|
-
// const key = output + 'abs';
|
|
580
|
-
//
|
|
581
|
-
// if (!(key in cache)) {
|
|
582
|
-
//
|
|
583
|
-
// // @ts-ignore
|
|
584
|
-
// cache[key] = options.dirname(options.resolve(output, options.cwd).absolute);
|
|
585
|
-
// }
|
|
586
|
-
//
|
|
587
|
-
// // @ts-ignore
|
|
588
|
-
// cache[token.original] = options.resolve(token.original, cache[key]).relative;
|
|
589
|
-
// }
|
|
590
|
-
//
|
|
591
|
-
// // @ts-ignore
|
|
592
|
-
// token.val = cache[token.original];
|
|
593
|
-
// }
|
|
594
|
-
// }
|
|
595
696
|
case EnumToken.HashTokenType:
|
|
596
697
|
case EnumToken.IdenTokenType:
|
|
597
|
-
case EnumToken.AtRuleTokenType:
|
|
598
698
|
case EnumToken.StringTokenType:
|
|
599
699
|
case EnumToken.LiteralTokenType:
|
|
600
700
|
case EnumToken.DashedIdenTokenType:
|
|
601
701
|
case EnumToken.PseudoPageTokenType:
|
|
602
702
|
case EnumToken.ClassSelectorTokenType:
|
|
603
|
-
return
|
|
703
|
+
return token.val;
|
|
604
704
|
case EnumToken.NestingSelectorTokenType:
|
|
605
|
-
return
|
|
705
|
+
return "&";
|
|
606
706
|
case EnumToken.InvalidAttrTokenType:
|
|
607
|
-
return
|
|
707
|
+
return ("[" +
|
|
708
|
+
token.chi.reduce((acc, curr) => acc + renderToken(curr, options, cache), ""));
|
|
608
709
|
case EnumToken.InvalidClassSelectorTokenType:
|
|
609
710
|
return token.val;
|
|
711
|
+
case EnumToken.SupportsQueryUnaryConditionTokenType:
|
|
712
|
+
case EnumToken.WhenElseUnaryConditionTokenType:
|
|
713
|
+
return (renderToken(token.l, options, cache, reducer, errors) +
|
|
714
|
+
" " +
|
|
715
|
+
token.r.reduce((acc, curr) => acc + renderToken(curr, options, cache, reducer, errors), ""));
|
|
716
|
+
case EnumToken.SupportsQueryConditionTokenType:
|
|
717
|
+
case EnumToken.WhenElseQueryConditionTokenType:
|
|
718
|
+
return (token.l.reduce((acc, curr) => acc + renderToken(curr, options, cache, reducer, errors), "") +
|
|
719
|
+
" " +
|
|
720
|
+
renderToken(token.op, options, cache, reducer, errors) +
|
|
721
|
+
" " +
|
|
722
|
+
token.r.reduce((acc, curr) => acc + renderToken(curr, options, cache, reducer, errors), ""));
|
|
723
|
+
case EnumToken.IfConditionTokenType:
|
|
724
|
+
return token.l.length == 0 ? '' : (token.l.reduce((acc, curr) => acc + renderToken(curr, options, cache, reducer, errors), "") +
|
|
725
|
+
':' +
|
|
726
|
+
token.r.reduce((acc, curr) => acc + renderToken(curr, options, cache, reducer, errors), ""));
|
|
727
|
+
case EnumToken.IfElseConditionTokenType:
|
|
728
|
+
return (renderToken(token.l) +
|
|
729
|
+
renderToken(token.r));
|
|
610
730
|
case EnumToken.DeclarationNodeType:
|
|
611
|
-
return token.nam +
|
|
612
|
-
|
|
613
|
-
|
|
731
|
+
return (token.nam +
|
|
732
|
+
":" +
|
|
733
|
+
(options.minify ? filterValues(token.val) : token.val).reduce((acc, curr) => acc + renderToken(curr, options, cache), ""));
|
|
734
|
+
case EnumToken.MediaQueryUnaryFeatureTokenType:
|
|
735
|
+
return (renderToken(token.l, options, cache, reducer, errors) +
|
|
736
|
+
" " +
|
|
737
|
+
token.r.reduce((acc, curr) => acc + renderToken(curr, options, cache), ""));
|
|
738
|
+
case EnumToken.MediaQueryConditionTokenType: {
|
|
739
|
+
const indent = token.op.typ == EnumToken.LtTokenType ||
|
|
740
|
+
token.op.typ == EnumToken.GtTokenType ||
|
|
741
|
+
token.op.typ == EnumToken.ColonTokenType ||
|
|
742
|
+
token.op.typ == EnumToken.DelimTokenType ||
|
|
743
|
+
token.op.typ == EnumToken.LteTokenType ||
|
|
744
|
+
token.op.typ == EnumToken.GteTokenType
|
|
745
|
+
? ""
|
|
746
|
+
: " ";
|
|
747
|
+
return (token.l.reduce((acc, curr) => acc + renderToken(curr, options, cache, reducer, errors), "") +
|
|
748
|
+
indent +
|
|
749
|
+
renderToken(token.op, options, cache, reducer, errors) +
|
|
750
|
+
indent +
|
|
751
|
+
token.r.reduce((acc, curr) => acc + renderToken(curr, options, cache), ""));
|
|
752
|
+
}
|
|
753
|
+
case EnumToken.MediaRangeQueryTokenType:
|
|
754
|
+
return (token.l.reduce((acc, curr) => acc + renderToken(curr, options, cache), "") +
|
|
755
|
+
renderToken(token.op1) +
|
|
756
|
+
token.val.reduce((acc, curr) => acc + renderToken(curr, options, cache, reducer, errors), "") +
|
|
757
|
+
renderToken(token.op2) +
|
|
758
|
+
token.r.reduce((acc, curr) => acc + renderToken(curr, options, cache), ""));
|
|
614
759
|
case EnumToken.MediaFeatureTokenType:
|
|
615
760
|
return token.val;
|
|
616
|
-
case EnumToken.
|
|
617
|
-
return
|
|
618
|
-
case EnumToken.
|
|
619
|
-
return
|
|
620
|
-
case EnumToken.
|
|
621
|
-
return
|
|
622
|
-
case EnumToken.
|
|
623
|
-
return
|
|
761
|
+
case EnumToken.NotTokenType:
|
|
762
|
+
return "not";
|
|
763
|
+
case EnumToken.OnlyTokenType:
|
|
764
|
+
return "only";
|
|
765
|
+
case EnumToken.AndTokenType:
|
|
766
|
+
return "and";
|
|
767
|
+
case EnumToken.OrTokenType:
|
|
768
|
+
return "or";
|
|
769
|
+
case EnumToken.InvalidMediaQueryTokenType:
|
|
770
|
+
case EnumToken.InvalidDeclarationNodeType:
|
|
771
|
+
case EnumToken.InvalidCommentTokenType:
|
|
772
|
+
case EnumToken.BadCommentTokenType:
|
|
773
|
+
case EnumToken.BadCdoTokenType:
|
|
774
|
+
case EnumToken.BadStringTokenType:
|
|
775
|
+
case EnumToken.EOFTokenType:
|
|
776
|
+
return "";
|
|
777
|
+
default:
|
|
778
|
+
console.debug({ token });
|
|
779
|
+
throw new Error(`Unsupported token type for ${EnumToken[token.typ]}`);
|
|
624
780
|
}
|
|
625
|
-
errors?.push({ action:
|
|
626
|
-
return
|
|
781
|
+
errors?.push({ action: "ignore", message: `render: unexpected token ${JSON.stringify(token, null, 1)}` });
|
|
782
|
+
return "";
|
|
627
783
|
}
|
|
628
784
|
/**
|
|
629
785
|
* Remove whitespace tokens that are not needed
|
|
@@ -634,10 +790,13 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
|
|
|
634
790
|
function filterValues(values) {
|
|
635
791
|
let i = 0;
|
|
636
792
|
for (; i < values.length; i++) {
|
|
637
|
-
if (values[i].typ == EnumToken.ImportantTokenType && values[i - 1]?.typ
|
|
793
|
+
if (values[i].typ == EnumToken.ImportantTokenType && values[i - 1]?.typ === EnumToken.WhitespaceTokenType) {
|
|
638
794
|
values.splice(i - 1, 1);
|
|
639
795
|
}
|
|
640
|
-
else if (
|
|
796
|
+
else if (tokensfuncSet.has(values[i].typ) &&
|
|
797
|
+
"chi" in values[i] &&
|
|
798
|
+
values[i].typ != EnumToken.WildCardFunctionTokenType &&
|
|
799
|
+
values[i + 1]?.typ == EnumToken.WhitespaceTokenType) {
|
|
641
800
|
values.splice(i + 1, 1);
|
|
642
801
|
}
|
|
643
802
|
}
|