@tbela99/css-parser 2.1.0 → 2.2.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 +42 -1
- package/README.md +14 -7
- package/architecture.png +0 -0
- package/dist/{src/data → data}/properties.json.js +86 -2
- package/dist/{src/data → data}/validation.json.js +5 -2
- package/dist/index-umd-web.js +10738 -4359
- package/dist/index.d.ts +734 -100
- package/dist/{src/lib → lib}/ast/arena-node.js +10 -10
- package/dist/{src/lib → lib}/ast/arena.js +304 -139
- package/dist/{src/lib → lib}/ast/cache.js +2 -2
- package/dist/{src/lib → lib}/ast/features/calc.js +11 -9
- package/dist/{src/lib → lib}/ast/features/if.js +6 -6
- package/dist/{src/lib → lib}/ast/features/index.js +3 -2
- package/dist/{src/lib → lib}/ast/features/inlinecssvariables.js +12 -10
- package/dist/lib/ast/features/mixin.js +953 -0
- package/dist/{src/lib → lib}/ast/features/prefix.js +39 -39
- package/dist/lib/ast/features/shorthand.js +129 -0
- package/dist/lib/ast/features/transform.js +188 -0
- package/dist/{src/lib → lib}/ast/features/type.js +2 -2
- package/dist/{src/lib → lib}/ast/math/expression.js +24 -24
- package/dist/{src/lib → lib}/ast/math/math.js +2 -2
- package/dist/{src/lib → lib}/ast/minify.js +745 -201
- package/dist/lib/ast/mixins/custom-function.js +393 -0
- package/dist/lib/ast/mixins/utils.js +53 -0
- package/dist/lib/ast/mixins/variables.js +678 -0
- package/dist/lib/ast/strings.js +906 -0
- package/dist/lib/ast/transform/compute.js +560 -0
- package/dist/lib/ast/transform/matrix.js +67 -0
- package/dist/lib/ast/transform/minify.js +325 -0
- package/dist/lib/ast/transform/perspective.js +24 -0
- package/dist/{src/lib → lib}/ast/transform/rotate.js +16 -7
- package/dist/lib/ast/transform/scale.js +43 -0
- package/dist/lib/ast/transform/skew.js +38 -0
- package/dist/lib/ast/transform/translate.js +56 -0
- package/dist/lib/ast/transform/utils.js +390 -0
- package/dist/{src/lib → lib}/ast/types.js +2 -2
- package/dist/{src/lib → lib}/ast/walk.js +2 -2
- package/dist/{src/lib → lib}/fs/resolve.js +2 -2
- package/dist/{src/lib → lib}/parser/declaration/list.js +25 -8
- package/dist/{src/lib → lib}/parser/declaration/map.js +15 -15
- package/dist/{src/lib → lib}/parser/declaration/set.js +5 -5
- package/dist/{src/lib → lib}/parser/input.js +18 -3
- package/dist/{src/lib → lib}/parser/linesmap.js +2 -2
- package/dist/{src/lib → lib}/parser/parse.js +425 -258
- package/dist/{src/lib → lib}/parser/source.js +2 -2
- package/dist/{src/lib → lib}/parser/tokenize.js +480 -312
- package/dist/{src/lib → lib}/parser/utils/angle.js +3 -3
- package/dist/{src/lib → lib}/parser/utils/at-rule-container.js +12 -12
- package/dist/{src/lib → lib}/parser/utils/at-rule-font-feature-values.js +3 -3
- package/dist/{src/lib → lib}/parser/utils/at-rule-generic.js +7 -7
- package/dist/{src/lib → lib}/parser/utils/at-rule-import.js +10 -10
- package/dist/{src/lib → lib}/parser/utils/at-rule-media.js +14 -14
- package/dist/{src/lib → lib}/parser/utils/at-rule-support.js +9 -14
- package/dist/{src/lib → lib}/parser/utils/at-rule-when-else.js +6 -6
- package/dist/{src/lib → lib}/parser/utils/at-rule.js +3 -3
- package/dist/{src/lib → lib}/parser/utils/base64.js +2 -2
- package/dist/lib/parser/utils/cache.js +53 -0
- package/dist/lib/parser/utils/canonical.js +78 -0
- package/dist/lib/parser/utils/codes.js +93 -0
- package/dist/{src/lib → lib}/parser/utils/config.js +2 -2
- package/dist/{src/lib → lib}/parser/utils/declaration.js +31 -26
- package/dist/{src/lib → lib}/parser/utils/eq.js +2 -2
- package/dist/{src/lib → lib}/parser/utils/hash.js +2 -2
- package/dist/{src/lib → lib}/parser/utils/selector.js +305 -63
- package/dist/{src/lib → lib}/parser/utils/text.js +2 -2
- package/dist/{src/lib → lib}/parser/utils/token.js +2 -2
- package/dist/{src/lib → lib}/parser/utils/type.js +9 -7
- package/dist/{src/lib → lib}/printer/render.js +420 -152
- package/dist/lib/printer/result.js +148 -0
- package/dist/{src/lib → lib}/printer/sourcemap/lib/codec.js +2 -2
- package/dist/{src/lib → lib}/printer/sourcemap/sourcemap.js +2 -2
- package/dist/lib/printer/writer.js +252 -0
- package/dist/{src/lib → lib}/syntax/color/a98rgb.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/alpha.js +10 -10
- package/dist/{src/lib → lib}/syntax/color/cmyk.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/color-mix.js +18 -18
- package/dist/{src/lib → lib}/syntax/color/color.js +18 -18
- package/dist/{src/lib → lib}/syntax/color/hex.js +3 -3
- package/dist/{src/lib → lib}/syntax/color/hsl.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/hwb.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/lab.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/lch.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/oklab.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/oklch.js +3 -3
- package/dist/{src/lib → lib}/syntax/color/p3.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/prophotorgb.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/rec2020.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/relative-color.js +14 -14
- package/dist/{src/lib → lib}/syntax/color/rgb.js +4 -4
- package/dist/{src/lib → lib}/syntax/color/srgb.js +5 -5
- package/dist/{src/lib → lib}/syntax/color/utils/color-type.js +4 -4
- package/dist/{src/lib → lib}/syntax/color/utils/colorspace.js +10 -10
- package/dist/{src/lib → lib}/syntax/color/utils/components.js +5 -5
- package/dist/{src/lib → lib}/syntax/color/utils/distance.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/utils/make-color.js +3 -3
- package/dist/{src/lib → lib}/syntax/color/utils/matrix.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/xyz.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/xyzd50.js +2 -2
- package/dist/{src/lib → lib}/syntax/constants.js +26 -3
- package/dist/{src/lib → lib}/syntax/syntax.js +37 -29
- package/dist/{src/lib → lib}/validation/config.js +23 -10
- package/dist/lib/validation/first.js +571 -0
- package/dist/{src/lib → lib}/validation/match.js +292 -150
- package/dist/{src/lib → lib}/validation/parser/parse.js +2 -2
- package/dist/{src/lib → lib}/validation/parser/typedef.js +2 -2
- package/dist/{src/lib → lib}/validation/utils/list.js +3 -19
- package/dist/{src/node.js → node.js} +52 -34
- package/dist/{src/types.js → types.js} +2 -2
- package/dist/{src/utils → utils}/sync.js +10 -5
- package/dist/version.d.ts +4 -0
- package/dist/version.js +13 -0
- package/dist/{src/web.js → web.js} +47 -34
- package/package.json +12 -12
- 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
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/*
|
|
2
|
+
CSS parser, minifier and validator for node, deno, bun and the browser
|
|
3
|
+
|
|
4
|
+
Author(s): Thierry Bela
|
|
5
|
+
Version: 2.2.0
|
|
6
|
+
License: MIT
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* shared decoder
|
|
10
|
+
*/
|
|
11
|
+
const decoder = new TextDecoder();
|
|
12
|
+
/**
|
|
13
|
+
* Render result.
|
|
14
|
+
*
|
|
15
|
+
* The generated css is stored as UTF-8 bytes in {@link RenderResult.write}: it can be written to a file or sent over
|
|
16
|
+
* the network as is. {@link RenderResult.code} decodes the bytes the first time it is read, the string is kept.
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* import {render} from '@tbela99/css-parser';
|
|
20
|
+
* import {writeFile} from 'node:fs/promises';
|
|
21
|
+
*
|
|
22
|
+
* const result = render(parseResult.arena, parseResult.ast);
|
|
23
|
+
*
|
|
24
|
+
* // bytes, no string conversion
|
|
25
|
+
* await writeFile('out.css', result.write);
|
|
26
|
+
*
|
|
27
|
+
* // string, decoded once
|
|
28
|
+
* console.log(result.code);
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
class RenderResult {
|
|
32
|
+
/**
|
|
33
|
+
* An instance is kept alive: the hidden class of the instances would otherwise be collected by a full garbage
|
|
34
|
+
* collection when no instance is alive, and the optimized code that depends on it discarded (deoptimized)
|
|
35
|
+
*
|
|
36
|
+
* @internal
|
|
37
|
+
*/
|
|
38
|
+
static shape = new RenderResult(new Uint8Array(0), [], { total: "0ms" });
|
|
39
|
+
/**
|
|
40
|
+
* Rendered CSS, encoded as UTF-8
|
|
41
|
+
*/
|
|
42
|
+
write;
|
|
43
|
+
/**
|
|
44
|
+
* Render errors
|
|
45
|
+
*/
|
|
46
|
+
errors;
|
|
47
|
+
/**
|
|
48
|
+
* Render stats
|
|
49
|
+
*/
|
|
50
|
+
stats;
|
|
51
|
+
/**
|
|
52
|
+
* Source map
|
|
53
|
+
*/
|
|
54
|
+
map;
|
|
55
|
+
/**
|
|
56
|
+
* decoded css
|
|
57
|
+
*/
|
|
58
|
+
#code = null;
|
|
59
|
+
/**
|
|
60
|
+
* length of the decoded css: `code.length`
|
|
61
|
+
*/
|
|
62
|
+
#codeLength;
|
|
63
|
+
/**
|
|
64
|
+
* @param write rendered css, encoded as UTF-8
|
|
65
|
+
* @param errors
|
|
66
|
+
* @param stats
|
|
67
|
+
* @param map
|
|
68
|
+
* @param codeLength length of the decoded css (`code.length`), computed when it is not provided
|
|
69
|
+
*/
|
|
70
|
+
constructor(write, errors, stats, map, codeLength = -1) {
|
|
71
|
+
this.write = write;
|
|
72
|
+
this.errors = errors;
|
|
73
|
+
this.stats = stats;
|
|
74
|
+
this.map = map;
|
|
75
|
+
this.#codeLength = codeLength;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* length of the rendered css once decoded (`result.code.length`), without decoding it
|
|
79
|
+
* @param result
|
|
80
|
+
*
|
|
81
|
+
* @internal
|
|
82
|
+
*/
|
|
83
|
+
static codeLength(result) {
|
|
84
|
+
return result.#codeLength >= 0 ? result.#codeLength : result.code.length;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Rendered CSS. The bytes of {@link RenderResult.write} are decoded the first time the property is read
|
|
88
|
+
*/
|
|
89
|
+
get code() {
|
|
90
|
+
if (this.#code === null) {
|
|
91
|
+
this.#code = decoder.decode(this.write);
|
|
92
|
+
}
|
|
93
|
+
return this.#code;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* JSON representation: the css is serialized as a string, the bytes are not serialized
|
|
97
|
+
*/
|
|
98
|
+
toJSON() {
|
|
99
|
+
const { write, ...json } = this;
|
|
100
|
+
json.code = this.code;
|
|
101
|
+
return json;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Transform result: the parse result and the render result
|
|
106
|
+
*/
|
|
107
|
+
class TransformResult extends RenderResult {
|
|
108
|
+
/**
|
|
109
|
+
* An instance is kept alive: the hidden class of the instances would otherwise be collected by a full garbage
|
|
110
|
+
* collection when no instance is alive, and the optimized code that depends on it discarded (deoptimized)
|
|
111
|
+
*
|
|
112
|
+
* @internal
|
|
113
|
+
*/
|
|
114
|
+
static shape = new TransformResult({ ast: 0, arena: null, errors: [], stats: {} }, RenderResult.shape, {});
|
|
115
|
+
/**
|
|
116
|
+
* Parsed ast tree: the id of the stylesheet node in {@link TransformResult.arena}
|
|
117
|
+
*/
|
|
118
|
+
ast;
|
|
119
|
+
/**
|
|
120
|
+
* arena the ast nodes are stored in
|
|
121
|
+
*/
|
|
122
|
+
arena;
|
|
123
|
+
/**
|
|
124
|
+
* @param parseResult
|
|
125
|
+
* @param renderResult
|
|
126
|
+
* @param stats
|
|
127
|
+
*/
|
|
128
|
+
constructor(parseResult, renderResult, stats) {
|
|
129
|
+
super(renderResult.write, parseResult.errors.concat(renderResult.errors), stats, renderResult.map, RenderResult.codeLength(renderResult));
|
|
130
|
+
this.ast = parseResult.ast;
|
|
131
|
+
this.arena = parseResult.arena;
|
|
132
|
+
// css module properties are only defined when they are set
|
|
133
|
+
if (parseResult.mapping !== undefined) {
|
|
134
|
+
this.mapping = parseResult.mapping;
|
|
135
|
+
}
|
|
136
|
+
if (parseResult.cssModuleVariables !== undefined) {
|
|
137
|
+
this.cssModuleVariables = parseResult.cssModuleVariables;
|
|
138
|
+
}
|
|
139
|
+
if (parseResult.importMapping !== undefined) {
|
|
140
|
+
this.importMapping = parseResult.importMapping;
|
|
141
|
+
}
|
|
142
|
+
if (parseResult.revMapping !== undefined) {
|
|
143
|
+
this.revMapping = parseResult.revMapping;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export { RenderResult, TransformResult };
|
|
@@ -1,8 +1,8 @@
|
|
|
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.2.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
// from https://github.com/Rich-Harris/vlq/tree/master
|
|
@@ -1,8 +1,8 @@
|
|
|
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.2.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { toBase64 } from '../../parser/utils/base64.js';
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
/*
|
|
2
|
+
CSS parser, minifier and validator for node, deno, bun and the browser
|
|
3
|
+
|
|
4
|
+
Author(s): Thierry Bela
|
|
5
|
+
Version: 2.2.0
|
|
6
|
+
License: MIT
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* shared encoder
|
|
10
|
+
*/
|
|
11
|
+
const encoder = new TextEncoder();
|
|
12
|
+
/**
|
|
13
|
+
* strings longer than this are encoded natively ({@link TextEncoder.encodeInto} or the node.js Buffer): a loop is
|
|
14
|
+
* faster for very short strings. the strings are of different representations (flat, concatenated, sliced, ...): the
|
|
15
|
+
* loop is not specialized for one of them, it is slow for long strings
|
|
16
|
+
*/
|
|
17
|
+
const NATIVE_THRESHOLD = 24;
|
|
18
|
+
/**
|
|
19
|
+
* node.js `Buffer` class, see {@link useNativeBuffer}
|
|
20
|
+
*/
|
|
21
|
+
let NativeBufferClass = null;
|
|
22
|
+
/**
|
|
23
|
+
* encode the strings with the node.js `Buffer` class: faster than {@link TextEncoder.encodeInto}. the rendered bytes are
|
|
24
|
+
* still stored in a `Uint8Array`
|
|
25
|
+
* @param bufferClass node.js `Buffer`
|
|
26
|
+
*
|
|
27
|
+
* @internal
|
|
28
|
+
*/
|
|
29
|
+
function useNativeBuffer(bufferClass) {
|
|
30
|
+
NativeBufferClass = bufferClass;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* the node.js `Buffer` class, null when it is not used (see {@link useNativeBuffer})
|
|
34
|
+
*
|
|
35
|
+
* @internal
|
|
36
|
+
*/
|
|
37
|
+
function nativeBufferClass() {
|
|
38
|
+
return NativeBufferClass;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* last writer id
|
|
42
|
+
*/
|
|
43
|
+
let writerId = 0;
|
|
44
|
+
/**
|
|
45
|
+
* a new writer id: a positive 31 bits integer (the ids are stored in an Int32Array, 0 is not an id)
|
|
46
|
+
*/
|
|
47
|
+
function nextWriterId() {
|
|
48
|
+
writerId = (writerId % 0x7ffffffe) + 1;
|
|
49
|
+
return writerId;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* default capacity of a writer, in bytes
|
|
53
|
+
*/
|
|
54
|
+
const DEFAULT_CAPACITY = 1 << 12;
|
|
55
|
+
/**
|
|
56
|
+
* Growable buffer the rendered css is written to, encoded as UTF-8.
|
|
57
|
+
*
|
|
58
|
+
* The renderer writes the rendered css directly into the buffer: the strings and the single characters. Identical
|
|
59
|
+
* declarations are copied from the bytes already written ({@link copy}). The bytes written last can be discarded
|
|
60
|
+
* ({@link truncate}): the renderer drops the empty rules and the semicolon that ends the last declaration of a block.
|
|
61
|
+
* The bytes of a declaration that is copied are never discarded: a rule that contains a declaration is not empty.
|
|
62
|
+
*
|
|
63
|
+
* The capacity should be large enough for the whole output (the renderer allocates it from the input length, see
|
|
64
|
+
* `outputCapacity()`): the buffer is copied when it grows.
|
|
65
|
+
*
|
|
66
|
+
* @internal
|
|
67
|
+
*/
|
|
68
|
+
class Writer {
|
|
69
|
+
/**
|
|
70
|
+
* An instance is kept alive: the hidden class of the instances would otherwise be collected by a full garbage
|
|
71
|
+
* collection when no instance is alive, and the optimized code that depends on it discarded (deoptimized)
|
|
72
|
+
*/
|
|
73
|
+
static shape = new Writer(16);
|
|
74
|
+
/**
|
|
75
|
+
* underlying buffer. only the first {@link length} bytes are meaningful. it is replaced when the writer grows
|
|
76
|
+
*/
|
|
77
|
+
buffer;
|
|
78
|
+
/**
|
|
79
|
+
* unique id of the content of the writer: it changes when the writer is {@link reset}
|
|
80
|
+
*/
|
|
81
|
+
id = nextWriterId();
|
|
82
|
+
/**
|
|
83
|
+
* number of bytes written. it can be decreased to drop the bytes written last
|
|
84
|
+
*/
|
|
85
|
+
length = 0;
|
|
86
|
+
/**
|
|
87
|
+
* number of bytes written minus the number of UTF-16 code units of the written strings: 0 when only ascii is
|
|
88
|
+
* written. the length of the decoded css is {@link codeLength}
|
|
89
|
+
*/
|
|
90
|
+
extra = 0;
|
|
91
|
+
/**
|
|
92
|
+
* node.js `Buffer` view of {@link buffer}, null when `Buffer` is not available
|
|
93
|
+
*/
|
|
94
|
+
view;
|
|
95
|
+
/**
|
|
96
|
+
* @param capacity initial capacity in bytes
|
|
97
|
+
*/
|
|
98
|
+
constructor(capacity = DEFAULT_CAPACITY) {
|
|
99
|
+
this.buffer = new Uint8Array(capacity > 0 ? capacity : DEFAULT_CAPACITY);
|
|
100
|
+
this.view =
|
|
101
|
+
NativeBufferClass == null ? null : NativeBufferClass.from(this.buffer.buffer, 0, this.buffer.length);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* write a string, encoded as UTF-8
|
|
105
|
+
* @param str
|
|
106
|
+
*/
|
|
107
|
+
write(str) {
|
|
108
|
+
const len = str.length;
|
|
109
|
+
let pos = this.length;
|
|
110
|
+
if (len > NATIVE_THRESHOLD) {
|
|
111
|
+
if (this.view !== null) {
|
|
112
|
+
// a UTF-16 code unit is encoded with at most 3 bytes
|
|
113
|
+
if (pos + len * 3 > this.buffer.length &&
|
|
114
|
+
pos + NativeBufferClass.byteLength(str, "utf8") > this.buffer.length) {
|
|
115
|
+
this.grow(pos + NativeBufferClass.byteLength(str, "utf8"));
|
|
116
|
+
}
|
|
117
|
+
const written = this.view.write(str, pos);
|
|
118
|
+
this.length = pos + written;
|
|
119
|
+
this.extra += written - len;
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
this.encode(str);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
// enough room for ascii strings
|
|
126
|
+
if (pos + len > this.buffer.length) {
|
|
127
|
+
this.grow(pos + len);
|
|
128
|
+
}
|
|
129
|
+
const buffer = this.buffer;
|
|
130
|
+
let i = 0;
|
|
131
|
+
for (; i < len; i++) {
|
|
132
|
+
const code = str.charCodeAt(i);
|
|
133
|
+
if (code > 0x7f) {
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
buffer[pos++] = code;
|
|
137
|
+
}
|
|
138
|
+
this.length = pos;
|
|
139
|
+
if (i < len) {
|
|
140
|
+
this.encode(i === 0 ? str : str.slice(i));
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* write an ascii character
|
|
145
|
+
* @param code character code, less than 0x80
|
|
146
|
+
*/
|
|
147
|
+
writeByte(code) {
|
|
148
|
+
if (this.length === this.buffer.length) {
|
|
149
|
+
this.grow(this.length + 1);
|
|
150
|
+
}
|
|
151
|
+
this.buffer[this.length++] = code;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* copy bytes already written: the bytes between `start` and `end` are written again at the end of the buffer
|
|
155
|
+
* @param start start offset
|
|
156
|
+
* @param end end offset
|
|
157
|
+
* @param extra number of bytes minus the number of UTF-16 code units of the copied bytes
|
|
158
|
+
*/
|
|
159
|
+
copy(start, end, extra) {
|
|
160
|
+
const pos = this.length;
|
|
161
|
+
if (pos + end - start > this.buffer.length) {
|
|
162
|
+
this.grow(pos + end - start);
|
|
163
|
+
}
|
|
164
|
+
this.buffer.copyWithin(pos, start, end);
|
|
165
|
+
this.length = pos + end - start;
|
|
166
|
+
this.extra += extra;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* length of the written css once decoded, in UTF-16 code units (`code.length`)
|
|
170
|
+
*/
|
|
171
|
+
get codeLength() {
|
|
172
|
+
return this.length - this.extra;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* drop the written bytes: the writer is reused. it gets a new id
|
|
176
|
+
*/
|
|
177
|
+
reset() {
|
|
178
|
+
this.id = nextWriterId();
|
|
179
|
+
this.length = 0;
|
|
180
|
+
this.extra = 0;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* drop the bytes written after a position
|
|
184
|
+
* @param length position, a previous value of {@link length}
|
|
185
|
+
* @param extra value of {@link extra} at this position
|
|
186
|
+
*/
|
|
187
|
+
truncate(length, extra) {
|
|
188
|
+
this.length = length;
|
|
189
|
+
this.extra = extra;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* last byte written, -1 when nothing is written
|
|
193
|
+
*/
|
|
194
|
+
lastByte() {
|
|
195
|
+
return this.length === 0 ? -1 : this.buffer[this.length - 1];
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* make room for `size` more bytes
|
|
199
|
+
* @param size
|
|
200
|
+
*/
|
|
201
|
+
reserve(size) {
|
|
202
|
+
if (this.length + size > this.buffer.length) {
|
|
203
|
+
this.grow(this.length + size);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* the written bytes: the buffer, or `buffer.subarray(0, length)` when the buffer is larger than the written bytes
|
|
208
|
+
*/
|
|
209
|
+
bytes() {
|
|
210
|
+
return this.length === this.buffer.length ? this.buffer : this.buffer.subarray(0, this.length);
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* encode a string with the native encoder
|
|
214
|
+
* @param str
|
|
215
|
+
*/
|
|
216
|
+
encode(str) {
|
|
217
|
+
let pos = this.length;
|
|
218
|
+
// every code unit is read: the difference is the number of written bytes minus the length
|
|
219
|
+
this.extra -= str.length + pos;
|
|
220
|
+
while (true) {
|
|
221
|
+
const result = encoder.encodeInto(str, pos === 0 ? this.buffer : this.buffer.subarray(pos));
|
|
222
|
+
pos += result.written;
|
|
223
|
+
if (result.read === str.length) {
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
// a UTF-16 code unit is encoded with at most 3 bytes
|
|
227
|
+
str = str.slice(result.read);
|
|
228
|
+
this.length = pos;
|
|
229
|
+
this.grow(pos + str.length * 3);
|
|
230
|
+
}
|
|
231
|
+
this.length = pos;
|
|
232
|
+
this.extra += pos;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* make room for at least `size` bytes. the capacity is at least doubled
|
|
236
|
+
* @param size minimum capacity
|
|
237
|
+
*/
|
|
238
|
+
grow(size) {
|
|
239
|
+
let capacity = this.buffer.length * 2;
|
|
240
|
+
if (capacity < size) {
|
|
241
|
+
capacity = size;
|
|
242
|
+
}
|
|
243
|
+
const buffer = new Uint8Array(capacity);
|
|
244
|
+
buffer.set(this.length === this.buffer.length ? this.buffer : this.buffer.subarray(0, this.length));
|
|
245
|
+
this.buffer = buffer;
|
|
246
|
+
if (NativeBufferClass != null) {
|
|
247
|
+
this.view = NativeBufferClass.from(buffer.buffer, 0, capacity);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export { DEFAULT_CAPACITY, Writer, nativeBufferClass, useNativeBuffer };
|
|
@@ -1,8 +1,8 @@
|
|
|
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.2.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { ColorType, EnumToken } from '../../ast/types.js';
|
|
@@ -34,7 +34,7 @@ function alpha(arena, color, alpha) {
|
|
|
34
34
|
// inlined: arena.walkValues(arena.tokens(alpha))
|
|
35
35
|
for (const value of new ValueWalker(arena, arena.tokens(alpha))) {
|
|
36
36
|
if (arena.dataTypes[value] === EnumToken.IdenTokenType) {
|
|
37
|
-
if (equalsIgnoreCase(arena.
|
|
37
|
+
if (equalsIgnoreCase(arena.resolveString(arena.stringValue[value]), "alpha")) {
|
|
38
38
|
// replace the identifier with the alpha value
|
|
39
39
|
if (components?.[3]) {
|
|
40
40
|
arena.copyToken(value, components[3]);
|
|
@@ -44,7 +44,7 @@ function alpha(arena, color, alpha) {
|
|
|
44
44
|
}
|
|
45
45
|
// continue;
|
|
46
46
|
}
|
|
47
|
-
// else if (equalsIgnoreCase(arena.
|
|
47
|
+
// else if (equalsIgnoreCase(arena.resolveString(arena.stringValue[value]), "none")) {
|
|
48
48
|
// Object.assign(value, {
|
|
49
49
|
// typ: EnumToken.NumberTokenType,
|
|
50
50
|
// val: 0,
|
|
@@ -68,13 +68,13 @@ function alpha(arena, color, alpha) {
|
|
|
68
68
|
return null;
|
|
69
69
|
}
|
|
70
70
|
if (arena.dataTypes[alpha] === EnumToken.IdenTokenType) {
|
|
71
|
-
if (equalsIgnoreCase(arena.
|
|
72
|
-
equalsIgnoreCase(arena.
|
|
73
|
-
equalsIgnoreCase(arena.
|
|
71
|
+
if (equalsIgnoreCase(arena.resolveString(arena.stringValue[alpha]), "NaN") ||
|
|
72
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[alpha]), "Infinity") ||
|
|
73
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[alpha]), "-Infinity")) {
|
|
74
74
|
return null;
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
|
-
if (arena.colorType[color] === ColorType.COLOR_MIX || arena.
|
|
77
|
+
if (arena.colorType[color] === ColorType.COLOR_MIX || arena.resolveString(arena.unit[color]) === "rel") {
|
|
78
78
|
const converted = convertColor(arena, color, getColorType(arena, color));
|
|
79
79
|
if (converted == null) {
|
|
80
80
|
return null;
|
|
@@ -86,13 +86,13 @@ function alpha(arena, color, alpha) {
|
|
|
86
86
|
return null;
|
|
87
87
|
}
|
|
88
88
|
if (arena.dataTypes[alpha] === EnumToken.IdenTokenType) {
|
|
89
|
-
// if (equalsIgnoreCase(arena.
|
|
89
|
+
// if (equalsIgnoreCase(arena.resolveString(arena.stringValue[alpha]), "alpha")) {
|
|
90
90
|
// alpha = components[3] ?? {
|
|
91
91
|
// typ: EnumToken.NumberTokenType,
|
|
92
92
|
// val: 1,
|
|
93
93
|
// };
|
|
94
94
|
// } else
|
|
95
|
-
if (equalsIgnoreCase(arena.
|
|
95
|
+
if (equalsIgnoreCase(arena.resolveString(arena.stringValue[alpha]), "none")) {
|
|
96
96
|
arena.resetToken(alpha, EnumToken.NumberTokenType, 0);
|
|
97
97
|
}
|
|
98
98
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
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.2.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { EnumToken, ColorType } from '../../ast/types.js';
|
|
@@ -1,8 +1,8 @@
|
|
|
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.2.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { EnumToken, ColorType, EnumAstNodeStatus } from '../../ast/types.js';
|
|
@@ -81,27 +81,27 @@ function colorMix(arena, ...args) {
|
|
|
81
81
|
const colorComponents = [];
|
|
82
82
|
const colors = [];
|
|
83
83
|
if (arena.dataTypes[args[i]] === EnumToken.IdenTokenType) {
|
|
84
|
-
if (!equalsIgnoreCase(arena.
|
|
84
|
+
if (!equalsIgnoreCase(arena.resolveString(arena.stringValue[args[i++]]), "in")) {
|
|
85
85
|
return null;
|
|
86
86
|
}
|
|
87
87
|
if (arena.dataTypes[args[i]] !== EnumToken.IdenTokenType) {
|
|
88
88
|
return null;
|
|
89
89
|
}
|
|
90
90
|
if (isRectangularOrthogonalColorspace(arena, args[i])) {
|
|
91
|
-
colorSpace = arena.
|
|
91
|
+
colorSpace = arena.resolveString(arena.stringValue[args[i++]]);
|
|
92
92
|
}
|
|
93
93
|
else if (isPolarColorspace(arena, args[i])) {
|
|
94
|
-
colorSpace = arena.
|
|
94
|
+
colorSpace = arena.resolveString(arena.stringValue[args[i++]]);
|
|
95
95
|
if (arena.dataTypes[args[i]] !== EnumToken.IdenTokenType &&
|
|
96
96
|
arena.dataTypes[args[i]] !== EnumToken.ColorTokenType) {
|
|
97
97
|
return null;
|
|
98
98
|
}
|
|
99
99
|
if (arena.dataTypes[args[i]] === EnumToken.IdenTokenType &&
|
|
100
|
-
!equalsIgnoreCase(arena.
|
|
100
|
+
!equalsIgnoreCase(arena.resolveString(arena.stringValue[args[i]]), "hue")) {
|
|
101
101
|
if (arena.dataTypes[args[i]] !== EnumToken.IdenTokenType) {
|
|
102
102
|
return null;
|
|
103
103
|
}
|
|
104
|
-
hueInterpolationMethod = arena.
|
|
104
|
+
hueInterpolationMethod = arena.resolveString(arena.stringValue[args[i++]]);
|
|
105
105
|
switch (hueInterpolationMethod) {
|
|
106
106
|
case "increasing":
|
|
107
107
|
case "decreasing":
|
|
@@ -111,7 +111,7 @@ function colorMix(arena, ...args) {
|
|
|
111
111
|
default:
|
|
112
112
|
return null;
|
|
113
113
|
}
|
|
114
|
-
if (!equalsIgnoreCase(arena.
|
|
114
|
+
if (!equalsIgnoreCase(arena.resolveString(arena.stringValue[args[i++]]), "hue")) {
|
|
115
115
|
return null;
|
|
116
116
|
}
|
|
117
117
|
}
|
|
@@ -123,7 +123,7 @@ function colorMix(arena, ...args) {
|
|
|
123
123
|
ColorType.DPSYS === arena.colorType[args[i]] ||
|
|
124
124
|
ColorType.NON_STD === arena.colorType[args[i]] ||
|
|
125
125
|
ColorType.CUSTOM_COLOR === arena.colorType[args[i]] ||
|
|
126
|
-
equalsIgnoreCase(arena.
|
|
126
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[args[i]]), "currentcolor")) {
|
|
127
127
|
return null;
|
|
128
128
|
}
|
|
129
129
|
colorComponents.push(getColorComponents(arena, args[i]));
|
|
@@ -253,7 +253,7 @@ function colorMix(arena, ...args) {
|
|
|
253
253
|
let offset1 = arena.colorType[colors[0]] === ColorType.COLOR ? 1 : 0;
|
|
254
254
|
if (colorComponents[0][offset1 + 3] != null &&
|
|
255
255
|
arena.dataTypes[colorComponents[0][offset1 + 3]] === EnumToken.IdenTokenType &&
|
|
256
|
-
arena.
|
|
256
|
+
arena.resolveString(arena.stringValue[colorComponents[0][offset1 + 3]]) === "none" &&
|
|
257
257
|
colorComponents[1].length === offset1 + 4) {
|
|
258
258
|
// fix powerless alpha for last color if previous color has alpha
|
|
259
259
|
stack[stack.length - 1].color[3] = stack[stack.length - 2].color[3];
|
|
@@ -265,19 +265,19 @@ function colorMix(arena, ...args) {
|
|
|
265
265
|
// console.error(JSON.stringify({colorSpace1, colorSpace, stack, colorComponents}, null, 1));
|
|
266
266
|
if ((stack[stack.length - 1].color[0] === 0 &&
|
|
267
267
|
arena.dataTypes[colorComponents[0][offset1]] === EnumToken.IdenTokenType &&
|
|
268
|
-
arena.
|
|
268
|
+
arena.resolveString(arena.stringValue[colorComponents[0][offset1]]) === "none") ||
|
|
269
269
|
arena.flags[colorComponents[0][0]] & EnumAstNodeStatus.PowerlessColorComponent) {
|
|
270
270
|
stack[stack.length - 1].color[0] = stack[stack.length - 2].color[0];
|
|
271
271
|
}
|
|
272
272
|
if ((stack[stack.length - 1].color[1] === 0 &&
|
|
273
273
|
arena.dataTypes[colorComponents[0][offset1 + 1]] === EnumToken.IdenTokenType &&
|
|
274
|
-
arena.
|
|
274
|
+
arena.resolveString(arena.stringValue[colorComponents[0][offset1 + 1]]) === "none") ||
|
|
275
275
|
arena.flags[colorComponents[0][1]] & EnumAstNodeStatus.PowerlessColorComponent) {
|
|
276
276
|
stack[stack.length - 1].color[1] = stack[stack.length - 2].color[1];
|
|
277
277
|
}
|
|
278
278
|
if ((stack[stack.length - 1].color[2] === 0 &&
|
|
279
279
|
arena.dataTypes[colorComponents[0][offset1 + 2]] === EnumToken.IdenTokenType &&
|
|
280
|
-
arena.
|
|
280
|
+
arena.resolveString(arena.stringValue[colorComponents[0][offset1 + 2]]) === "none") ||
|
|
281
281
|
arena.flags[colorComponents[0][2]] & EnumAstNodeStatus.PowerlessColorComponent ||
|
|
282
282
|
(isLCHOrOKLCH &&
|
|
283
283
|
(stack[stack.length - 1].color[1] === 0 ||
|
|
@@ -288,7 +288,7 @@ function colorMix(arena, ...args) {
|
|
|
288
288
|
}
|
|
289
289
|
// }
|
|
290
290
|
}
|
|
291
|
-
// console.error([arena.dataTypes[colorComponents[0][offset1]] == EnumToken.IdenTokenType, arena.
|
|
291
|
+
// console.error([arena.dataTypes[colorComponents[0][offset1]] == EnumToken.IdenTokenType, arena.resolveString(arena.stringValue[colorComponents[0][offset1]]) == "none"]);
|
|
292
292
|
// console.error(isLCHOrOKLCH, colorComponents, stack);
|
|
293
293
|
while (stack.length > 1) {
|
|
294
294
|
r1 = stack.pop();
|
|
@@ -297,26 +297,26 @@ function colorMix(arena, ...args) {
|
|
|
297
297
|
// r2 powerless alpha
|
|
298
298
|
if (colorComponents[currentIndex][offset1 + 3] != null &&
|
|
299
299
|
arena.dataTypes[colorComponents[currentIndex][offset1 + 3]] === EnumToken.IdenTokenType &&
|
|
300
|
-
arena.
|
|
300
|
+
arena.resolveString(arena.stringValue[colorComponents[currentIndex][offset1 + 3]]) === "none") {
|
|
301
301
|
// fix powerless alpha for last color if previous color has alpha
|
|
302
302
|
r2.color[3] = r1.color[3];
|
|
303
303
|
}
|
|
304
304
|
// powerless hue for lch spaces
|
|
305
305
|
if ((r2.color[0] === 0 &&
|
|
306
306
|
arena.dataTypes[colorComponents[currentIndex][offset1]] === EnumToken.IdenTokenType &&
|
|
307
|
-
arena.
|
|
307
|
+
arena.resolveString(arena.stringValue[colorComponents[currentIndex][offset1]]) === "none") ||
|
|
308
308
|
arena.flags[colorComponents[currentIndex][offset1]] & EnumAstNodeStatus.PowerlessColorComponent) {
|
|
309
309
|
r2.color[0] = r1.color[0];
|
|
310
310
|
}
|
|
311
311
|
if ((r2.color[1] === 0 &&
|
|
312
312
|
arena.dataTypes[colorComponents[currentIndex][offset1 + 1]] === EnumToken.IdenTokenType &&
|
|
313
|
-
arena.
|
|
313
|
+
arena.resolveString(arena.stringValue[colorComponents[currentIndex][offset1 + 1]]) === "none") ||
|
|
314
314
|
arena.flags[colorComponents[currentIndex][offset1 + 1]] & EnumAstNodeStatus.PowerlessColorComponent) {
|
|
315
315
|
r2.color[1] = r1.color[1];
|
|
316
316
|
}
|
|
317
317
|
if ((r2.color[2] === 0 &&
|
|
318
318
|
arena.dataTypes[colorComponents[currentIndex][offset1 + 2]] === EnumToken.IdenTokenType &&
|
|
319
|
-
arena.
|
|
319
|
+
arena.resolveString(arena.stringValue[colorComponents[currentIndex][offset1 + 2]]) === "none") ||
|
|
320
320
|
arena.flags[colorComponents[currentIndex][offset1 + 2]] & EnumAstNodeStatus.PowerlessColorComponent ||
|
|
321
321
|
(isLCHOrOKLCH &&
|
|
322
322
|
(r2.color[1] === 0 ||
|