@tbela99/css-parser 1.4.7 → 1.4.9
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 +33 -2
- package/dist/index-umd-web.js +12953 -13248
- package/dist/index.cjs +10358 -10655
- package/dist/index.d.ts +820 -89
- package/dist/lib/ast/features/calc.js +3 -1
- package/dist/lib/ast/features/if.js +2 -96
- package/dist/lib/ast/features/prefix.js +7 -2
- package/dist/lib/ast/features/shorthand.js +1 -1
- package/dist/lib/ast/features/transform.js +1 -1
- package/dist/lib/ast/find.js +6 -6
- package/dist/lib/ast/math/expression.js +30 -12
- package/dist/lib/ast/minify.js +34 -5
- package/dist/lib/ast/transform/perspective.js +1 -1
- package/dist/lib/ast/transform/utils.js +29 -21
- package/dist/lib/ast/types.js +12 -0
- package/dist/lib/fs/resolve.js +2 -17
- package/dist/lib/parser/declaration/list.js +6 -1
- package/dist/lib/parser/linesmap.js +78 -0
- package/dist/lib/parser/parse.js +1264 -151
- package/dist/lib/parser/source.js +103 -0
- package/dist/lib/parser/tokenize.js +112 -133
- package/dist/lib/parser/utils/at-rule-container.js +12 -44
- package/dist/lib/parser/utils/at-rule-generic.js +12 -24
- package/dist/lib/parser/utils/at-rule-import.js +4 -172
- package/dist/lib/parser/utils/at-rule-media.js +27 -263
- package/dist/lib/parser/utils/at-rule-support.js +8 -258
- package/dist/lib/parser/utils/at-rule-when-else.js +4 -214
- package/dist/lib/parser/utils/declaration.js +5 -95
- package/dist/lib/parser/utils/hash.js +54 -1
- package/dist/lib/parser/utils/selector.js +1 -1
- package/dist/lib/parser/utils/token.js +1 -0
- package/dist/lib/renderer/render.js +79 -45
- package/dist/lib/renderer/sourcemap/sourcemap.js +47 -43
- package/dist/lib/syntax/color/cmyk.js +3 -3
- package/dist/lib/syntax/color/color-mix.js +15 -8
- package/dist/lib/syntax/color/color.js +19 -11
- package/dist/lib/syntax/color/hex.js +1 -1
- package/dist/lib/syntax/color/hsl.js +6 -6
- package/dist/lib/syntax/color/hwb.js +6 -6
- package/dist/lib/syntax/color/lab.js +5 -5
- package/dist/lib/syntax/color/lch.js +6 -6
- package/dist/lib/syntax/color/oklab.js +5 -5
- package/dist/lib/syntax/color/oklch.js +5 -5
- package/dist/lib/syntax/color/p3.js +9 -1
- package/dist/lib/syntax/color/{relativecolor.js → relative-color.js} +8 -1
- package/dist/lib/syntax/color/utils/distance.js +7 -2
- package/dist/lib/syntax/constants.js +5 -1
- package/dist/lib/syntax/syntax.js +12 -21
- package/dist/lib/validation/config.json.js +27 -6
- package/dist/lib/validation/match.js +42 -845
- package/dist/node.js +136 -11
- package/dist/web.js +135 -8
- package/llms.txt +73 -0
- package/package.json +14 -17
|
@@ -42,46 +42,6 @@ function parseGridTemplate(template) {
|
|
|
42
42
|
}
|
|
43
43
|
return buffer.length > 0 ? result + buffer : result;
|
|
44
44
|
}
|
|
45
|
-
/**
|
|
46
|
-
*
|
|
47
|
-
* @param tokens
|
|
48
|
-
* @returns
|
|
49
|
-
*/
|
|
50
|
-
// export function isDeclarationValue(tokens: Token[]): { success: boolean; errors: ErrorDescription[] } {
|
|
51
|
-
// const stack: Token[] = [];
|
|
52
|
-
// let i: number = 0;
|
|
53
|
-
// for (; i < tokens.length; i++) {
|
|
54
|
-
// if (tokens[i].typ === EnumToken.WhitespaceTokenType || tokens[i].typ === EnumToken.CommentTokenType) {
|
|
55
|
-
// continue;
|
|
56
|
-
// } else if (tokensfuncDefMap.has(tokens[i].typ) || tokens[i].typ === EnumToken.StartParensTokenType) {
|
|
57
|
-
// stack.push(tokens[i]);
|
|
58
|
-
// } else if (tokens[i].typ === EnumToken.CommaTokenType || tokens[i].typ === EnumToken.LiteralTokenType) {
|
|
59
|
-
// stack.push(tokens[i]);
|
|
60
|
-
// } else if (isValue(tokens[i])) {
|
|
61
|
-
// if (stack.at(-1)?.typ === EnumToken.LiteralTokenType) {
|
|
62
|
-
// stack.pop();
|
|
63
|
-
// }
|
|
64
|
-
// } else if (tokens[i].typ === EnumToken.EndParensTokenType) {
|
|
65
|
-
// if (stack.at(-1)?.typ === EnumToken.StartParensTokenType || tokensfuncDefMap.has(stack.at(-1)?.typ)) {
|
|
66
|
-
// stack.pop();
|
|
67
|
-
// }
|
|
68
|
-
// }
|
|
69
|
-
// }
|
|
70
|
-
// return {
|
|
71
|
-
// success: stack.length === 0,
|
|
72
|
-
// errors:
|
|
73
|
-
// stack.length > 0
|
|
74
|
-
// ? [
|
|
75
|
-
// {
|
|
76
|
-
// action: "drop",
|
|
77
|
-
// message: `unexpected declaration value at ${stack.at(-1)?.[LOC]?.src}:${stack.at(-1)?.[LOC]?.sta.lin}:${stack.at(-1)?.[LOC]?.sta.col}`,
|
|
78
|
-
// node: stack.at(-1),
|
|
79
|
-
// location: stack.at(-1)?.[LOC],
|
|
80
|
-
// },
|
|
81
|
-
// ]
|
|
82
|
-
// : [],
|
|
83
|
-
// };
|
|
84
|
-
// }
|
|
85
45
|
/**
|
|
86
46
|
* Parse declaration
|
|
87
47
|
* @param tokens
|
|
@@ -129,58 +89,6 @@ function parseDeclaration(tokens, parent, options, errors) {
|
|
|
129
89
|
});
|
|
130
90
|
}
|
|
131
91
|
tokens = trimArray(tokens.slice(i + 1));
|
|
132
|
-
// for (i = 0; i < tokens.length; i++) {
|
|
133
|
-
// const token = tokens[i];
|
|
134
|
-
// if (
|
|
135
|
-
// token.typ == EnumToken.WhitespaceTokenType ||
|
|
136
|
-
// token.typ == EnumToken.CommentTokenType ||
|
|
137
|
-
// token.typ == EnumToken.InvalidCommentTokenType
|
|
138
|
-
// ) {
|
|
139
|
-
// continue;
|
|
140
|
-
// }
|
|
141
|
-
// if (tokens[i].typ === EnumToken.UrlFunctionTokenDefType) {
|
|
142
|
-
// let k: number = i;
|
|
143
|
-
// while (k < tokens.length) {
|
|
144
|
-
// if (
|
|
145
|
-
// tokens[++k]?.typ === EnumToken.WhitespaceTokenType ||
|
|
146
|
-
// tokens[k]?.typ === EnumToken.CommentTokenType
|
|
147
|
-
// ) {
|
|
148
|
-
// continue;
|
|
149
|
-
// }
|
|
150
|
-
// if (tokens[k]?.typ !== EnumToken.EndParensTokenType) {
|
|
151
|
-
// break;
|
|
152
|
-
// }
|
|
153
|
-
// }
|
|
154
|
-
// if (
|
|
155
|
-
// tokens[k].typ === EnumToken.LiteralTokenType ||
|
|
156
|
-
// tokens[k].typ === EnumToken.IdenTokenType ||
|
|
157
|
-
// tokens[k].typ === EnumToken.DashedIdenTokenType ||
|
|
158
|
-
// tokens[k].typ === EnumToken.HashTokenType ||
|
|
159
|
-
// tokens[k].typ === EnumToken.ClassSelectorTokenType
|
|
160
|
-
// ) {
|
|
161
|
-
// let j: number = k;
|
|
162
|
-
// let val: string = (tokens[k] as LiteralToken | IdentToken | DashedIdentToken | HashToken).val;
|
|
163
|
-
// while (j + 1 < tokens.length) {
|
|
164
|
-
// if (
|
|
165
|
-
// tokens[++j].typ !== EnumToken.LiteralTokenType &&
|
|
166
|
-
// tokens[j].typ !== EnumToken.IdenTokenType &&
|
|
167
|
-
// tokens[j].typ !== EnumToken.DashedIdenTokenType &&
|
|
168
|
-
// tokens[j].typ !== EnumToken.HashTokenType &&
|
|
169
|
-
// tokens[j].typ !== EnumToken.ClassSelectorTokenType
|
|
170
|
-
// ) {
|
|
171
|
-
// break;
|
|
172
|
-
// }
|
|
173
|
-
// val += (tokens[j] as LiteralToken | IdentToken | DashedIdentToken | HashToken).val;
|
|
174
|
-
// }
|
|
175
|
-
// Object.assign(tokens[k] as LiteralToken | IdentToken | DashedIdentToken | HashToken, {
|
|
176
|
-
// typ: EnumToken.UrlTokenTokenType,
|
|
177
|
-
// val,
|
|
178
|
-
// });
|
|
179
|
-
// tokens[k][LOC]!.end = tokens[j][LOC]!.end;
|
|
180
|
-
// tokens.splice(k + 1, j - k - 1);
|
|
181
|
-
// }
|
|
182
|
-
// }
|
|
183
|
-
// }
|
|
184
92
|
if (validate && name.typ === EnumToken.IdenTokenType) {
|
|
185
93
|
if (parent != null &&
|
|
186
94
|
parent.typ === EnumToken.AtRuleNodeType &&
|
|
@@ -271,7 +179,7 @@ function parseDeclaration(tokens, parent, options, errors) {
|
|
|
271
179
|
action: "drop",
|
|
272
180
|
message: "declaration value missing",
|
|
273
181
|
node: name,
|
|
274
|
-
location: name[LOC],
|
|
182
|
+
location: options.source.getSourceLocation(name[LOC].sta),
|
|
275
183
|
});
|
|
276
184
|
name[LOC] = {
|
|
277
185
|
...name[LOC],
|
|
@@ -486,7 +394,9 @@ function parseDeclaration(tokens, parent, options, errors) {
|
|
|
486
394
|
tokens[index].typ = EnumToken.FunctionTokenType;
|
|
487
395
|
errors.push({
|
|
488
396
|
action: "drop",
|
|
489
|
-
message: `invalid color
|
|
397
|
+
message: `invalid color`,
|
|
398
|
+
node: tokens[index],
|
|
399
|
+
location: options.source.getSourceLocation(tokens[index][LOC].sta),
|
|
490
400
|
});
|
|
491
401
|
}
|
|
492
402
|
}
|
|
@@ -538,7 +448,7 @@ function parseDeclaration(tokens, parent, options, errors) {
|
|
|
538
448
|
action: "drop",
|
|
539
449
|
message: "unbalanced token",
|
|
540
450
|
node: stack[stack.length - 1],
|
|
541
|
-
location: stack[stack.length - 1][LOC],
|
|
451
|
+
location: options.source.getSourceLocation(stack[stack.length - 1][LOC].sta),
|
|
542
452
|
});
|
|
543
453
|
name[LOC] = {
|
|
544
454
|
...name[LOC],
|
|
@@ -34,6 +34,34 @@ function hashId(input, length = 6) {
|
|
|
34
34
|
}
|
|
35
35
|
return chars.join("");
|
|
36
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Object to string
|
|
39
|
+
* @param input
|
|
40
|
+
* @returns
|
|
41
|
+
*/
|
|
42
|
+
function toSortedString(input) {
|
|
43
|
+
if (input == null) {
|
|
44
|
+
return "null";
|
|
45
|
+
}
|
|
46
|
+
if (typeof input !== "object") {
|
|
47
|
+
return String(input);
|
|
48
|
+
}
|
|
49
|
+
if (Array.isArray(input)) {
|
|
50
|
+
return JSON.stringify(input.map(toSortedString));
|
|
51
|
+
}
|
|
52
|
+
return `{${Object.keys(input)
|
|
53
|
+
.sort()
|
|
54
|
+
.map((k) => `${k}:${toSortedString(input[k])}`)
|
|
55
|
+
.join(",")}}`;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Object hash
|
|
59
|
+
* @param object
|
|
60
|
+
* @returns
|
|
61
|
+
*/
|
|
62
|
+
function objectHash(object) {
|
|
63
|
+
return hashId(toSortedString(object));
|
|
64
|
+
}
|
|
37
65
|
/**
|
|
38
66
|
* convert input to hex
|
|
39
67
|
* @param input
|
|
@@ -82,5 +110,30 @@ async function hash(input, length = 6, algo) {
|
|
|
82
110
|
}
|
|
83
111
|
return hashId(input, length);
|
|
84
112
|
}
|
|
113
|
+
/**
|
|
114
|
+
* generate a hash
|
|
115
|
+
* @param input
|
|
116
|
+
* @param length
|
|
117
|
+
* @param algo
|
|
118
|
+
*/
|
|
119
|
+
function syncHash(input, length = 6, algo) {
|
|
120
|
+
let result;
|
|
121
|
+
if (algo != null) {
|
|
122
|
+
switch (algo) {
|
|
123
|
+
case "hex":
|
|
124
|
+
return toHex(input).slice(0, length);
|
|
125
|
+
case "base64url":
|
|
126
|
+
case "base64":
|
|
127
|
+
result = btoa(input);
|
|
128
|
+
if (algo == "base64url") {
|
|
129
|
+
result = result.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
130
|
+
}
|
|
131
|
+
return result.slice(0, length);
|
|
132
|
+
default:
|
|
133
|
+
throw new Error(`Unsupported hash algorithm: ${algo}`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return hashId(input, length);
|
|
137
|
+
}
|
|
85
138
|
|
|
86
|
-
export { DIGITS, FIRST_ALPHABET, FULL_ALPHABET, LOWER, hash, hashAlgorithms, hashId };
|
|
139
|
+
export { DIGITS, FIRST_ALPHABET, FULL_ALPHABET, LOWER, hash, hashAlgorithms, hashId, objectHash, syncHash };
|
|
@@ -58,7 +58,7 @@ function parseSelector(tokens, context, options, errors) {
|
|
|
58
58
|
chi: [],
|
|
59
59
|
[LOC]: {
|
|
60
60
|
...tokens[0][LOC],
|
|
61
|
-
end:
|
|
61
|
+
end: tokens[tokens.length - 1]?.[LOC]?.end ?? tokens[0]?.[LOC]?.end
|
|
62
62
|
},
|
|
63
63
|
[TOKENS]: tokens.length === 0 ? null : tokens,
|
|
64
64
|
[STATE]: result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
|
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import { convertColor, toPrecisionAngle, getAngle } from '../syntax/color/color.js';
|
|
1
|
+
import { convertColor, toPrecisionAngle, toPrecisionValue, getAngle } from '../syntax/color/color.js';
|
|
2
2
|
import { reduceHexValue } from '../syntax/color/hex.js';
|
|
3
3
|
import { EnumToken, minifyNumber, ColorType } from '../ast/types.js';
|
|
4
4
|
import { expand } from '../ast/expand.js';
|
|
5
5
|
import { SourceMap } from './sourcemap/sourcemap.js';
|
|
6
6
|
import { pseudoElements, urlTokenMatcher, PARENT, tokensfuncSet, LOC, colorPrecision } from '../syntax/constants.js';
|
|
7
7
|
import { reducegradientBackgroundPosition, reduceConicColorStops, reduceColorStops, parseColor } from '../syntax/syntax.js';
|
|
8
|
-
import { move } from '../parser/tokenize.js';
|
|
9
8
|
import { equalsIgnoreCase } from '../parser/utils/text.js';
|
|
10
9
|
import { toDegrees } from '../parser/utils/angle.js';
|
|
10
|
+
import { LineMap } from '../parser/linesmap.js';
|
|
11
|
+
import { dirname } from '../fs/resolve.js';
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* render ast
|
|
@@ -61,11 +62,12 @@ function doRender(data, options = {}, mapping) {
|
|
|
61
62
|
const errors = [];
|
|
62
63
|
const sourcemap = options.sourcemap ? new SourceMap() : null;
|
|
63
64
|
const cache = Object.create(null);
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
65
|
+
const sourceLocation = {
|
|
66
|
+
srcId: 0,
|
|
67
|
+
sta: 0,
|
|
68
|
+
end: 0,
|
|
68
69
|
};
|
|
70
|
+
const linesMap = new LineMap([]);
|
|
69
71
|
let code = "";
|
|
70
72
|
if (mapping != null) {
|
|
71
73
|
if (mapping.importMapping != null) {
|
|
@@ -74,7 +76,11 @@ function doRender(data, options = {}, mapping) {
|
|
|
74
76
|
}
|
|
75
77
|
}
|
|
76
78
|
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}`;
|
|
77
|
-
move(
|
|
79
|
+
move(sourceLocation, linesMap, code);
|
|
80
|
+
}
|
|
81
|
+
if (options.output != null) {
|
|
82
|
+
// @ts-ignore
|
|
83
|
+
options.output = options.resolve(options.output, options.cwd).absolute;
|
|
78
84
|
}
|
|
79
85
|
const result = {
|
|
80
86
|
code: code +
|
|
@@ -82,7 +88,7 @@ function doRender(data, options = {}, mapping) {
|
|
|
82
88
|
[EnumToken.StyleSheetNodeType, EnumToken.AtRuleNodeType, EnumToken.RuleNodeType].includes(data.typ) &&
|
|
83
89
|
"chi" in data
|
|
84
90
|
? expand(data)
|
|
85
|
-
: data, options, sourcemap,
|
|
91
|
+
: data, options, sourcemap, sourceLocation, linesMap, errors, function reducer(acc, curr) {
|
|
86
92
|
if (curr.typ == EnumToken.CommentTokenType && options.removeComments) {
|
|
87
93
|
if (!options.preserveLicense || !curr.val.startsWith("/*!")) {
|
|
88
94
|
return acc;
|
|
@@ -96,14 +102,10 @@ function doRender(data, options = {}, mapping) {
|
|
|
96
102
|
total: `${(performance.now() - startTime).toFixed(2)}ms`,
|
|
97
103
|
},
|
|
98
104
|
};
|
|
99
|
-
if (options.output != null) {
|
|
100
|
-
// @ts-ignore
|
|
101
|
-
options.output = options.resolve(options.output, options.cwd).absolute;
|
|
102
|
-
}
|
|
103
105
|
if (sourcemap != null) {
|
|
104
106
|
result.map = sourcemap;
|
|
105
107
|
if (options.sourcemap === "inline") {
|
|
106
|
-
result.code += `\n/*# sourceMappingURL
|
|
108
|
+
result.code += `\n/*# sourceMappingURL=${result.map.toUrl()} */`;
|
|
107
109
|
}
|
|
108
110
|
}
|
|
109
111
|
return result;
|
|
@@ -119,24 +121,55 @@ function doRender(data, options = {}, mapping) {
|
|
|
119
121
|
*
|
|
120
122
|
* @internal
|
|
121
123
|
*/
|
|
122
|
-
function updateSourceMap(node, options, cache, sourcemap,
|
|
124
|
+
function updateSourceMap(node, options, cache, sourcemap, sourceLocation, linesMap, str) {
|
|
123
125
|
if ([
|
|
124
126
|
EnumToken.RuleNodeType,
|
|
125
127
|
EnumToken.AtRuleNodeType,
|
|
126
128
|
EnumToken.KeyFramesRuleNodeType,
|
|
127
129
|
EnumToken.KeyframesAtRuleNodeType,
|
|
128
130
|
].includes(node.typ)) {
|
|
129
|
-
let
|
|
130
|
-
|
|
131
|
-
|
|
131
|
+
let srcId = node[LOC]?.srcId ?? 0;
|
|
132
|
+
let sourceFileName = options.sourcesMap?.get(srcId)?.getFileName?.() || null;
|
|
133
|
+
if (sourceFileName != null && options.output != null) {
|
|
134
|
+
if (!cache.has(sourceFileName)) {
|
|
135
|
+
cache.set(sourceFileName, options.resolve(sourceFileName, dirname(options.output)).relative);
|
|
136
|
+
}
|
|
137
|
+
sourceFileName = cache.get(sourceFileName);
|
|
138
|
+
}
|
|
132
139
|
// @ts-ignore
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
140
|
+
sourcemap.add(...linesMap.getOffsets(sourceLocation.end), srcId,
|
|
141
|
+
// @ts-ignore
|
|
142
|
+
...options.sourcesMap?.get(srcId)?.getOffsets(sourceLocation.sta), sourceFileName, options.sourcesMap?.get(srcId)?.getContent?.());
|
|
143
|
+
}
|
|
144
|
+
move(sourceLocation, linesMap, str);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Update position
|
|
148
|
+
* @param position
|
|
149
|
+
* @param str
|
|
150
|
+
*/
|
|
151
|
+
function move(sourceLocation, linesMap, str) {
|
|
152
|
+
let i = 0;
|
|
153
|
+
let codepoint;
|
|
154
|
+
let char;
|
|
155
|
+
for (; i < str.length; i++) {
|
|
156
|
+
char = str.charAt(i);
|
|
157
|
+
codepoint = char.charCodeAt(0);
|
|
158
|
+
sourceLocation.end += char.length;
|
|
159
|
+
if (codepoint == 0xa || // \n
|
|
160
|
+
codepoint == 0xb || // \v
|
|
161
|
+
codepoint == 0xc || // \f
|
|
162
|
+
codepoint == 0xd || // \r
|
|
163
|
+
codepoint == 0x2028 || // \u2028
|
|
164
|
+
codepoint == 0x2029 // \u2029
|
|
165
|
+
) {
|
|
166
|
+
if (codepoint == 0xd && i + 1 < str.length && str.charCodeAt(i + 1) == 0xa) ;
|
|
167
|
+
else if (codepoint == 0xa && i > 0 && str.charCodeAt(i - 1) == 0xd) ;
|
|
168
|
+
else {
|
|
169
|
+
linesMap.addLineStart(sourceLocation.end);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
138
172
|
}
|
|
139
|
-
move(position, str);
|
|
140
173
|
}
|
|
141
174
|
/**
|
|
142
175
|
* render ast node
|
|
@@ -152,7 +185,7 @@ function updateSourceMap(node, options, cache, sourcemap, position, str) {
|
|
|
152
185
|
*
|
|
153
186
|
* @internal
|
|
154
187
|
*/
|
|
155
|
-
function renderAstNode(data, options, sourcemap,
|
|
188
|
+
function renderAstNode(data, options, sourcemap, sourceLocation, linesMap, errors, reducer, cache, level = 0, indents = []) {
|
|
156
189
|
if (indents.length < level + 1) {
|
|
157
190
|
indents.push(options.indent.repeat(level));
|
|
158
191
|
}
|
|
@@ -177,19 +210,16 @@ function renderAstNode(data, options, sourcemap, position, errors, reducer, cach
|
|
|
177
210
|
: "";
|
|
178
211
|
case EnumToken.StyleSheetNodeType:
|
|
179
212
|
return data.chi.reduce((css, node) => {
|
|
180
|
-
const
|
|
213
|
+
const hasPreviousContent = css !== "";
|
|
214
|
+
const str = renderAstNode(node, options, sourcemap, sourceLocation, linesMap, errors, reducer, cache, level, indents);
|
|
181
215
|
if (str === "") {
|
|
182
216
|
return css;
|
|
183
217
|
}
|
|
184
|
-
if (css === "") {
|
|
185
|
-
if (sourcemap != null && node[LOC] != null) {
|
|
186
|
-
updateSourceMap(node, options, cache, sourcemap, position, str);
|
|
187
|
-
}
|
|
188
|
-
return str;
|
|
189
|
-
}
|
|
190
218
|
if (sourcemap != null && node[LOC] != null) {
|
|
191
|
-
|
|
192
|
-
|
|
219
|
+
updateSourceMap(node, options, cache, sourcemap, sourceLocation, linesMap, (hasPreviousContent ? options.newLine : "") + str);
|
|
220
|
+
}
|
|
221
|
+
if (!hasPreviousContent) {
|
|
222
|
+
return str;
|
|
193
223
|
}
|
|
194
224
|
return `${css}${options.newLine}${str}`;
|
|
195
225
|
}, "");
|
|
@@ -228,9 +258,9 @@ function renderAstNode(data, options, sourcemap, position, errors, reducer, cach
|
|
|
228
258
|
}
|
|
229
259
|
// else if (node.typ == EnumToken.AtRuleNodeType && !("chi" in node)) {
|
|
230
260
|
// str = `${(<AstAtRule>node).val === "" ? "" : options.indent || " "}${(<AstAtRule>node).val};`;
|
|
231
|
-
// }
|
|
261
|
+
// }
|
|
232
262
|
else {
|
|
233
|
-
str = renderAstNode(node, options, sourcemap,
|
|
263
|
+
str = renderAstNode(node, options, sourcemap, sourceLocation, linesMap, errors, reducer, cache, level + 1, indents);
|
|
234
264
|
}
|
|
235
265
|
if (css === "") {
|
|
236
266
|
return str;
|
|
@@ -246,17 +276,20 @@ function renderAstNode(data, options, sourcemap, position, errors, reducer, cach
|
|
|
246
276
|
if (children.endsWith(";")) {
|
|
247
277
|
children = children.slice(0, -1);
|
|
248
278
|
}
|
|
249
|
-
|
|
250
|
-
|
|
279
|
+
const rendered = [EnumToken.AtRuleNodeType, EnumToken.KeyframesAtRuleNodeType].includes(data.typ)
|
|
280
|
+
? `@${data.nam}${data.val === "" ? "" : options.indent || " "}${data.val}${options.indent}{${options.newLine}` +
|
|
251
281
|
(children === "" ? "" : indentSub + children + options.newLine) +
|
|
252
282
|
indent +
|
|
253
|
-
`}`
|
|
283
|
+
`}`
|
|
284
|
+
: data.sel +
|
|
285
|
+
`${options.indent}{${options.newLine}` +
|
|
286
|
+
(children === "" ? "" : indentSub + children + options.newLine) +
|
|
287
|
+
indent +
|
|
288
|
+
`}`;
|
|
289
|
+
if (sourcemap != null && data[LOC] != null) {
|
|
290
|
+
updateSourceMap(data, options, cache, sourcemap, { ...sourceLocation }, linesMap.clone(), rendered);
|
|
254
291
|
}
|
|
255
|
-
return
|
|
256
|
-
`${options.indent}{${options.newLine}` +
|
|
257
|
-
(children === "" ? "" : indentSub + children + options.newLine) +
|
|
258
|
-
indent +
|
|
259
|
-
`}`);
|
|
292
|
+
return rendered;
|
|
260
293
|
// case EnumToken.CssVariableTokenType:
|
|
261
294
|
// case EnumToken.CssVariableImportTokenType:
|
|
262
295
|
// return `@value ${(<CssVariableToken | CssVariableImportTokenType>data).val}:${options.indent}${filterValues(
|
|
@@ -1136,7 +1169,8 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
|
|
|
1136
1169
|
if (token.typ == EnumToken.ResolutionTokenType && unit == "dppx") {
|
|
1137
1170
|
unit = "x";
|
|
1138
1171
|
}
|
|
1139
|
-
|
|
1172
|
+
// @ts-ignore
|
|
1173
|
+
return val.includes("/") ? val.replace("/", unit + "/") : minifyNumber(toPrecisionValue(val)) + unit;
|
|
1140
1174
|
case EnumToken.FlexTokenType:
|
|
1141
1175
|
case EnumToken.PercentageTokenType:
|
|
1142
1176
|
const uni = token.typ == EnumToken.PercentageTokenType ? "%" : "fr";
|
|
@@ -1275,4 +1309,4 @@ function filterValues(values) {
|
|
|
1275
1309
|
return values;
|
|
1276
1310
|
}
|
|
1277
1311
|
|
|
1278
|
-
export { doRender, filterValues, renderValue };
|
|
1312
|
+
export { doRender, filterValues, move, renderValue };
|
|
@@ -13,69 +13,73 @@ class SourceMap {
|
|
|
13
13
|
* Version
|
|
14
14
|
* @private
|
|
15
15
|
*/
|
|
16
|
-
|
|
16
|
+
version = 3;
|
|
17
|
+
/**
|
|
18
|
+
* Sources map
|
|
19
|
+
* @private
|
|
20
|
+
*/
|
|
21
|
+
sourcesMap = [];
|
|
17
22
|
/**
|
|
18
23
|
* Sources
|
|
19
24
|
* @private
|
|
20
25
|
*/
|
|
21
|
-
|
|
26
|
+
sources = [];
|
|
22
27
|
/**
|
|
23
28
|
* Map
|
|
24
29
|
* @private
|
|
25
30
|
*/
|
|
26
|
-
|
|
31
|
+
map = new Map();
|
|
27
32
|
/**
|
|
28
33
|
* Line
|
|
29
34
|
* @private
|
|
30
35
|
*/
|
|
31
|
-
|
|
36
|
+
line = -1;
|
|
32
37
|
/**
|
|
33
38
|
* Add a location
|
|
34
39
|
* @param source
|
|
35
40
|
* @param original
|
|
36
41
|
*/
|
|
37
|
-
add(
|
|
38
|
-
if (
|
|
39
|
-
if (
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
const line = source.sta.lin - 1;
|
|
43
|
-
let record;
|
|
44
|
-
if (line > this.#line) {
|
|
45
|
-
this.#line = line;
|
|
46
|
-
}
|
|
47
|
-
if (!this.#map.has(line)) {
|
|
48
|
-
record = [
|
|
49
|
-
Math.max(0, source.sta.col - 1),
|
|
50
|
-
this.#sources.indexOf(original.src),
|
|
51
|
-
original.sta.lin - 1,
|
|
52
|
-
original.sta.col - 1,
|
|
53
|
-
];
|
|
54
|
-
this.#map.set(line, [record]);
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
const arr = this.#map.get(line);
|
|
58
|
-
record = [
|
|
59
|
-
Math.max(0, source.sta.col - 1 - arr[0][0]),
|
|
60
|
-
this.#sources.indexOf(original.src) - arr[0][1],
|
|
61
|
-
original.sta.lin - 1,
|
|
62
|
-
original.sta.col - 1,
|
|
63
|
-
];
|
|
64
|
-
arr.push(record);
|
|
65
|
-
}
|
|
66
|
-
if (this.lastLocation != null) {
|
|
67
|
-
record[2] -= this.lastLocation.sta.lin - 1;
|
|
68
|
-
record[3] -= this.lastLocation.sta.col - 1;
|
|
42
|
+
add(newLine, newColumn, srcId, ln, col, sourceFileName, sourceContent) {
|
|
43
|
+
if (!this.sourcesMap.includes(srcId)) {
|
|
44
|
+
if (sourceFileName == null && sourceContent != null) {
|
|
45
|
+
sourceFileName = "data:text/css;charset=utf-8;base64," + btoa(sourceContent);
|
|
69
46
|
}
|
|
70
|
-
this.
|
|
47
|
+
this.sourcesMap.push(srcId);
|
|
48
|
+
this.sources.push(sourceFileName || null);
|
|
49
|
+
}
|
|
50
|
+
const line = newLine - 1;
|
|
51
|
+
let record;
|
|
52
|
+
if (line > this.line) {
|
|
53
|
+
this.line = line;
|
|
54
|
+
}
|
|
55
|
+
if (!this.map.has(line)) {
|
|
56
|
+
record = [Math.max(0, newColumn - 1), this.sourcesMap.indexOf(srcId), ln - 1, col - 1];
|
|
57
|
+
this.map.set(line, [record]);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
const arr = this.map.get(line);
|
|
61
|
+
record = [
|
|
62
|
+
Math.max(0, newColumn - 1 - arr[0][0]),
|
|
63
|
+
this.sourcesMap.indexOf(srcId) - arr[0][1],
|
|
64
|
+
ln - 1,
|
|
65
|
+
col - 1,
|
|
66
|
+
];
|
|
67
|
+
arr.push(record);
|
|
68
|
+
}
|
|
69
|
+
if (this.lastLocation != null) {
|
|
70
|
+
record[2] -= this.lastLocation.ln - 1;
|
|
71
|
+
record[3] -= this.lastLocation.col - 1;
|
|
71
72
|
}
|
|
73
|
+
this.lastLocation ??= { ln, col };
|
|
74
|
+
this.lastLocation.ln = ln;
|
|
75
|
+
this.lastLocation.col = col;
|
|
72
76
|
}
|
|
73
77
|
/**
|
|
74
78
|
* Convert to URL encoded string
|
|
75
79
|
*/
|
|
76
80
|
toUrl() {
|
|
77
81
|
// /*# sourceMappingURL = ${url} */
|
|
78
|
-
return `data:application/json,${
|
|
82
|
+
return `data:application/json;charset=utf-8;base64,${btoa(JSON.stringify(this.toJSON()))}`;
|
|
79
83
|
}
|
|
80
84
|
/**
|
|
81
85
|
* Convert to JSON object
|
|
@@ -83,17 +87,17 @@ class SourceMap {
|
|
|
83
87
|
toJSON() {
|
|
84
88
|
const mappings = [];
|
|
85
89
|
let i = 0;
|
|
86
|
-
for (; i <= this
|
|
87
|
-
if (!this
|
|
90
|
+
for (; i <= this.line; i++) {
|
|
91
|
+
if (!this.map.has(i)) {
|
|
88
92
|
mappings.push("");
|
|
89
93
|
}
|
|
90
94
|
else {
|
|
91
|
-
mappings.push(this
|
|
95
|
+
mappings.push(this.map.get(i).reduce((acc, curr) => acc + (acc === "" ? "" : ",") + encode(curr), ""));
|
|
92
96
|
}
|
|
93
97
|
}
|
|
94
98
|
return {
|
|
95
|
-
version: this
|
|
96
|
-
sources: this
|
|
99
|
+
version: this.version,
|
|
100
|
+
sources: this.sources.slice(),
|
|
97
101
|
mappings: mappings.join(";"),
|
|
98
102
|
};
|
|
99
103
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ColorType, EnumToken } from '../../ast/types.js';
|
|
2
|
-
import { color2srgbvalues
|
|
2
|
+
import { color2srgbvalues } from './color.js';
|
|
3
3
|
import { lch2srgbvalues, lab2srgbvalues, oklch2srgbvalues, oklab2srgbvalues, hwb2srgbvalues, rgb2srgbvalues } from './srgb.js';
|
|
4
4
|
import { hsl2srgbvalues } from './rgb.js';
|
|
5
5
|
|
|
@@ -88,7 +88,7 @@ function cmyktoken(values) {
|
|
|
88
88
|
{
|
|
89
89
|
typ: EnumToken.PercentageTokenType,
|
|
90
90
|
// @ts-ignore
|
|
91
|
-
val:
|
|
91
|
+
val: curr * 100,
|
|
92
92
|
},
|
|
93
93
|
]
|
|
94
94
|
: [
|
|
@@ -99,7 +99,7 @@ function cmyktoken(values) {
|
|
|
99
99
|
},
|
|
100
100
|
{
|
|
101
101
|
typ: EnumToken.PercentageTokenType,
|
|
102
|
-
val:
|
|
102
|
+
val: curr * 100,
|
|
103
103
|
},
|
|
104
104
|
], []),
|
|
105
105
|
kin: ColorType.DEVICE_CMYK,
|