@tbela99/css-parser 1.4.11 → 1.5.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 +6 -0
- package/README.md +11 -4
- package/dist/index-umd-web.js +2116 -1699
- package/dist/index.cjs +2128 -1694
- package/dist/index.d.ts +781 -269
- package/dist/lib/ast/clone.js +13 -8
- package/dist/lib/ast/expand.js +45 -9
- package/dist/lib/ast/features/calc.js +2 -1
- package/dist/lib/ast/features/if.js +24 -4
- package/dist/lib/ast/features/prefix.js +13 -31
- package/dist/lib/ast/features/shorthand.js +1 -1
- package/dist/lib/ast/features/transform.js +5 -2
- package/dist/lib/ast/find.js +7 -5
- package/dist/lib/ast/minify.js +80 -35
- package/dist/lib/ast/node.js +50 -0
- package/dist/lib/ast/types.js +1 -1
- package/dist/lib/ast/walk.js +27 -7
- package/dist/lib/fs/resolve.js +60 -28
- package/dist/lib/parser/declaration/map.js +14 -5
- package/dist/lib/parser/linesmap.js +5 -13
- package/dist/lib/parser/parse.js +489 -719
- package/dist/lib/parser/source.js +16 -2
- package/dist/lib/parser/tokenize.js +382 -278
- package/dist/lib/parser/utils/at-rule-container.js +2 -1
- package/dist/lib/parser/utils/selector.js +88 -10
- package/dist/lib/renderer/render.js +174 -99
- package/dist/lib/renderer/sourcemap/lib/codec.js +78 -0
- package/dist/lib/renderer/sourcemap/sourcemap.js +190 -34
- package/dist/lib/syntax/syntax.js +14 -151
- package/dist/lib/validation/match.js +81 -1
- package/dist/node.js +41 -26
- package/dist/utils/sync.d.ts +16 -0
- package/dist/utils/sync.js +52 -0
- package/dist/web.js +26 -28
- package/llms.txt +12 -5
- package/package.json +1 -1
- package/dist/lib/renderer/sourcemap/lib/encode.js +0 -37
|
@@ -138,7 +138,8 @@ function parseAtRuleContainerQueryList(stream, context, options = {}) {
|
|
|
138
138
|
success = false;
|
|
139
139
|
errors.push({
|
|
140
140
|
action: "drop",
|
|
141
|
-
node:
|
|
141
|
+
node: stream[i],
|
|
142
|
+
location: options.source.getSourceLocation(stream[i][LOC].sta),
|
|
142
143
|
message: `<or> is not allowed outside of parentheses`,
|
|
143
144
|
});
|
|
144
145
|
break;
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { EnumToken, EnumAstNodeStatus } from '../../ast/types.js';
|
|
2
2
|
import { renderValue } from '../../renderer/render.js';
|
|
3
|
-
import { LOC, ERRORS, STATE, TOKENS, pseudoElements,
|
|
3
|
+
import { LOC, ERRORS, STATE, TOKENS, pseudoElements, combinators, tokensfuncDefMap, PARENT } from '../../syntax/constants.js';
|
|
4
4
|
import { isHash } from '../../syntax/syntax.js';
|
|
5
5
|
import { getParsedSyntax, getSyntaxRule, getSyntaxConfig } from '../../validation/config.js';
|
|
6
6
|
import { matchAllSyntaxes, createValidationContext, trimArray, matchSelectorSyntax } from '../../validation/match.js';
|
|
7
7
|
import { ValidationSyntaxGroupEnum, ValidationTokenEnum } from '../../validation/parser/typedef.js';
|
|
8
8
|
import { splitTokenList } from '../../validation/utils/list.js';
|
|
9
9
|
import { trimWhiteSpace } from '../parse.js';
|
|
10
|
+
import { equalsIgnoreCase } from './text.js';
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
13
|
* parse selector
|
|
@@ -48,7 +49,7 @@ function parseSelector(tokens, context, options, errors) {
|
|
|
48
49
|
return acc;
|
|
49
50
|
}, []));
|
|
50
51
|
return {
|
|
51
|
-
typ: EnumToken.
|
|
52
|
+
typ: EnumToken.KeyframesRuleNodeType,
|
|
52
53
|
sel: [
|
|
53
54
|
...splitTokenList(trimArray(tokens)).reduce((acc, curr) => {
|
|
54
55
|
acc.add(curr.reduce((acc, curr) => acc + renderValue(curr, { minify: false }), ""));
|
|
@@ -58,7 +59,7 @@ function parseSelector(tokens, context, options, errors) {
|
|
|
58
59
|
chi: [],
|
|
59
60
|
[LOC]: {
|
|
60
61
|
...tokens[0][LOC],
|
|
61
|
-
end: tokens[tokens.length - 1]?.[LOC]?.end ?? tokens[0]?.[LOC]?.end
|
|
62
|
+
end: tokens[tokens.length - 1]?.[LOC]?.end ?? tokens[0]?.[LOC]?.end,
|
|
62
63
|
},
|
|
63
64
|
[TOKENS]: tokens.length === 0 ? null : tokens,
|
|
64
65
|
[STATE]: result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid,
|
|
@@ -151,16 +152,70 @@ function parseSelector(tokens, context, options, errors) {
|
|
|
151
152
|
}
|
|
152
153
|
}
|
|
153
154
|
if (tokens[i].typ == EnumToken.ColorTokenType) {
|
|
154
|
-
// if (isIdent((tokens[i] as ColorToken).val)) {
|
|
155
|
-
// Object.assign(tokens[i], {
|
|
156
|
-
// typ: EnumToken.IdenTokenType,
|
|
157
|
-
// });
|
|
158
|
-
// } else
|
|
159
155
|
if (isHash(tokens[i].val)) {
|
|
160
156
|
Object.assign(tokens[i], {
|
|
161
157
|
typ: EnumToken.HashTokenType,
|
|
162
158
|
});
|
|
163
159
|
}
|
|
160
|
+
else {
|
|
161
|
+
return {
|
|
162
|
+
typ: EnumToken.RuleNodeType,
|
|
163
|
+
sel: [
|
|
164
|
+
...tokens
|
|
165
|
+
.reduce((acc, curr, index, array) => {
|
|
166
|
+
// if (curr.typ == EnumToken.CommentTokenType) {
|
|
167
|
+
// return acc;
|
|
168
|
+
// }
|
|
169
|
+
if (curr.typ == EnumToken.WhitespaceTokenType) {
|
|
170
|
+
if (trimWhiteSpace.includes(array[index - 1]?.typ) ||
|
|
171
|
+
trimWhiteSpace.includes(array[index + 1]?.typ) ||
|
|
172
|
+
combinators.includes(array[index - 1]?.val) ||
|
|
173
|
+
combinators.includes(array[index + 1]?.val)) {
|
|
174
|
+
return acc;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
let t = renderValue(curr, { minify: false });
|
|
178
|
+
if (t == ",") {
|
|
179
|
+
acc.push([]);
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
acc[acc.length - 1].push(t);
|
|
183
|
+
}
|
|
184
|
+
return acc;
|
|
185
|
+
}, [[]])
|
|
186
|
+
.reduce((acc, curr) => {
|
|
187
|
+
let i = 0;
|
|
188
|
+
for (; i < curr.length; i++) {
|
|
189
|
+
if (i + 1 < curr.length && curr[i] == "*") {
|
|
190
|
+
if (curr[i] == "*") {
|
|
191
|
+
let index = curr[i + 1] == " " ? 2 : 1;
|
|
192
|
+
if (![">", "~", "+"].includes(curr[index])) {
|
|
193
|
+
curr.splice(i, index);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
acc.set(curr.join(""), curr);
|
|
199
|
+
return acc;
|
|
200
|
+
}, uniq)
|
|
201
|
+
.keys(),
|
|
202
|
+
].join(","),
|
|
203
|
+
chi: [],
|
|
204
|
+
[LOC]: {
|
|
205
|
+
...tokens[0][LOC],
|
|
206
|
+
end: tokens[tokens.length - 1][LOC].end,
|
|
207
|
+
},
|
|
208
|
+
[TOKENS]: tokens,
|
|
209
|
+
[STATE]: EnumAstNodeStatus.Invalid,
|
|
210
|
+
[ERRORS]: [
|
|
211
|
+
{
|
|
212
|
+
action: "drop",
|
|
213
|
+
node: tokens[i],
|
|
214
|
+
message: "invalid hash id",
|
|
215
|
+
},
|
|
216
|
+
],
|
|
217
|
+
};
|
|
218
|
+
}
|
|
164
219
|
}
|
|
165
220
|
}
|
|
166
221
|
const result = matchSelectorSyntax(tokens, errors, options, nested === true);
|
|
@@ -212,6 +267,29 @@ function parseSelector(tokens, context, options, errors) {
|
|
|
212
267
|
func.val == ":nth-last-child" ||
|
|
213
268
|
func.val == ":nth-of-type" ||
|
|
214
269
|
func.val == ":nth-last-of-type") {
|
|
270
|
+
const list = [];
|
|
271
|
+
let index;
|
|
272
|
+
for (index = 0; index < func.chi.length; index++) {
|
|
273
|
+
if (func.chi[index].typ == EnumToken.CommentTokenType || func.chi[index].typ == EnumToken.WhitespaceTokenType) {
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
if (func.chi[index].typ == EnumToken.IdenTokenType && equalsIgnoreCase('of', func.chi[index].val)) {
|
|
277
|
+
index--;
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
list.push(func.chi[index]);
|
|
281
|
+
}
|
|
282
|
+
if (list.length == 3) {
|
|
283
|
+
if (list[0].typ == EnumToken.IdenTokenType && ('n' == list[0].val || '-n' == list[0].val || '+n' == list[0].val)) {
|
|
284
|
+
if (list[1].typ == EnumToken.NextSiblingCombinatorTokenType) {
|
|
285
|
+
if (list[2].typ == EnumToken.NumberTokenType && (0 == list[2].val)) {
|
|
286
|
+
list[0].val = 'n';
|
|
287
|
+
func.chi.splice(0, index, list[0]);
|
|
288
|
+
break;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
215
293
|
const token = func.chi.find((t) => t.typ != EnumToken.WhitespaceTokenType && t.typ != EnumToken.CommentTokenType);
|
|
216
294
|
if (token?.typ == EnumToken.IdenTokenType || token?.typ == EnumToken.LiteralTokenType) {
|
|
217
295
|
if (token.typ == EnumToken.IdenTokenType &&
|
|
@@ -301,7 +379,7 @@ function parseSelector(tokens, context, options, errors) {
|
|
|
301
379
|
// } else {
|
|
302
380
|
// Object.assign(token, { typ: EnumToken.NumberTokenType, val: b1 });
|
|
303
381
|
// }
|
|
304
|
-
// } else
|
|
382
|
+
// } else
|
|
305
383
|
if (b1 === 0) {
|
|
306
384
|
Object.assign(token, Math.abs(a1) === 1
|
|
307
385
|
? {
|
|
@@ -372,7 +450,7 @@ function parseSelector(tokens, context, options, errors) {
|
|
|
372
450
|
// func.chi.splice(0, i);
|
|
373
451
|
// }
|
|
374
452
|
// break;
|
|
375
|
-
// } else
|
|
453
|
+
// } else
|
|
376
454
|
if (num.val === 0) {
|
|
377
455
|
func.chi.splice(index + 1, i - index);
|
|
378
456
|
if (token.val < 0) {
|
|
@@ -61,22 +61,28 @@ function doRender(data, options = {}, mapping) {
|
|
|
61
61
|
const startTime = performance.now();
|
|
62
62
|
const errors = [];
|
|
63
63
|
const sourcemap = options.sourcemap ? new SourceMap() : null;
|
|
64
|
+
const sourcemaps = options.sourcemap ? { sources: [], maps: [] } : null;
|
|
64
65
|
const cache = Object.create(null);
|
|
65
66
|
const sourceLocation = {
|
|
66
|
-
srcId: 0,
|
|
67
|
-
sta: 0,
|
|
68
67
|
end: 0,
|
|
69
68
|
};
|
|
70
|
-
const linesMap = new LineMap(
|
|
69
|
+
const linesMap = options.sourcemap ? new LineMap() : null;
|
|
71
70
|
let code = "";
|
|
72
71
|
if (mapping != null) {
|
|
73
72
|
if (mapping.importMapping != null) {
|
|
74
|
-
|
|
73
|
+
const absolutePath = options.resolve(options.output != null ? dirname(options.output) : dirname(options.src), options.cwd).absolute;
|
|
74
|
+
for (let [key, value] of Object.entries(mapping.importMapping)) {
|
|
75
|
+
key = options.resolve(options.resolve(key, options.cwd).absolute, absolutePath).relative;
|
|
76
|
+
if (!key.startsWith("/") && !key.startsWith(".")) {
|
|
77
|
+
key = "./" + key;
|
|
78
|
+
}
|
|
75
79
|
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}`;
|
|
76
80
|
}
|
|
77
81
|
}
|
|
78
82
|
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}`;
|
|
79
|
-
|
|
83
|
+
if (sourcemap != null) {
|
|
84
|
+
move(sourceLocation, linesMap, code);
|
|
85
|
+
}
|
|
80
86
|
}
|
|
81
87
|
if (options.output != null) {
|
|
82
88
|
// @ts-ignore
|
|
@@ -88,7 +94,7 @@ function doRender(data, options = {}, mapping) {
|
|
|
88
94
|
[EnumToken.StyleSheetNodeType, EnumToken.AtRuleNodeType, EnumToken.RuleNodeType].includes(data.typ) &&
|
|
89
95
|
"chi" in data
|
|
90
96
|
? expand(data)
|
|
91
|
-
: data, options,
|
|
97
|
+
: data, options, sourcemaps, sourceLocation, linesMap, errors, function reducer(acc, curr) {
|
|
92
98
|
if (curr.typ == EnumToken.CommentTokenType && options.removeComments) {
|
|
93
99
|
if (!options.preserveLicense || !curr.val.startsWith("/*!")) {
|
|
94
100
|
return acc;
|
|
@@ -103,6 +109,12 @@ function doRender(data, options = {}, mapping) {
|
|
|
103
109
|
},
|
|
104
110
|
};
|
|
105
111
|
if (sourcemap != null) {
|
|
112
|
+
let source;
|
|
113
|
+
for (const sourceId of sourcemaps.sources) {
|
|
114
|
+
source = options.sourcesMap.get(sourceId);
|
|
115
|
+
sourcemap.addSourceContent(source.id, source.getFileName(), source.getContent());
|
|
116
|
+
}
|
|
117
|
+
sourcemap.add(...sourcemaps.maps);
|
|
106
118
|
result.map = sourcemap;
|
|
107
119
|
if (options.sourcemap === "inline") {
|
|
108
120
|
result.code += `\n/*# sourceMappingURL=${result.map.toUrl()} */`;
|
|
@@ -115,37 +127,93 @@ function doRender(data, options = {}, mapping) {
|
|
|
115
127
|
* @param node
|
|
116
128
|
* @param options
|
|
117
129
|
* @param cache
|
|
118
|
-
* @param
|
|
119
|
-
* @param
|
|
130
|
+
* @param sourcemaps
|
|
131
|
+
* @param sourceLocation
|
|
132
|
+
* @param linesMap
|
|
120
133
|
* @param str
|
|
121
134
|
*
|
|
122
135
|
* @internal
|
|
123
136
|
*/
|
|
124
|
-
function updateSourceMap(node, options, cache,
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
137
|
+
function updateSourceMap(node, options, cache, sourcemaps, sourceLocation, linesMap, str) {
|
|
138
|
+
let offset = 0;
|
|
139
|
+
while (true) {
|
|
140
|
+
if (str.charAt(offset) == options.newLine) {
|
|
141
|
+
offset += options.newLine.length;
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (str.charAt(offset) == options.indent) {
|
|
145
|
+
offset += options.indent.length;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
if (offset > 0) {
|
|
151
|
+
move(sourceLocation, linesMap, str.slice(0, offset));
|
|
152
|
+
}
|
|
153
|
+
if (node[LOC] != null &&
|
|
154
|
+
[
|
|
155
|
+
EnumToken.RuleNodeType,
|
|
156
|
+
EnumToken.AtRuleNodeType,
|
|
157
|
+
EnumToken.KeyframesRuleNodeType,
|
|
158
|
+
EnumToken.KeyframesAtRuleNodeType,
|
|
159
|
+
].includes(node.typ)) {
|
|
160
|
+
const source = options.sourcesMap.get(node[LOC].srcId);
|
|
161
|
+
const inputSourceMap = source.getInputSourceMap();
|
|
162
|
+
const offsets = source.getOffsets(node[LOC].sta);
|
|
163
|
+
const [newLine, newColumn] = linesMap.getOffsets(sourceLocation.end);
|
|
164
|
+
let records = null;
|
|
165
|
+
let srcId = node[LOC].srcId;
|
|
166
|
+
let sourceFileName = source.getFileName() || null;
|
|
167
|
+
source.getContent() || null;
|
|
168
|
+
if (inputSourceMap != null && (records = inputSourceMap.find(offsets[0], offsets[1])) != null) {
|
|
169
|
+
for (const record of records) {
|
|
170
|
+
// @ts-ignore
|
|
171
|
+
sourceFileName = record[0] || null;
|
|
172
|
+
// @ts-ignore
|
|
173
|
+
offsets[0] = record[1];
|
|
174
|
+
// @ts-ignore
|
|
175
|
+
offsets[1] = record[2];
|
|
176
|
+
if (sourceFileName != null && options.output != null && !sourceFileName.startsWith("data:")) {
|
|
177
|
+
if (cache[sourceFileName] == null) {
|
|
178
|
+
const absolute = options.resolve(dirname(options.output), options.cwd)
|
|
179
|
+
.absolute;
|
|
180
|
+
const absoluteSourcePath = options.resolve(dirname(options.src || ""), options.cwd).absolute;
|
|
181
|
+
// resolution is relative to the source file
|
|
182
|
+
const absoluteSourceFileName = options.resolve(sourceFileName, absoluteSourcePath)
|
|
183
|
+
.absolute;
|
|
184
|
+
cache[sourceFileName] = options.resolve(absoluteSourceFileName, absolute).relative;
|
|
185
|
+
}
|
|
186
|
+
sourceFileName = cache[sourceFileName];
|
|
187
|
+
}
|
|
188
|
+
if (!sourcemaps.sources.includes(srcId)) {
|
|
189
|
+
sourcemaps.sources.push(srcId);
|
|
190
|
+
}
|
|
191
|
+
sourcemaps.maps.push([newLine, newColumn, srcId, ...offsets]);
|
|
136
192
|
}
|
|
137
|
-
sourceFileName = cache[sourceFileName];
|
|
138
193
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
194
|
+
else {
|
|
195
|
+
if (sourceFileName != null && options.output != null && !sourceFileName.startsWith("data:")) {
|
|
196
|
+
if (cache[sourceFileName] == null) {
|
|
197
|
+
const absolute = options.resolve(dirname(options.output), options.cwd)
|
|
198
|
+
.absolute;
|
|
199
|
+
const absoluteSourceFileName = options.resolve(sourceFileName, options.cwd)
|
|
200
|
+
.absolute;
|
|
201
|
+
cache[sourceFileName] = options.resolve(absoluteSourceFileName, absolute).relative;
|
|
202
|
+
}
|
|
203
|
+
sourceFileName = cache[sourceFileName];
|
|
204
|
+
}
|
|
205
|
+
if (!sourcemaps.sources.includes(srcId)) {
|
|
206
|
+
sourcemaps.sources.push(srcId);
|
|
207
|
+
}
|
|
208
|
+
sourcemaps.maps.push([newLine, newColumn, srcId, ...offsets]);
|
|
209
|
+
}
|
|
143
210
|
}
|
|
144
|
-
move(sourceLocation, linesMap, str);
|
|
211
|
+
move(sourceLocation, linesMap, offset > 0 ? str.slice(offset) : str);
|
|
145
212
|
}
|
|
146
213
|
/**
|
|
147
214
|
* Update position
|
|
148
|
-
* @param
|
|
215
|
+
* @param sourceLocation
|
|
216
|
+
* @param linesMap
|
|
149
217
|
* @param str
|
|
150
218
|
*/
|
|
151
219
|
function move(sourceLocation, linesMap, str) {
|
|
@@ -175,8 +243,9 @@ function move(sourceLocation, linesMap, str) {
|
|
|
175
243
|
* render ast node
|
|
176
244
|
* @param data
|
|
177
245
|
* @param options
|
|
178
|
-
* @param
|
|
179
|
-
* @param
|
|
246
|
+
* @param sourcemaps
|
|
247
|
+
* @param sourceLocation
|
|
248
|
+
* @param linesMap
|
|
180
249
|
* @param errors
|
|
181
250
|
* @param reducer
|
|
182
251
|
* @param cache
|
|
@@ -185,13 +254,16 @@ function move(sourceLocation, linesMap, str) {
|
|
|
185
254
|
*
|
|
186
255
|
* @internal
|
|
187
256
|
*/
|
|
188
|
-
function renderAstNode(data, options,
|
|
257
|
+
function renderAstNode(data, options, sourcemaps, sourceLocation, linesMap, errors, reducer, cache, level = 0, indents = []) {
|
|
189
258
|
if (indents.length < level + 1) {
|
|
190
259
|
indents.push(options.indent.repeat(level));
|
|
191
260
|
}
|
|
192
261
|
if (indents.length < level + 2) {
|
|
193
262
|
indents.push(options.indent.repeat(level + 1));
|
|
194
263
|
}
|
|
264
|
+
// @ts-ignore
|
|
265
|
+
let children = "";
|
|
266
|
+
let str = "";
|
|
195
267
|
const indent = indents[level];
|
|
196
268
|
const indentSub = indents[level + 1];
|
|
197
269
|
switch (data.typ) {
|
|
@@ -202,37 +274,47 @@ function renderAstNode(data, options, sourcemap, sourceLocation, linesMap, error
|
|
|
202
274
|
case EnumToken.CommentNodeType:
|
|
203
275
|
case EnumToken.CDOCOMMNodeType:
|
|
204
276
|
if (data.val.startsWith("/*# sourceMappingURL=")) {
|
|
205
|
-
// ignore sourcemap
|
|
277
|
+
// ignore sourcemap comment
|
|
206
278
|
return "";
|
|
207
279
|
}
|
|
208
280
|
return !options.removeComments || (options.preserveLicense && data.val.startsWith("/*!"))
|
|
209
281
|
? data.val
|
|
210
282
|
: "";
|
|
211
283
|
case EnumToken.StyleSheetNodeType:
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
const str = renderAstNode(node, options, sourcemap, sourceLocation, linesMap, errors, reducer, cache, level, indents);
|
|
284
|
+
for (const node of data.chi) {
|
|
285
|
+
str = renderAstNode(node, options, sourcemaps, sourceLocation, linesMap, errors, reducer, cache, level, indents);
|
|
215
286
|
if (str === "") {
|
|
216
|
-
|
|
287
|
+
continue;
|
|
217
288
|
}
|
|
218
|
-
if (
|
|
219
|
-
|
|
289
|
+
if (children.length > 0) {
|
|
290
|
+
str = options.newLine + str;
|
|
220
291
|
}
|
|
221
|
-
|
|
222
|
-
|
|
292
|
+
children += str;
|
|
293
|
+
if (sourcemaps != null && str !== "" && options.newLine) {
|
|
294
|
+
move(sourceLocation, linesMap, options.newLine);
|
|
223
295
|
}
|
|
224
|
-
|
|
225
|
-
|
|
296
|
+
}
|
|
297
|
+
return children;
|
|
226
298
|
case EnumToken.AtRuleNodeType:
|
|
227
299
|
case EnumToken.RuleNodeType:
|
|
228
|
-
case EnumToken.
|
|
300
|
+
case EnumToken.KeyframesRuleNodeType:
|
|
229
301
|
case EnumToken.KeyframesAtRuleNodeType:
|
|
230
302
|
if ([EnumToken.AtRuleNodeType, EnumToken.KeyframesAtRuleNodeType].includes(data.typ) && !("chi" in data)) {
|
|
231
303
|
return `${indent}@${data.nam}${data.val === "" ? "" : options.indent || " "}${data.val};`;
|
|
232
304
|
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
305
|
+
const lineMapLength = linesMap ? linesMap.getLineStarts().length : 0;
|
|
306
|
+
const prelude = (indent.length > 0 ? options.newLine : "") +
|
|
307
|
+
indent +
|
|
308
|
+
([EnumToken.AtRuleNodeType, EnumToken.KeyframesAtRuleNodeType].includes(data.typ)
|
|
309
|
+
? `@${data.nam}${data.val === "" ? "" : options.indent || " "}${data.val}${options.indent}{`
|
|
310
|
+
: data.sel + `${options.indent}{`);
|
|
311
|
+
if (sourcemaps != null) {
|
|
312
|
+
updateSourceMap(data, options, cache, sourcemaps, sourceLocation, linesMap, prelude);
|
|
313
|
+
}
|
|
314
|
+
let node;
|
|
315
|
+
let recordDeclarationSourceMap = data.typ == EnumToken.AtRuleNodeType;
|
|
316
|
+
for (let i = 0; i < data.chi.length; i++) {
|
|
317
|
+
node = data.chi[i];
|
|
236
318
|
if (node.typ == EnumToken.CommentNodeType) {
|
|
237
319
|
str =
|
|
238
320
|
options.removeComments &&
|
|
@@ -241,73 +323,63 @@ function renderAstNode(data, options, sourcemap, sourceLocation, linesMap, error
|
|
|
241
323
|
: node.val;
|
|
242
324
|
}
|
|
243
325
|
else if (node.typ == EnumToken.DeclarationNodeType) {
|
|
244
|
-
// if (!(<AstDeclaration>node).nam.startsWith("--") && (<AstDeclaration>node).val.length === 0) {
|
|
245
|
-
// // @ts-ignore
|
|
246
|
-
// errors.push(<ErrorDescription>{
|
|
247
|
-
// action: "ignore",
|
|
248
|
-
// message: `render: invalid declaration ${JSON.stringify(node)}`,
|
|
249
|
-
// location: node[LOC],
|
|
250
|
-
// });
|
|
251
|
-
// return "";
|
|
252
|
-
// }
|
|
253
326
|
str = `${node.nam}:${options.indent}${(options.minify
|
|
254
327
|
? filterValues(node.val)
|
|
255
328
|
: node.val)
|
|
256
329
|
.reduce(reducer, "")
|
|
257
330
|
.trimEnd()};`;
|
|
258
331
|
}
|
|
259
|
-
// else if (node.typ == EnumToken.AtRuleNodeType && !("chi" in node)) {
|
|
260
|
-
// str = `${(<AstAtRule>node).val === "" ? "" : options.indent || " "}${(<AstAtRule>node).val};`;
|
|
261
|
-
// }
|
|
262
332
|
else {
|
|
263
|
-
str = renderAstNode(node, options,
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
333
|
+
str = renderAstNode(node, options, sourcemaps, sourceLocation, linesMap, errors, reducer, cache, level + 1, indents);
|
|
334
|
+
if (str === "") {
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
children += str;
|
|
338
|
+
str = "";
|
|
339
|
+
continue;
|
|
267
340
|
}
|
|
268
341
|
if (str === "") {
|
|
269
|
-
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
str = options.newLine + indentSub + str;
|
|
345
|
+
children += str;
|
|
346
|
+
if (sourcemaps != null && str !== "") {
|
|
347
|
+
move(sourceLocation, linesMap, str);
|
|
348
|
+
if (node.typ == EnumToken.DeclarationNodeType && recordDeclarationSourceMap) {
|
|
349
|
+
// if declaration is child of at-rule, then record it
|
|
350
|
+
// .rule {
|
|
351
|
+
// @media screen {
|
|
352
|
+
// color: red;
|
|
353
|
+
// }
|
|
354
|
+
// }
|
|
355
|
+
const source = options.sourcesMap.get(node[LOC].srcId);
|
|
356
|
+
if (!sourcemaps.sources.includes(node[LOC].srcId)) {
|
|
357
|
+
sourcemaps.sources.push(node[LOC].srcId);
|
|
358
|
+
}
|
|
359
|
+
sourcemaps.maps.push([
|
|
360
|
+
...linesMap.getOffsets(sourceLocation.end - str.length + options.newLine.length + indentSub.length),
|
|
361
|
+
node[LOC].srcId,
|
|
362
|
+
...source.getOffsets(node[LOC].sta),
|
|
363
|
+
]);
|
|
364
|
+
}
|
|
270
365
|
}
|
|
271
|
-
return `${css}${options.newLine}${indentSub}${str}`;
|
|
272
|
-
}, "");
|
|
273
|
-
if (options.removeEmpty && children === "") {
|
|
274
|
-
return "";
|
|
275
366
|
}
|
|
276
367
|
if (children.endsWith(";")) {
|
|
277
368
|
children = children.slice(0, -1);
|
|
369
|
+
sourceLocation.end--;
|
|
370
|
+
}
|
|
371
|
+
if (options.removeEmpty && children === "") {
|
|
372
|
+
if (sourcemaps != null) {
|
|
373
|
+
sourceLocation.end -= prelude.length;
|
|
374
|
+
linesMap.getLineStarts().length = lineMapLength;
|
|
375
|
+
}
|
|
376
|
+
return "";
|
|
278
377
|
}
|
|
279
|
-
const
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
indent +
|
|
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);
|
|
378
|
+
const end = options.newLine + indent + `}`;
|
|
379
|
+
if (sourcemaps != null) {
|
|
380
|
+
move(sourceLocation, linesMap, end);
|
|
291
381
|
}
|
|
292
|
-
return
|
|
293
|
-
// case EnumToken.CssVariableTokenType:
|
|
294
|
-
// case EnumToken.CssVariableImportTokenType:
|
|
295
|
-
// return `@value ${(<CssVariableToken | CssVariableImportTokenType>data).val}:${options.indent}${filterValues(
|
|
296
|
-
// options.minify
|
|
297
|
-
// ? (<CssVariableToken | CssVariableImportTokenType>data).val
|
|
298
|
-
// : (<CssVariableToken>data).val,
|
|
299
|
-
// )
|
|
300
|
-
// .reduce(reducer, "")
|
|
301
|
-
// .trim()};`;
|
|
302
|
-
// case EnumToken.CssVariableDeclarationMapTokenType:
|
|
303
|
-
// return `@value ${filterValues((data as CssVariableMapTokenType).vars)
|
|
304
|
-
// .reduce((acc, curr) => acc + renderValue(curr), "")
|
|
305
|
-
// .trim()} from ${filterValues((data as CssVariableMapTokenType).from)
|
|
306
|
-
// .reduce((acc, curr) => acc + renderValue(curr), "")
|
|
307
|
-
// .trim()};`;
|
|
308
|
-
// case EnumToken.InvalidDeclarationNodeType:
|
|
309
|
-
// case EnumToken.InvalidRuleNodeType:
|
|
310
|
-
// case EnumToken.InvalidAtRuleNodeType:
|
|
382
|
+
return prelude + children + end;
|
|
311
383
|
default:
|
|
312
384
|
return "";
|
|
313
385
|
}
|
|
@@ -316,6 +388,9 @@ function renderAstNode(data, options, sourcemap, sourceLocation, linesMap, error
|
|
|
316
388
|
* render ast token
|
|
317
389
|
* @param token
|
|
318
390
|
* @param options
|
|
391
|
+
* @param cache
|
|
392
|
+
* @param reducer
|
|
393
|
+
* @param errors
|
|
319
394
|
* @private
|
|
320
395
|
*/
|
|
321
396
|
function renderValue(token, options = {}, cache = Object.create(null), reducer, errors) {
|
|
@@ -392,8 +467,8 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
|
|
|
392
467
|
return " + ";
|
|
393
468
|
case EnumToken.Sub:
|
|
394
469
|
return " - ";
|
|
395
|
-
case EnumToken.Star:
|
|
396
470
|
case EnumToken.UniversalSelectorTokenType:
|
|
471
|
+
case EnumToken.Star:
|
|
397
472
|
case EnumToken.Mul:
|
|
398
473
|
return "*";
|
|
399
474
|
case EnumToken.Div:
|
|
@@ -1232,11 +1307,11 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
|
|
|
1232
1307
|
case EnumToken.OrTokenType:
|
|
1233
1308
|
return "or";
|
|
1234
1309
|
case EnumToken.InvalidMediaQueryTokenType:
|
|
1235
|
-
// case EnumToken.InvalidDeclarationNodeType:
|
|
1236
1310
|
case EnumToken.InvalidCommentTokenType:
|
|
1237
1311
|
case EnumToken.BadCommentTokenType:
|
|
1238
1312
|
case EnumToken.BadCdoTokenType:
|
|
1239
1313
|
case EnumToken.BadStringTokenType:
|
|
1314
|
+
case EnumToken.BadUrlTokenType:
|
|
1240
1315
|
case EnumToken.EOFTokenType:
|
|
1241
1316
|
return "";
|
|
1242
1317
|
default:
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// from https://github.com/Rich-Harris/vlq/tree/master
|
|
2
|
+
// credit: Rich Harris
|
|
3
|
+
const integer_to_char = {};
|
|
4
|
+
const char_to_integer = {};
|
|
5
|
+
let i = 0;
|
|
6
|
+
for (const char of 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=') {
|
|
7
|
+
char_to_integer[char] = i;
|
|
8
|
+
integer_to_char[i++] = char;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* @param {string} str
|
|
12
|
+
*/
|
|
13
|
+
function decode(str) {
|
|
14
|
+
/** @type {number[]} */
|
|
15
|
+
let result = [];
|
|
16
|
+
let shift = 0;
|
|
17
|
+
let value = 0;
|
|
18
|
+
for (let i = 0; i < str.length; i += 1) {
|
|
19
|
+
let integer = char_to_integer[str[i]];
|
|
20
|
+
// if (integer === undefined) {
|
|
21
|
+
// throw new Error('Invalid character (' + str[i] + ')');
|
|
22
|
+
// }
|
|
23
|
+
const has_continuation_bit = integer & 32;
|
|
24
|
+
integer &= 31;
|
|
25
|
+
value += integer << shift;
|
|
26
|
+
if (has_continuation_bit) {
|
|
27
|
+
shift += 5;
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
const should_negate = value & 1;
|
|
31
|
+
value >>>= 1;
|
|
32
|
+
if (should_negate) {
|
|
33
|
+
result.push(value === 0 ? -2147483648 : -value);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
result.push(value);
|
|
37
|
+
}
|
|
38
|
+
// reset
|
|
39
|
+
value = shift = 0;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return result;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
*
|
|
46
|
+
* @param value
|
|
47
|
+
* @returns
|
|
48
|
+
*/
|
|
49
|
+
function encode(value) {
|
|
50
|
+
if (typeof value === 'number') {
|
|
51
|
+
return encode_integer(value);
|
|
52
|
+
}
|
|
53
|
+
let result = '';
|
|
54
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
55
|
+
result += encode_integer(value[i]);
|
|
56
|
+
}
|
|
57
|
+
return result;
|
|
58
|
+
}
|
|
59
|
+
function encode_integer(num) {
|
|
60
|
+
let result = '';
|
|
61
|
+
if (num < 0) {
|
|
62
|
+
num = (-num << 1) | 1;
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
num <<= 1;
|
|
66
|
+
}
|
|
67
|
+
do {
|
|
68
|
+
let clamped = num & 31;
|
|
69
|
+
num >>>= 5;
|
|
70
|
+
if (num > 0) {
|
|
71
|
+
clamped |= 32;
|
|
72
|
+
}
|
|
73
|
+
result += integer_to_char[clamped];
|
|
74
|
+
} while (num > 0);
|
|
75
|
+
return result;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export { decode, encode };
|