@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,16 +2,16 @@
|
|
|
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
8
|
import { convertColor, getNumber } from './color.js';
|
|
9
9
|
import { EnumToken, ColorType } from '../../ast/types.js';
|
|
10
|
-
import { walkValues } from '../../ast/walk.js';
|
|
11
10
|
import { evaluateFunc, evaluate } from '../../ast/math/expression.js';
|
|
12
|
-
import { colorsFunc, colorFuncColorSpace,
|
|
11
|
+
import { colorsFunc, colorFuncColorSpace, colorRange, mathFuncs } from '../constants.js';
|
|
13
12
|
import { equalsIgnoreCase } from '../../parser/utils/text.js';
|
|
14
13
|
import { getColorComponents } from './utils/components.js';
|
|
14
|
+
import { ValueWalker } from '../../ast/walk.js';
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
17
|
* Parse relative color components
|
|
@@ -23,7 +23,7 @@ import { getColorComponents } from './utils/components.js';
|
|
|
23
23
|
* @param aExp
|
|
24
24
|
* @returns
|
|
25
25
|
*/
|
|
26
|
-
function parseRelativeColorComponents(relativeKeys, original, rExp, gExp, bExp, aExp) {
|
|
26
|
+
function parseRelativeColorComponents(arena, relativeKeys, original, rExp, gExp, bExp, aExp) {
|
|
27
27
|
let r;
|
|
28
28
|
let g;
|
|
29
29
|
let b;
|
|
@@ -36,8 +36,8 @@ function parseRelativeColorComponents(relativeKeys, original, rExp, gExp, bExp,
|
|
|
36
36
|
: ["srgb", "srgb-linear", "display-p3", "a98-rgb", "prophoto-rgb", "rec2020", "rgb"].some((t) => equalsIgnoreCase(t, relativeKeys))
|
|
37
37
|
? "rgb"
|
|
38
38
|
: relativeKeys.slice(-3);
|
|
39
|
-
const allComponents = [rExp, gExp, bExp, aExp];
|
|
40
|
-
const components = getColorComponents(original);
|
|
39
|
+
const allComponents = aExp == null ? [rExp, gExp, bExp] : [rExp, gExp, bExp, aExp];
|
|
40
|
+
const components = getColorComponents(arena, original);
|
|
41
41
|
const validKeys = names.split("");
|
|
42
42
|
let val = "";
|
|
43
43
|
if (components != null) {
|
|
@@ -50,8 +50,8 @@ function parseRelativeColorComponents(relativeKeys, original, rExp, gExp, bExp,
|
|
|
50
50
|
if (component == null) {
|
|
51
51
|
continue;
|
|
52
52
|
}
|
|
53
|
-
if (component
|
|
54
|
-
val = component
|
|
53
|
+
if (arena.dataTypes[component] == EnumToken.IdenTokenType) {
|
|
54
|
+
val = arena.strings[arena.stringValue[component]];
|
|
55
55
|
if (
|
|
56
56
|
// @ts-expect-error
|
|
57
57
|
typeof Math[val.toUpperCase()] !== "number" &&
|
|
@@ -71,11 +71,12 @@ function parseRelativeColorComponents(relativeKeys, original, rExp, gExp, bExp,
|
|
|
71
71
|
}
|
|
72
72
|
continue;
|
|
73
73
|
}
|
|
74
|
-
if (component
|
|
75
|
-
equalsIgnoreCase("calc", component
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
74
|
+
if (arena.dataTypes[component] === EnumToken.MathFunctionTokenType &&
|
|
75
|
+
equalsIgnoreCase("calc", arena.strings[arena.stringValue[component]])) {
|
|
76
|
+
// inlined: arena.walkValues(arena.tokens(component))
|
|
77
|
+
for (const value of new ValueWalker(arena, arena.tokens(component))) {
|
|
78
|
+
if (arena.dataTypes[value] == EnumToken.IdenTokenType) {
|
|
79
|
+
val = arena.strings[arena.stringValue[value]].toLowerCase();
|
|
79
80
|
if (
|
|
80
81
|
// @ts-expect-error
|
|
81
82
|
typeof Math[val.toUpperCase()] !== "number" &&
|
|
@@ -97,96 +98,49 @@ function parseRelativeColorComponents(relativeKeys, original, rExp, gExp, bExp,
|
|
|
97
98
|
}
|
|
98
99
|
}
|
|
99
100
|
}
|
|
100
|
-
const converted =
|
|
101
|
+
const converted = convertColor(arena, original, ColorType[relativeKeys.toUpperCase().replaceAll("-", "_")]);
|
|
101
102
|
if (converted == null) {
|
|
102
103
|
return null;
|
|
103
104
|
}
|
|
104
|
-
const children = converted.
|
|
105
|
-
[r, g, b, alpha] = converted
|
|
105
|
+
const children = arena.tokens(converted).filter((t) => ![EnumToken.WhitespaceTokenType, EnumToken.LiteralTokenType, EnumToken.CommentTokenType].includes(arena.dataTypes[t]));
|
|
106
|
+
[r, g, b, alpha] = arena.colorType[converted] == ColorType.COLOR ? children.slice(1) : children;
|
|
106
107
|
values = {
|
|
107
|
-
[names[0]]: getValue(r, converted, names[0]),
|
|
108
|
-
[names[1]]: getValue(g, converted, names[1]), // string,
|
|
109
|
-
[names[2]]: getValue(b, converted, names[2]),
|
|
108
|
+
[names[0]]: getValue(arena, r, converted, names[0]),
|
|
109
|
+
[names[1]]: getValue(arena, g, converted, names[1]), // string,
|
|
110
|
+
[names[2]]: getValue(arena, b, converted, names[2]),
|
|
110
111
|
// @ts-ignore
|
|
111
112
|
alpha: alpha == null
|
|
112
|
-
?
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
[LOCEND]: b[LOCEND],
|
|
118
|
-
}
|
|
119
|
-
: alpha.typ == EnumToken.IdenTokenType && alpha.val == "none"
|
|
120
|
-
? {
|
|
121
|
-
typ: EnumToken.NumberTokenType,
|
|
122
|
-
val: 0,
|
|
123
|
-
[LOCSRCID]: alpha[LOCSRCID],
|
|
124
|
-
[LOCSTA]: alpha[LOCSTA],
|
|
125
|
-
[LOCEND]: alpha[LOCEND],
|
|
126
|
-
}
|
|
127
|
-
: alpha.typ == EnumToken.PercentageTokenType
|
|
128
|
-
? {
|
|
129
|
-
typ: EnumToken.NumberTokenType,
|
|
130
|
-
val: getNumber(alpha),
|
|
131
|
-
[LOCSRCID]: alpha[LOCSRCID],
|
|
132
|
-
[LOCSTA]: alpha[LOCSTA],
|
|
133
|
-
[LOCEND]: alpha[LOCEND],
|
|
134
|
-
}
|
|
113
|
+
? numberToken(arena, 1, b)
|
|
114
|
+
: arena.dataTypes[alpha] == EnumToken.IdenTokenType && arena.strings[arena.stringValue[alpha]] == "none"
|
|
115
|
+
? numberToken(arena, 0, alpha)
|
|
116
|
+
: arena.dataTypes[alpha] == EnumToken.PercentageTokenType
|
|
117
|
+
? numberToken(arena, getNumber(arena, alpha), alpha)
|
|
135
118
|
: alpha,
|
|
136
119
|
};
|
|
137
120
|
keys = {
|
|
138
|
-
[names[0]]: getValue(rExp, converted, names[0]),
|
|
139
|
-
[names[1]]: getValue(gExp, converted, names[1]),
|
|
140
|
-
[names[2]]: getValue(bExp, converted, names[2]),
|
|
121
|
+
[names[0]]: getValue(arena, rExp, converted, names[0]),
|
|
122
|
+
[names[1]]: getValue(arena, gExp, converted, names[1]),
|
|
123
|
+
[names[2]]: getValue(arena, bExp, converted, names[2]),
|
|
141
124
|
// @ts-ignore
|
|
142
|
-
alpha: getValue(aExp == null
|
|
143
|
-
?
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
[LOCSRCID]: bExp[LOCSRCID],
|
|
147
|
-
[LOCSTA]: bExp[LOCSTA],
|
|
148
|
-
[LOCEND]: bExp[LOCEND],
|
|
149
|
-
}
|
|
150
|
-
: aExp.typ == EnumToken.IdenTokenType && aExp.val == "none"
|
|
151
|
-
? {
|
|
152
|
-
typ: EnumToken.NumberTokenType,
|
|
153
|
-
val: 0,
|
|
154
|
-
[LOCSRCID]: aExp[LOCSRCID],
|
|
155
|
-
[LOCSTA]: aExp[LOCSTA],
|
|
156
|
-
[LOCEND]: aExp[LOCEND],
|
|
157
|
-
}
|
|
125
|
+
alpha: getValue(arena, aExp == null
|
|
126
|
+
? numberToken(arena, 1, bExp)
|
|
127
|
+
: arena.dataTypes[aExp] == EnumToken.IdenTokenType && arena.strings[arena.stringValue[aExp]] == "none"
|
|
128
|
+
? numberToken(arena, 0, aExp)
|
|
158
129
|
: aExp),
|
|
159
130
|
};
|
|
160
|
-
const result = computeComponentValue(keys, values);
|
|
131
|
+
const result = computeComponentValue(arena, keys, values);
|
|
161
132
|
if (result != null) {
|
|
162
133
|
if ("hsl" == relativeKeys) {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
// @ts-ignore
|
|
166
|
-
val: result.s.val * 100,
|
|
167
|
-
});
|
|
168
|
-
Object.assign(result.l, {
|
|
169
|
-
typ: EnumToken.PercentageTokenType,
|
|
170
|
-
// @ts-ignore
|
|
171
|
-
val: result.l.val * 100,
|
|
172
|
-
});
|
|
134
|
+
toPercentage(arena, result.s);
|
|
135
|
+
toPercentage(arena, result.l);
|
|
173
136
|
}
|
|
174
137
|
else if ("hwb" == relativeKeys) {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
typ: EnumToken.PercentageTokenType,
|
|
178
|
-
// @ts-ignore
|
|
179
|
-
val: result.w.val * 100,
|
|
180
|
-
});
|
|
181
|
-
Object.assign(result.b, {
|
|
182
|
-
typ: EnumToken.PercentageTokenType,
|
|
183
|
-
// @ts-ignore
|
|
184
|
-
val: result.b.val * 100,
|
|
185
|
-
});
|
|
138
|
+
toPercentage(arena, result.w);
|
|
139
|
+
toPercentage(arena, result.b);
|
|
186
140
|
}
|
|
187
141
|
}
|
|
188
142
|
// @ts-ignore
|
|
189
|
-
if (result?.alpha
|
|
143
|
+
if (arena.dataTypes[result?.alpha] == EnumToken.NumberTokenType && arena.numericValue[result.alpha] === 1) {
|
|
190
144
|
// @ts-ignore
|
|
191
145
|
const { alpha, ...components } = result;
|
|
192
146
|
return components;
|
|
@@ -200,23 +154,17 @@ function parseRelativeColorComponents(relativeKeys, original, rExp, gExp, bExp,
|
|
|
200
154
|
* @param component
|
|
201
155
|
* @returns
|
|
202
156
|
*/
|
|
203
|
-
function getValue(t, converted, component) {
|
|
204
|
-
if (t
|
|
205
|
-
let value = getNumber(t);
|
|
157
|
+
function getValue(arena, t, converted, component) {
|
|
158
|
+
if (arena.dataTypes[t] == EnumToken.PercentageTokenType) {
|
|
159
|
+
let value = getNumber(arena, t);
|
|
206
160
|
if (converted != null) {
|
|
207
|
-
let colorSpace = ColorType[converted
|
|
161
|
+
let colorSpace = ColorType[arena.colorType[converted]].toLowerCase().replaceAll("-", "_");
|
|
208
162
|
if (colorSpace in colorRange) {
|
|
209
163
|
// @ts-ignore
|
|
210
164
|
value *= colorRange[colorSpace][component].at(-1);
|
|
211
165
|
}
|
|
212
166
|
}
|
|
213
|
-
return
|
|
214
|
-
typ: EnumToken.NumberTokenType,
|
|
215
|
-
val: value,
|
|
216
|
-
[LOCSRCID]: t[LOCSRCID],
|
|
217
|
-
[LOCSTA]: t[LOCSTA],
|
|
218
|
-
[LOCEND]: t[LOCEND],
|
|
219
|
-
};
|
|
167
|
+
return numberToken(arena, value, t);
|
|
220
168
|
}
|
|
221
169
|
return t;
|
|
222
170
|
}
|
|
@@ -226,13 +174,14 @@ function getValue(t, converted, component) {
|
|
|
226
174
|
* @param values
|
|
227
175
|
* @returns
|
|
228
176
|
*/
|
|
229
|
-
function computeComponentValue(expr, values) {
|
|
177
|
+
function computeComponentValue(arena, expr, values) {
|
|
230
178
|
for (const object of [values, expr]) {
|
|
231
179
|
if ("h" in object) {
|
|
232
180
|
// normalize hue
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
181
|
+
// inlined: arena.walkValues([object.h as Token])
|
|
182
|
+
for (const k of new ValueWalker(arena, [object.h])) {
|
|
183
|
+
if (arena.dataTypes[k] == EnumToken.AngleTokenType && arena.strings[arena.unit[k]] == "deg") {
|
|
184
|
+
arena.dataTypes[k] = EnumToken.NumberTokenType;
|
|
236
185
|
}
|
|
237
186
|
}
|
|
238
187
|
}
|
|
@@ -243,60 +192,61 @@ function computeComponentValue(expr, values) {
|
|
|
243
192
|
EnumToken.PercentageTokenType,
|
|
244
193
|
EnumToken.AngleTokenType,
|
|
245
194
|
EnumToken.LengthTokenType,
|
|
246
|
-
].includes(exp
|
|
247
|
-
else if (exp
|
|
248
|
-
expr[key] = values[exp
|
|
195
|
+
].includes(arena.dataTypes[exp])) ;
|
|
196
|
+
else if (arena.dataTypes[exp] == EnumToken.IdenTokenType && arena.strings[arena.stringValue[exp]] in values) {
|
|
197
|
+
expr[key] = values[arena.strings[arena.stringValue[exp]]];
|
|
249
198
|
}
|
|
250
|
-
else if (exp
|
|
251
|
-
(exp
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
});
|
|
199
|
+
else if (arena.dataTypes[exp] == EnumToken.MathFunctionTokenType ||
|
|
200
|
+
(arena.dataTypes[exp] == EnumToken.FunctionTokenType && mathFuncs.includes(arena.strings[arena.stringValue[exp]]))) {
|
|
201
|
+
// inlined: arena.walkValues(arena.tokens(exp))
|
|
202
|
+
for (const value of new ValueWalker(arena, arena.tokens(exp))) {
|
|
203
|
+
if (arena.dataTypes[value] == EnumToken.IdenTokenType) {
|
|
204
|
+
const name = arena.strings[arena.stringValue[value]];
|
|
205
|
+
const replacement = values[name];
|
|
206
|
+
// replace the identifier by its value
|
|
207
|
+
if (replacement != null) {
|
|
208
|
+
arena.copyToken(value, replacement);
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
// @ts-ignore
|
|
212
|
+
const constant = Math[name.toUpperCase()];
|
|
213
|
+
arena.resetToken(value, EnumToken.NumberTokenType, constant);
|
|
214
|
+
}
|
|
267
215
|
}
|
|
268
216
|
}
|
|
269
|
-
const result = (exp
|
|
270
|
-
(exp
|
|
271
|
-
exp
|
|
272
|
-
? evaluateFunc(exp)
|
|
273
|
-
: evaluate(exp
|
|
274
|
-
if (result.length == 1 && result[0]
|
|
217
|
+
const result = (arena.dataTypes[exp] === EnumToken.MathFunctionTokenType ||
|
|
218
|
+
(arena.dataTypes[exp] == EnumToken.FunctionTokenType && mathFuncs.includes(arena.strings[arena.stringValue[exp]]))) &&
|
|
219
|
+
arena.strings[arena.stringValue[exp]] !== "calc"
|
|
220
|
+
? evaluateFunc(arena, exp)
|
|
221
|
+
: evaluate(arena, arena.tokens(exp));
|
|
222
|
+
if (result.length == 1 && arena.dataTypes[result[0]] != EnumToken.BinaryExpressionTokenType) {
|
|
275
223
|
expr[key] = result[0];
|
|
276
224
|
}
|
|
277
225
|
}
|
|
278
226
|
}
|
|
279
227
|
return expr;
|
|
280
228
|
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
return;
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
else {
|
|
295
|
-
pr.chi.splice(pr.chi.indexOf(val), 1, newValue);
|
|
296
|
-
return;
|
|
297
|
-
}
|
|
298
|
-
}
|
|
229
|
+
/**
|
|
230
|
+
* allocate a number token with the location of another token
|
|
231
|
+
* @param arena
|
|
232
|
+
* @param val
|
|
233
|
+
* @param location
|
|
234
|
+
*/
|
|
235
|
+
function numberToken(arena, val, location) {
|
|
236
|
+
const token = arena.createToken(EnumToken.NumberTokenType, val);
|
|
237
|
+
if (location != null) {
|
|
238
|
+
arena.copyLocation(token, location);
|
|
299
239
|
}
|
|
240
|
+
return token;
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* convert a number token to a percentage (in place)
|
|
244
|
+
* @param arena
|
|
245
|
+
* @param token
|
|
246
|
+
*/
|
|
247
|
+
function toPercentage(arena, token) {
|
|
248
|
+
arena.numericValue[token] = getNumber(arena, token) * 100;
|
|
249
|
+
arena.dataTypes[token] = EnumToken.PercentageTokenType;
|
|
300
250
|
}
|
|
301
251
|
|
|
302
|
-
export { parseRelativeColorComponents
|
|
252
|
+
export { parseRelativeColorComponents };
|
|
@@ -2,7 +2,7 @@
|
|
|
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
8
|
import { color2srgbvalues, minmax } from './color.js';
|
|
@@ -14,83 +14,80 @@ import { COLORS_NAMES } from '../constants.js';
|
|
|
14
14
|
function srgb2rgb(value) {
|
|
15
15
|
return minmax(Math.round(value * 255), 0, 255);
|
|
16
16
|
}
|
|
17
|
-
function hex2RgbToken(token) {
|
|
18
|
-
return rgb2RgbToken(hex2rgbvalues(token));
|
|
17
|
+
function hex2RgbToken(arena, token) {
|
|
18
|
+
return rgb2RgbToken(arena, hex2rgbvalues(arena, token));
|
|
19
19
|
}
|
|
20
|
-
function hsl2RgbToken(token) {
|
|
21
|
-
const values = hsl2rgbvalues(token);
|
|
20
|
+
function hsl2RgbToken(arena, token) {
|
|
21
|
+
const values = hsl2rgbvalues(arena, token);
|
|
22
22
|
if (values == null) {
|
|
23
23
|
return null;
|
|
24
24
|
}
|
|
25
|
-
return rgb2RgbToken(values);
|
|
25
|
+
return rgb2RgbToken(arena, values);
|
|
26
26
|
}
|
|
27
|
-
function hwb2RgbToken(token) {
|
|
28
|
-
const values = hwb2rgbvalues(token);
|
|
27
|
+
function hwb2RgbToken(arena, token) {
|
|
28
|
+
const values = hwb2rgbvalues(arena, token);
|
|
29
29
|
if (values == null) {
|
|
30
30
|
return null;
|
|
31
31
|
}
|
|
32
|
-
return rgb2RgbToken(values);
|
|
32
|
+
return rgb2RgbToken(arena, values);
|
|
33
33
|
}
|
|
34
|
-
function cmyk2RgbToken(token) {
|
|
35
|
-
const values = cmyk2rgbvalues(token);
|
|
34
|
+
function cmyk2RgbToken(arena, token) {
|
|
35
|
+
const values = cmyk2rgbvalues(arena, token);
|
|
36
36
|
if (values == null) {
|
|
37
37
|
return null;
|
|
38
38
|
}
|
|
39
|
-
return rgb2RgbToken(values);
|
|
39
|
+
return rgb2RgbToken(arena, values);
|
|
40
40
|
}
|
|
41
|
-
function oklab2RgbToken(token) {
|
|
42
|
-
const values = oklab2rgbvalues(token);
|
|
41
|
+
function oklab2RgbToken(arena, token) {
|
|
42
|
+
const values = oklab2rgbvalues(arena, token);
|
|
43
43
|
if (values == null) {
|
|
44
44
|
return null;
|
|
45
45
|
}
|
|
46
|
-
return rgb2RgbToken(values);
|
|
46
|
+
return rgb2RgbToken(arena, values);
|
|
47
47
|
}
|
|
48
|
-
function oklch2RgbToken(token) {
|
|
49
|
-
const values = oklch2rgbvalues(token);
|
|
48
|
+
function oklch2RgbToken(arena, token) {
|
|
49
|
+
const values = oklch2rgbvalues(arena, token);
|
|
50
50
|
if (values == null) {
|
|
51
51
|
return null;
|
|
52
52
|
}
|
|
53
|
-
return rgb2RgbToken(values);
|
|
53
|
+
return rgb2RgbToken(arena, values);
|
|
54
54
|
}
|
|
55
|
-
function lab2RgbToken(token) {
|
|
56
|
-
const values = lab2rgbvalues(token);
|
|
55
|
+
function lab2RgbToken(arena, token) {
|
|
56
|
+
const values = lab2rgbvalues(arena, token);
|
|
57
57
|
if (values == null) {
|
|
58
58
|
return null;
|
|
59
59
|
}
|
|
60
|
-
return rgb2RgbToken(values);
|
|
60
|
+
return rgb2RgbToken(arena, values);
|
|
61
61
|
}
|
|
62
|
-
function lch2RgbToken(token) {
|
|
63
|
-
const values = lch2rgbvalues(token);
|
|
62
|
+
function lch2RgbToken(arena, token) {
|
|
63
|
+
const values = lch2rgbvalues(arena, token);
|
|
64
64
|
if (values == null) {
|
|
65
65
|
return null;
|
|
66
66
|
}
|
|
67
|
-
return rgb2RgbToken(values);
|
|
67
|
+
return rgb2RgbToken(arena, values);
|
|
68
68
|
}
|
|
69
|
-
function color2RgbToken(token) {
|
|
70
|
-
const values = color2srgbvalues(token);
|
|
69
|
+
function color2RgbToken(arena, token) {
|
|
70
|
+
const values = color2srgbvalues(arena, token);
|
|
71
71
|
if (values == null) {
|
|
72
72
|
return null;
|
|
73
73
|
}
|
|
74
|
-
return rgb2RgbToken(values.map((t, index) => (index == 3 ? t : srgb2rgb(t))));
|
|
74
|
+
return rgb2RgbToken(arena, values.map((t, index) => (index == 3 ? t : srgb2rgb(t))));
|
|
75
75
|
}
|
|
76
|
-
function rgb2RgbToken(values) {
|
|
76
|
+
function rgb2RgbToken(arena, values) {
|
|
77
77
|
const chi = [
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
78
|
+
arena.createToken(EnumToken.NumberTokenType, values[0]),
|
|
79
|
+
arena.createToken(EnumToken.NumberTokenType, values[1]),
|
|
80
|
+
arena.createToken(EnumToken.NumberTokenType, values[2]),
|
|
81
81
|
];
|
|
82
82
|
if (values.length == 4) {
|
|
83
|
-
chi.push(
|
|
83
|
+
chi.push(arena.createToken(EnumToken.PercentageTokenType, values[3] * 100));
|
|
84
84
|
}
|
|
85
|
-
return
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
}
|
|
92
|
-
function hex2rgbvalues(token) {
|
|
93
|
-
const value = expandHexValue(token.kin == ColorType.LIT ? COLORS_NAMES[token.val.toLowerCase()] : token.val);
|
|
85
|
+
return arena.createColor("rgb", ColorType.RGB, chi);
|
|
86
|
+
}
|
|
87
|
+
function hex2rgbvalues(arena, token) {
|
|
88
|
+
const value = expandHexValue(arena.colorType[token] == ColorType.LIT
|
|
89
|
+
? COLORS_NAMES[arena.strings[arena.stringValue[token]].toLowerCase()]
|
|
90
|
+
: arena.strings[arena.stringValue[token]]);
|
|
94
91
|
const rgb = [];
|
|
95
92
|
for (let i = 1; i < value.length; i += 2) {
|
|
96
93
|
rgb.push(parseInt(value.slice(i, i + 2), 16));
|
|
@@ -105,11 +102,11 @@ function hex2rgbvalues(token) {
|
|
|
105
102
|
}
|
|
106
103
|
return rgb;
|
|
107
104
|
}
|
|
108
|
-
function hwb2rgbvalues(token) {
|
|
109
|
-
return hwb2srgbvalues(token)?.map?.((t, index) => (index == 3 ? t : srgb2rgb(t))) ?? null;
|
|
105
|
+
function hwb2rgbvalues(arena, token) {
|
|
106
|
+
return hwb2srgbvalues(arena, token)?.map?.((t, index) => (index == 3 ? t : srgb2rgb(t))) ?? null;
|
|
110
107
|
}
|
|
111
|
-
function hsl2rgbvalues(token) {
|
|
112
|
-
let { h, s, l, a } = hslvalues(token) ?? {};
|
|
108
|
+
function hsl2rgbvalues(arena, token) {
|
|
109
|
+
let { h, s, l, a } = hslvalues(arena, token) ?? {};
|
|
113
110
|
if (h == null || s == null || l == null) {
|
|
114
111
|
return null;
|
|
115
112
|
}
|
|
@@ -117,27 +114,27 @@ function hsl2rgbvalues(token) {
|
|
|
117
114
|
.map((t) => minmax(Math.round(t * 255), 0, 255))
|
|
118
115
|
.concat(a == 1 || a == null ? [] : [a]);
|
|
119
116
|
}
|
|
120
|
-
function hsl2srgbvalues(token) {
|
|
121
|
-
let { h, s, l, a } = hslvalues(token) ?? {};
|
|
117
|
+
function hsl2srgbvalues(arena, token) {
|
|
118
|
+
let { h, s, l, a } = hslvalues(arena, token) ?? {};
|
|
122
119
|
if (h == null || s == null || l == null) {
|
|
123
120
|
return null;
|
|
124
121
|
}
|
|
125
122
|
return hslvalues2srgbvalues(h, s, l).concat(a == 1 || a == null ? [] : [a]);
|
|
126
123
|
}
|
|
127
|
-
function cmyk2rgbvalues(token) {
|
|
128
|
-
return cmyk2srgbvalues(token)?.map?.((t, index) => (index == 3 ? t : srgb2rgb(t))) ?? null;
|
|
124
|
+
function cmyk2rgbvalues(arena, token) {
|
|
125
|
+
return cmyk2srgbvalues(arena, token)?.map?.((t, index) => (index == 3 ? t : srgb2rgb(t))) ?? null;
|
|
129
126
|
}
|
|
130
|
-
function oklab2rgbvalues(token) {
|
|
131
|
-
return oklab2srgbvalues(token)?.map?.((t, index) => (index == 3 ? t : srgb2rgb(t))) ?? null;
|
|
127
|
+
function oklab2rgbvalues(arena, token) {
|
|
128
|
+
return oklab2srgbvalues(arena, token)?.map?.((t, index) => (index == 3 ? t : srgb2rgb(t))) ?? null;
|
|
132
129
|
}
|
|
133
|
-
function oklch2rgbvalues(token) {
|
|
134
|
-
return oklch2srgbvalues(token)?.map?.((t, index) => (index == 3 ? t : srgb2rgb(t))) ?? null;
|
|
130
|
+
function oklch2rgbvalues(arena, token) {
|
|
131
|
+
return oklch2srgbvalues(arena, token)?.map?.((t, index) => (index == 3 ? t : srgb2rgb(t))) ?? null;
|
|
135
132
|
}
|
|
136
|
-
function lab2rgbvalues(token) {
|
|
137
|
-
return lab2srgbvalues(token)?.map?.((t, index) => (index == 3 ? t : srgb2rgb(t))) ?? null;
|
|
133
|
+
function lab2rgbvalues(arena, token) {
|
|
134
|
+
return lab2srgbvalues(arena, token)?.map?.((t, index) => (index == 3 ? t : srgb2rgb(t))) ?? null;
|
|
138
135
|
}
|
|
139
|
-
function lch2rgbvalues(token) {
|
|
140
|
-
return lch2srgbvalues(token)?.map?.((t, index) => (index == 3 ? t : srgb2rgb(t))) ?? null;
|
|
136
|
+
function lch2rgbvalues(arena, token) {
|
|
137
|
+
return lch2srgbvalues(arena, token)?.map?.((t, index) => (index == 3 ? t : srgb2rgb(t))) ?? null;
|
|
141
138
|
}
|
|
142
139
|
|
|
143
140
|
export { cmyk2RgbToken, cmyk2rgbvalues, color2RgbToken, hex2RgbToken, hex2rgbvalues, hsl2RgbToken, hsl2rgbvalues, hsl2srgbvalues, hwb2RgbToken, hwb2rgbvalues, lab2RgbToken, lab2rgbvalues, lch2RgbToken, lch2rgbvalues, oklab2RgbToken, oklab2rgbvalues, oklch2RgbToken, oklch2rgbvalues, srgb2rgb };
|