@tbela99/css-parser 2.1.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +66 -1
- package/README.md +14 -7
- package/architecture.png +0 -0
- package/dist/{src/lib/ast → ast}/arena-node.js +10 -10
- package/dist/{src/lib/ast → ast}/arena.js +304 -139
- package/dist/{src/lib/ast → ast}/cache.js +40 -24
- package/dist/{src/lib/ast → ast}/features/calc.js +11 -9
- package/dist/{src/lib/ast → ast}/features/if.js +6 -6
- package/dist/ast/features/index.d.ts +7 -0
- package/dist/{src/lib/ast → ast}/features/index.js +3 -2
- package/dist/{src/lib/ast → ast}/features/inlinecssvariables.js +12 -10
- package/dist/ast/features/mixin.js +953 -0
- package/dist/{src/lib/ast → ast}/features/prefix.js +39 -39
- package/dist/ast/features/shorthand.js +129 -0
- package/dist/ast/features/transform.js +188 -0
- package/dist/{src/lib/ast → ast}/features/type.js +2 -2
- package/dist/{src/lib/ast → ast}/math/expression.js +24 -24
- package/dist/{src/lib/ast → ast}/math/math.js +2 -2
- package/dist/{src/lib/ast → ast}/minify.js +745 -201
- package/dist/ast/mixins/custom-function.js +393 -0
- package/dist/ast/mixins/utils.js +53 -0
- package/dist/ast/mixins/variables.js +678 -0
- package/dist/ast/strings.js +910 -0
- package/dist/ast/transform/compute.js +560 -0
- package/dist/ast/transform/matrix.js +67 -0
- package/dist/ast/transform/minify.js +325 -0
- package/dist/ast/transform/perspective.js +24 -0
- package/dist/{src/lib/ast → ast}/transform/rotate.js +16 -7
- package/dist/ast/transform/scale.js +43 -0
- package/dist/ast/transform/skew.js +38 -0
- package/dist/ast/transform/translate.js +56 -0
- package/dist/ast/transform/utils.js +390 -0
- package/dist/{src/lib/ast → ast}/types.js +2 -2
- package/dist/{src/lib/ast → ast}/walk.js +2 -2
- package/dist/{src/data → data}/properties.json.js +86 -2
- package/dist/{src/data → data}/validation.json.js +30 -21
- package/dist/{src/lib/fs → fs}/resolve.js +2 -2
- package/dist/index-umd-web.js +19727 -10719
- package/dist/index.d.ts +738 -100
- package/dist/{src/node.js → node.js} +70 -52
- package/dist/{src/lib/parser → parser}/declaration/list.js +25 -8
- package/dist/{src/lib/parser → parser}/declaration/map.js +15 -15
- package/dist/{src/lib/parser → parser}/declaration/set.js +5 -5
- package/dist/{src/lib/parser → parser}/input.js +18 -3
- package/dist/{src/lib/parser → parser}/linesmap.js +2 -2
- package/dist/{src/lib/parser → parser}/parse.js +444 -278
- package/dist/{src/lib/parser → parser}/source.js +3 -3
- package/dist/{src/lib/parser → parser}/tokenize.js +480 -312
- package/dist/{src/lib/parser → parser}/utils/angle.js +3 -3
- package/dist/{src/lib/parser → parser}/utils/at-rule-container.js +12 -12
- package/dist/{src/lib/parser → parser}/utils/at-rule-font-feature-values.js +3 -3
- package/dist/{src/lib/parser → parser}/utils/at-rule-generic.js +7 -7
- package/dist/{src/lib/parser → parser}/utils/at-rule-import.js +10 -10
- package/dist/{src/lib/parser → parser}/utils/at-rule-media.js +14 -14
- package/dist/{src/lib/parser → parser}/utils/at-rule-support.js +9 -14
- package/dist/{src/lib/parser → parser}/utils/at-rule-when-else.js +6 -6
- package/dist/{src/lib/parser → parser}/utils/at-rule.js +3 -3
- package/dist/{src/lib/parser → parser}/utils/base64.js +2 -2
- package/dist/parser/utils/cache.js +53 -0
- package/dist/parser/utils/canonical.js +78 -0
- package/dist/parser/utils/codes.js +93 -0
- package/dist/{src/lib/parser → parser}/utils/config.js +3 -3
- package/dist/{src/lib/parser → parser}/utils/declaration.js +31 -26
- package/dist/{src/lib/parser → parser}/utils/eq.js +2 -2
- package/dist/{src/lib/parser → parser}/utils/hash.js +2 -2
- package/dist/{src/lib/parser → parser}/utils/selector.js +339 -87
- package/dist/{src/lib/parser → parser}/utils/text.js +2 -2
- package/dist/{src/lib/parser → parser}/utils/token.js +2 -2
- package/dist/{src/lib/parser → parser}/utils/type.js +9 -7
- package/dist/{src/lib/printer/sourcemap → printer}/lib/codec.js +2 -2
- package/dist/{src/lib/printer → printer}/render.js +582 -153
- package/dist/printer/result.js +148 -0
- package/dist/{src/lib/printer/sourcemap → printer}/sourcemap.js +4 -3
- package/dist/printer/writer.js +252 -0
- package/dist/{src/lib/syntax → syntax}/color/a98rgb.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/alpha.js +10 -10
- package/dist/{src/lib/syntax → syntax}/color/cmyk.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/color-mix.js +18 -18
- package/dist/{src/lib/syntax → syntax}/color/color.js +18 -18
- package/dist/{src/lib/syntax → syntax}/color/hex.js +3 -3
- package/dist/{src/lib/syntax → syntax}/color/hsl.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/hwb.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/lab.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/lch.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/oklab.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/oklch.js +3 -3
- package/dist/{src/lib/syntax → syntax}/color/p3.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/prophotorgb.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/rec2020.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/relative-color.js +14 -14
- package/dist/{src/lib/syntax → syntax}/color/rgb.js +4 -4
- package/dist/{src/lib/syntax → syntax}/color/srgb.js +5 -5
- package/dist/{src/lib/syntax → syntax}/color/utils/color-type.js +4 -4
- package/dist/{src/lib/syntax → syntax}/color/utils/colorspace.js +10 -10
- package/dist/{src/lib/syntax → syntax}/color/utils/components.js +5 -5
- package/dist/{src/lib/syntax → syntax}/color/utils/distance.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/utils/make-color.js +3 -3
- package/dist/{src/lib/syntax → syntax}/color/utils/matrix.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/xyz.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/xyzd50.js +2 -2
- package/dist/{src/lib/syntax → syntax}/constants.js +27 -4
- package/dist/{src/lib/syntax → syntax}/syntax.js +37 -29
- package/dist/{src/types.js → types.js} +2 -2
- package/dist/{src/utils → utils}/sync.js +12 -7
- package/dist/validation/compiled/builtins.js +32 -0
- package/dist/validation/compiled/runtime.js +705 -0
- package/dist/validation/compiled/syntaxes.js +1620 -0
- package/dist/{src/lib/validation → validation}/config.js +63 -16
- package/dist/validation/first.js +574 -0
- package/dist/{src/lib/validation → validation}/match.js +376 -161
- package/dist/{src/lib/validation → validation}/parser/parse.js +2 -2
- package/dist/{src/lib/validation → validation}/parser/typedef.js +2 -2
- package/dist/{src/lib/validation → validation}/utils/list.js +3 -19
- package/dist/version.js +13 -0
- package/dist/{src/web.js → web.js} +68 -55
- package/package.json +15 -13
- package/dist/package.json.js +0 -10
- package/dist/src/lib/ast/features/shorthand.js +0 -95
- package/dist/src/lib/ast/features/transform.js +0 -90
- package/dist/src/lib/ast/transform/compute.js +0 -362
- package/dist/src/lib/ast/transform/matrix.js +0 -88
- package/dist/src/lib/ast/transform/minify.js +0 -267
- package/dist/src/lib/ast/transform/perspective.js +0 -16
- package/dist/src/lib/ast/transform/scale.js +0 -39
- package/dist/src/lib/ast/transform/skew.js +0 -30
- package/dist/src/lib/ast/transform/translate.js +0 -39
- package/dist/src/lib/ast/transform/utils.js +0 -287
- package/dist/src/lib/parser/utils/cache.js +0 -54
- package/dist/types.d.ts +0 -21
- package/dist/utils/sync.d.ts +0 -16
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
/*
|
|
2
|
-
CSS parser, minifier and validator for node and the browser
|
|
2
|
+
CSS parser, minifier and validator for node, deno, bun and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version: 2.
|
|
5
|
+
Version: 2.3.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { EnumToken, EnumAstNodeStatus } from '../../ast/types.js';
|
|
9
|
+
import { StringBuilder } from '../../ast/strings.js';
|
|
9
10
|
import { renderValue } from '../../printer/render.js';
|
|
10
|
-
import {
|
|
11
|
+
import { isFunctionDefType, tokensfuncDefMap, pseudoElements, combinators } from '../../syntax/constants.js';
|
|
11
12
|
import { isHash, isIdent } from '../../syntax/syntax.js';
|
|
12
|
-
import { getParsedSyntax, getSyntaxRule
|
|
13
|
-
import { matchAllSyntaxes, createValidationContext, trimArray, matchSelectorSyntax } from '../../validation/match.js';
|
|
13
|
+
import { getParsedSyntax, getSyntaxRule } from '../../validation/config.js';
|
|
14
|
+
import { matchAllSyntaxes, createValidationContext, trimArray, selectorFunctionSyntax, matchSelectorSyntax } from '../../validation/match.js';
|
|
14
15
|
import { ValidationSyntaxGroupEnum, ValidationTokenEnum } from '../../validation/parser/typedef.js';
|
|
15
16
|
import { splitTokenList } from '../../validation/utils/list.js';
|
|
16
17
|
import { trimWhiteSpace } from '../parse.js';
|
|
@@ -31,7 +32,7 @@ function minifyNthExpression(arena, func) {
|
|
|
31
32
|
continue;
|
|
32
33
|
}
|
|
33
34
|
if (arena.dataTypes[chi[index]] === EnumToken.IdenTokenType &&
|
|
34
|
-
equalsIgnoreCase("of", arena.
|
|
35
|
+
equalsIgnoreCase("of", arena.resolveString(arena.stringValue[chi[index]]))) {
|
|
35
36
|
index--;
|
|
36
37
|
break;
|
|
37
38
|
}
|
|
@@ -70,16 +71,16 @@ function minifyNthExpression(arena, func) {
|
|
|
70
71
|
}
|
|
71
72
|
if (list.length === 1) {
|
|
72
73
|
if (arena.dataTypes[list[0]] === EnumToken.IdenTokenType &&
|
|
73
|
-
equalsIgnoreCase("-n", arena.
|
|
74
|
+
equalsIgnoreCase("-n", arena.resolveString(arena.stringValue[list[0]]))) {
|
|
74
75
|
// inlined: arena.setStr(list[0], "n")
|
|
75
76
|
arena.stringValue[list[0]] = arena.internString("n");
|
|
76
77
|
}
|
|
77
78
|
}
|
|
78
79
|
if (list.length === 3) {
|
|
79
80
|
if (arena.dataTypes[list[0]] === EnumToken.IdenTokenType &&
|
|
80
|
-
("n" === arena.
|
|
81
|
-
"-n" === arena.
|
|
82
|
-
"+n" === arena.
|
|
81
|
+
("n" === arena.resolveString(arena.stringValue[list[0]]) ||
|
|
82
|
+
"-n" === arena.resolveString(arena.stringValue[list[0]]) ||
|
|
83
|
+
"+n" === arena.resolveString(arena.stringValue[list[0]]))) {
|
|
83
84
|
if (arena.dataTypes[list[1]] === EnumToken.NextSiblingCombinatorTokenType) {
|
|
84
85
|
if (arena.dataTypes[list[2]] === EnumToken.NumberTokenType && 0 === arena.numericValue[list[2]]) {
|
|
85
86
|
// inlined: arena.setStr(list[0], "n")
|
|
@@ -95,7 +96,7 @@ function minifyNthExpression(arena, func) {
|
|
|
95
96
|
if (token != null) {
|
|
96
97
|
if (arena.dataTypes[token] === EnumToken.IdenTokenType ||
|
|
97
98
|
arena.dataTypes[token] === EnumToken.LiteralTokenType) {
|
|
98
|
-
const val = arena.
|
|
99
|
+
const val = arena.resolveString(arena.stringValue[token]);
|
|
99
100
|
if (arena.dataTypes[token] === EnumToken.IdenTokenType && (val === "odd" || val === "even")) {
|
|
100
101
|
if (val === "even") {
|
|
101
102
|
arena.resetToken(token, EnumToken.DimensionTokenType, 2, "n");
|
|
@@ -156,7 +157,7 @@ function splitUnicodeRanges(arena, tokens) {
|
|
|
156
157
|
for (let i = 0; i < tokens.length; i++) {
|
|
157
158
|
const token = tokens[i];
|
|
158
159
|
if (arena.dataTypes[token] === EnumToken.UniCodeRange) {
|
|
159
|
-
const text = arena.
|
|
160
|
+
const text = arena.resolveString(arena.stringValue[token]);
|
|
160
161
|
const rest = text.slice(2);
|
|
161
162
|
const split = [
|
|
162
163
|
arena.createToken(EnumToken.IdenTokenType, text.charAt(0)),
|
|
@@ -203,7 +204,7 @@ function parseSelector(arena, tokens, context, options, errors) {
|
|
|
203
204
|
arena.dataTypes[token] !== EnumToken.CommentTokenType);
|
|
204
205
|
if (filtered.length === 1) {
|
|
205
206
|
if (arena.dataTypes[filtered[0]] === EnumToken.IdenTokenType &&
|
|
206
|
-
"from" === arena.
|
|
207
|
+
"from" === arena.resolveString(arena.stringValue[filtered[0]]).toLowerCase()) {
|
|
207
208
|
arena.resetToken(filtered[0], EnumToken.PercentageTokenType, 0);
|
|
208
209
|
}
|
|
209
210
|
else if (arena.dataTypes[filtered[0]] === EnumToken.PercentageTokenType &&
|
|
@@ -238,34 +239,25 @@ function parseSelector(arena, tokens, context, options, errors) {
|
|
|
238
239
|
let index;
|
|
239
240
|
let parent = context;
|
|
240
241
|
let nested = false;
|
|
241
|
-
let val;
|
|
242
242
|
if (contextType !== EnumToken.StyleSheetNodeType && contextType !== EnumToken.RuleNodeType) {
|
|
243
243
|
allowed = false;
|
|
244
244
|
if (contextType === EnumToken.AtRuleNodeType) {
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
const rules = syntaxRule?.getBlockRules?.();
|
|
249
|
-
if (rules != null) {
|
|
250
|
-
for (const rule of rules) {
|
|
251
|
-
if (ValidationTokenEnum.PropertyType === rule.typ) {
|
|
252
|
-
if ("block-contents" === rule.val ||
|
|
253
|
-
"rule-list" === rule.val) {
|
|
254
|
-
allowed = true;
|
|
255
|
-
break;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
}
|
|
245
|
+
// computed once per at-rule name: the name is not concatenated for each selector
|
|
246
|
+
atRuleArena = arena;
|
|
247
|
+
allowed = arena.stringDerived(arena.name[context], 3 /* StringDerived.AtRuleAcceptsRules */, atRuleAcceptsRules);
|
|
261
248
|
}
|
|
262
249
|
}
|
|
263
250
|
while (parent != null && parent > 0) {
|
|
264
|
-
if (arena.dataTypes[parent] === EnumToken.AtRuleNodeType && "media" === arena.
|
|
251
|
+
if (arena.dataTypes[parent] === EnumToken.AtRuleNodeType && "media" === arena.resolveString(arena.name[parent])) {
|
|
265
252
|
parent = arena.parents[parent];
|
|
266
253
|
continue;
|
|
267
254
|
}
|
|
268
|
-
nested
|
|
255
|
+
// the rules of a mixin body are nested in the rule the mixin is applied to:
|
|
256
|
+
// '&' represents the rule that contains the '@apply' rule
|
|
257
|
+
nested =
|
|
258
|
+
arena.dataTypes[parent] === EnumToken.RuleNodeType ||
|
|
259
|
+
(arena.dataTypes[parent] === EnumToken.AtRuleNodeType &&
|
|
260
|
+
"mixin" === arena.resolveString(arena.name[parent]));
|
|
269
261
|
if (nested) {
|
|
270
262
|
break;
|
|
271
263
|
}
|
|
@@ -279,21 +271,20 @@ function parseSelector(arena, tokens, context, options, errors) {
|
|
|
279
271
|
const next = tokens[read + 1];
|
|
280
272
|
if (arena.dataTypes[next] === EnumToken.IdenTokenType) {
|
|
281
273
|
arena.dataTypes[token] = EnumToken.PseudoElementTokenType;
|
|
282
|
-
//
|
|
283
|
-
arena.stringValue[token] = arena.
|
|
274
|
+
// ":" + name, no string is created
|
|
275
|
+
arena.stringValue[token] = arena.internBuilder(arena.appendString(pseudoBuilder.reset().appendCode(0x3a), arena.stringValue[next]));
|
|
284
276
|
arena.spanLocation(token, token, next);
|
|
285
277
|
all[write++] = token;
|
|
286
278
|
read++;
|
|
287
279
|
continue;
|
|
288
280
|
}
|
|
289
281
|
if (arena.dataTypes[next] === EnumToken.FunctionTokenDefType) {
|
|
290
|
-
|
|
282
|
+
// ":" + name, no string is created. `:name()` is a selector function: looked up once per name
|
|
283
|
+
arena.stringValue[token] = arena.internBuilder(arena.appendString(pseudoBuilder.reset().appendCode(0x3a), arena.stringValue[next]));
|
|
291
284
|
arena.dataTypes[token] =
|
|
292
|
-
|
|
285
|
+
selectorFunctionSyntax(arena, token) != null
|
|
293
286
|
? EnumToken.PseudoClassFunctionTokenDefType
|
|
294
287
|
: arena.dataTypes[next];
|
|
295
|
-
// inlined: arena.setStr(token, val)
|
|
296
|
-
arena.stringValue[token] = arena.internString(val);
|
|
297
288
|
arena.spanLocation(token, token, next);
|
|
298
289
|
all[write++] = token;
|
|
299
290
|
read++;
|
|
@@ -302,24 +293,27 @@ function parseSelector(arena, tokens, context, options, errors) {
|
|
|
302
293
|
}
|
|
303
294
|
if (arena.dataTypes[token] === EnumToken.DoubleColonTokenType) {
|
|
304
295
|
const next = tokens[read + 1];
|
|
305
|
-
val = ":" + arena.strings[arena.stringValue[next]];
|
|
306
296
|
if (arena.dataTypes[next] === EnumToken.IdenTokenType) {
|
|
307
297
|
arena.dataTypes[token] = EnumToken.PseudoClassTokenType;
|
|
308
|
-
//
|
|
309
|
-
|
|
298
|
+
// ":" + name for the pseudo elements with a single colon syntax, "::" + name otherwise. no string is
|
|
299
|
+
// created
|
|
300
|
+
pseudoBuilder.reset().appendCode(0x3a);
|
|
301
|
+
if (!isSingleColonPseudoElement(arena, arena.stringValue[next])) {
|
|
302
|
+
pseudoBuilder.appendCode(0x3a);
|
|
303
|
+
}
|
|
304
|
+
arena.stringValue[token] = arena.internBuilder(arena.appendString(pseudoBuilder, arena.stringValue[next]));
|
|
310
305
|
arena.spanLocation(token, token, next);
|
|
311
306
|
all[write++] = token;
|
|
312
307
|
read++;
|
|
313
308
|
continue;
|
|
314
309
|
}
|
|
315
310
|
if (arena.dataTypes[next] === EnumToken.FunctionTokenDefType) {
|
|
316
|
-
|
|
311
|
+
// "::" + name, no string is created
|
|
312
|
+
arena.stringValue[token] = arena.internBuilder(arena.appendString(pseudoBuilder.reset().appendCode(0x3a).appendCode(0x3a), arena.stringValue[next]));
|
|
317
313
|
arena.dataTypes[token] =
|
|
318
|
-
|
|
314
|
+
selectorFunctionSyntax(arena, token) != null
|
|
319
315
|
? EnumToken.PseudoClassFunctionTokenDefType
|
|
320
316
|
: EnumToken.FunctionTokenDefType;
|
|
321
|
-
// inlined: arena.setStr(token, val)
|
|
322
|
-
arena.stringValue[token] = arena.internString(val);
|
|
323
317
|
arena.spanLocation(token, token, next);
|
|
324
318
|
all[write++] = token;
|
|
325
319
|
read++;
|
|
@@ -327,7 +321,7 @@ function parseSelector(arena, tokens, context, options, errors) {
|
|
|
327
321
|
}
|
|
328
322
|
}
|
|
329
323
|
if (arena.dataTypes[token] === EnumToken.ColorTokenType) {
|
|
330
|
-
if (isHash(arena.
|
|
324
|
+
if (isHash(arena.resolveString(arena.stringValue[token]))) {
|
|
331
325
|
arena.dataTypes[token] = EnumToken.HashTokenType;
|
|
332
326
|
all[write++] = token;
|
|
333
327
|
continue;
|
|
@@ -384,14 +378,14 @@ function parseSelector(arena, tokens, context, options, errors) {
|
|
|
384
378
|
index = tokens.indexOf(func);
|
|
385
379
|
arena.spanLocation(func, func, token);
|
|
386
380
|
tokens.splice(i, 1);
|
|
387
|
-
if (
|
|
381
|
+
if (isFunctionDefType(arena.dataTypes[func])) {
|
|
388
382
|
arena.dataTypes[func] = tokensfuncDefMap.get(arena.dataTypes[func]);
|
|
389
383
|
arena.setTokenList(func, trimArray(arena, tokens.splice(index + 1, i - index - 1)));
|
|
390
384
|
}
|
|
391
385
|
if (result.success && options.minify) {
|
|
392
386
|
// parse an+b
|
|
393
387
|
// an+b produces an ident such as 'n-0', a literal such as '+2n-0' or a list of tokens such as[2n\s?[+-]\s?3]
|
|
394
|
-
const name = arena.
|
|
388
|
+
const name = arena.resolveString(arena.stringValue[func]);
|
|
395
389
|
if (name === ":nth-child" ||
|
|
396
390
|
name === ":nth-last-child" ||
|
|
397
391
|
name === ":nth-of-type" ||
|
|
@@ -417,62 +411,320 @@ function parseSelector(arena, tokens, context, options, errors) {
|
|
|
417
411
|
}
|
|
418
412
|
// options of the rendering of the selector tokens
|
|
419
413
|
const selectorRenderOptions = { minify: false };
|
|
414
|
+
/**
|
|
415
|
+
* builder of the pseudo classes and pseudo elements
|
|
416
|
+
*/
|
|
417
|
+
const pseudoBuilder = new StringBuilder();
|
|
418
|
+
/**
|
|
419
|
+
* the arena of the at-rule names read by {@link atRuleAcceptsRules}: the function passed to the string table is not a
|
|
420
|
+
* closure
|
|
421
|
+
*/
|
|
422
|
+
let atRuleArena = null;
|
|
423
|
+
/**
|
|
424
|
+
* true if the rules are allowed in the block of an at-rule, by name (see {@link StringDerived.AtRuleAcceptsRules})
|
|
425
|
+
* @param id the at-rule name
|
|
426
|
+
*/
|
|
427
|
+
function atRuleAcceptsRules(id) {
|
|
428
|
+
const syntaxRule = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" + atRuleArena.resolveString(id));
|
|
429
|
+
if (syntaxRule?.acceptAnyRule) {
|
|
430
|
+
return true;
|
|
431
|
+
}
|
|
432
|
+
const rules = syntaxRule?.getBlockRules?.();
|
|
433
|
+
if (rules != null) {
|
|
434
|
+
for (const rule of rules) {
|
|
435
|
+
if (ValidationTokenEnum.PropertyType === rule.typ &&
|
|
436
|
+
("block-contents" === rule.val ||
|
|
437
|
+
"rule-list" === rule.val)) {
|
|
438
|
+
return true;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
return false;
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* true if ":" + the string is a pseudo element with a single colon syntax (`:before`). the string is not created
|
|
446
|
+
* @param arena
|
|
447
|
+
* @param id
|
|
448
|
+
*/
|
|
449
|
+
function isSingleColonPseudoElement(arena, id) {
|
|
450
|
+
const length = arena.stringLength(id);
|
|
451
|
+
for (let i = 0; i < pseudoElements.length; i++) {
|
|
452
|
+
const pseudo = pseudoElements[i];
|
|
453
|
+
if (pseudo.length === length + 1 && pseudo.charCodeAt(0) === 0x3a) {
|
|
454
|
+
let k = 0;
|
|
455
|
+
while (k < length && arena.stringCodeAt(id, k) === pseudo.charCodeAt(k + 1)) {
|
|
456
|
+
k++;
|
|
457
|
+
}
|
|
458
|
+
if (k === length) {
|
|
459
|
+
return true;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
return false;
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* builder of the rendered tokens of the current selector
|
|
467
|
+
*/
|
|
468
|
+
const partBuilder = new StringBuilder();
|
|
469
|
+
/**
|
|
470
|
+
* builder of the selector of a rule
|
|
471
|
+
*/
|
|
472
|
+
const selectorBuilder = new StringBuilder();
|
|
473
|
+
/**
|
|
474
|
+
* offsets of the rendered tokens of the current selector in {@link partBuilder}: start and end of each token
|
|
475
|
+
*/
|
|
476
|
+
let partOffsets = new Int32Array(64);
|
|
477
|
+
/**
|
|
478
|
+
* offsets of the distinct selectors in {@link selectorBuilder}: start and end of each selector
|
|
479
|
+
*/
|
|
480
|
+
let selectorOffsets = new Int32Array(16);
|
|
481
|
+
/**
|
|
482
|
+
* true if the string of a token is a combinator. the string is not created
|
|
483
|
+
* @param arena
|
|
484
|
+
* @param token
|
|
485
|
+
*/
|
|
486
|
+
function isCombinatorString(arena, token) {
|
|
487
|
+
if (token === undefined) {
|
|
488
|
+
return false;
|
|
489
|
+
}
|
|
490
|
+
const id = arena.stringValue[token];
|
|
491
|
+
const length = arena.stringLength(id);
|
|
492
|
+
if (length === 0 || length > 2) {
|
|
493
|
+
return false;
|
|
494
|
+
}
|
|
495
|
+
for (let i = 0; i < combinators.length; i++) {
|
|
496
|
+
if (arena.stringEquals(id, combinators[i])) {
|
|
497
|
+
return true;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
return false;
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* append the rendering of a selector token to the builder. the strings of the tokens are copied, no string is created:
|
|
504
|
+
* the tokens that are rendered as their string value, the symbols. the other tokens are rendered with renderValue()
|
|
505
|
+
* @param arena
|
|
506
|
+
* @param token
|
|
507
|
+
* @param builder
|
|
508
|
+
*/
|
|
509
|
+
function appendSelectorToken(arena, token, builder) {
|
|
510
|
+
if (arena.firstToken[token] === 0 && arena.firstChild[token] === 0) {
|
|
511
|
+
switch (arena.dataTypes[token]) {
|
|
512
|
+
case EnumToken.HashTokenType:
|
|
513
|
+
case EnumToken.IdenTokenType:
|
|
514
|
+
case EnumToken.LiteralTokenType:
|
|
515
|
+
case EnumToken.DashedIdenTokenType:
|
|
516
|
+
case EnumToken.ClassSelectorTokenType:
|
|
517
|
+
case EnumToken.PseudoClassTokenType:
|
|
518
|
+
arena.appendString(builder, arena.stringValue[token]);
|
|
519
|
+
return;
|
|
520
|
+
case EnumToken.WhitespaceTokenType:
|
|
521
|
+
case EnumToken.DescendantCombinatorTokenType:
|
|
522
|
+
builder.appendCode(0x20);
|
|
523
|
+
return;
|
|
524
|
+
case EnumToken.CommaTokenType:
|
|
525
|
+
builder.appendCode(0x2c);
|
|
526
|
+
return;
|
|
527
|
+
case EnumToken.Star:
|
|
528
|
+
case EnumToken.UniversalSelectorTokenType:
|
|
529
|
+
builder.appendCode(0x2a);
|
|
530
|
+
return;
|
|
531
|
+
case EnumToken.NestingSelectorTokenType:
|
|
532
|
+
builder.appendCode(0x26);
|
|
533
|
+
return;
|
|
534
|
+
case EnumToken.GtTokenType:
|
|
535
|
+
case EnumToken.ChildCombinatorTokenType:
|
|
536
|
+
builder.appendCode(0x3e);
|
|
537
|
+
return;
|
|
538
|
+
case EnumToken.Plus:
|
|
539
|
+
case EnumToken.NextSiblingCombinatorTokenType:
|
|
540
|
+
builder.appendCode(0x2b);
|
|
541
|
+
return;
|
|
542
|
+
case EnumToken.Tilda:
|
|
543
|
+
case EnumToken.SubsequentSiblingCombinatorTokenType:
|
|
544
|
+
builder.appendCode(0x7e);
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
builder.appendString(renderValue(arena, token, selectorRenderOptions));
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* true if two ranges of code units are equal
|
|
552
|
+
* @param codes
|
|
553
|
+
* @param start1
|
|
554
|
+
* @param end1
|
|
555
|
+
* @param start2
|
|
556
|
+
* @param end2
|
|
557
|
+
*/
|
|
558
|
+
function equalCodes(codes, start1, end1, start2, end2) {
|
|
559
|
+
if (end1 - start1 !== end2 - start2) {
|
|
560
|
+
return false;
|
|
561
|
+
}
|
|
562
|
+
for (let i = 0; i < end1 - start1; i++) {
|
|
563
|
+
if (codes[start1 + i] !== codes[start2 + i]) {
|
|
564
|
+
return false;
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
return true;
|
|
568
|
+
}
|
|
569
|
+
/**
|
|
570
|
+
* true if a rendered token of the current selector is a single code unit
|
|
571
|
+
* @param index token index
|
|
572
|
+
* @param count number of tokens
|
|
573
|
+
* @param code
|
|
574
|
+
*/
|
|
575
|
+
function isPartCode(index, count, code) {
|
|
576
|
+
if (index < 0 || index >= count) {
|
|
577
|
+
return false;
|
|
578
|
+
}
|
|
579
|
+
const start = partOffsets[index * 2];
|
|
580
|
+
return partOffsets[index * 2 + 1] - start === 1 && partBuilder.codes[start] === code;
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* true if the rendered token is a simple selector that is not a type selector: a class (`.a`), an id (`#a`), an
|
|
584
|
+
* attribute (`[a]`), a pseudo-class (`:hover`) or a pseudo-element (`::before`)
|
|
585
|
+
* @param index index of the rendered token
|
|
586
|
+
* @param count number of rendered tokens
|
|
587
|
+
*/
|
|
588
|
+
function isSubclassPart(index, count) {
|
|
589
|
+
if (index < 0 || index >= count || partOffsets[index * 2 + 1] === partOffsets[index * 2]) {
|
|
590
|
+
return false;
|
|
591
|
+
}
|
|
592
|
+
const code = partBuilder.codes[partOffsets[index * 2]];
|
|
593
|
+
// '.', '#', ':', '['
|
|
594
|
+
return code === 0x2e || code === 0x23 || code === 0x3a || code === 0x5b;
|
|
595
|
+
}
|
|
596
|
+
/**
|
|
597
|
+
* true if the rendered token is a type selector other than `html`: `span`, `svg|circle`
|
|
598
|
+
* @param index index of the rendered token
|
|
599
|
+
* @param count number of rendered tokens
|
|
600
|
+
*/
|
|
601
|
+
function isTypePart(index, count) {
|
|
602
|
+
if (index >= count) {
|
|
603
|
+
return false;
|
|
604
|
+
}
|
|
605
|
+
const start = partOffsets[index * 2];
|
|
606
|
+
const end = partOffsets[index * 2 + 1];
|
|
607
|
+
const code = partBuilder.codes[start];
|
|
608
|
+
// a namespace prefix: `ns|span`
|
|
609
|
+
if (isPartCode(index + 1, count, 0x7c)) {
|
|
610
|
+
return false;
|
|
611
|
+
}
|
|
612
|
+
if (!((code >= 0x61 && code <= 0x7a) || (code >= 0x41 && code <= 0x5a) || code === 0x5f || code >= 0x80)) {
|
|
613
|
+
return false;
|
|
614
|
+
}
|
|
615
|
+
// `html`, case-insensitive
|
|
616
|
+
return !(end - start === 4 &&
|
|
617
|
+
(partBuilder.codes[start] | 0x20) === 0x68 &&
|
|
618
|
+
(partBuilder.codes[start + 1] | 0x20) === 0x74 &&
|
|
619
|
+
(partBuilder.codes[start + 2] | 0x20) === 0x6d &&
|
|
620
|
+
(partBuilder.codes[start + 3] | 0x20) === 0x6c);
|
|
621
|
+
}
|
|
622
|
+
/**
|
|
623
|
+
* add the current selector (the rendered tokens in {@link partBuilder}) to the selectors of the rule when it is not
|
|
624
|
+
* already present. a universal selector is removed before a compound selector
|
|
625
|
+
* @param count number of rendered tokens
|
|
626
|
+
* @param selectors number of selectors
|
|
627
|
+
* @returns the number of selectors
|
|
628
|
+
*/
|
|
629
|
+
function addSelectorCodes(count, selectors) {
|
|
630
|
+
// `*` is removed when it is followed by another simple selector of the same compound selector:
|
|
631
|
+
// `*.a` -> `.a`, `*:hover` -> `:hover`, `*[a]` -> `[a]`, `*::before` -> `::before`.
|
|
632
|
+
// it is kept when it is the only simple selector of the compound selector: it is followed by a combinator
|
|
633
|
+
// (`.a * .b`, `.a *+.b`) or it ends the selector. it is kept in a namespace selector: `*|*`, `ns|*.a`
|
|
634
|
+
for (let i = 0; i + 1 < count; i++) {
|
|
635
|
+
if (isPartCode(i, count, 0x2a) && !isPartCode(i - 1, count, 0x7c) && isSubclassPart(i + 1, count)) {
|
|
636
|
+
partOffsets.copyWithin(i * 2, (i + 1) * 2, count * 2);
|
|
637
|
+
count--;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
// a leading `* ` is removed before a type selector: `* span` -> `span`. every element but the root element has a
|
|
641
|
+
// parent element, the root element is `html`. it is kept before the other selectors: `* .dark` does not match
|
|
642
|
+
// `html.dark`
|
|
643
|
+
if (count > 2 && isPartCode(0, count, 0x2a) && isPartCode(1, count, 0x20) && isTypePart(2, count)) {
|
|
644
|
+
partOffsets.copyWithin(0, 4, count * 2);
|
|
645
|
+
count -= 2;
|
|
646
|
+
}
|
|
647
|
+
// the selector: the rendered tokens, at the end of the selector builder
|
|
648
|
+
const start = selectorBuilder.length + (selectors > 0 ? 1 : 0);
|
|
649
|
+
if (selectors > 0) {
|
|
650
|
+
selectorBuilder.appendCode(0x2c);
|
|
651
|
+
}
|
|
652
|
+
for (let i = 0; i < count; i++) {
|
|
653
|
+
const tokenStart = partOffsets[i * 2];
|
|
654
|
+
const tokenEnd = partOffsets[i * 2 + 1];
|
|
655
|
+
for (let k = tokenStart; k < tokenEnd; k++) {
|
|
656
|
+
selectorBuilder.appendCode(partBuilder.codes[k]);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
const end = selectorBuilder.length;
|
|
660
|
+
// distinct selectors
|
|
661
|
+
for (let k = 0; k < selectors; k++) {
|
|
662
|
+
if (equalCodes(selectorBuilder.codes, selectorOffsets[k * 2], selectorOffsets[k * 2 + 1], start, end)) {
|
|
663
|
+
selectorBuilder.length = selectors > 0 ? start - 1 : start;
|
|
664
|
+
return selectors;
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
if ((selectors + 1) * 2 > selectorOffsets.length) {
|
|
668
|
+
const offsets = new Int32Array(selectorOffsets.length * 2);
|
|
669
|
+
offsets.set(selectorOffsets);
|
|
670
|
+
selectorOffsets = offsets;
|
|
671
|
+
}
|
|
672
|
+
selectorOffsets[selectors * 2] = start;
|
|
673
|
+
selectorOffsets[selectors * 2 + 1] = end;
|
|
674
|
+
return selectors + 1;
|
|
675
|
+
}
|
|
420
676
|
/**
|
|
421
677
|
* selector of a rule: the distinct comma separated selectors, the whitespace around the combinators is removed.
|
|
422
|
-
* a universal selector is removed before a compound selector
|
|
678
|
+
* a universal selector is removed before a compound selector. the selector is built from the code units of the
|
|
679
|
+
* tokens and interned: no string is created
|
|
423
680
|
* @param arena
|
|
424
681
|
* @param tokens
|
|
682
|
+
* @returns the interned string id of the selector
|
|
425
683
|
*/
|
|
426
684
|
function renderSelector(arena, tokens) {
|
|
427
|
-
// the
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
685
|
+
// number of rendered tokens of the current selector, number of distinct selectors
|
|
686
|
+
let count = 0;
|
|
687
|
+
let selectors = 0;
|
|
688
|
+
partBuilder.reset();
|
|
689
|
+
selectorBuilder.reset();
|
|
431
690
|
for (let index = 0; index < tokens.length; index++) {
|
|
432
691
|
const curr = tokens[index];
|
|
433
|
-
|
|
692
|
+
const type = arena.dataTypes[curr];
|
|
693
|
+
if (type === EnumToken.WhitespaceTokenType) {
|
|
434
694
|
if (trimWhiteSpace.includes(arena.dataTypes[tokens[index - 1]]) ||
|
|
435
695
|
trimWhiteSpace.includes(arena.dataTypes[tokens[index + 1]]) ||
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
// inlined: arena.str(tokens[index + 1])
|
|
439
|
-
combinators.includes(arena.strings[arena.stringValue[tokens[index + 1]]])) {
|
|
696
|
+
isCombinatorString(arena, tokens[index - 1]) ||
|
|
697
|
+
isCombinatorString(arena, tokens[index + 1])) {
|
|
440
698
|
continue;
|
|
441
699
|
}
|
|
442
700
|
}
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
701
|
+
if (type === EnumToken.CommaTokenType) {
|
|
702
|
+
selectors = addSelectorCodes(count, selectors);
|
|
703
|
+
count = 0;
|
|
704
|
+
partBuilder.reset();
|
|
705
|
+
continue;
|
|
447
706
|
}
|
|
448
|
-
|
|
449
|
-
|
|
707
|
+
const start = partBuilder.length;
|
|
708
|
+
appendSelectorToken(arena, curr, partBuilder);
|
|
709
|
+
// a token rendered as `,`
|
|
710
|
+
if (partBuilder.length - start === 1 && partBuilder.codes[start] === 0x2c) {
|
|
711
|
+
partBuilder.length = start;
|
|
712
|
+
selectors = addSelectorCodes(count, selectors);
|
|
713
|
+
count = 0;
|
|
714
|
+
partBuilder.reset();
|
|
715
|
+
continue;
|
|
450
716
|
}
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
for (const selector of selectors) {
|
|
456
|
-
result = first ? selector : result + "," + selector;
|
|
457
|
-
first = false;
|
|
458
|
-
}
|
|
459
|
-
return result;
|
|
460
|
-
}
|
|
461
|
-
/**
|
|
462
|
-
* add a selector from its rendered tokens
|
|
463
|
-
* @param selectors
|
|
464
|
-
* @param curr
|
|
465
|
-
*/
|
|
466
|
-
function addSelector(selectors, curr) {
|
|
467
|
-
for (let i = 0; i < curr.length; i++) {
|
|
468
|
-
if (i + 1 < curr.length && curr[i] === "*") {
|
|
469
|
-
const index = curr[i + 1] === " " ? 2 : 1;
|
|
470
|
-
if (![">", "~", "+"].includes(curr[index])) {
|
|
471
|
-
curr.splice(i, index);
|
|
472
|
-
}
|
|
717
|
+
if ((count + 1) * 2 > partOffsets.length) {
|
|
718
|
+
const offsets = new Int32Array(partOffsets.length * 2);
|
|
719
|
+
offsets.set(partOffsets);
|
|
720
|
+
partOffsets = offsets;
|
|
473
721
|
}
|
|
722
|
+
partOffsets[count * 2] = start;
|
|
723
|
+
partOffsets[count * 2 + 1] = partBuilder.length;
|
|
724
|
+
count++;
|
|
474
725
|
}
|
|
475
|
-
selectors
|
|
726
|
+
addSelectorCodes(count, selectors);
|
|
727
|
+
return arena.internBuilder(selectorBuilder);
|
|
476
728
|
}
|
|
477
729
|
|
|
478
730
|
export { parseSelector };
|
|
@@ -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.3.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { EnumToken } from '../../ast/types.js';
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/*
|
|
2
|
-
CSS parser, minifier and validator for node and the browser
|
|
2
|
+
CSS parser, minifier and validator for node, deno, bun and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version: 2.
|
|
5
|
+
Version: 2.3.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { EnumToken } from '../../ast/types.js';
|
|
9
|
-
import {
|
|
9
|
+
import { isFunctionType } from '../../syntax/constants.js';
|
|
10
10
|
import { isColor } from '../../syntax/syntax.js';
|
|
11
11
|
|
|
12
12
|
const mathTokenTypes = [
|
|
@@ -34,8 +34,10 @@ const mathTokenTypes = [
|
|
|
34
34
|
*/
|
|
35
35
|
function matchType(arena, val, properties) {
|
|
36
36
|
const typ = arena.dataTypes[val];
|
|
37
|
-
if ((typ === EnumToken.IdenTokenType &&
|
|
38
|
-
|
|
37
|
+
if ((typ === EnumToken.IdenTokenType &&
|
|
38
|
+
// a property may have no keyword (`text-emphasis-color`)
|
|
39
|
+
properties.keywords?.includes(arena.resolveString(arena.stringValue[val]))) ||
|
|
40
|
+
properties.types?.some(
|
|
39
41
|
// @ts-expect-error
|
|
40
42
|
(t) =>
|
|
41
43
|
// @ts-expect-error
|
|
@@ -46,13 +48,13 @@ function matchType(arena, val, properties) {
|
|
|
46
48
|
}
|
|
47
49
|
if (typ === EnumToken.NumberTokenType && arena.numericValue[val] === 0) {
|
|
48
50
|
// @ts-ignore
|
|
49
|
-
return properties.types.some((type) => {
|
|
51
|
+
return (properties.types ?? []).some((type) => {
|
|
50
52
|
// @ts-ignore
|
|
51
53
|
const typ = EnumToken[type];
|
|
52
54
|
return typ === EnumToken.LengthTokenType || typ === EnumToken.AngleTokenType;
|
|
53
55
|
});
|
|
54
56
|
}
|
|
55
|
-
if (
|
|
57
|
+
if (isFunctionType(typ) && typ === EnumToken.MathFunctionTokenType) {
|
|
56
58
|
for (let t = arena.firstToken[val]; t !== 0; t = arena.nextSibling[t]) {
|
|
57
59
|
if (!mathTokenTypes.includes(arena.dataTypes[t]) && !matchType(arena, t, properties)) {
|
|
58
60
|
return false;
|
|
@@ -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.3.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
// from https://github.com/Rich-Harris/vlq/tree/master
|