@tbela99/css-parser 2.1.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +66 -1
- package/README.md +14 -7
- package/architecture.png +0 -0
- package/dist/{src/lib/ast → ast}/arena-node.js +10 -10
- package/dist/{src/lib/ast → ast}/arena.js +304 -139
- package/dist/{src/lib/ast → ast}/cache.js +40 -24
- package/dist/{src/lib/ast → ast}/features/calc.js +11 -9
- package/dist/{src/lib/ast → ast}/features/if.js +6 -6
- package/dist/ast/features/index.d.ts +7 -0
- package/dist/{src/lib/ast → ast}/features/index.js +3 -2
- package/dist/{src/lib/ast → ast}/features/inlinecssvariables.js +12 -10
- package/dist/ast/features/mixin.js +953 -0
- package/dist/{src/lib/ast → ast}/features/prefix.js +39 -39
- package/dist/ast/features/shorthand.js +129 -0
- package/dist/ast/features/transform.js +188 -0
- package/dist/{src/lib/ast → ast}/features/type.js +2 -2
- package/dist/{src/lib/ast → ast}/math/expression.js +24 -24
- package/dist/{src/lib/ast → ast}/math/math.js +2 -2
- package/dist/{src/lib/ast → ast}/minify.js +745 -201
- package/dist/ast/mixins/custom-function.js +393 -0
- package/dist/ast/mixins/utils.js +53 -0
- package/dist/ast/mixins/variables.js +678 -0
- package/dist/ast/strings.js +910 -0
- package/dist/ast/transform/compute.js +560 -0
- package/dist/ast/transform/matrix.js +67 -0
- package/dist/ast/transform/minify.js +325 -0
- package/dist/ast/transform/perspective.js +24 -0
- package/dist/{src/lib/ast → ast}/transform/rotate.js +16 -7
- package/dist/ast/transform/scale.js +43 -0
- package/dist/ast/transform/skew.js +38 -0
- package/dist/ast/transform/translate.js +56 -0
- package/dist/ast/transform/utils.js +390 -0
- package/dist/{src/lib/ast → ast}/types.js +2 -2
- package/dist/{src/lib/ast → ast}/walk.js +2 -2
- package/dist/{src/data → data}/properties.json.js +86 -2
- package/dist/{src/data → data}/validation.json.js +30 -21
- package/dist/{src/lib/fs → fs}/resolve.js +2 -2
- package/dist/index-umd-web.js +19727 -10719
- package/dist/index.d.ts +738 -100
- package/dist/{src/node.js → node.js} +70 -52
- package/dist/{src/lib/parser → parser}/declaration/list.js +25 -8
- package/dist/{src/lib/parser → parser}/declaration/map.js +15 -15
- package/dist/{src/lib/parser → parser}/declaration/set.js +5 -5
- package/dist/{src/lib/parser → parser}/input.js +18 -3
- package/dist/{src/lib/parser → parser}/linesmap.js +2 -2
- package/dist/{src/lib/parser → parser}/parse.js +444 -278
- package/dist/{src/lib/parser → parser}/source.js +3 -3
- package/dist/{src/lib/parser → parser}/tokenize.js +480 -312
- package/dist/{src/lib/parser → parser}/utils/angle.js +3 -3
- package/dist/{src/lib/parser → parser}/utils/at-rule-container.js +12 -12
- package/dist/{src/lib/parser → parser}/utils/at-rule-font-feature-values.js +3 -3
- package/dist/{src/lib/parser → parser}/utils/at-rule-generic.js +7 -7
- package/dist/{src/lib/parser → parser}/utils/at-rule-import.js +10 -10
- package/dist/{src/lib/parser → parser}/utils/at-rule-media.js +14 -14
- package/dist/{src/lib/parser → parser}/utils/at-rule-support.js +9 -14
- package/dist/{src/lib/parser → parser}/utils/at-rule-when-else.js +6 -6
- package/dist/{src/lib/parser → parser}/utils/at-rule.js +3 -3
- package/dist/{src/lib/parser → parser}/utils/base64.js +2 -2
- package/dist/parser/utils/cache.js +53 -0
- package/dist/parser/utils/canonical.js +78 -0
- package/dist/parser/utils/codes.js +93 -0
- package/dist/{src/lib/parser → parser}/utils/config.js +3 -3
- package/dist/{src/lib/parser → parser}/utils/declaration.js +31 -26
- package/dist/{src/lib/parser → parser}/utils/eq.js +2 -2
- package/dist/{src/lib/parser → parser}/utils/hash.js +2 -2
- package/dist/{src/lib/parser → parser}/utils/selector.js +339 -87
- package/dist/{src/lib/parser → parser}/utils/text.js +2 -2
- package/dist/{src/lib/parser → parser}/utils/token.js +2 -2
- package/dist/{src/lib/parser → parser}/utils/type.js +9 -7
- package/dist/{src/lib/printer/sourcemap → printer}/lib/codec.js +2 -2
- package/dist/{src/lib/printer → printer}/render.js +582 -153
- package/dist/printer/result.js +148 -0
- package/dist/{src/lib/printer/sourcemap → printer}/sourcemap.js +4 -3
- package/dist/printer/writer.js +252 -0
- package/dist/{src/lib/syntax → syntax}/color/a98rgb.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/alpha.js +10 -10
- package/dist/{src/lib/syntax → syntax}/color/cmyk.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/color-mix.js +18 -18
- package/dist/{src/lib/syntax → syntax}/color/color.js +18 -18
- package/dist/{src/lib/syntax → syntax}/color/hex.js +3 -3
- package/dist/{src/lib/syntax → syntax}/color/hsl.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/hwb.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/lab.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/lch.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/oklab.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/oklch.js +3 -3
- package/dist/{src/lib/syntax → syntax}/color/p3.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/prophotorgb.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/rec2020.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/relative-color.js +14 -14
- package/dist/{src/lib/syntax → syntax}/color/rgb.js +4 -4
- package/dist/{src/lib/syntax → syntax}/color/srgb.js +5 -5
- package/dist/{src/lib/syntax → syntax}/color/utils/color-type.js +4 -4
- package/dist/{src/lib/syntax → syntax}/color/utils/colorspace.js +10 -10
- package/dist/{src/lib/syntax → syntax}/color/utils/components.js +5 -5
- package/dist/{src/lib/syntax → syntax}/color/utils/distance.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/utils/make-color.js +3 -3
- package/dist/{src/lib/syntax → syntax}/color/utils/matrix.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/xyz.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/xyzd50.js +2 -2
- package/dist/{src/lib/syntax → syntax}/constants.js +27 -4
- package/dist/{src/lib/syntax → syntax}/syntax.js +37 -29
- package/dist/{src/types.js → types.js} +2 -2
- package/dist/{src/utils → utils}/sync.js +12 -7
- package/dist/validation/compiled/builtins.js +32 -0
- package/dist/validation/compiled/runtime.js +705 -0
- package/dist/validation/compiled/syntaxes.js +1620 -0
- package/dist/{src/lib/validation → validation}/config.js +63 -16
- package/dist/validation/first.js +574 -0
- package/dist/{src/lib/validation → validation}/match.js +376 -161
- package/dist/{src/lib/validation → validation}/parser/parse.js +2 -2
- package/dist/{src/lib/validation → validation}/parser/typedef.js +2 -2
- package/dist/{src/lib/validation → validation}/utils/list.js +3 -19
- package/dist/version.js +13 -0
- package/dist/{src/web.js → web.js} +68 -55
- package/package.json +15 -13
- package/dist/package.json.js +0 -10
- package/dist/src/lib/ast/features/shorthand.js +0 -95
- package/dist/src/lib/ast/features/transform.js +0 -90
- package/dist/src/lib/ast/transform/compute.js +0 -362
- package/dist/src/lib/ast/transform/matrix.js +0 -88
- package/dist/src/lib/ast/transform/minify.js +0 -267
- package/dist/src/lib/ast/transform/perspective.js +0 -16
- package/dist/src/lib/ast/transform/scale.js +0 -39
- package/dist/src/lib/ast/transform/skew.js +0 -30
- package/dist/src/lib/ast/transform/translate.js +0 -39
- package/dist/src/lib/ast/transform/utils.js +0 -287
- package/dist/src/lib/parser/utils/cache.js +0 -54
- package/dist/types.d.ts +0 -21
- package/dist/utils/sync.d.ts +0 -16
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
/*
|
|
2
|
-
CSS parser, minifier and validator for node and the browser
|
|
2
|
+
CSS parser, minifier and validator for node, deno, bun and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version: 2.
|
|
5
|
+
Version: 2.3.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { getAngle, convertColor } from '../syntax/color/color.js';
|
|
9
9
|
import { reduceHexValue } from '../syntax/color/hex.js';
|
|
10
10
|
import { EnumToken, ColorType, EnumAstNodeStatus } from '../ast/types.js';
|
|
11
|
-
import { SourceMap } from './sourcemap
|
|
12
|
-
import { pseudoElements, anglePrecision, urlTokenMatcher,
|
|
11
|
+
import { SourceMap } from './sourcemap.js';
|
|
12
|
+
import { pseudoElements, anglePrecision, urlTokenMatcher, isFunctionType } from '../syntax/constants.js';
|
|
13
13
|
import { minifyNumber, toPrecisionAngle, toPrecisionValue, reducegradientBackgroundPosition, reduceConicColorStops, reduceColorStops, parseColor, isWhiteSpace } from '../syntax/syntax.js';
|
|
14
14
|
import { equalsIgnoreCase } from '../parser/utils/text.js';
|
|
15
15
|
import { toDegrees } from '../parser/utils/angle.js';
|
|
@@ -18,6 +18,8 @@ import { dirname } from '../fs/resolve.js';
|
|
|
18
18
|
import { SourceFile } from '../parser/source.js';
|
|
19
19
|
import { TokenField } from '../ast/arena.js';
|
|
20
20
|
import { StructuralMemo } from '../ast/cache.js';
|
|
21
|
+
import { Writer, DEFAULT_CAPACITY } from './writer.js';
|
|
22
|
+
import { RenderResult } from './result.js';
|
|
21
23
|
|
|
22
24
|
/**
|
|
23
25
|
* Rendered strings shared by the renderer.
|
|
@@ -57,6 +59,17 @@ class RenderMemo {
|
|
|
57
59
|
* rendered constructs: colors and tokens with sub tokens (functions, parentheses, binary expressions, ...)
|
|
58
60
|
*/
|
|
59
61
|
constructs = new StructuralMemo();
|
|
62
|
+
/**
|
|
63
|
+
* location of the declarations in the render buffer, by declaration slot: writer id, start offset, end offset and
|
|
64
|
+
* number of bytes minus the number of UTF-16 code units (4 values per slot). an identical declaration written to the
|
|
65
|
+
* same buffer is copied from there (see {@link Writer.copy})
|
|
66
|
+
*/
|
|
67
|
+
locations = new Int32Array(1024);
|
|
68
|
+
/**
|
|
69
|
+
* rendered values of the declarations (without the name), by declaration slot: a declaration written to another
|
|
70
|
+
* buffer is not rendered again
|
|
71
|
+
*/
|
|
72
|
+
values = [];
|
|
60
73
|
/**
|
|
61
74
|
* a cleared memo
|
|
62
75
|
*/
|
|
@@ -72,9 +85,35 @@ class RenderMemo {
|
|
|
72
85
|
if (RenderMemo.free.length < 4 && memo.declarations.footprint + memo.constructs.footprint <= 1 << 20) {
|
|
73
86
|
memo.declarations.clear();
|
|
74
87
|
memo.constructs.clear();
|
|
88
|
+
memo.values.length = 0;
|
|
89
|
+
// the writer ids are never reused: the locations are not cleared
|
|
75
90
|
RenderMemo.free.push(memo);
|
|
76
91
|
}
|
|
77
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* record the location of a declaration in a render buffer
|
|
95
|
+
* @param slot declaration slot
|
|
96
|
+
* @param writer render buffer
|
|
97
|
+
* @param start start offset
|
|
98
|
+
* @param extra value of {@link Writer.extra} at the start offset
|
|
99
|
+
*/
|
|
100
|
+
locate(slot, writer, start, extra) {
|
|
101
|
+
let index = slot << 2;
|
|
102
|
+
if (index + 4 > this.locations.length) {
|
|
103
|
+
let size = this.locations.length * 2;
|
|
104
|
+
while (size < index + 4) {
|
|
105
|
+
size *= 2;
|
|
106
|
+
}
|
|
107
|
+
const locations = new Int32Array(size);
|
|
108
|
+
locations.set(this.locations);
|
|
109
|
+
this.locations = locations;
|
|
110
|
+
}
|
|
111
|
+
const locations = this.locations;
|
|
112
|
+
locations[index++] = writer.id;
|
|
113
|
+
locations[index++] = start;
|
|
114
|
+
locations[index++] = writer.length;
|
|
115
|
+
locations[index] = writer.extra - extra;
|
|
116
|
+
}
|
|
78
117
|
}
|
|
79
118
|
/**
|
|
80
119
|
* Render state shared by the nodes and the tokens of a render
|
|
@@ -184,10 +223,15 @@ function resolveRenderOptions(options) {
|
|
|
184
223
|
}
|
|
185
224
|
/**
|
|
186
225
|
* Render nodes with the same options: the options are resolved once and identical values are rendered once.
|
|
187
|
-
* Same output as {@link doRender}, used by the minifier to compare the size of the nodes
|
|
226
|
+
* Same output as {@link doRender}, used by the minifier to compare the size of the nodes. The output is not kept: only
|
|
227
|
+
* its size is returned
|
|
188
228
|
*
|
|
189
229
|
* @internal
|
|
190
230
|
*/
|
|
231
|
+
/**
|
|
232
|
+
* `CSS_PARSER_CHECK_SIZE`: the computed sizes are checked against the rendered sizes (tests)
|
|
233
|
+
*/
|
|
234
|
+
const checkSize = globalThis.process?.env?.CSS_PARSER_CHECK_SIZE != null;
|
|
191
235
|
class NodeRenderer {
|
|
192
236
|
/**
|
|
193
237
|
* An instance is kept alive: the hidden class of the instances would otherwise be collected by a full garbage
|
|
@@ -230,6 +274,10 @@ class NodeRenderer {
|
|
|
230
274
|
* the nodes are rendered without doRender(): no nesting expansion, no parents, no source map
|
|
231
275
|
*/
|
|
232
276
|
direct;
|
|
277
|
+
/**
|
|
278
|
+
* output buffer, reused by the renders: only the size of the output is read
|
|
279
|
+
*/
|
|
280
|
+
writer = new Writer();
|
|
233
281
|
/**
|
|
234
282
|
* @param options render options
|
|
235
283
|
*/
|
|
@@ -244,11 +292,11 @@ class NodeRenderer {
|
|
|
244
292
|
this.resolved.output == null;
|
|
245
293
|
}
|
|
246
294
|
/**
|
|
247
|
-
*
|
|
295
|
+
* rendered size of a node: the length of the rendered css (UTF-16 code units)
|
|
248
296
|
* @param arena
|
|
249
297
|
* @param node
|
|
250
298
|
*/
|
|
251
|
-
|
|
299
|
+
size(arena, node) {
|
|
252
300
|
if (this.arena !== arena) {
|
|
253
301
|
if (this.arena != null) {
|
|
254
302
|
this.cache = new RenderCache(new RenderMemo(), this.resolved);
|
|
@@ -257,11 +305,46 @@ class NodeRenderer {
|
|
|
257
305
|
this.reducer = createReducer(arena, this.resolved, this.cache);
|
|
258
306
|
}
|
|
259
307
|
if (!this.direct) {
|
|
260
|
-
return doRender(arena, node, this.options, null, this.cache.memo)
|
|
308
|
+
return RenderResult.codeLength(doRender(arena, node, this.options, null, this.cache.memo));
|
|
309
|
+
}
|
|
310
|
+
// the size is computed from the sizes of the parts: the node is not rendered
|
|
311
|
+
const size = nodeSize(arena, node, this.resolved, this.reducer, this.cache, 0) >> 1;
|
|
312
|
+
if (checkSize) {
|
|
313
|
+
const writer = this.writer;
|
|
314
|
+
writer.reset();
|
|
315
|
+
renderAstNode(arena, node, this.resolved, null, this.location, null, this.errors, this.reducer, this.cache, writer, 0, this.indents);
|
|
316
|
+
if (size !== writer.codeLength) {
|
|
317
|
+
throw new Error(`computed size of node ${node} (${size}) differs from its rendered size (${writer.codeLength})`);
|
|
318
|
+
}
|
|
261
319
|
}
|
|
262
|
-
return
|
|
320
|
+
return size;
|
|
263
321
|
}
|
|
264
322
|
}
|
|
323
|
+
/**
|
|
324
|
+
* extra capacity of the render buffer: the output may be larger than the input (beautified minified css, css modules
|
|
325
|
+
* exports, inline source map, ...)
|
|
326
|
+
*/
|
|
327
|
+
const OUTPUT_PADDING = 1 << 12;
|
|
328
|
+
/**
|
|
329
|
+
* capacity of the buffer a node is rendered to. the minified or beautified output is usually not larger than the
|
|
330
|
+
* input: the length of the input the node was parsed from, padded
|
|
331
|
+
* - stylesheet: the length of the css parsed into the stylesheet, imported files included
|
|
332
|
+
* - other nodes: the length of their location in the source
|
|
333
|
+
* @param arena
|
|
334
|
+
* @param node
|
|
335
|
+
*
|
|
336
|
+
* @internal
|
|
337
|
+
*/
|
|
338
|
+
function outputCapacity(arena, node) {
|
|
339
|
+
let length = 0;
|
|
340
|
+
if (arena.dataTypes[node] === EnumToken.StyleSheetNodeType) {
|
|
341
|
+
length = arena.inputLengths[node] ?? 0;
|
|
342
|
+
}
|
|
343
|
+
else if (arena.flags[node] & EnumAstNodeStatus.HasLocation) {
|
|
344
|
+
length = arena.getSpanEnd(node) - arena.getSpanStart(node);
|
|
345
|
+
}
|
|
346
|
+
return length > 0 ? length + OUTPUT_PADDING : DEFAULT_CAPACITY;
|
|
347
|
+
}
|
|
265
348
|
/**
|
|
266
349
|
* render ast
|
|
267
350
|
* @param arena
|
|
@@ -273,6 +356,8 @@ class NodeRenderer {
|
|
|
273
356
|
*/
|
|
274
357
|
function doRender(arena, data, options = {}, mapping, memo) {
|
|
275
358
|
options = resolveRenderOptions(options);
|
|
359
|
+
// estimated from the node to render: the nodes rendered below may be copies
|
|
360
|
+
const capacity = outputCapacity(arena, data);
|
|
276
361
|
const expandNesting = options.expandNestingRules &&
|
|
277
362
|
[EnumToken.StyleSheetNodeType, EnumToken.AtRuleNodeType, EnumToken.RuleNodeType].includes(arena.dataTypes[data]) &&
|
|
278
363
|
(arena.flags[data] & EnumAstNodeStatus.HasChildren) !== 0;
|
|
@@ -313,8 +398,10 @@ function doRender(arena, data, options = {}, mapping, memo) {
|
|
|
313
398
|
end: 0,
|
|
314
399
|
};
|
|
315
400
|
const linesMap = options.sourcemap ? new LineMap() : null;
|
|
316
|
-
|
|
401
|
+
// the generated css is written to the buffer. its size is estimated from the input: it should not grow
|
|
402
|
+
const writer = new Writer(capacity);
|
|
317
403
|
if (mapping != null) {
|
|
404
|
+
let code = "";
|
|
318
405
|
if (mapping.importMapping != null) {
|
|
319
406
|
const absolutePath = options.resolve(options.output != null ? dirname(options.output) : dirname(options.src), options.cwd).absolute;
|
|
320
407
|
for (let [key, value] of Object.entries(mapping.importMapping)) {
|
|
@@ -326,6 +413,7 @@ function doRender(arena, data, options = {}, mapping, memo) {
|
|
|
326
413
|
}
|
|
327
414
|
}
|
|
328
415
|
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}`;
|
|
416
|
+
writer.write(code);
|
|
329
417
|
if (sourcemap != null) {
|
|
330
418
|
move(sourceLocation, linesMap, code);
|
|
331
419
|
}
|
|
@@ -334,13 +422,9 @@ function doRender(arena, data, options = {}, mapping, memo) {
|
|
|
334
422
|
// @ts-ignore
|
|
335
423
|
options.output = options.resolve(options.output, options.cwd).absolute;
|
|
336
424
|
}
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
errors,
|
|
341
|
-
stats: {
|
|
342
|
-
total: `${(performance.now() - startTime).toFixed(2)}ms`,
|
|
343
|
-
},
|
|
425
|
+
renderAstNode(arena, data, options, sourcemaps, sourceLocation, linesMap, errors, createReducer(arena, options, cache), cache, writer);
|
|
426
|
+
const stats = {
|
|
427
|
+
total: `${(performance.now() - startTime).toFixed(2)}ms`,
|
|
344
428
|
};
|
|
345
429
|
if (ownMemo) {
|
|
346
430
|
RenderMemo.release(cache.memo);
|
|
@@ -358,12 +442,11 @@ function doRender(arena, data, options = {}, mapping, memo) {
|
|
|
358
442
|
sourcemap.addSourceContent(source.id, source.file, source.content);
|
|
359
443
|
}
|
|
360
444
|
sourcemap.add(sourcemaps.maps);
|
|
361
|
-
result.map = sourcemap;
|
|
362
445
|
if (options.sourcemap === "inline") {
|
|
363
|
-
|
|
446
|
+
writer.write(`\n/*# sourceMappingURL=${sourcemap.toUrl()} */`);
|
|
364
447
|
}
|
|
365
448
|
}
|
|
366
|
-
return
|
|
449
|
+
return new RenderResult(writer.bytes(), errors, stats, sourcemap ?? undefined, writer.codeLength);
|
|
367
450
|
}
|
|
368
451
|
/**
|
|
369
452
|
* Update source map
|
|
@@ -497,7 +580,28 @@ function move(sourceLocation, linesMap, str, start, end) {
|
|
|
497
580
|
}
|
|
498
581
|
}
|
|
499
582
|
/**
|
|
500
|
-
*
|
|
583
|
+
* write an interned string: an ascii string is copied from the string table, the string is not created
|
|
584
|
+
* @param arena
|
|
585
|
+
* @param id
|
|
586
|
+
* @param writer
|
|
587
|
+
*/
|
|
588
|
+
function writeString(arena, id, writer) {
|
|
589
|
+
const length = arena.stringLength(id);
|
|
590
|
+
if (length === 0) {
|
|
591
|
+
return;
|
|
592
|
+
}
|
|
593
|
+
writer.reserve(length);
|
|
594
|
+
const written = arena.copyStringAscii(id, writer.buffer, writer.length);
|
|
595
|
+
if (written >= 0) {
|
|
596
|
+
writer.length += written;
|
|
597
|
+
}
|
|
598
|
+
else {
|
|
599
|
+
writer.write(arena.resolveString(id));
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
/**
|
|
603
|
+
* render ast node: the rendered css is written to the writer. nothing is written when the node is rendered as an empty
|
|
604
|
+
* string (removed comments, empty rules, ...)
|
|
501
605
|
* @param arena
|
|
502
606
|
* @param data node id
|
|
503
607
|
* @param options
|
|
@@ -507,117 +611,210 @@ function move(sourceLocation, linesMap, str, start, end) {
|
|
|
507
611
|
* @param errors
|
|
508
612
|
* @param reducer
|
|
509
613
|
* @param cache
|
|
614
|
+
* @param writer output
|
|
510
615
|
* @param level
|
|
511
616
|
* @param indents
|
|
512
617
|
*
|
|
513
618
|
* @internal
|
|
514
619
|
*/
|
|
515
|
-
function renderAstNode(arena, data, options, sourcemaps, sourceLocation, linesMap, errors, reducer, cache, level = 0, indents = []) {
|
|
620
|
+
function renderAstNode(arena, data, options, sourcemaps, sourceLocation, linesMap, errors, reducer, cache, writer, level = 0, indents = []) {
|
|
516
621
|
if (indents.length < level + 1) {
|
|
517
622
|
indents.push(options.indent.repeat(level));
|
|
518
623
|
}
|
|
519
624
|
if (indents.length < level + 2) {
|
|
520
625
|
indents.push(options.indent.repeat(level + 1));
|
|
521
626
|
}
|
|
522
|
-
// @ts-ignore
|
|
523
|
-
let children = "";
|
|
524
|
-
let str = "";
|
|
525
627
|
const indent = indents[level];
|
|
526
|
-
const indentSub = indents[level + 1];
|
|
527
628
|
const typ = arena.dataTypes[data];
|
|
528
629
|
switch (typ) {
|
|
529
630
|
case EnumToken.DeclarationNodeType:
|
|
530
|
-
|
|
631
|
+
writeString(arena, arena.name[data], writer);
|
|
632
|
+
writer.writeByte(0x3a);
|
|
633
|
+
writer.write(options.indent);
|
|
634
|
+
writer.write(renderDeclarationValue(arena, data, options.minifyValues, reducer));
|
|
635
|
+
return;
|
|
531
636
|
case EnumToken.CommentNodeType:
|
|
532
637
|
case EnumToken.CDOCOMMNodeType: {
|
|
533
|
-
const
|
|
534
|
-
if (
|
|
638
|
+
const id = arena.stringValue[data];
|
|
639
|
+
if (arena.stringStartsWith(id, "/*# sourceMappingURL=")) {
|
|
535
640
|
// ignore sourcemap comment
|
|
536
|
-
return
|
|
641
|
+
return;
|
|
537
642
|
}
|
|
538
|
-
|
|
643
|
+
// the removed comments are not created
|
|
644
|
+
if (!options.removeComments || (options.preserveLicense && arena.stringStartsWith(id, "/*!"))) {
|
|
645
|
+
writeString(arena, id, writer);
|
|
646
|
+
}
|
|
647
|
+
return;
|
|
539
648
|
}
|
|
540
|
-
case EnumToken.StyleSheetNodeType:
|
|
649
|
+
case EnumToken.StyleSheetNodeType: {
|
|
650
|
+
const newLine = options.newLine;
|
|
651
|
+
let empty = true;
|
|
652
|
+
let mark;
|
|
653
|
+
let markExtra;
|
|
654
|
+
let start;
|
|
541
655
|
for (let node = arena.firstChild[data]; node !== 0; node = arena.nextSibling[node]) {
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
656
|
+
mark = writer.length;
|
|
657
|
+
markExtra = writer.extra;
|
|
658
|
+
// the separator is dropped when the node is empty
|
|
659
|
+
if (!empty && newLine !== "") {
|
|
660
|
+
writer.write(newLine);
|
|
545
661
|
}
|
|
546
|
-
|
|
547
|
-
|
|
662
|
+
start = writer.length;
|
|
663
|
+
renderAstNode(arena, node, options, sourcemaps, sourceLocation, linesMap, errors, reducer, cache, writer, level, indents);
|
|
664
|
+
if (writer.length === start) {
|
|
665
|
+
writer.truncate(mark, markExtra);
|
|
666
|
+
continue;
|
|
548
667
|
}
|
|
549
|
-
|
|
550
|
-
if (sourcemaps != null &&
|
|
551
|
-
move(sourceLocation, linesMap,
|
|
668
|
+
empty = false;
|
|
669
|
+
if (sourcemaps != null && newLine) {
|
|
670
|
+
move(sourceLocation, linesMap, newLine);
|
|
552
671
|
}
|
|
553
672
|
}
|
|
554
|
-
return
|
|
673
|
+
return;
|
|
674
|
+
}
|
|
555
675
|
case EnumToken.AtRuleNodeType:
|
|
556
676
|
case EnumToken.RuleNodeType:
|
|
557
677
|
case EnumToken.KeyframesRuleNodeType:
|
|
558
678
|
case EnumToken.KeyframesAtRuleNodeType: {
|
|
559
679
|
const isAtRule = typ === EnumToken.AtRuleNodeType || typ === EnumToken.KeyframesAtRuleNodeType;
|
|
560
|
-
|
|
561
|
-
const
|
|
562
|
-
|
|
563
|
-
|
|
680
|
+
// the name and the value are copied from the string table: the strings are not created
|
|
681
|
+
const nameId = arena.name[data];
|
|
682
|
+
const valueId = arena.stringValue[data];
|
|
683
|
+
const newLine = options.newLine;
|
|
684
|
+
// start of the node: the node is removed when it has no children
|
|
685
|
+
const start = writer.length;
|
|
686
|
+
const startExtra = writer.extra;
|
|
687
|
+
if (indent.length > 0 && newLine !== "") {
|
|
688
|
+
writer.write(newLine);
|
|
689
|
+
}
|
|
690
|
+
if (indent.length > 0) {
|
|
691
|
+
writer.write(indent);
|
|
692
|
+
}
|
|
693
|
+
if (isAtRule) {
|
|
694
|
+
writer.writeByte(0x40);
|
|
695
|
+
writeString(arena, nameId, writer);
|
|
696
|
+
if (arena.stringLength(valueId) > 0) {
|
|
697
|
+
writer.write(options.indent || " ");
|
|
698
|
+
writeString(arena, valueId, writer);
|
|
699
|
+
}
|
|
700
|
+
if (!(arena.flags[data] & EnumAstNodeStatus.HasChildren)) {
|
|
701
|
+
// @rule prelude;
|
|
702
|
+
writer.writeByte(0x3b);
|
|
703
|
+
return;
|
|
704
|
+
}
|
|
564
705
|
}
|
|
706
|
+
else {
|
|
707
|
+
writeString(arena, nameId, writer);
|
|
708
|
+
}
|
|
709
|
+
if (options.indent !== "") {
|
|
710
|
+
writer.write(options.indent);
|
|
711
|
+
}
|
|
712
|
+
writer.writeByte(0x7b);
|
|
565
713
|
// inlined: linesMap.getLineStarts()
|
|
566
714
|
const lineMapLength = linesMap ? linesMap.lineStarts.length : 0;
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
(isAtRule
|
|
570
|
-
? `@${nam}${val === "" ? "" : options.indent || " "}${val}${options.indent}{`
|
|
571
|
-
: nam + `${options.indent}{`);
|
|
715
|
+
// the prelude is only needed by the source map
|
|
716
|
+
let prelude = "";
|
|
572
717
|
if (sourcemaps != null) {
|
|
718
|
+
const nam = arena.resolveString(nameId);
|
|
719
|
+
const val = arena.resolveString(valueId);
|
|
720
|
+
prelude =
|
|
721
|
+
(indent.length > 0 ? newLine : "") +
|
|
722
|
+
indent +
|
|
723
|
+
(isAtRule
|
|
724
|
+
? `@${nam}${val === "" ? "" : options.indent || " "}${val}${options.indent}{`
|
|
725
|
+
: nam + `${options.indent}{`);
|
|
573
726
|
updateSourceMap(arena, data, options, cache, sourcemaps, sourceLocation, linesMap, prelude);
|
|
574
727
|
}
|
|
728
|
+
const indentSub = indents[level + 1];
|
|
729
|
+
// the children are written after the prelude
|
|
730
|
+
const body = writer.length;
|
|
731
|
+
let str;
|
|
575
732
|
let node;
|
|
576
733
|
let nodeType;
|
|
734
|
+
let mark;
|
|
735
|
+
let childStart;
|
|
577
736
|
let recordDeclarationSourceMap = typ === EnumToken.AtRuleNodeType;
|
|
578
|
-
//
|
|
579
|
-
//
|
|
580
|
-
// the rendered strings are not read (reading a concatenated string flattens it)
|
|
581
|
-
let last = "";
|
|
737
|
+
// the last child ends with a semicolon that is not written yet: it is written before the next child.
|
|
738
|
+
// the semicolon that ends the last child is dropped
|
|
582
739
|
let semicolon = false;
|
|
583
740
|
for (node = arena.firstChild[data]; node !== 0; node = arena.nextSibling[node]) {
|
|
584
741
|
nodeType = arena.dataTypes[node];
|
|
742
|
+
if (nodeType === EnumToken.DeclarationNodeType && sourcemaps == null) {
|
|
743
|
+
// fast path: the declaration is written to the buffer
|
|
744
|
+
if (semicolon) {
|
|
745
|
+
writer.writeByte(0x3b);
|
|
746
|
+
}
|
|
747
|
+
if (newLine !== "") {
|
|
748
|
+
writer.write(newLine);
|
|
749
|
+
}
|
|
750
|
+
if (indentSub !== "") {
|
|
751
|
+
writer.write(indentSub);
|
|
752
|
+
}
|
|
753
|
+
writeDeclaration(arena, node, options, cache, reducer, writer);
|
|
754
|
+
semicolon = true;
|
|
755
|
+
continue;
|
|
756
|
+
}
|
|
585
757
|
if (nodeType === EnumToken.CommentNodeType) {
|
|
586
|
-
const
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
758
|
+
const commentId = arena.stringValue[node];
|
|
759
|
+
// the removed comments are not created
|
|
760
|
+
if (options.removeComments &&
|
|
761
|
+
(!options.preserveLicense || !arena.stringStartsWith(commentId, "/*!"))) {
|
|
762
|
+
continue;
|
|
763
|
+
}
|
|
764
|
+
str = arena.resolveString(commentId);
|
|
591
765
|
}
|
|
592
766
|
else if (nodeType === EnumToken.DeclarationNodeType) {
|
|
593
767
|
// the semicolon is not part of the rendered declaration
|
|
594
768
|
str = renderDeclaration(arena, node, options, cache, reducer);
|
|
595
769
|
}
|
|
596
770
|
else {
|
|
597
|
-
|
|
598
|
-
if (
|
|
771
|
+
mark = writer.length;
|
|
772
|
+
if (semicolon) {
|
|
773
|
+
writer.writeByte(0x3b);
|
|
774
|
+
}
|
|
775
|
+
childStart = writer.length;
|
|
776
|
+
renderAstNode(arena, node, options, sourcemaps, sourceLocation, linesMap, errors, reducer, cache, writer, level + 1, indents);
|
|
777
|
+
if (writer.length === childStart) {
|
|
778
|
+
// empty node: the pending semicolon is not written
|
|
779
|
+
writer.length = mark;
|
|
599
780
|
continue;
|
|
600
781
|
}
|
|
601
|
-
children += semicolon ? last + ";" : last;
|
|
602
782
|
// at-rules without a block ('@layer name;') end with a semicolon
|
|
603
|
-
semicolon = endsWithSemicolon(arena, node,
|
|
604
|
-
|
|
605
|
-
|
|
783
|
+
semicolon = endsWithSemicolon(arena, node, writer);
|
|
784
|
+
if (semicolon) {
|
|
785
|
+
writer.length--;
|
|
786
|
+
}
|
|
606
787
|
continue;
|
|
607
788
|
}
|
|
608
789
|
if (str === "") {
|
|
609
790
|
continue;
|
|
610
791
|
}
|
|
611
|
-
|
|
612
|
-
|
|
792
|
+
if (semicolon) {
|
|
793
|
+
writer.writeByte(0x3b);
|
|
794
|
+
}
|
|
795
|
+
if (sourcemaps == null) {
|
|
796
|
+
// comments
|
|
797
|
+
if (newLine !== "") {
|
|
798
|
+
writer.write(newLine);
|
|
799
|
+
}
|
|
800
|
+
if (indentSub !== "") {
|
|
801
|
+
writer.write(indentSub);
|
|
802
|
+
}
|
|
803
|
+
writer.write(str);
|
|
804
|
+
}
|
|
805
|
+
else {
|
|
806
|
+
str = newLine + indentSub + str;
|
|
807
|
+
writer.write(str);
|
|
808
|
+
}
|
|
613
809
|
if (nodeType === EnumToken.DeclarationNodeType) {
|
|
614
|
-
last = str;
|
|
615
810
|
semicolon = true;
|
|
616
811
|
}
|
|
617
812
|
else {
|
|
618
|
-
// comments
|
|
619
|
-
semicolon =
|
|
620
|
-
|
|
813
|
+
// comments
|
|
814
|
+
semicolon = writer.lastByte() === 0x3b;
|
|
815
|
+
if (semicolon) {
|
|
816
|
+
writer.length--;
|
|
817
|
+
}
|
|
621
818
|
}
|
|
622
819
|
if (sourcemaps != null) {
|
|
623
820
|
if (nodeType === EnumToken.DeclarationNodeType && recordDeclarationSourceMap) {
|
|
@@ -642,32 +839,45 @@ function renderAstNode(arena, data, options, sourcemaps, sourceLocation, linesMa
|
|
|
642
839
|
if (semicolon) {
|
|
643
840
|
sourceLocation.end--;
|
|
644
841
|
}
|
|
645
|
-
|
|
646
|
-
|
|
842
|
+
// an empty contents block is kept: `@apply --mixin {}` does not use the default contents of the mixin.
|
|
843
|
+
// an empty mixin or custom function is a definition
|
|
844
|
+
if (options.removeEmpty &&
|
|
845
|
+
writer.length === body &&
|
|
846
|
+
!(typ === EnumToken.AtRuleNodeType &&
|
|
847
|
+
(arena.stringEquals(nameId, "apply") ||
|
|
848
|
+
arena.stringEquals(nameId, "mixin") ||
|
|
849
|
+
arena.stringEquals(nameId, "function")))) {
|
|
647
850
|
if (sourcemaps != null) {
|
|
648
851
|
sourceLocation.end -= prelude.length;
|
|
649
852
|
// inlined: linesMap!.getLineStarts()
|
|
650
853
|
linesMap.lineStarts.length = lineMapLength;
|
|
651
854
|
}
|
|
652
|
-
|
|
855
|
+
writer.truncate(start, startExtra);
|
|
856
|
+
return;
|
|
857
|
+
}
|
|
858
|
+
if (newLine !== "") {
|
|
859
|
+
writer.write(newLine);
|
|
653
860
|
}
|
|
654
|
-
|
|
861
|
+
if (indent !== "") {
|
|
862
|
+
writer.write(indent);
|
|
863
|
+
}
|
|
864
|
+
writer.writeByte(0x7d);
|
|
655
865
|
if (sourcemaps != null) {
|
|
656
|
-
move(sourceLocation, linesMap,
|
|
866
|
+
move(sourceLocation, linesMap, newLine + indent + "}");
|
|
657
867
|
}
|
|
658
|
-
return
|
|
868
|
+
return;
|
|
659
869
|
}
|
|
660
870
|
default:
|
|
661
|
-
return
|
|
871
|
+
return;
|
|
662
872
|
}
|
|
663
873
|
}
|
|
664
874
|
/**
|
|
665
875
|
* true if a rendered child node ends with a semicolon
|
|
666
876
|
* @param arena
|
|
667
877
|
* @param node child node
|
|
668
|
-
* @param
|
|
878
|
+
* @param writer output, the child node is the last node written, it is not empty
|
|
669
879
|
*/
|
|
670
|
-
function endsWithSemicolon(arena, node,
|
|
880
|
+
function endsWithSemicolon(arena, node, writer) {
|
|
671
881
|
switch (arena.dataTypes[node]) {
|
|
672
882
|
case EnumToken.AtRuleNodeType:
|
|
673
883
|
case EnumToken.KeyframesAtRuleNodeType:
|
|
@@ -679,7 +889,7 @@ function endsWithSemicolon(arena, node, rendered) {
|
|
|
679
889
|
return false;
|
|
680
890
|
default:
|
|
681
891
|
// comments: flat strings
|
|
682
|
-
return
|
|
892
|
+
return writer.lastByte() === 0x3b;
|
|
683
893
|
}
|
|
684
894
|
}
|
|
685
895
|
function renderTokenList(tokens, reducer) {
|
|
@@ -689,6 +899,34 @@ function renderTokenList(tokens, reducer) {
|
|
|
689
899
|
}
|
|
690
900
|
return result;
|
|
691
901
|
}
|
|
902
|
+
/**
|
|
903
|
+
* render a declaration without memoization: `name:value`, without the semicolon
|
|
904
|
+
* @param arena
|
|
905
|
+
* @param node declaration
|
|
906
|
+
* @param options
|
|
907
|
+
* @param cache
|
|
908
|
+
* @param reducer
|
|
909
|
+
*/
|
|
910
|
+
function renderDeclarationString(arena, node, options, cache, reducer) {
|
|
911
|
+
if (options.minifyValues && isUnminifiedDeclaration(arena, node)) {
|
|
912
|
+
// override options to avoid minifying values for custom properties
|
|
913
|
+
options = { ...options, minifyValues: false };
|
|
914
|
+
// override the reducer
|
|
915
|
+
reducer = createReducer(arena, options, cache);
|
|
916
|
+
}
|
|
917
|
+
return `${arena.resolveString(arena.name[node])}:${options.indent}${renderDeclarationValue(arena, node, options.minifyValues, reducer).trimEnd()}`;
|
|
918
|
+
}
|
|
919
|
+
/**
|
|
920
|
+
* true if the value of the declaration is not minified:
|
|
921
|
+
* - the custom properties
|
|
922
|
+
* - the `initial-value` descriptor of `@property`: the value is parsed with the syntax of the registered custom
|
|
923
|
+
* property, a zero dimension keeps its unit: `0deg` is a valid `<angle>`, `0` is not
|
|
924
|
+
* @param arena
|
|
925
|
+
* @param node declaration
|
|
926
|
+
*/
|
|
927
|
+
function isUnminifiedDeclaration(arena, node) {
|
|
928
|
+
return arena.startsWith(node, "nam", "--") || arena.stringEquals(arena.name[node], "initial-value");
|
|
929
|
+
}
|
|
692
930
|
/**
|
|
693
931
|
* render a declaration of a block: `name:value`, without the semicolon. identical declarations (same name, same value
|
|
694
932
|
* tokens) are rendered once per render
|
|
@@ -700,30 +938,221 @@ function renderTokenList(tokens, reducer) {
|
|
|
700
938
|
*/
|
|
701
939
|
function renderDeclaration(arena, node, options, cache, reducer) {
|
|
702
940
|
const memo = getRenderMemo(cache, options);
|
|
703
|
-
let minifyValues = options.minifyValues;
|
|
704
|
-
if (minifyValues) {
|
|
705
|
-
minifyValues = !arena.startsWith(node, "nam", "--");
|
|
706
|
-
if (!minifyValues) {
|
|
707
|
-
// override options to avoid minifying values for custom properties
|
|
708
|
-
options = { ...options, minifyValues: false };
|
|
709
|
-
// override the reducer
|
|
710
|
-
reducer = createReducer(arena, options, cache);
|
|
711
|
-
}
|
|
712
|
-
}
|
|
713
941
|
if (memo == null) {
|
|
714
|
-
return
|
|
942
|
+
return renderDeclarationString(arena, node, options, cache, reducer);
|
|
715
943
|
}
|
|
716
944
|
// the name is part of the key
|
|
717
945
|
const slot = memo.declarations.list(arena, arena.firstToken[node], arena.name[node]);
|
|
718
946
|
// inlined: memo.declarations.get(slot)
|
|
719
947
|
let rendered = memo.declarations.values[slot];
|
|
720
948
|
if (rendered === undefined) {
|
|
721
|
-
rendered =
|
|
949
|
+
rendered = renderDeclarationString(arena, node, options, cache, reducer);
|
|
722
950
|
// inlined: memo.declarations.set(slot, rendered)
|
|
723
951
|
memo.declarations.values[slot] = rendered;
|
|
724
952
|
}
|
|
725
953
|
return rendered;
|
|
726
954
|
}
|
|
955
|
+
/**
|
|
956
|
+
* the rendered value of a declaration (without the name): rendered once per structure, see {@link RenderMemo}
|
|
957
|
+
* @param arena
|
|
958
|
+
* @param node declaration
|
|
959
|
+
* @param options
|
|
960
|
+
* @param cache
|
|
961
|
+
* @param reducer
|
|
962
|
+
* @param slot the memo slot of the declaration when it has been looked up, -1 otherwise
|
|
963
|
+
*/
|
|
964
|
+
function declarationValue(arena, node, options, cache, reducer, slot = -1) {
|
|
965
|
+
const memo = getRenderMemo(cache, options);
|
|
966
|
+
if (memo !== null) {
|
|
967
|
+
if (slot === -1) {
|
|
968
|
+
// the name is part of the key
|
|
969
|
+
slot = memo.declarations.list(arena, arena.firstToken[node], arena.name[node]);
|
|
970
|
+
}
|
|
971
|
+
const value = memo.values[slot];
|
|
972
|
+
if (value !== undefined) {
|
|
973
|
+
return value;
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
if (options.minifyValues && isUnminifiedDeclaration(arena, node)) {
|
|
977
|
+
// override options to avoid minifying values for custom properties
|
|
978
|
+
options = { ...options, minifyValues: false };
|
|
979
|
+
// override the reducer
|
|
980
|
+
reducer = createReducer(arena, options, cache);
|
|
981
|
+
}
|
|
982
|
+
const value = renderDeclarationValue(arena, node, options.minifyValues, reducer).trimEnd();
|
|
983
|
+
if (memo !== null) {
|
|
984
|
+
memo.values[slot] = value;
|
|
985
|
+
}
|
|
986
|
+
return value;
|
|
987
|
+
}
|
|
988
|
+
function writeDeclaration(arena, node, options, cache, reducer, writer) {
|
|
989
|
+
const memo = getRenderMemo(cache, options);
|
|
990
|
+
let slot = -1;
|
|
991
|
+
if (memo !== null) {
|
|
992
|
+
// the name is part of the key
|
|
993
|
+
slot = memo.declarations.list(arena, arena.firstToken[node], arena.name[node]);
|
|
994
|
+
const index = slot << 2;
|
|
995
|
+
// an identical declaration is already written to the buffer: its bytes are copied
|
|
996
|
+
if (index < memo.locations.length && memo.locations[index] === writer.id) {
|
|
997
|
+
writer.copy(memo.locations[index + 1], memo.locations[index + 2], memo.locations[index + 3]);
|
|
998
|
+
return;
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
const start = writer.length;
|
|
1002
|
+
const extra = writer.extra;
|
|
1003
|
+
// the rendered value of an identical declaration, rendered to another buffer (see NodeRenderer)
|
|
1004
|
+
const value = declarationValue(arena, node, options, cache, reducer, slot);
|
|
1005
|
+
// the parts are written directly: `name`, `:`, `indent`, `value`. the name is copied from the string table
|
|
1006
|
+
writeString(arena, arena.name[node], writer);
|
|
1007
|
+
writer.writeByte(0x3a);
|
|
1008
|
+
if (options.indent !== "") {
|
|
1009
|
+
writer.write(options.indent);
|
|
1010
|
+
}
|
|
1011
|
+
writer.write(value);
|
|
1012
|
+
if (memo !== null) {
|
|
1013
|
+
memo.locate(slot, writer, start, extra);
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
/**
|
|
1017
|
+
* the size of a rendered node, computed without rendering it: the declarations are measured from their rendered values
|
|
1018
|
+
* (rendered once per structure, see {@link declarationValue}), the layout follows {@link renderAstNode} (no source
|
|
1019
|
+
* map). the result is `size * 2 + 1` when the output ends with a semicolon, `size * 2` otherwise (0 when the node
|
|
1020
|
+
* renders nothing): no object is allocated
|
|
1021
|
+
* @param arena
|
|
1022
|
+
* @param data
|
|
1023
|
+
* @param options
|
|
1024
|
+
* @param reducer
|
|
1025
|
+
* @param cache
|
|
1026
|
+
* @param level
|
|
1027
|
+
*/
|
|
1028
|
+
function nodeSize(arena, data, options, reducer, cache, level) {
|
|
1029
|
+
const optIndent = options.indent;
|
|
1030
|
+
const newLine = options.newLine;
|
|
1031
|
+
const indentLength = optIndent.length * level;
|
|
1032
|
+
const typ = arena.dataTypes[data];
|
|
1033
|
+
switch (typ) {
|
|
1034
|
+
case EnumToken.DeclarationNodeType: {
|
|
1035
|
+
const value = declarationValue(arena, data, options, cache, reducer);
|
|
1036
|
+
return ((arena.stringLength(arena.name[data]) + 1 + optIndent.length + value.length) * 2 +
|
|
1037
|
+
(value.endsWith(";") ? 1 : 0));
|
|
1038
|
+
}
|
|
1039
|
+
case EnumToken.CommentNodeType:
|
|
1040
|
+
case EnumToken.CDOCOMMNodeType: {
|
|
1041
|
+
const id = arena.stringValue[data];
|
|
1042
|
+
if (arena.stringStartsWith(id, "/*# sourceMappingURL=")) {
|
|
1043
|
+
return 0;
|
|
1044
|
+
}
|
|
1045
|
+
if (!options.removeComments || (options.preserveLicense && arena.stringStartsWith(id, "/*!"))) {
|
|
1046
|
+
const length = arena.stringLength(id);
|
|
1047
|
+
return length * 2 + (length > 0 && arena.stringCodeAt(id, length - 1) === 0x3b ? 1 : 0);
|
|
1048
|
+
}
|
|
1049
|
+
return 0;
|
|
1050
|
+
}
|
|
1051
|
+
case EnumToken.StyleSheetNodeType: {
|
|
1052
|
+
let size = 0;
|
|
1053
|
+
let empty = true;
|
|
1054
|
+
let last = 0;
|
|
1055
|
+
for (let node = arena.firstChild[data]; node !== 0; node = arena.nextSibling[node]) {
|
|
1056
|
+
const child = nodeSize(arena, node, options, reducer, cache, level);
|
|
1057
|
+
if (child >= 2) {
|
|
1058
|
+
if (!empty) {
|
|
1059
|
+
size += newLine.length;
|
|
1060
|
+
}
|
|
1061
|
+
size += child >> 1;
|
|
1062
|
+
empty = false;
|
|
1063
|
+
last = child & 1;
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
return size * 2 + last;
|
|
1067
|
+
}
|
|
1068
|
+
case EnumToken.AtRuleNodeType:
|
|
1069
|
+
case EnumToken.RuleNodeType:
|
|
1070
|
+
case EnumToken.KeyframesRuleNodeType:
|
|
1071
|
+
case EnumToken.KeyframesAtRuleNodeType: {
|
|
1072
|
+
const isAtRule = typ === EnumToken.AtRuleNodeType || typ === EnumToken.KeyframesAtRuleNodeType;
|
|
1073
|
+
const nameId = arena.name[data];
|
|
1074
|
+
const valueId = arena.stringValue[data];
|
|
1075
|
+
let size = 0;
|
|
1076
|
+
if (indentLength > 0 && newLine !== "") {
|
|
1077
|
+
size += newLine.length;
|
|
1078
|
+
}
|
|
1079
|
+
size += indentLength;
|
|
1080
|
+
if (isAtRule) {
|
|
1081
|
+
size += 1 + arena.stringLength(nameId);
|
|
1082
|
+
if (arena.stringLength(valueId) > 0) {
|
|
1083
|
+
size += (optIndent || " ").length + arena.stringLength(valueId);
|
|
1084
|
+
}
|
|
1085
|
+
if (!(arena.flags[data] & EnumAstNodeStatus.HasChildren)) {
|
|
1086
|
+
return (size + 1) * 2 + 1;
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
else {
|
|
1090
|
+
size += arena.stringLength(nameId);
|
|
1091
|
+
}
|
|
1092
|
+
size += optIndent.length + 1;
|
|
1093
|
+
const indentSubLength = optIndent.length * (level + 1);
|
|
1094
|
+
let body = 0;
|
|
1095
|
+
let semicolon = false;
|
|
1096
|
+
for (let node = arena.firstChild[data]; node !== 0; node = arena.nextSibling[node]) {
|
|
1097
|
+
const nodeType = arena.dataTypes[node];
|
|
1098
|
+
if (nodeType === EnumToken.DeclarationNodeType) {
|
|
1099
|
+
body +=
|
|
1100
|
+
(semicolon ? 1 : 0) +
|
|
1101
|
+
newLine.length +
|
|
1102
|
+
indentSubLength +
|
|
1103
|
+
(nodeSize(arena, node, options, reducer, cache, level + 1) >> 1);
|
|
1104
|
+
semicolon = true;
|
|
1105
|
+
}
|
|
1106
|
+
else if (nodeType === EnumToken.CommentNodeType) {
|
|
1107
|
+
const commentId = arena.stringValue[node];
|
|
1108
|
+
if (options.removeComments && (!options.preserveLicense || !arena.stringStartsWith(commentId, "/*!"))) {
|
|
1109
|
+
continue;
|
|
1110
|
+
}
|
|
1111
|
+
const length = arena.stringLength(commentId);
|
|
1112
|
+
if (length > 0) {
|
|
1113
|
+
body += (semicolon ? 1 : 0) + newLine.length + indentSubLength + length;
|
|
1114
|
+
// a trailing semicolon is removed: it is written before the next node
|
|
1115
|
+
semicolon = arena.stringCodeAt(commentId, length - 1) === 0x3b;
|
|
1116
|
+
if (semicolon) {
|
|
1117
|
+
body--;
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
else {
|
|
1122
|
+
const child = nodeSize(arena, node, options, reducer, cache, level + 1);
|
|
1123
|
+
if (child >= 2) {
|
|
1124
|
+
body += (semicolon ? 1 : 0) + (child >> 1);
|
|
1125
|
+
switch (nodeType) {
|
|
1126
|
+
case EnumToken.AtRuleNodeType:
|
|
1127
|
+
case EnumToken.KeyframesAtRuleNodeType:
|
|
1128
|
+
semicolon = !(arena.flags[node] & EnumAstNodeStatus.HasChildren);
|
|
1129
|
+
break;
|
|
1130
|
+
case EnumToken.RuleNodeType:
|
|
1131
|
+
case EnumToken.KeyframesRuleNodeType:
|
|
1132
|
+
semicolon = false;
|
|
1133
|
+
break;
|
|
1134
|
+
default:
|
|
1135
|
+
semicolon = (child & 1) === 1;
|
|
1136
|
+
}
|
|
1137
|
+
if (semicolon) {
|
|
1138
|
+
body--;
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
if (options.removeEmpty &&
|
|
1144
|
+
body === 0 &&
|
|
1145
|
+
!(typ === EnumToken.AtRuleNodeType &&
|
|
1146
|
+
(arena.stringEquals(nameId, "apply") ||
|
|
1147
|
+
arena.stringEquals(nameId, "mixin") ||
|
|
1148
|
+
arena.stringEquals(nameId, "function")))) {
|
|
1149
|
+
return 0;
|
|
1150
|
+
}
|
|
1151
|
+
return (size + body + newLine.length + indentLength + 1) * 2;
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
return 0;
|
|
1155
|
+
}
|
|
727
1156
|
/**
|
|
728
1157
|
* render the value tokens of a declaration with the reducer. with minifyValues, the whitespace that is not needed is
|
|
729
1158
|
* skipped like {@link filterValues} does, without copying the tokens into an array: the whitespace before
|
|
@@ -764,7 +1193,7 @@ function renderDeclarationValue(arena, node, minifyValues, reducer) {
|
|
|
764
1193
|
pending = 0;
|
|
765
1194
|
check = false;
|
|
766
1195
|
}
|
|
767
|
-
else if (
|
|
1196
|
+
else if (isFunctionType(type) &&
|
|
768
1197
|
// color keywords and hex colors are not functions
|
|
769
1198
|
(type !== EnumToken.ColorTokenType || arena.firstToken[token] !== 0) &&
|
|
770
1199
|
type !== EnumToken.WildCardFunctionTokenType &&
|
|
@@ -795,10 +1224,10 @@ const defaultCache = Object.create(null);
|
|
|
795
1224
|
function createReducer(arena, options, cache, errors) {
|
|
796
1225
|
const reducer = (acc, curr) => {
|
|
797
1226
|
if (arena.dataTypes[curr] === EnumToken.CommentTokenType && options.removeComments) {
|
|
798
|
-
if (!options.preserveLicense || !arena.
|
|
1227
|
+
if (!options.preserveLicense || !arena.resolveString(arena.stringValue[curr]).startsWith("/*!")) {
|
|
799
1228
|
return acc;
|
|
800
1229
|
}
|
|
801
|
-
return acc + arena.
|
|
1230
|
+
return acc + arena.resolveString(arena.stringValue[curr]);
|
|
802
1231
|
}
|
|
803
1232
|
return acc + renderValue(arena, curr, options, cache, reducer);
|
|
804
1233
|
};
|
|
@@ -821,8 +1250,8 @@ function renderReduced(arena, first, options, cache, reducer, errors) {
|
|
|
821
1250
|
result = reducer(result, token);
|
|
822
1251
|
}
|
|
823
1252
|
else if (arena.dataTypes[token] === EnumToken.CommentTokenType && options.removeComments) {
|
|
824
|
-
if (options.preserveLicense && arena.
|
|
825
|
-
result += arena.
|
|
1253
|
+
if (options.preserveLicense && arena.resolveString(arena.stringValue[token]).startsWith("/*!")) {
|
|
1254
|
+
result += arena.resolveString(arena.stringValue[token]);
|
|
826
1255
|
}
|
|
827
1256
|
}
|
|
828
1257
|
else {
|
|
@@ -881,25 +1310,25 @@ function renderColor(arena, token, options, cache, reducer, errors) {
|
|
|
881
1310
|
token = value;
|
|
882
1311
|
}
|
|
883
1312
|
if (arena.colorType[token] === ColorType.HEX || arena.colorType[token] === ColorType.LIT) {
|
|
884
|
-
return reduceHexValue(arena.
|
|
1313
|
+
return reduceHexValue(arena.resolveString(arena.stringValue[token]));
|
|
885
1314
|
}
|
|
886
1315
|
}
|
|
887
1316
|
if (arena.colorType[token] === ColorType.SYS ||
|
|
888
1317
|
arena.colorType[token] === ColorType.DPSYS ||
|
|
889
1318
|
arena.colorType[token] === ColorType.NON_STD) {
|
|
890
|
-
return arena.
|
|
1319
|
+
return arena.resolveString(arena.stringValue[token]);
|
|
891
1320
|
}
|
|
892
1321
|
if (arena.colorType[token] === ColorType.HEX || arena.colorType[token] === ColorType.LIT) {
|
|
893
|
-
return arena.
|
|
1322
|
+
return arena.resolveString(arena.stringValue[token]);
|
|
894
1323
|
}
|
|
895
1324
|
if (arena.firstToken[token] === 0) {
|
|
896
1325
|
// rendered like a function (no argument)
|
|
897
|
-
return ((arena.
|
|
1326
|
+
return ((arena.resolveString(arena.stringValue[token]) ?? "") +
|
|
898
1327
|
"(" +
|
|
899
1328
|
renderReduced(arena, arena.firstToken[token], options, cache, reducer) +
|
|
900
1329
|
")");
|
|
901
1330
|
}
|
|
902
|
-
const fnName = arena.
|
|
1331
|
+
const fnName = arena.resolveString(arena.stringValue[token]);
|
|
903
1332
|
const count = arena.tokenCount(token);
|
|
904
1333
|
const isLegacy = legacyColorFunctions.has(fnName);
|
|
905
1334
|
const hasAlpha = alphaColorFunctions.has(fnName);
|
|
@@ -922,7 +1351,7 @@ function renderColor(arena, token, options, cache, reducer, errors) {
|
|
|
922
1351
|
result = result.trimEnd() + " ";
|
|
923
1352
|
}
|
|
924
1353
|
else if (arena.dataTypes[curr] === EnumToken.CommaTokenType ||
|
|
925
|
-
(arena.dataTypes[curr] === EnumToken.LiteralTokenType && arena.
|
|
1354
|
+
(arena.dataTypes[curr] === EnumToken.LiteralTokenType && arena.resolveString(arena.stringValue[curr]) === "/")) {
|
|
926
1355
|
result = result.trimEnd() + (arena.dataTypes[curr] === EnumToken.CommaTokenType ? "," : "/");
|
|
927
1356
|
}
|
|
928
1357
|
else {
|
|
@@ -975,7 +1404,7 @@ function renderValue(arena, token, options = defaultRenderOptions, cache = defau
|
|
|
975
1404
|
*/
|
|
976
1405
|
function renderToken(arena, token, options, cache, reducer, errors) {
|
|
977
1406
|
if (arena.dataTypes[token] === EnumToken.WhenElseFunctionTokenType &&
|
|
978
|
-
equalsIgnoreCase(arena.
|
|
1407
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[token]), "supports")) {
|
|
979
1408
|
options = { ...options, minifyValues: false, minify: false, convertColor: false };
|
|
980
1409
|
reducer = null;
|
|
981
1410
|
}
|
|
@@ -999,7 +1428,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
|
|
|
999
1428
|
case EnumToken.GeneralEnclosedFunctionTokenDefType:
|
|
1000
1429
|
case EnumToken.CustomFunctionTokenDefType:
|
|
1001
1430
|
case EnumToken.WildCardFunctionTokenDefType:
|
|
1002
|
-
return arena.
|
|
1431
|
+
return arena.resolveString(arena.stringValue[token]) + "(";
|
|
1003
1432
|
case EnumToken.ListToken:
|
|
1004
1433
|
return renderList(arena, arena.firstToken[token], options, cache);
|
|
1005
1434
|
case EnumToken.BinaryExpressionTokenType: {
|
|
@@ -1068,7 +1497,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
|
|
|
1068
1497
|
}
|
|
1069
1498
|
result += renderValue(arena, curr, options, cache);
|
|
1070
1499
|
}
|
|
1071
|
-
return arena.
|
|
1500
|
+
return arena.resolveString(arena.stringValue[token]) + "(" + result + ")";
|
|
1072
1501
|
}
|
|
1073
1502
|
return renderColor(arena, token, options, cache, reducer);
|
|
1074
1503
|
}
|
|
@@ -1080,7 +1509,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
|
|
|
1080
1509
|
}
|
|
1081
1510
|
for (let child = arena.firstToken[token]; child !== 0; child = arena.nextSibling[child]) {
|
|
1082
1511
|
if (arena.dataTypes[child] === EnumToken.StringTokenType) {
|
|
1083
|
-
const val = arena.
|
|
1512
|
+
const val = arena.resolveString(arena.stringValue[child]);
|
|
1084
1513
|
if (val.slice(1, 5) !== "data:" && urlTokenMatcher.test(val)) {
|
|
1085
1514
|
arena.dataTypes[child] = EnumToken.UrlTokenTokenType;
|
|
1086
1515
|
// inlined: arena.setStr(child, val.slice(1, -1))
|
|
@@ -1096,7 +1525,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
|
|
|
1096
1525
|
case EnumToken.ImageFunctionTokenType:
|
|
1097
1526
|
if (options.minifyValues && arena.dataTypes[token] === EnumToken.ImageFunctionTokenType) {
|
|
1098
1527
|
const slice = arena.tokens(token);
|
|
1099
|
-
switch (arena.
|
|
1528
|
+
switch (arena.resolveString(arena.stringValue[token])) {
|
|
1100
1529
|
case "linear-gradient":
|
|
1101
1530
|
case "repeating-linear-gradient":
|
|
1102
1531
|
{
|
|
@@ -1110,7 +1539,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
|
|
|
1110
1539
|
break;
|
|
1111
1540
|
}
|
|
1112
1541
|
if (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType &&
|
|
1113
|
-
equalsIgnoreCase(arena.
|
|
1542
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "to")) {
|
|
1114
1543
|
k = i + 1;
|
|
1115
1544
|
while (k < slice.length &&
|
|
1116
1545
|
(arena.dataTypes[slice[k]] === EnumToken.WhitespaceTokenType ||
|
|
@@ -1119,7 +1548,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
|
|
|
1119
1548
|
}
|
|
1120
1549
|
if (k < slice.length && arena.dataTypes[slice[k]] === EnumToken.IdenTokenType) {
|
|
1121
1550
|
// convert keyword to angle
|
|
1122
|
-
key = arena.
|
|
1551
|
+
key = arena.resolveString(arena.stringValue[slice[k]]).toLowerCase();
|
|
1123
1552
|
switch (key) {
|
|
1124
1553
|
case "right":
|
|
1125
1554
|
slice.splice(i, k - i + 1, arena.createToken(EnumToken.AngleTokenType, 90, "deg"));
|
|
@@ -1200,16 +1629,16 @@ function renderToken(arena, token, options, cache, reducer, errors) {
|
|
|
1200
1629
|
const size = [];
|
|
1201
1630
|
const colorSpaceDef = [];
|
|
1202
1631
|
if (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType) {
|
|
1203
|
-
if (equalsIgnoreCase(arena.
|
|
1204
|
-
equalsIgnoreCase(arena.
|
|
1632
|
+
if (equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "farthest-corner") ||
|
|
1633
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "ellipse")) {
|
|
1205
1634
|
i++;
|
|
1206
1635
|
}
|
|
1207
|
-
else if (equalsIgnoreCase(arena.
|
|
1208
|
-
equalsIgnoreCase(arena.
|
|
1209
|
-
equalsIgnoreCase(arena.
|
|
1636
|
+
else if (equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "closest-side") ||
|
|
1637
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "closest-corner") ||
|
|
1638
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "farthest-side")) {
|
|
1210
1639
|
size.push(slice[i++]);
|
|
1211
1640
|
}
|
|
1212
|
-
else if (equalsIgnoreCase(arena.
|
|
1641
|
+
else if (equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "circle")) {
|
|
1213
1642
|
form.push(slice[i++]);
|
|
1214
1643
|
}
|
|
1215
1644
|
while (arena.dataTypes[slice[i]] === EnumToken.WhitespaceTokenType ||
|
|
@@ -1218,16 +1647,16 @@ function renderToken(arena, token, options, cache, reducer, errors) {
|
|
|
1218
1647
|
}
|
|
1219
1648
|
}
|
|
1220
1649
|
if (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType) {
|
|
1221
|
-
if (equalsIgnoreCase(arena.
|
|
1222
|
-
equalsIgnoreCase(arena.
|
|
1650
|
+
if (equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "farthest-corner") ||
|
|
1651
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "ellipse")) {
|
|
1223
1652
|
i++;
|
|
1224
1653
|
}
|
|
1225
|
-
else if (equalsIgnoreCase(arena.
|
|
1226
|
-
equalsIgnoreCase(arena.
|
|
1227
|
-
equalsIgnoreCase(arena.
|
|
1654
|
+
else if (equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "closest-side") ||
|
|
1655
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "closest-corner") ||
|
|
1656
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "farthest-side")) {
|
|
1228
1657
|
size.push(slice[i++]);
|
|
1229
1658
|
}
|
|
1230
|
-
else if (equalsIgnoreCase(arena.
|
|
1659
|
+
else if (equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "circle")) {
|
|
1231
1660
|
form.push(slice[i++]);
|
|
1232
1661
|
}
|
|
1233
1662
|
while (arena.dataTypes[slice[i]] === EnumToken.WhitespaceTokenType ||
|
|
@@ -1236,7 +1665,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
|
|
|
1236
1665
|
}
|
|
1237
1666
|
}
|
|
1238
1667
|
if (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType &&
|
|
1239
|
-
equalsIgnoreCase(arena.
|
|
1668
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "at")) {
|
|
1240
1669
|
i++;
|
|
1241
1670
|
}
|
|
1242
1671
|
while (arena.dataTypes[slice[i]] === EnumToken.WhitespaceTokenType ||
|
|
@@ -1247,14 +1676,14 @@ function renderToken(arena, token, options, cache, reducer, errors) {
|
|
|
1247
1676
|
(arena.dataTypes[slice[i]] === EnumToken.NumberTokenType &&
|
|
1248
1677
|
0 === arena.numericValue[slice[i]]) ||
|
|
1249
1678
|
(arena.dataTypes[slice[i]] === EnumToken.IdenTokenType &&
|
|
1250
|
-
(equalsIgnoreCase(arena.
|
|
1251
|
-
equalsIgnoreCase(arena.
|
|
1252
|
-
equalsIgnoreCase(arena.
|
|
1253
|
-
equalsIgnoreCase(arena.
|
|
1254
|
-
equalsIgnoreCase(arena.
|
|
1679
|
+
(equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "center") ||
|
|
1680
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "top") ||
|
|
1681
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "bottom") ||
|
|
1682
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "left") ||
|
|
1683
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "right")))) {
|
|
1255
1684
|
do {
|
|
1256
1685
|
if (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType &&
|
|
1257
|
-
"in" === arena.
|
|
1686
|
+
"in" === arena.resolveString(arena.stringValue[slice[i]])) {
|
|
1258
1687
|
break;
|
|
1259
1688
|
}
|
|
1260
1689
|
positions.push(slice[i]);
|
|
@@ -1274,10 +1703,10 @@ function renderToken(arena, token, options, cache, reducer, errors) {
|
|
|
1274
1703
|
for (k = 0; k < positions.length; k++) {
|
|
1275
1704
|
if (arena.dataTypes[positions[k]] === EnumToken.IdenTokenType) {
|
|
1276
1705
|
if (position1.length === 0) {
|
|
1277
|
-
position1 = arena.
|
|
1706
|
+
position1 = arena.resolveString(arena.stringValue[positions[k]]);
|
|
1278
1707
|
}
|
|
1279
1708
|
else {
|
|
1280
|
-
position2 = arena.
|
|
1709
|
+
position2 = arena.resolveString(arena.stringValue[positions[k]]);
|
|
1281
1710
|
}
|
|
1282
1711
|
}
|
|
1283
1712
|
else if (arena.dataTypes[positions[k]] === EnumToken.PercentageTokenType ||
|
|
@@ -1404,7 +1833,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
|
|
|
1404
1833
|
// i++;
|
|
1405
1834
|
// }
|
|
1406
1835
|
if (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType &&
|
|
1407
|
-
equalsIgnoreCase(arena.
|
|
1836
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "from")) {
|
|
1408
1837
|
angles.push(slice[i++]);
|
|
1409
1838
|
while (i < slice.length &&
|
|
1410
1839
|
(arena.dataTypes[slice[i]] === EnumToken.WhitespaceTokenType ||
|
|
@@ -1428,7 +1857,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
|
|
|
1428
1857
|
}
|
|
1429
1858
|
}
|
|
1430
1859
|
if (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType &&
|
|
1431
|
-
equalsIgnoreCase(arena.
|
|
1860
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[slice[i]]), "at")) {
|
|
1432
1861
|
i++;
|
|
1433
1862
|
while (i < slice.length &&
|
|
1434
1863
|
(arena.dataTypes[slice[i]] === EnumToken.WhitespaceTokenType ||
|
|
@@ -1438,8 +1867,8 @@ function renderToken(arena, token, options, cache, reducer, errors) {
|
|
|
1438
1867
|
let position1 = "";
|
|
1439
1868
|
let position2 = "";
|
|
1440
1869
|
if (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType &&
|
|
1441
|
-
!equalsIgnoreCase("in", arena.
|
|
1442
|
-
position1 = arena.
|
|
1870
|
+
!equalsIgnoreCase("in", arena.resolveString(arena.stringValue[slice[i]]))) {
|
|
1871
|
+
position1 = arena.resolveString(arena.stringValue[slice[i]]);
|
|
1443
1872
|
positions.push(slice[i++]);
|
|
1444
1873
|
}
|
|
1445
1874
|
else if (arena.dataTypes[slice[i]] === EnumToken.PercentageTokenType ||
|
|
@@ -1453,8 +1882,8 @@ function renderToken(arena, token, options, cache, reducer, errors) {
|
|
|
1453
1882
|
positions.push(slice[i++]);
|
|
1454
1883
|
}
|
|
1455
1884
|
if (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType &&
|
|
1456
|
-
!equalsIgnoreCase("in", arena.
|
|
1457
|
-
position2 = arena.
|
|
1885
|
+
!equalsIgnoreCase("in", arena.resolveString(arena.stringValue[slice[i]]))) {
|
|
1886
|
+
position2 = arena.resolveString(arena.stringValue[slice[i]]);
|
|
1458
1887
|
positions.push(slice[i++]);
|
|
1459
1888
|
}
|
|
1460
1889
|
else if (arena.dataTypes[slice[i]] === EnumToken.PercentageTokenType ||
|
|
@@ -1477,7 +1906,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
|
|
|
1477
1906
|
i++;
|
|
1478
1907
|
}
|
|
1479
1908
|
if (arena.dataTypes[slice[i]] === EnumToken.IdenTokenType &&
|
|
1480
|
-
equalsIgnoreCase("in", arena.
|
|
1909
|
+
equalsIgnoreCase("in", arena.resolveString(arena.stringValue[slice[i]]))) {
|
|
1481
1910
|
while (i < slice.length && arena.dataTypes[slice[i]] !== EnumToken.CommaTokenType) {
|
|
1482
1911
|
colorSpaceDef.push(slice[i++]);
|
|
1483
1912
|
}
|
|
@@ -1524,7 +1953,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
|
|
|
1524
1953
|
}
|
|
1525
1954
|
break;
|
|
1526
1955
|
}
|
|
1527
|
-
return (arena.
|
|
1956
|
+
return (arena.resolveString(arena.stringValue[token]) +
|
|
1528
1957
|
"(" +
|
|
1529
1958
|
renderTokenList(slice, reducer ?? createReducer(arena, options, cache)) +
|
|
1530
1959
|
")");
|
|
@@ -1548,7 +1977,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
|
|
|
1548
1977
|
firstType !== EnumToken.FractionTokenType &&
|
|
1549
1978
|
firstType !== EnumToken.IdenTokenType &&
|
|
1550
1979
|
!arena.hasFraction(first)) {
|
|
1551
|
-
return (arena.
|
|
1980
|
+
return (arena.resolveString(arena.stringValue[token]) +
|
|
1552
1981
|
"(" +
|
|
1553
1982
|
renderValue(arena, first, options, cache,
|
|
1554
1983
|
// the reducer renders with the options of this token
|
|
@@ -1556,7 +1985,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
|
|
|
1556
1985
|
")");
|
|
1557
1986
|
}
|
|
1558
1987
|
}
|
|
1559
|
-
return ((arena.
|
|
1988
|
+
return ((arena.resolveString(arena.stringValue[token]) ?? "") +
|
|
1560
1989
|
"(" +
|
|
1561
1990
|
renderReduced(arena, arena.firstToken[token], options, cache, reducer) +
|
|
1562
1991
|
")");
|
|
@@ -1630,13 +2059,13 @@ function renderToken(arena, token, options, cache, reducer, errors) {
|
|
|
1630
2059
|
let val = arena.hasFraction(token)
|
|
1631
2060
|
? renderValue(arena, arena.getFraction(token), options, cache)
|
|
1632
2061
|
: minifyNumber(arena.numericValue[token]);
|
|
1633
|
-
let unit = arena.
|
|
2062
|
+
let unit = arena.resolveString(arena.unit[token]);
|
|
1634
2063
|
if (arena.dataTypes[token] === EnumToken.AngleTokenType && !val.includes("/")) {
|
|
1635
2064
|
const angle = getAngle(arena, token);
|
|
1636
2065
|
let v;
|
|
1637
2066
|
let value = val + unit;
|
|
1638
2067
|
for (const u of angleUnits) {
|
|
1639
|
-
if (arena.
|
|
2068
|
+
if (arena.resolveString(arena.unit[token]) === u) {
|
|
1640
2069
|
continue;
|
|
1641
2070
|
}
|
|
1642
2071
|
switch (u) {
|
|
@@ -1725,19 +2154,19 @@ function renderToken(arena, token, options, cache, reducer, errors) {
|
|
|
1725
2154
|
case EnumToken.NaNTokenType:
|
|
1726
2155
|
return "NaN";
|
|
1727
2156
|
case EnumToken.AtRuleTokenType:
|
|
1728
|
-
return "@" + arena.
|
|
2157
|
+
return "@" + arena.resolveString(arena.name[token]);
|
|
1729
2158
|
case EnumToken.CommentTokenType:
|
|
1730
2159
|
case EnumToken.CDOCOMMNodeType:
|
|
1731
2160
|
if (options.removeComments &&
|
|
1732
|
-
(!options.preserveLicense || !arena.
|
|
2161
|
+
(!options.preserveLicense || !arena.resolveString(arena.stringValue[token]).startsWith("/*!"))) {
|
|
1733
2162
|
return "";
|
|
1734
2163
|
}
|
|
1735
2164
|
case EnumToken.PseudoClassTokenType:
|
|
1736
2165
|
case EnumToken.PseudoElementTokenType:
|
|
1737
2166
|
// https://www.w3.org/TR/selectors-4/#single-colon-pseudos
|
|
1738
2167
|
if (arena.dataTypes[token] === EnumToken.PseudoElementTokenType &&
|
|
1739
|
-
pseudoElements.includes(arena.
|
|
1740
|
-
return arena.
|
|
2168
|
+
pseudoElements.includes(arena.resolveString(arena.stringValue[token]).slice(1))) {
|
|
2169
|
+
return arena.resolveString(arena.stringValue[token]).slice(1);
|
|
1741
2170
|
}
|
|
1742
2171
|
case EnumToken.CssTypeTokenType:
|
|
1743
2172
|
case EnumToken.UrlTokenTokenType:
|
|
@@ -1749,7 +2178,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
|
|
|
1749
2178
|
case EnumToken.DashedIdenTokenType:
|
|
1750
2179
|
case EnumToken.PseudoPageTokenType:
|
|
1751
2180
|
case EnumToken.ClassSelectorTokenType:
|
|
1752
|
-
return arena.
|
|
2181
|
+
return arena.resolveString(arena.stringValue[token]);
|
|
1753
2182
|
case EnumToken.NestingSelectorTokenType:
|
|
1754
2183
|
return "&";
|
|
1755
2184
|
case EnumToken.SupportsQueryUnaryConditionTokenType:
|
|
@@ -1777,7 +2206,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
|
|
|
1777
2206
|
(options.indent ?? "") +
|
|
1778
2207
|
renderValue(arena, arena.field(token, TokenField.R), options, cache, reducer));
|
|
1779
2208
|
case EnumToken.DeclarationNodeType:
|
|
1780
|
-
return (arena.
|
|
2209
|
+
return (arena.resolveString(arena.name[token]) +
|
|
1781
2210
|
":" +
|
|
1782
2211
|
renderTokenList(options.minifyValues ? filterValues(arena, arena.tokens(token)) : arena.tokens(token), (acc, curr) => acc + renderValue(arena, curr, options, cache, reducer)));
|
|
1783
2212
|
case EnumToken.MediaQueryUnaryFeatureTokenType:
|
|
@@ -1808,7 +2237,7 @@ function renderToken(arena, token, options, cache, reducer, errors) {
|
|
|
1808
2237
|
renderValue(arena, arena.field(token, TokenField.Op2), options, cache, reducer) +
|
|
1809
2238
|
renderField(arena, token, TokenField.R, options, cache, reducer));
|
|
1810
2239
|
case EnumToken.MediaFeatureTokenType:
|
|
1811
|
-
return arena.
|
|
2240
|
+
return arena.resolveString(arena.stringValue[token]);
|
|
1812
2241
|
case EnumToken.NotTokenType:
|
|
1813
2242
|
return "not";
|
|
1814
2243
|
case EnumToken.OnlyTokenType:
|
|
@@ -1845,7 +2274,7 @@ function filterValues(arena, values) {
|
|
|
1845
2274
|
arena.dataTypes[values[i - 1]] === EnumToken.WhitespaceTokenType) {
|
|
1846
2275
|
values.splice(i - 1, 1);
|
|
1847
2276
|
}
|
|
1848
|
-
else if (
|
|
2277
|
+
else if (isFunctionType(arena.dataTypes[values[i]]) &&
|
|
1849
2278
|
// color keywords and hex colors are not functions
|
|
1850
2279
|
(arena.dataTypes[values[i]] !== EnumToken.ColorTokenType || arena.firstToken[values[i]] !== 0) &&
|
|
1851
2280
|
arena.dataTypes[values[i]] !== EnumToken.WildCardFunctionTokenType &&
|
|
@@ -1856,4 +2285,4 @@ function filterValues(arena, values) {
|
|
|
1856
2285
|
return values;
|
|
1857
2286
|
}
|
|
1858
2287
|
|
|
1859
|
-
export { NodeRenderer, RenderMemo, doRender, filterValues, move, renderValue };
|
|
2288
|
+
export { NodeRenderer, RenderMemo, doRender, filterValues, move, outputCapacity, renderValue };
|