@tbela99/css-parser 1.8.2 → 2.0.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 +62 -0
- package/README.md +129 -31
- package/dist/index-umd-web.js +27383 -24083
- package/dist/index.d.ts +2737 -2200
- package/dist/package.json.js +2 -2
- package/dist/src/data/properties.json.js +1 -1
- package/dist/src/data/validation.json.js +1 -1
- package/dist/src/lib/ast/arena-node.js +548 -0
- package/dist/src/lib/ast/arena.js +3198 -0
- package/dist/src/lib/ast/features/calc.js +85 -76
- package/dist/src/lib/ast/features/if.js +155 -115
- package/dist/src/lib/ast/features/index.js +1 -1
- package/dist/src/lib/ast/features/inlinecssvariables.js +77 -84
- package/dist/src/lib/ast/features/prefix.js +184 -183
- package/dist/src/lib/ast/features/shorthand.js +40 -46
- package/dist/src/lib/ast/features/transform.js +24 -24
- package/dist/src/lib/ast/features/type.js +1 -1
- package/dist/src/lib/ast/fingerprint.js +333 -0
- package/dist/src/lib/ast/math/expression.js +458 -543
- package/dist/src/lib/ast/math/math.js +19 -41
- package/dist/src/lib/ast/minify.js +590 -647
- package/dist/src/lib/ast/transform/compute.js +82 -85
- package/dist/src/lib/ast/transform/matrix.js +21 -38
- package/dist/src/lib/ast/transform/minify.js +82 -193
- package/dist/src/lib/ast/transform/perspective.js +2 -3
- package/dist/src/lib/ast/transform/rotate.js +1 -1
- package/dist/src/lib/ast/transform/scale.js +1 -1
- package/dist/src/lib/ast/transform/skew.js +1 -1
- package/dist/src/lib/ast/transform/translate.js +1 -1
- package/dist/src/lib/ast/transform/utils.js +2 -2
- package/dist/src/lib/ast/types.js +263 -258
- package/dist/src/lib/ast/walk.js +246 -610
- package/dist/src/lib/fs/resolve.js +1 -1
- package/dist/src/lib/parser/declaration/list.js +223 -139
- package/dist/src/lib/parser/declaration/map.js +251 -238
- package/dist/src/lib/parser/declaration/set.js +74 -66
- package/dist/src/lib/parser/linesmap.js +1 -1
- package/dist/src/lib/parser/parse.js +1379 -1658
- package/dist/src/lib/parser/source.js +7 -4
- package/dist/src/lib/parser/tokenize.js +406 -199
- package/dist/src/lib/parser/utils/angle.js +11 -13
- package/dist/src/lib/parser/utils/at-rule-container.js +183 -168
- package/dist/src/lib/parser/utils/at-rule-font-feature-values.js +5 -5
- package/dist/src/lib/parser/utils/at-rule-generic.js +31 -24
- package/dist/src/lib/parser/utils/at-rule-import.js +44 -44
- package/dist/src/lib/parser/utils/at-rule-media.js +216 -205
- package/dist/src/lib/parser/utils/at-rule-support.js +83 -88
- package/dist/src/lib/parser/utils/at-rule-when-else.js +47 -47
- package/dist/src/lib/parser/utils/at-rule.js +5 -5
- package/dist/src/lib/parser/utils/base64.js +2 -2
- package/dist/src/lib/parser/utils/cache.js +1 -1
- package/dist/src/lib/parser/utils/config.js +1 -1
- package/dist/src/lib/parser/utils/declaration.js +233 -234
- package/dist/src/lib/parser/utils/eq.js +50 -3
- package/dist/src/lib/parser/utils/hash.js +2 -35
- package/dist/src/lib/parser/utils/selector.js +309 -387
- package/dist/src/lib/parser/utils/text.js +18 -4
- package/dist/src/lib/parser/utils/token.js +16 -69
- package/dist/src/lib/parser/utils/type.js +37 -24
- package/dist/src/lib/printer/render.js +965 -511
- package/dist/src/lib/printer/sourcemap/lib/codec.js +1 -1
- package/dist/src/lib/printer/sourcemap/sourcemap.js +1 -1
- package/dist/src/lib/syntax/color/a98rgb.js +1 -1
- package/dist/src/lib/syntax/color/alpha.js +36 -37
- package/dist/src/lib/syntax/color/cmyk.js +35 -52
- package/dist/src/lib/syntax/color/color-mix.js +96 -132
- package/dist/src/lib/syntax/color/color.js +222 -231
- package/dist/src/lib/syntax/color/hex.js +45 -55
- package/dist/src/lib/syntax/color/hsl.js +54 -62
- package/dist/src/lib/syntax/color/hwb.js +60 -68
- package/dist/src/lib/syntax/color/lab.js +59 -67
- package/dist/src/lib/syntax/color/lch.js +59 -67
- package/dist/src/lib/syntax/color/oklab.js +60 -68
- package/dist/src/lib/syntax/color/oklch.js +60 -67
- package/dist/src/lib/syntax/color/p3.js +1 -1
- package/dist/src/lib/syntax/color/prophotorgb.js +1 -1
- package/dist/src/lib/syntax/color/rec2020.js +1 -1
- package/dist/src/lib/syntax/color/relative-color.js +94 -144
- package/dist/src/lib/syntax/color/rgb.js +54 -57
- package/dist/src/lib/syntax/color/srgb.js +55 -53
- package/dist/src/lib/syntax/color/utils/color-type.js +13 -10
- package/dist/src/lib/syntax/color/utils/colorspace.js +11 -11
- package/dist/src/lib/syntax/color/utils/components.js +19 -20
- package/dist/src/lib/syntax/color/utils/distance.js +15 -13
- package/dist/src/lib/syntax/color/utils/make-color.js +15 -19
- package/dist/src/lib/syntax/color/utils/matrix.js +2 -2
- package/dist/src/lib/syntax/color/xyz.js +1 -1
- package/dist/src/lib/syntax/color/xyzd50.js +1 -1
- package/dist/src/lib/syntax/constants.js +2 -16
- package/dist/src/lib/syntax/syntax.js +301 -407
- package/dist/src/lib/validation/config.js +1 -1
- package/dist/src/lib/validation/match.js +959 -723
- package/dist/src/lib/validation/parser/parse.js +230 -112
- package/dist/src/lib/validation/parser/typedef.js +1 -1
- package/dist/src/lib/validation/utils/list.js +27 -12
- package/dist/src/node.js +12 -72
- package/dist/src/types.js +1 -1
- package/dist/src/utils/sync.js +9 -6
- package/dist/src/web.js +11 -73
- package/package.json +9 -10
- package/dist/index.cjs +0 -35870
- package/dist/src/lib/ast/clone.js +0 -51
- package/dist/src/lib/ast/expand.js +0 -311
- package/dist/src/lib/ast/find.js +0 -192
- package/dist/src/lib/ast/node.js +0 -59
|
@@ -2,29 +2,38 @@
|
|
|
2
2
|
CSS parser, minifier and validator for node and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version:
|
|
5
|
+
Version: 2.0.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
|
-
import {
|
|
8
|
+
import { numericTokenTypes } from '../../ast/arena.js';
|
|
9
|
+
import { eqToken } from '../utils/eq.js';
|
|
9
10
|
import { EnumToken } from '../../ast/types.js';
|
|
10
11
|
import { isLength } from '../../syntax/syntax.js';
|
|
11
12
|
|
|
12
|
-
function dedup(values) {
|
|
13
|
+
function dedup(arena, values) {
|
|
13
14
|
for (const value of values) {
|
|
14
15
|
let i = value.length;
|
|
15
16
|
while (i-- > 1) {
|
|
16
17
|
const t = value[i];
|
|
17
18
|
const k = value[i == 1 ? 0 : i % 2];
|
|
18
|
-
if (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
if (
|
|
20
|
+
// inlined: arena.isNumeric(t)
|
|
21
|
+
numericTokenTypes.has(arena.dataTypes[t]) &&
|
|
22
|
+
// inlined: arena.isNumeric(k)
|
|
23
|
+
numericTokenTypes.has(arena.dataTypes[k]) &&
|
|
24
|
+
arena.numericValue[t] == 0 &&
|
|
25
|
+
arena.numericValue[k] == 0 &&
|
|
26
|
+
!arena.hasFraction(t) &&
|
|
27
|
+
!arena.hasFraction(k)) {
|
|
28
|
+
if ((arena.dataTypes[t] == EnumToken.NumberTokenType && isLength(arena, k)) ||
|
|
29
|
+
(arena.dataTypes[k] == EnumToken.NumberTokenType && isLength(arena, t)) ||
|
|
30
|
+
isLength(arena, k) ||
|
|
31
|
+
isLength(arena, t)) {
|
|
23
32
|
value.splice(i, 1);
|
|
24
33
|
continue;
|
|
25
34
|
}
|
|
26
35
|
}
|
|
27
|
-
if (
|
|
36
|
+
if (eqToken(arena, t, k)) {
|
|
28
37
|
value.splice(i, 1);
|
|
29
38
|
continue;
|
|
30
39
|
}
|
|
@@ -33,15 +42,23 @@ function dedup(values) {
|
|
|
33
42
|
}
|
|
34
43
|
return values;
|
|
35
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* set of declarations that can be merged into a single shorthand declaration.
|
|
47
|
+
* declarations are arena node ids
|
|
48
|
+
*/
|
|
36
49
|
class PropertySet {
|
|
37
50
|
config;
|
|
51
|
+
arena;
|
|
38
52
|
declarations;
|
|
39
|
-
constructor(config) {
|
|
53
|
+
constructor(arena, config) {
|
|
54
|
+
this.arena = arena;
|
|
40
55
|
this.config = config;
|
|
41
56
|
this.declarations = new Map();
|
|
42
57
|
}
|
|
43
58
|
add(declaration) {
|
|
44
|
-
|
|
59
|
+
const arena = this.arena;
|
|
60
|
+
const nam = arena.strings[arena.name[declaration]];
|
|
61
|
+
if (nam == this.config.shorthand) {
|
|
45
62
|
this.declarations = new Map();
|
|
46
63
|
}
|
|
47
64
|
else {
|
|
@@ -50,13 +67,13 @@ class PropertySet {
|
|
|
50
67
|
let isValid = true;
|
|
51
68
|
let current = -1;
|
|
52
69
|
const tokens = [];
|
|
53
|
-
|
|
54
|
-
|
|
70
|
+
for (const token of arena.tokens(this.declarations.get(this.config.shorthand))) {
|
|
71
|
+
const typ = arena.dataTypes[token];
|
|
55
72
|
if (
|
|
56
73
|
// @ts-expect-error
|
|
57
|
-
this.config.types.some((t) =>
|
|
58
|
-
(
|
|
59
|
-
token
|
|
74
|
+
this.config.types.some((t) => typ == EnumToken[t]) ||
|
|
75
|
+
(typ == EnumToken.NumberTokenType &&
|
|
76
|
+
arena.numericValue[token] == 0 &&
|
|
60
77
|
(this.config.types.includes("Length") ||
|
|
61
78
|
this.config.types.includes("Angle") ||
|
|
62
79
|
this.config.types.includes("Dimension")))) {
|
|
@@ -67,17 +84,17 @@ class PropertySet {
|
|
|
67
84
|
tokens[current].push(token);
|
|
68
85
|
continue;
|
|
69
86
|
}
|
|
70
|
-
if (
|
|
71
|
-
if (
|
|
72
|
-
this.config.keywords.includes(token
|
|
87
|
+
if (typ != EnumToken.WhitespaceTokenType && typ != EnumToken.CommentTokenType) {
|
|
88
|
+
if (typ == EnumToken.IdenTokenType &&
|
|
89
|
+
this.config.keywords.includes(arena.strings[arena.stringValue[token]])) {
|
|
73
90
|
if (tokens.length == 0) {
|
|
74
91
|
tokens.push([]);
|
|
75
92
|
current++;
|
|
76
93
|
}
|
|
77
94
|
tokens[current].push(token);
|
|
78
95
|
}
|
|
79
|
-
|
|
80
|
-
|
|
96
|
+
if (typ == EnumToken.LiteralTokenType &&
|
|
97
|
+
arena.strings[arena.stringValue[token]] == this.config.separator?.val) {
|
|
81
98
|
tokens.push([]);
|
|
82
99
|
current++;
|
|
83
100
|
continue;
|
|
@@ -91,11 +108,7 @@ class PropertySet {
|
|
|
91
108
|
for (const values of tokens) {
|
|
92
109
|
this.config.properties.forEach((property, index) => {
|
|
93
110
|
if (!this.declarations.has(property)) {
|
|
94
|
-
this.declarations.set(property,
|
|
95
|
-
typ: EnumToken.DeclarationNodeType,
|
|
96
|
-
nam: property,
|
|
97
|
-
val: [],
|
|
98
|
-
});
|
|
111
|
+
this.declarations.set(property, arena.createDeclaration(EnumToken.DeclarationNodeType, property, [], null, null));
|
|
99
112
|
}
|
|
100
113
|
while (index > 0 && index >= values.length) {
|
|
101
114
|
if (index > 1) {
|
|
@@ -106,20 +119,19 @@ class PropertySet {
|
|
|
106
119
|
break;
|
|
107
120
|
}
|
|
108
121
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
val.push({ typ: EnumToken.WhitespaceTokenType });
|
|
122
|
+
const node = this.declarations.get(property);
|
|
123
|
+
if (arena.firstToken[node] != 0) {
|
|
124
|
+
arena.appendToken(node, arena.createToken(EnumToken.WhitespaceTokenType));
|
|
113
125
|
}
|
|
114
|
-
|
|
126
|
+
arena.appendToken(node, arena.clone(values[index], true));
|
|
115
127
|
});
|
|
116
128
|
}
|
|
117
129
|
}
|
|
118
|
-
this.declarations.set(
|
|
130
|
+
this.declarations.set(nam, declaration);
|
|
119
131
|
return this;
|
|
120
132
|
}
|
|
121
133
|
}
|
|
122
|
-
this.declarations.set(
|
|
134
|
+
this.declarations.set(nam, declaration);
|
|
123
135
|
return this;
|
|
124
136
|
}
|
|
125
137
|
isShortHand() {
|
|
@@ -130,23 +142,26 @@ class PropertySet {
|
|
|
130
142
|
}
|
|
131
143
|
[Symbol.iterator]() {
|
|
132
144
|
let iterator;
|
|
145
|
+
const arena = this.arena;
|
|
133
146
|
const declarations = this.declarations;
|
|
134
147
|
if (declarations.size < this.config.properties.length) {
|
|
135
148
|
const values = [...declarations.values()];
|
|
136
149
|
if (this.isShortHand()) {
|
|
137
|
-
const val = values[0].
|
|
138
|
-
if (
|
|
150
|
+
const val = arena.tokens(values[0]).reduce((acc, curr) => {
|
|
151
|
+
if (arena.dataTypes[curr] != EnumToken.WhitespaceTokenType &&
|
|
152
|
+
arena.dataTypes[curr] != EnumToken.CommentTokenType) {
|
|
139
153
|
acc.push(curr);
|
|
140
154
|
}
|
|
141
155
|
return acc;
|
|
142
156
|
}, []);
|
|
143
|
-
values[0]
|
|
157
|
+
// inlined: arena.setTokens(values[0], ...val.reduce((acc: Token[], curr: Token) => { if (acc.length > 0) { acc.push(arena.createToken(EnumToken.WhitespaceTokenType)); } acc.push(curr); return acc; }, <Token[]>[]))
|
|
158
|
+
arena.setTokenList(values[0], val.reduce((acc, curr) => {
|
|
144
159
|
if (acc.length > 0) {
|
|
145
|
-
acc.push(
|
|
160
|
+
acc.push(arena.createToken(EnumToken.WhitespaceTokenType));
|
|
146
161
|
}
|
|
147
162
|
acc.push(curr);
|
|
148
163
|
return acc;
|
|
149
|
-
}, []);
|
|
164
|
+
}, []));
|
|
150
165
|
}
|
|
151
166
|
return values[Symbol.iterator]();
|
|
152
167
|
}
|
|
@@ -154,12 +169,9 @@ class PropertySet {
|
|
|
154
169
|
const values = [];
|
|
155
170
|
this.config.properties.forEach((property) => {
|
|
156
171
|
let index = 0;
|
|
157
|
-
|
|
158
|
-
for (const token of
|
|
159
|
-
if (token ==
|
|
160
|
-
return this.declarations.values()[Symbol.iterator]();
|
|
161
|
-
}
|
|
162
|
-
if (token.typ == EnumToken.WhitespaceTokenType) {
|
|
172
|
+
const id = this.declarations.get(property);
|
|
173
|
+
for (const token of id == null ? [] : arena.tokens(id)) {
|
|
174
|
+
if (arena.dataTypes[token] == EnumToken.WhitespaceTokenType) {
|
|
163
175
|
continue;
|
|
164
176
|
}
|
|
165
177
|
if (values.length == index) {
|
|
@@ -170,31 +182,27 @@ class PropertySet {
|
|
|
170
182
|
}
|
|
171
183
|
});
|
|
172
184
|
if (this.config.valueSeparator == null) {
|
|
173
|
-
dedup(values);
|
|
185
|
+
dedup(arena, values);
|
|
174
186
|
}
|
|
175
187
|
iterator = [
|
|
176
|
-
{
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
while (i < k) {
|
|
184
|
-
curr.splice(i, 0, { typ: EnumToken.WhitespaceTokenType });
|
|
185
|
-
i += 2;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
if (acc.length > 0) {
|
|
189
|
-
// @ts-ignore
|
|
190
|
-
acc.push({ ...this.config.separator, typ: EnumToken.LiteralTokenType });
|
|
191
|
-
}
|
|
192
|
-
for (const token of curr) {
|
|
193
|
-
acc.push(token);
|
|
188
|
+
arena.createDeclaration(EnumToken.DeclarationNodeType, this.config.shorthand, values.reduce((acc, curr) => {
|
|
189
|
+
if (curr.length > 1) {
|
|
190
|
+
const k = curr.length * 2 - 1;
|
|
191
|
+
let i = 1;
|
|
192
|
+
while (i < k) {
|
|
193
|
+
curr.splice(i, 0, arena.createToken(EnumToken.WhitespaceTokenType));
|
|
194
|
+
i += 2;
|
|
194
195
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
196
|
+
}
|
|
197
|
+
if (acc.length > 0) {
|
|
198
|
+
acc.push(arena.createToken(EnumToken.LiteralTokenType, this.config.separator.val));
|
|
199
|
+
}
|
|
200
|
+
for (const token of curr) {
|
|
201
|
+
// tokens are shared with the original declarations
|
|
202
|
+
acc.push(arena.parents[token] != 0 ? arena.clone(token, true) : token);
|
|
203
|
+
}
|
|
204
|
+
return acc;
|
|
205
|
+
}, []), null, null),
|
|
198
206
|
][Symbol.iterator]();
|
|
199
207
|
}
|
|
200
208
|
return iterator;
|