@tbela99/css-parser 2.0.1 → 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 +86 -1
- package/README.md +15 -7
- package/architecture.png +0 -0
- package/dist/{src/data → data}/properties.json.js +86 -2
- package/dist/{src/data → data}/validation.json.js +4848 -3750
- package/dist/index-umd-web.js +18234 -9498
- package/dist/index.d.ts +1251 -373
- package/dist/{src/lib → lib}/ast/arena-node.js +119 -57
- package/dist/{src/lib → lib}/ast/arena.js +1034 -655
- package/dist/{src/lib → lib}/ast/cache.js +36 -31
- package/dist/{src/lib → lib}/ast/features/calc.js +37 -27
- package/dist/{src/lib → lib}/ast/features/if.js +24 -19
- package/dist/{src/lib → lib}/ast/features/index.js +3 -2
- package/dist/{src/lib → lib}/ast/features/inlinecssvariables.js +42 -23
- package/dist/lib/ast/features/mixin.js +953 -0
- package/dist/{src/lib → lib}/ast/features/prefix.js +82 -69
- 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 +150 -150
- package/dist/{src/lib → lib}/ast/math/math.js +8 -8
- package/dist/{src/lib → lib}/ast/minify.js +932 -354
- 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 +7 -15
- package/dist/{src/lib → lib}/ast/walk.js +19 -19
- package/dist/{src/lib → lib}/fs/resolve.js +15 -15
- package/dist/{src/lib → lib}/parser/declaration/list.js +55 -26
- package/dist/{src/lib → lib}/parser/declaration/map.js +85 -72
- package/dist/{src/lib → lib}/parser/declaration/set.js +32 -27
- package/dist/lib/parser/input.js +85 -0
- package/dist/{src/lib → lib}/parser/linesmap.js +3 -16
- package/dist/{src/lib → lib}/parser/parse.js +852 -619
- package/dist/lib/parser/source.js +71 -0
- package/dist/{src/lib → lib}/parser/tokenize.js +886 -722
- package/dist/{src/lib → lib}/parser/utils/angle.js +3 -3
- package/dist/{src/lib → lib}/parser/utils/at-rule-container.js +68 -35
- 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 +31 -13
- package/dist/{src/lib → lib}/parser/utils/at-rule-import.js +29 -16
- package/dist/{src/lib → lib}/parser/utils/at-rule-media.js +77 -58
- package/dist/{src/lib → lib}/parser/utils/at-rule-support.js +18 -20
- package/dist/{src/lib → lib}/parser/utils/at-rule-when-else.js +9 -8
- 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 +67 -49
- package/dist/{src/lib → lib}/parser/utils/eq.js +14 -14
- package/dist/{src/lib → lib}/parser/utils/hash.js +4 -4
- package/dist/lib/parser/utils/selector.js +720 -0
- 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 +11 -9
- package/dist/{src/lib → lib}/printer/render.js +536 -243
- package/dist/lib/printer/result.js +148 -0
- package/dist/{src/lib → lib}/printer/sourcemap/lib/codec.js +5 -8
- package/dist/{src/lib → lib}/printer/sourcemap/sourcemap.js +13 -13
- package/dist/lib/printer/writer.js +252 -0
- package/dist/{src/lib → lib}/syntax/color/a98rgb.js +6 -6
- package/dist/{src/lib → lib}/syntax/color/alpha.js +12 -11
- package/dist/{src/lib → lib}/syntax/color/cmyk.js +11 -6
- package/dist/{src/lib → lib}/syntax/color/color-mix.js +78 -67
- package/dist/{src/lib → lib}/syntax/color/color.js +52 -47
- package/dist/{src/lib → lib}/syntax/color/hex.js +17 -17
- package/dist/{src/lib → lib}/syntax/color/hsl.js +8 -8
- package/dist/{src/lib → lib}/syntax/color/hwb.js +6 -7
- package/dist/{src/lib → lib}/syntax/color/lab.js +8 -8
- package/dist/{src/lib → lib}/syntax/color/lch.js +12 -11
- package/dist/{src/lib → lib}/syntax/color/oklab.js +9 -9
- package/dist/{src/lib → lib}/syntax/color/oklch.js +5 -5
- package/dist/{src/lib → lib}/syntax/color/p3.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/prophotorgb.js +4 -4
- package/dist/{src/lib → lib}/syntax/color/rec2020.js +3 -3
- package/dist/{src/lib → lib}/syntax/color/relative-color.js +41 -34
- package/dist/{src/lib → lib}/syntax/color/rgb.js +17 -17
- package/dist/{src/lib → lib}/syntax/color/srgb.js +20 -20
- package/dist/{src/lib → lib}/syntax/color/utils/color-type.js +6 -4
- package/dist/{src/lib → lib}/syntax/color/utils/colorspace.js +12 -14
- package/dist/{src/lib → lib}/syntax/color/utils/components.js +13 -13
- package/dist/{src/lib → lib}/syntax/color/utils/distance.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/utils/make-color.js +6 -4
- package/dist/{src/lib → lib}/syntax/color/utils/matrix.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/xyz.js +5 -10
- package/dist/{src/lib → lib}/syntax/color/xyzd50.js +3 -3
- package/dist/{src/lib → lib}/syntax/constants.js +30 -3
- package/dist/{src/lib → lib}/syntax/syntax.js +151 -116
- package/dist/lib/validation/config.js +173 -0
- package/dist/lib/validation/first.js +571 -0
- package/dist/{src/lib → lib}/validation/match.js +1256 -579
- package/dist/{src/lib → lib}/validation/parser/parse.js +85 -59
- package/dist/{src/lib → lib}/validation/parser/typedef.js +6 -2
- package/dist/{src/lib → lib}/validation/utils/list.js +3 -19
- package/dist/{src/node.js → node.js} +92 -80
- package/dist/{src/types.js → types.js} +2 -2
- package/dist/{src/utils → utils}/sync.js +13 -8
- package/dist/version.d.ts +4 -0
- package/dist/version.js +13 -0
- package/dist/{src/web.js → web.js} +80 -75
- package/package.json +13 -13
- package/dist/package.json.js +0 -10
- package/dist/src/lib/ast/features/shorthand.js +0 -87
- package/dist/src/lib/ast/features/transform.js +0 -85
- package/dist/src/lib/ast/transform/compute.js +0 -358
- 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/source.js +0 -127
- package/dist/src/lib/parser/utils/cache.js +0 -54
- package/dist/src/lib/parser/utils/selector.js +0 -440
- package/dist/src/lib/validation/config.js +0 -120
|
@@ -1,13 +1,13 @@
|
|
|
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.0
|
|
5
|
+
Version: 2.2.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { numericTokenTypes } from '../../ast/arena.js';
|
|
9
9
|
import { eqToken } from '../utils/eq.js';
|
|
10
|
-
import { EnumToken } from '../../ast/types.js';
|
|
10
|
+
import { EnumToken, EnumAstNodeStatus } from '../../ast/types.js';
|
|
11
11
|
import { isLength } from '../../syntax/syntax.js';
|
|
12
12
|
|
|
13
13
|
function dedup(arena, values) {
|
|
@@ -15,18 +15,18 @@ function dedup(arena, values) {
|
|
|
15
15
|
let i = value.length;
|
|
16
16
|
while (i-- > 1) {
|
|
17
17
|
const t = value[i];
|
|
18
|
-
const k = value[i
|
|
18
|
+
const k = value[i === 1 ? 0 : i % 2];
|
|
19
19
|
if (
|
|
20
20
|
// inlined: arena.isNumeric(t)
|
|
21
21
|
numericTokenTypes.has(arena.dataTypes[t]) &&
|
|
22
22
|
// inlined: arena.isNumeric(k)
|
|
23
23
|
numericTokenTypes.has(arena.dataTypes[k]) &&
|
|
24
|
-
arena.numericValue[t]
|
|
25
|
-
arena.numericValue[k]
|
|
24
|
+
arena.numericValue[t] === 0 &&
|
|
25
|
+
arena.numericValue[k] === 0 &&
|
|
26
26
|
!arena.hasFraction(t) &&
|
|
27
27
|
!arena.hasFraction(k)) {
|
|
28
|
-
if ((arena.dataTypes[t]
|
|
29
|
-
(arena.dataTypes[k]
|
|
28
|
+
if ((arena.dataTypes[t] === EnumToken.NumberTokenType && isLength(arena, k)) ||
|
|
29
|
+
(arena.dataTypes[k] === EnumToken.NumberTokenType && isLength(arena, t)) ||
|
|
30
30
|
isLength(arena, k) ||
|
|
31
31
|
isLength(arena, t)) {
|
|
32
32
|
value.splice(i, 1);
|
|
@@ -57,8 +57,8 @@ class PropertySet {
|
|
|
57
57
|
}
|
|
58
58
|
add(declaration) {
|
|
59
59
|
const arena = this.arena;
|
|
60
|
-
const nam = arena.
|
|
61
|
-
if (nam
|
|
60
|
+
const nam = arena.resolveString(arena.name[declaration]);
|
|
61
|
+
if (nam === this.config.shorthand) {
|
|
62
62
|
this.declarations = new Map();
|
|
63
63
|
}
|
|
64
64
|
else {
|
|
@@ -72,29 +72,29 @@ class PropertySet {
|
|
|
72
72
|
if (
|
|
73
73
|
// @ts-expect-error
|
|
74
74
|
this.config.types.some((t) => typ == EnumToken[t]) ||
|
|
75
|
-
(typ
|
|
76
|
-
arena.numericValue[token]
|
|
75
|
+
(typ === EnumToken.NumberTokenType &&
|
|
76
|
+
arena.numericValue[token] === 0 &&
|
|
77
77
|
(this.config.types.includes("Length") ||
|
|
78
78
|
this.config.types.includes("Angle") ||
|
|
79
79
|
this.config.types.includes("Dimension")))) {
|
|
80
|
-
if (tokens.length
|
|
80
|
+
if (tokens.length === 0) {
|
|
81
81
|
tokens.push([]);
|
|
82
82
|
current++;
|
|
83
83
|
}
|
|
84
84
|
tokens[current].push(token);
|
|
85
85
|
continue;
|
|
86
86
|
}
|
|
87
|
-
if (typ
|
|
88
|
-
if (typ
|
|
89
|
-
this.config.keywords.includes(arena.
|
|
90
|
-
if (tokens.length
|
|
87
|
+
if (typ !== EnumToken.WhitespaceTokenType && typ !== EnumToken.CommentTokenType) {
|
|
88
|
+
if (typ === EnumToken.IdenTokenType &&
|
|
89
|
+
this.config.keywords.includes(arena.resolveString(arena.stringValue[token]))) {
|
|
90
|
+
if (tokens.length === 0) {
|
|
91
91
|
tokens.push([]);
|
|
92
92
|
current++;
|
|
93
93
|
}
|
|
94
94
|
tokens[current].push(token);
|
|
95
95
|
}
|
|
96
|
-
if (typ
|
|
97
|
-
arena.
|
|
96
|
+
if (typ === EnumToken.LiteralTokenType &&
|
|
97
|
+
arena.resolveString(arena.stringValue[token]) === this.config.separator?.val) {
|
|
98
98
|
tokens.push([]);
|
|
99
99
|
current++;
|
|
100
100
|
continue;
|
|
@@ -120,7 +120,7 @@ class PropertySet {
|
|
|
120
120
|
}
|
|
121
121
|
}
|
|
122
122
|
const node = this.declarations.get(property);
|
|
123
|
-
if (arena.firstToken[node]
|
|
123
|
+
if (arena.firstToken[node] !== 0) {
|
|
124
124
|
arena.appendToken(node, arena.createToken(EnumToken.WhitespaceTokenType));
|
|
125
125
|
}
|
|
126
126
|
arena.appendToken(node, arena.clone(values[index], true));
|
|
@@ -136,20 +136,25 @@ class PropertySet {
|
|
|
136
136
|
}
|
|
137
137
|
isShortHand() {
|
|
138
138
|
if (this.declarations.has(this.config.shorthand)) {
|
|
139
|
-
return this.declarations.size
|
|
139
|
+
return this.declarations.size === 1;
|
|
140
140
|
}
|
|
141
|
-
return this.config.properties.length
|
|
141
|
+
return this.config.properties.length === this.declarations.size;
|
|
142
142
|
}
|
|
143
143
|
[Symbol.iterator]() {
|
|
144
144
|
let iterator;
|
|
145
145
|
const arena = this.arena;
|
|
146
146
|
const declarations = this.declarations;
|
|
147
|
+
// the !important declarations are not merged: `column-gap: 16px !important; row-gap: 40px !important`
|
|
148
|
+
if (!declarations.has(this.config.shorthand) &&
|
|
149
|
+
[...declarations.values()].some((declaration) => arena.flags[declaration] & EnumAstNodeStatus.ImportantDeclaration)) {
|
|
150
|
+
return [...declarations.values()][Symbol.iterator]();
|
|
151
|
+
}
|
|
147
152
|
if (declarations.size < this.config.properties.length) {
|
|
148
153
|
const values = [...declarations.values()];
|
|
149
154
|
if (this.isShortHand()) {
|
|
150
155
|
const val = arena.tokens(values[0]).reduce((acc, curr) => {
|
|
151
|
-
if (arena.dataTypes[curr]
|
|
152
|
-
arena.dataTypes[curr]
|
|
156
|
+
if (arena.dataTypes[curr] !== EnumToken.WhitespaceTokenType &&
|
|
157
|
+
arena.dataTypes[curr] !== EnumToken.CommentTokenType) {
|
|
153
158
|
acc.push(curr);
|
|
154
159
|
}
|
|
155
160
|
return acc;
|
|
@@ -171,10 +176,10 @@ class PropertySet {
|
|
|
171
176
|
let index = 0;
|
|
172
177
|
const id = this.declarations.get(property);
|
|
173
178
|
for (const token of id == null ? [] : arena.tokens(id)) {
|
|
174
|
-
if (arena.dataTypes[token]
|
|
179
|
+
if (arena.dataTypes[token] === EnumToken.WhitespaceTokenType) {
|
|
175
180
|
continue;
|
|
176
181
|
}
|
|
177
|
-
if (values.length
|
|
182
|
+
if (values.length === index) {
|
|
178
183
|
values.push([]);
|
|
179
184
|
}
|
|
180
185
|
values[index].push(token);
|
|
@@ -199,7 +204,7 @@ class PropertySet {
|
|
|
199
204
|
}
|
|
200
205
|
for (const token of curr) {
|
|
201
206
|
// tokens are shared with the original declarations
|
|
202
|
-
acc.push(arena.parents[token]
|
|
207
|
+
acc.push(arena.parents[token] !== 0 ? arena.clone(token, true) : token);
|
|
203
208
|
}
|
|
204
209
|
return acc;
|
|
205
210
|
}, []), null, null),
|
|
@@ -0,0 +1,85 @@
|
|
|
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
|
+
import { nativeBufferClass } from '../printer/writer.js';
|
|
9
|
+
|
|
10
|
+
const encoder = new TextEncoder();
|
|
11
|
+
/**
|
|
12
|
+
* the code units of a Uint16Array are stored in the platform byte order
|
|
13
|
+
*/
|
|
14
|
+
const littleEndian = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
|
|
15
|
+
const decoder = new TextDecoder();
|
|
16
|
+
const nonAscii = /[^\x00-\x7f]/;
|
|
17
|
+
const empty = new Uint8Array(0);
|
|
18
|
+
/**
|
|
19
|
+
* true if the input is an ArrayBuffer or a view of an ArrayBuffer
|
|
20
|
+
* @param input
|
|
21
|
+
*/
|
|
22
|
+
function isBinaryInput(input) {
|
|
23
|
+
return input instanceof ArrayBuffer || ArrayBuffer.isView(input);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* true if the input is css: a string, an ArrayBuffer or a view of an ArrayBuffer
|
|
27
|
+
* @param input
|
|
28
|
+
*/
|
|
29
|
+
function isCssInput(input) {
|
|
30
|
+
return typeof input === "string" || isBinaryInput(input);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* the bytes of a binary input
|
|
34
|
+
* @param input
|
|
35
|
+
*/
|
|
36
|
+
function bytesOf(input) {
|
|
37
|
+
return input instanceof Uint8Array
|
|
38
|
+
? input
|
|
39
|
+
: input instanceof ArrayBuffer
|
|
40
|
+
? new Uint8Array(input)
|
|
41
|
+
: new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* the utf-16 code units of a text
|
|
45
|
+
* @param text
|
|
46
|
+
*/
|
|
47
|
+
function codeUnitsOf(text) {
|
|
48
|
+
const native = nativeBufferClass();
|
|
49
|
+
// node.js: the code units are written by the native encoder (little endian platforms)
|
|
50
|
+
if (native !== null && littleEndian) {
|
|
51
|
+
const buffer = native.allocUnsafeSlow(text.length * 2);
|
|
52
|
+
buffer.write(text, 0, text.length * 2, "utf16le");
|
|
53
|
+
return new Uint16Array(buffer.buffer, buffer.byteOffset, text.length);
|
|
54
|
+
}
|
|
55
|
+
const codes = new Uint16Array(text.length);
|
|
56
|
+
for (let i = 0; i < text.length; i++) {
|
|
57
|
+
codes[i] = text.charCodeAt(i);
|
|
58
|
+
}
|
|
59
|
+
return codes;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* convert the css input to code units: the tokenizer reads the code units. a string is encoded to an ArrayBuffer.
|
|
63
|
+
* an ascii input is read from its bytes: the code units are the bytes. the other inputs are decoded (utf-8) to
|
|
64
|
+
* utf-16 code units: the positions of the tokens are utf-16 positions, like the positions in a string
|
|
65
|
+
* @param input
|
|
66
|
+
*/
|
|
67
|
+
function toCodeUnits(input) {
|
|
68
|
+
if (input == null || input === "") {
|
|
69
|
+
return { codes: empty, text: "" };
|
|
70
|
+
}
|
|
71
|
+
if (typeof input === "string") {
|
|
72
|
+
// an ascii string has a byte for each code unit: the encoded string is the code units
|
|
73
|
+
const bytes = encoder.encode(input);
|
|
74
|
+
return { codes: bytes.length === input.length ? bytes : codeUnitsOf(input), text: input };
|
|
75
|
+
}
|
|
76
|
+
const bytes = bytesOf(input);
|
|
77
|
+
const text = decoder.decode(bytes);
|
|
78
|
+
// the text of an ascii input has a character for each byte. the byte order mark is removed by the decoder
|
|
79
|
+
if (text.length === bytes.length && !nonAscii.test(text)) {
|
|
80
|
+
return { codes: bytes, text };
|
|
81
|
+
}
|
|
82
|
+
return { codes: codeUnitsOf(text), text };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export { isBinaryInput, isCssInput, toCodeUnits };
|
|
@@ -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.0
|
|
5
|
+
Version: 2.2.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
/**
|
|
@@ -32,7 +32,7 @@ class LineMap {
|
|
|
32
32
|
const line = this.search(offset);
|
|
33
33
|
const column = offset - this.lineStarts[line];
|
|
34
34
|
// [line, column]
|
|
35
|
-
return [line + 1, line
|
|
35
|
+
return [line + 1, line === 0 ? column + 1 : column];
|
|
36
36
|
}
|
|
37
37
|
/**
|
|
38
38
|
* search the greatest index of the value less than or equal to offset
|
|
@@ -57,19 +57,6 @@ class LineMap {
|
|
|
57
57
|
}
|
|
58
58
|
return result;
|
|
59
59
|
}
|
|
60
|
-
/**
|
|
61
|
-
* get line starts
|
|
62
|
-
* @returns
|
|
63
|
-
*/
|
|
64
|
-
getLineStarts() {
|
|
65
|
-
return this.lineStarts;
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* add line start
|
|
69
|
-
*/
|
|
70
|
-
addLineStart(lineStart) {
|
|
71
|
-
this.lineStarts.push(lineStart);
|
|
72
|
-
}
|
|
73
60
|
}
|
|
74
61
|
|
|
75
62
|
export { LineMap };
|