@tbela99/css-parser 2.1.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +42 -1
- package/README.md +14 -7
- package/architecture.png +0 -0
- package/dist/{src/data → data}/properties.json.js +86 -2
- package/dist/{src/data → data}/validation.json.js +5 -2
- package/dist/index-umd-web.js +10738 -4359
- package/dist/index.d.ts +734 -100
- package/dist/{src/lib → lib}/ast/arena-node.js +10 -10
- package/dist/{src/lib → lib}/ast/arena.js +304 -139
- package/dist/{src/lib → lib}/ast/cache.js +2 -2
- package/dist/{src/lib → lib}/ast/features/calc.js +11 -9
- package/dist/{src/lib → lib}/ast/features/if.js +6 -6
- package/dist/{src/lib → lib}/ast/features/index.js +3 -2
- package/dist/{src/lib → lib}/ast/features/inlinecssvariables.js +12 -10
- package/dist/lib/ast/features/mixin.js +953 -0
- package/dist/{src/lib → lib}/ast/features/prefix.js +39 -39
- package/dist/lib/ast/features/shorthand.js +129 -0
- package/dist/lib/ast/features/transform.js +188 -0
- package/dist/{src/lib → lib}/ast/features/type.js +2 -2
- package/dist/{src/lib → lib}/ast/math/expression.js +24 -24
- package/dist/{src/lib → lib}/ast/math/math.js +2 -2
- package/dist/{src/lib → lib}/ast/minify.js +745 -201
- package/dist/lib/ast/mixins/custom-function.js +393 -0
- package/dist/lib/ast/mixins/utils.js +53 -0
- package/dist/lib/ast/mixins/variables.js +678 -0
- package/dist/lib/ast/strings.js +906 -0
- package/dist/lib/ast/transform/compute.js +560 -0
- package/dist/lib/ast/transform/matrix.js +67 -0
- package/dist/lib/ast/transform/minify.js +325 -0
- package/dist/lib/ast/transform/perspective.js +24 -0
- package/dist/{src/lib → lib}/ast/transform/rotate.js +16 -7
- package/dist/lib/ast/transform/scale.js +43 -0
- package/dist/lib/ast/transform/skew.js +38 -0
- package/dist/lib/ast/transform/translate.js +56 -0
- package/dist/lib/ast/transform/utils.js +390 -0
- package/dist/{src/lib → lib}/ast/types.js +2 -2
- package/dist/{src/lib → lib}/ast/walk.js +2 -2
- package/dist/{src/lib → lib}/fs/resolve.js +2 -2
- package/dist/{src/lib → lib}/parser/declaration/list.js +25 -8
- package/dist/{src/lib → lib}/parser/declaration/map.js +15 -15
- package/dist/{src/lib → lib}/parser/declaration/set.js +5 -5
- package/dist/{src/lib → lib}/parser/input.js +18 -3
- package/dist/{src/lib → lib}/parser/linesmap.js +2 -2
- package/dist/{src/lib → lib}/parser/parse.js +425 -258
- package/dist/{src/lib → lib}/parser/source.js +2 -2
- package/dist/{src/lib → lib}/parser/tokenize.js +480 -312
- package/dist/{src/lib → lib}/parser/utils/angle.js +3 -3
- package/dist/{src/lib → lib}/parser/utils/at-rule-container.js +12 -12
- package/dist/{src/lib → lib}/parser/utils/at-rule-font-feature-values.js +3 -3
- package/dist/{src/lib → lib}/parser/utils/at-rule-generic.js +7 -7
- package/dist/{src/lib → lib}/parser/utils/at-rule-import.js +10 -10
- package/dist/{src/lib → lib}/parser/utils/at-rule-media.js +14 -14
- package/dist/{src/lib → lib}/parser/utils/at-rule-support.js +9 -14
- package/dist/{src/lib → lib}/parser/utils/at-rule-when-else.js +6 -6
- package/dist/{src/lib → lib}/parser/utils/at-rule.js +3 -3
- package/dist/{src/lib → lib}/parser/utils/base64.js +2 -2
- package/dist/lib/parser/utils/cache.js +53 -0
- package/dist/lib/parser/utils/canonical.js +78 -0
- package/dist/lib/parser/utils/codes.js +93 -0
- package/dist/{src/lib → lib}/parser/utils/config.js +2 -2
- package/dist/{src/lib → lib}/parser/utils/declaration.js +31 -26
- package/dist/{src/lib → lib}/parser/utils/eq.js +2 -2
- package/dist/{src/lib → lib}/parser/utils/hash.js +2 -2
- package/dist/{src/lib → lib}/parser/utils/selector.js +305 -63
- package/dist/{src/lib → lib}/parser/utils/text.js +2 -2
- package/dist/{src/lib → lib}/parser/utils/token.js +2 -2
- package/dist/{src/lib → lib}/parser/utils/type.js +9 -7
- package/dist/{src/lib → lib}/printer/render.js +420 -152
- package/dist/lib/printer/result.js +148 -0
- package/dist/{src/lib → lib}/printer/sourcemap/lib/codec.js +2 -2
- package/dist/{src/lib → lib}/printer/sourcemap/sourcemap.js +2 -2
- package/dist/lib/printer/writer.js +252 -0
- package/dist/{src/lib → lib}/syntax/color/a98rgb.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/alpha.js +10 -10
- package/dist/{src/lib → lib}/syntax/color/cmyk.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/color-mix.js +18 -18
- package/dist/{src/lib → lib}/syntax/color/color.js +18 -18
- package/dist/{src/lib → lib}/syntax/color/hex.js +3 -3
- package/dist/{src/lib → lib}/syntax/color/hsl.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/hwb.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/lab.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/lch.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/oklab.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/oklch.js +3 -3
- package/dist/{src/lib → lib}/syntax/color/p3.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/prophotorgb.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/rec2020.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/relative-color.js +14 -14
- package/dist/{src/lib → lib}/syntax/color/rgb.js +4 -4
- package/dist/{src/lib → lib}/syntax/color/srgb.js +5 -5
- package/dist/{src/lib → lib}/syntax/color/utils/color-type.js +4 -4
- package/dist/{src/lib → lib}/syntax/color/utils/colorspace.js +10 -10
- package/dist/{src/lib → lib}/syntax/color/utils/components.js +5 -5
- package/dist/{src/lib → lib}/syntax/color/utils/distance.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/utils/make-color.js +3 -3
- package/dist/{src/lib → lib}/syntax/color/utils/matrix.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/xyz.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/xyzd50.js +2 -2
- package/dist/{src/lib → lib}/syntax/constants.js +26 -3
- package/dist/{src/lib → lib}/syntax/syntax.js +37 -29
- package/dist/{src/lib → lib}/validation/config.js +23 -10
- package/dist/lib/validation/first.js +571 -0
- package/dist/{src/lib → lib}/validation/match.js +292 -150
- package/dist/{src/lib → lib}/validation/parser/parse.js +2 -2
- package/dist/{src/lib → lib}/validation/parser/typedef.js +2 -2
- package/dist/{src/lib → lib}/validation/utils/list.js +3 -19
- package/dist/{src/node.js → node.js} +52 -34
- package/dist/{src/types.js → types.js} +2 -2
- package/dist/{src/utils → utils}/sync.js +10 -5
- package/dist/version.d.ts +4 -0
- package/dist/version.js +13 -0
- package/dist/{src/web.js → web.js} +47 -34
- package/package.json +12 -12
- package/dist/package.json.js +0 -10
- package/dist/src/lib/ast/features/shorthand.js +0 -95
- package/dist/src/lib/ast/features/transform.js +0 -90
- package/dist/src/lib/ast/transform/compute.js +0 -362
- package/dist/src/lib/ast/transform/matrix.js +0 -88
- package/dist/src/lib/ast/transform/minify.js +0 -267
- package/dist/src/lib/ast/transform/perspective.js +0 -16
- package/dist/src/lib/ast/transform/scale.js +0 -39
- package/dist/src/lib/ast/transform/skew.js +0 -30
- package/dist/src/lib/ast/transform/translate.js +0 -39
- package/dist/src/lib/ast/transform/utils.js +0 -287
- package/dist/src/lib/parser/utils/cache.js +0 -54
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
/*
|
|
2
|
-
CSS parser, minifier and validator for node and the browser
|
|
2
|
+
CSS parser, minifier and validator for node, deno, bun and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version: 2.
|
|
5
|
+
Version: 2.2.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { EnumToken, 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
13
|
import { getParsedSyntax, getSyntaxRule, getSyntaxConfig } from '../../validation/config.js';
|
|
13
14
|
import { matchAllSyntaxes, createValidationContext, trimArray, matchSelectorSyntax } from '../../validation/match.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 &&
|
|
@@ -242,7 +243,7 @@ function parseSelector(arena, tokens, context, options, errors) {
|
|
|
242
243
|
if (contextType !== EnumToken.StyleSheetNodeType && contextType !== EnumToken.RuleNodeType) {
|
|
243
244
|
allowed = false;
|
|
244
245
|
if (contextType === EnumToken.AtRuleNodeType) {
|
|
245
|
-
const syntaxRule = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" + arena.
|
|
246
|
+
const syntaxRule = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" + arena.resolveString(arena.name[context]));
|
|
246
247
|
allowed = syntaxRule?.acceptAnyRule ?? false;
|
|
247
248
|
if (!allowed) {
|
|
248
249
|
const rules = syntaxRule?.getBlockRules?.();
|
|
@@ -261,11 +262,16 @@ function parseSelector(arena, tokens, context, options, errors) {
|
|
|
261
262
|
}
|
|
262
263
|
}
|
|
263
264
|
while (parent != null && parent > 0) {
|
|
264
|
-
if (arena.dataTypes[parent] === EnumToken.AtRuleNodeType && "media" === arena.
|
|
265
|
+
if (arena.dataTypes[parent] === EnumToken.AtRuleNodeType && "media" === arena.resolveString(arena.name[parent])) {
|
|
265
266
|
parent = arena.parents[parent];
|
|
266
267
|
continue;
|
|
267
268
|
}
|
|
268
|
-
nested
|
|
269
|
+
// the rules of a mixin body are nested in the rule the mixin is applied to:
|
|
270
|
+
// '&' represents the rule that contains the '@apply' rule
|
|
271
|
+
nested =
|
|
272
|
+
arena.dataTypes[parent] === EnumToken.RuleNodeType ||
|
|
273
|
+
(arena.dataTypes[parent] === EnumToken.AtRuleNodeType &&
|
|
274
|
+
"mixin" === arena.resolveString(arena.name[parent]));
|
|
269
275
|
if (nested) {
|
|
270
276
|
break;
|
|
271
277
|
}
|
|
@@ -279,15 +285,15 @@ function parseSelector(arena, tokens, context, options, errors) {
|
|
|
279
285
|
const next = tokens[read + 1];
|
|
280
286
|
if (arena.dataTypes[next] === EnumToken.IdenTokenType) {
|
|
281
287
|
arena.dataTypes[token] = EnumToken.PseudoElementTokenType;
|
|
282
|
-
//
|
|
283
|
-
arena.stringValue[token] = arena.
|
|
288
|
+
// ":" + name, no string is created
|
|
289
|
+
arena.stringValue[token] = arena.internBuilder(arena.appendString(pseudoBuilder.reset().appendCode(0x3a), arena.stringValue[next]));
|
|
284
290
|
arena.spanLocation(token, token, next);
|
|
285
291
|
all[write++] = token;
|
|
286
292
|
read++;
|
|
287
293
|
continue;
|
|
288
294
|
}
|
|
289
295
|
if (arena.dataTypes[next] === EnumToken.FunctionTokenDefType) {
|
|
290
|
-
val = ":" + arena.
|
|
296
|
+
val = ":" + arena.resolveString(arena.stringValue[next]);
|
|
291
297
|
arena.dataTypes[token] =
|
|
292
298
|
val + "()" in getSyntaxConfig().selectors
|
|
293
299
|
? EnumToken.PseudoClassFunctionTokenDefType
|
|
@@ -302,18 +308,22 @@ function parseSelector(arena, tokens, context, options, errors) {
|
|
|
302
308
|
}
|
|
303
309
|
if (arena.dataTypes[token] === EnumToken.DoubleColonTokenType) {
|
|
304
310
|
const next = tokens[read + 1];
|
|
305
|
-
val = ":" + arena.strings[arena.stringValue[next]];
|
|
306
311
|
if (arena.dataTypes[next] === EnumToken.IdenTokenType) {
|
|
307
312
|
arena.dataTypes[token] = EnumToken.PseudoClassTokenType;
|
|
308
|
-
//
|
|
309
|
-
|
|
313
|
+
// ":" + name for the pseudo elements with a single colon syntax, "::" + name otherwise. no string is
|
|
314
|
+
// created
|
|
315
|
+
pseudoBuilder.reset().appendCode(0x3a);
|
|
316
|
+
if (!isSingleColonPseudoElement(arena, arena.stringValue[next])) {
|
|
317
|
+
pseudoBuilder.appendCode(0x3a);
|
|
318
|
+
}
|
|
319
|
+
arena.stringValue[token] = arena.internBuilder(arena.appendString(pseudoBuilder, arena.stringValue[next]));
|
|
310
320
|
arena.spanLocation(token, token, next);
|
|
311
321
|
all[write++] = token;
|
|
312
322
|
read++;
|
|
313
323
|
continue;
|
|
314
324
|
}
|
|
315
325
|
if (arena.dataTypes[next] === EnumToken.FunctionTokenDefType) {
|
|
316
|
-
val = "::" + arena.
|
|
326
|
+
val = "::" + arena.resolveString(arena.stringValue[next]);
|
|
317
327
|
arena.dataTypes[token] =
|
|
318
328
|
val + "()" in getSyntaxConfig().selectors
|
|
319
329
|
? EnumToken.PseudoClassFunctionTokenDefType
|
|
@@ -327,7 +337,7 @@ function parseSelector(arena, tokens, context, options, errors) {
|
|
|
327
337
|
}
|
|
328
338
|
}
|
|
329
339
|
if (arena.dataTypes[token] === EnumToken.ColorTokenType) {
|
|
330
|
-
if (isHash(arena.
|
|
340
|
+
if (isHash(arena.resolveString(arena.stringValue[token]))) {
|
|
331
341
|
arena.dataTypes[token] = EnumToken.HashTokenType;
|
|
332
342
|
all[write++] = token;
|
|
333
343
|
continue;
|
|
@@ -384,14 +394,14 @@ function parseSelector(arena, tokens, context, options, errors) {
|
|
|
384
394
|
index = tokens.indexOf(func);
|
|
385
395
|
arena.spanLocation(func, func, token);
|
|
386
396
|
tokens.splice(i, 1);
|
|
387
|
-
if (
|
|
397
|
+
if (isFunctionDefType(arena.dataTypes[func])) {
|
|
388
398
|
arena.dataTypes[func] = tokensfuncDefMap.get(arena.dataTypes[func]);
|
|
389
399
|
arena.setTokenList(func, trimArray(arena, tokens.splice(index + 1, i - index - 1)));
|
|
390
400
|
}
|
|
391
401
|
if (result.success && options.minify) {
|
|
392
402
|
// parse an+b
|
|
393
403
|
// 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.
|
|
404
|
+
const name = arena.resolveString(arena.stringValue[func]);
|
|
395
405
|
if (name === ":nth-child" ||
|
|
396
406
|
name === ":nth-last-child" ||
|
|
397
407
|
name === ":nth-of-type" ||
|
|
@@ -417,62 +427,294 @@ function parseSelector(arena, tokens, context, options, errors) {
|
|
|
417
427
|
}
|
|
418
428
|
// options of the rendering of the selector tokens
|
|
419
429
|
const selectorRenderOptions = { minify: false };
|
|
430
|
+
/**
|
|
431
|
+
* builder of the pseudo classes and pseudo elements
|
|
432
|
+
*/
|
|
433
|
+
const pseudoBuilder = new StringBuilder();
|
|
434
|
+
/**
|
|
435
|
+
* true if ":" + the string is a pseudo element with a single colon syntax (`:before`). the string is not created
|
|
436
|
+
* @param arena
|
|
437
|
+
* @param id
|
|
438
|
+
*/
|
|
439
|
+
function isSingleColonPseudoElement(arena, id) {
|
|
440
|
+
const length = arena.stringLength(id);
|
|
441
|
+
for (let i = 0; i < pseudoElements.length; i++) {
|
|
442
|
+
const pseudo = pseudoElements[i];
|
|
443
|
+
if (pseudo.length === length + 1 && pseudo.charCodeAt(0) === 0x3a) {
|
|
444
|
+
let k = 0;
|
|
445
|
+
while (k < length && arena.stringCodeAt(id, k) === pseudo.charCodeAt(k + 1)) {
|
|
446
|
+
k++;
|
|
447
|
+
}
|
|
448
|
+
if (k === length) {
|
|
449
|
+
return true;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
return false;
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* builder of the rendered tokens of the current selector
|
|
457
|
+
*/
|
|
458
|
+
const partBuilder = new StringBuilder();
|
|
459
|
+
/**
|
|
460
|
+
* builder of the selector of a rule
|
|
461
|
+
*/
|
|
462
|
+
const selectorBuilder = new StringBuilder();
|
|
463
|
+
/**
|
|
464
|
+
* offsets of the rendered tokens of the current selector in {@link partBuilder}: start and end of each token
|
|
465
|
+
*/
|
|
466
|
+
let partOffsets = new Int32Array(64);
|
|
467
|
+
/**
|
|
468
|
+
* offsets of the distinct selectors in {@link selectorBuilder}: start and end of each selector
|
|
469
|
+
*/
|
|
470
|
+
let selectorOffsets = new Int32Array(16);
|
|
471
|
+
/**
|
|
472
|
+
* true if the string of a token is a combinator. the string is not created
|
|
473
|
+
* @param arena
|
|
474
|
+
* @param token
|
|
475
|
+
*/
|
|
476
|
+
function isCombinatorString(arena, token) {
|
|
477
|
+
if (token === undefined) {
|
|
478
|
+
return false;
|
|
479
|
+
}
|
|
480
|
+
const id = arena.stringValue[token];
|
|
481
|
+
const length = arena.stringLength(id);
|
|
482
|
+
if (length === 0 || length > 2) {
|
|
483
|
+
return false;
|
|
484
|
+
}
|
|
485
|
+
for (let i = 0; i < combinators.length; i++) {
|
|
486
|
+
if (arena.stringEquals(id, combinators[i])) {
|
|
487
|
+
return true;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
return false;
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* append the rendering of a selector token to the builder. the strings of the tokens are copied, no string is created:
|
|
494
|
+
* the tokens that are rendered as their string value, the symbols. the other tokens are rendered with renderValue()
|
|
495
|
+
* @param arena
|
|
496
|
+
* @param token
|
|
497
|
+
* @param builder
|
|
498
|
+
*/
|
|
499
|
+
function appendSelectorToken(arena, token, builder) {
|
|
500
|
+
if (arena.firstToken[token] === 0 && arena.firstChild[token] === 0) {
|
|
501
|
+
switch (arena.dataTypes[token]) {
|
|
502
|
+
case EnumToken.HashTokenType:
|
|
503
|
+
case EnumToken.IdenTokenType:
|
|
504
|
+
case EnumToken.LiteralTokenType:
|
|
505
|
+
case EnumToken.DashedIdenTokenType:
|
|
506
|
+
case EnumToken.ClassSelectorTokenType:
|
|
507
|
+
case EnumToken.PseudoClassTokenType:
|
|
508
|
+
arena.appendString(builder, arena.stringValue[token]);
|
|
509
|
+
return;
|
|
510
|
+
case EnumToken.WhitespaceTokenType:
|
|
511
|
+
case EnumToken.DescendantCombinatorTokenType:
|
|
512
|
+
builder.appendCode(0x20);
|
|
513
|
+
return;
|
|
514
|
+
case EnumToken.CommaTokenType:
|
|
515
|
+
builder.appendCode(0x2c);
|
|
516
|
+
return;
|
|
517
|
+
case EnumToken.Star:
|
|
518
|
+
case EnumToken.UniversalSelectorTokenType:
|
|
519
|
+
builder.appendCode(0x2a);
|
|
520
|
+
return;
|
|
521
|
+
case EnumToken.NestingSelectorTokenType:
|
|
522
|
+
builder.appendCode(0x26);
|
|
523
|
+
return;
|
|
524
|
+
case EnumToken.GtTokenType:
|
|
525
|
+
case EnumToken.ChildCombinatorTokenType:
|
|
526
|
+
builder.appendCode(0x3e);
|
|
527
|
+
return;
|
|
528
|
+
case EnumToken.Plus:
|
|
529
|
+
case EnumToken.NextSiblingCombinatorTokenType:
|
|
530
|
+
builder.appendCode(0x2b);
|
|
531
|
+
return;
|
|
532
|
+
case EnumToken.Tilda:
|
|
533
|
+
case EnumToken.SubsequentSiblingCombinatorTokenType:
|
|
534
|
+
builder.appendCode(0x7e);
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
builder.appendString(renderValue(arena, token, selectorRenderOptions));
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* true if two ranges of code units are equal
|
|
542
|
+
* @param codes
|
|
543
|
+
* @param start1
|
|
544
|
+
* @param end1
|
|
545
|
+
* @param start2
|
|
546
|
+
* @param end2
|
|
547
|
+
*/
|
|
548
|
+
function equalCodes(codes, start1, end1, start2, end2) {
|
|
549
|
+
if (end1 - start1 !== end2 - start2) {
|
|
550
|
+
return false;
|
|
551
|
+
}
|
|
552
|
+
for (let i = 0; i < end1 - start1; i++) {
|
|
553
|
+
if (codes[start1 + i] !== codes[start2 + i]) {
|
|
554
|
+
return false;
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
return true;
|
|
558
|
+
}
|
|
559
|
+
/**
|
|
560
|
+
* true if a rendered token of the current selector is a single code unit
|
|
561
|
+
* @param index token index
|
|
562
|
+
* @param count number of tokens
|
|
563
|
+
* @param code
|
|
564
|
+
*/
|
|
565
|
+
function isPartCode(index, count, code) {
|
|
566
|
+
if (index < 0 || index >= count) {
|
|
567
|
+
return false;
|
|
568
|
+
}
|
|
569
|
+
const start = partOffsets[index * 2];
|
|
570
|
+
return partOffsets[index * 2 + 1] - start === 1 && partBuilder.codes[start] === code;
|
|
571
|
+
}
|
|
572
|
+
/**
|
|
573
|
+
* true if the rendered token is a simple selector that is not a type selector: a class (`.a`), an id (`#a`), an
|
|
574
|
+
* attribute (`[a]`), a pseudo-class (`:hover`) or a pseudo-element (`::before`)
|
|
575
|
+
* @param index index of the rendered token
|
|
576
|
+
* @param count number of rendered tokens
|
|
577
|
+
*/
|
|
578
|
+
function isSubclassPart(index, count) {
|
|
579
|
+
if (index < 0 || index >= count || partOffsets[index * 2 + 1] === partOffsets[index * 2]) {
|
|
580
|
+
return false;
|
|
581
|
+
}
|
|
582
|
+
const code = partBuilder.codes[partOffsets[index * 2]];
|
|
583
|
+
// '.', '#', ':', '['
|
|
584
|
+
return code === 0x2e || code === 0x23 || code === 0x3a || code === 0x5b;
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* true if the rendered token is a type selector other than `html`: `span`, `svg|circle`
|
|
588
|
+
* @param index index of the rendered token
|
|
589
|
+
* @param count number of rendered tokens
|
|
590
|
+
*/
|
|
591
|
+
function isTypePart(index, count) {
|
|
592
|
+
if (index >= count) {
|
|
593
|
+
return false;
|
|
594
|
+
}
|
|
595
|
+
const start = partOffsets[index * 2];
|
|
596
|
+
const end = partOffsets[index * 2 + 1];
|
|
597
|
+
const code = partBuilder.codes[start];
|
|
598
|
+
// a namespace prefix: `ns|span`
|
|
599
|
+
if (isPartCode(index + 1, count, 0x7c)) {
|
|
600
|
+
return false;
|
|
601
|
+
}
|
|
602
|
+
if (!((code >= 0x61 && code <= 0x7a) || (code >= 0x41 && code <= 0x5a) || code === 0x5f || code >= 0x80)) {
|
|
603
|
+
return false;
|
|
604
|
+
}
|
|
605
|
+
// `html`, case-insensitive
|
|
606
|
+
return !(end - start === 4 &&
|
|
607
|
+
(partBuilder.codes[start] | 0x20) === 0x68 &&
|
|
608
|
+
(partBuilder.codes[start + 1] | 0x20) === 0x74 &&
|
|
609
|
+
(partBuilder.codes[start + 2] | 0x20) === 0x6d &&
|
|
610
|
+
(partBuilder.codes[start + 3] | 0x20) === 0x6c);
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* add the current selector (the rendered tokens in {@link partBuilder}) to the selectors of the rule when it is not
|
|
614
|
+
* already present. a universal selector is removed before a compound selector
|
|
615
|
+
* @param count number of rendered tokens
|
|
616
|
+
* @param selectors number of selectors
|
|
617
|
+
* @returns the number of selectors
|
|
618
|
+
*/
|
|
619
|
+
function addSelectorCodes(count, selectors) {
|
|
620
|
+
// `*` is removed when it is followed by another simple selector of the same compound selector:
|
|
621
|
+
// `*.a` -> `.a`, `*:hover` -> `:hover`, `*[a]` -> `[a]`, `*::before` -> `::before`.
|
|
622
|
+
// it is kept when it is the only simple selector of the compound selector: it is followed by a combinator
|
|
623
|
+
// (`.a * .b`, `.a *+.b`) or it ends the selector. it is kept in a namespace selector: `*|*`, `ns|*.a`
|
|
624
|
+
for (let i = 0; i + 1 < count; i++) {
|
|
625
|
+
if (isPartCode(i, count, 0x2a) && !isPartCode(i - 1, count, 0x7c) && isSubclassPart(i + 1, count)) {
|
|
626
|
+
partOffsets.copyWithin(i * 2, (i + 1) * 2, count * 2);
|
|
627
|
+
count--;
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
// a leading `* ` is removed before a type selector: `* span` -> `span`. every element but the root element has a
|
|
631
|
+
// parent element, the root element is `html`. it is kept before the other selectors: `* .dark` does not match
|
|
632
|
+
// `html.dark`
|
|
633
|
+
if (count > 2 && isPartCode(0, count, 0x2a) && isPartCode(1, count, 0x20) && isTypePart(2, count)) {
|
|
634
|
+
partOffsets.copyWithin(0, 4, count * 2);
|
|
635
|
+
count -= 2;
|
|
636
|
+
}
|
|
637
|
+
// the selector: the rendered tokens, at the end of the selector builder
|
|
638
|
+
const start = selectorBuilder.length + (selectors > 0 ? 1 : 0);
|
|
639
|
+
if (selectors > 0) {
|
|
640
|
+
selectorBuilder.appendCode(0x2c);
|
|
641
|
+
}
|
|
642
|
+
for (let i = 0; i < count; i++) {
|
|
643
|
+
const tokenStart = partOffsets[i * 2];
|
|
644
|
+
const tokenEnd = partOffsets[i * 2 + 1];
|
|
645
|
+
for (let k = tokenStart; k < tokenEnd; k++) {
|
|
646
|
+
selectorBuilder.appendCode(partBuilder.codes[k]);
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
const end = selectorBuilder.length;
|
|
650
|
+
// distinct selectors
|
|
651
|
+
for (let k = 0; k < selectors; k++) {
|
|
652
|
+
if (equalCodes(selectorBuilder.codes, selectorOffsets[k * 2], selectorOffsets[k * 2 + 1], start, end)) {
|
|
653
|
+
selectorBuilder.length = selectors > 0 ? start - 1 : start;
|
|
654
|
+
return selectors;
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
if ((selectors + 1) * 2 > selectorOffsets.length) {
|
|
658
|
+
const offsets = new Int32Array(selectorOffsets.length * 2);
|
|
659
|
+
offsets.set(selectorOffsets);
|
|
660
|
+
selectorOffsets = offsets;
|
|
661
|
+
}
|
|
662
|
+
selectorOffsets[selectors * 2] = start;
|
|
663
|
+
selectorOffsets[selectors * 2 + 1] = end;
|
|
664
|
+
return selectors + 1;
|
|
665
|
+
}
|
|
420
666
|
/**
|
|
421
667
|
* 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
|
|
668
|
+
* a universal selector is removed before a compound selector. the selector is built from the code units of the
|
|
669
|
+
* tokens and interned: no string is created
|
|
423
670
|
* @param arena
|
|
424
671
|
* @param tokens
|
|
672
|
+
* @returns the interned string id of the selector
|
|
425
673
|
*/
|
|
426
674
|
function renderSelector(arena, tokens) {
|
|
427
|
-
// the
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
675
|
+
// number of rendered tokens of the current selector, number of distinct selectors
|
|
676
|
+
let count = 0;
|
|
677
|
+
let selectors = 0;
|
|
678
|
+
partBuilder.reset();
|
|
679
|
+
selectorBuilder.reset();
|
|
431
680
|
for (let index = 0; index < tokens.length; index++) {
|
|
432
681
|
const curr = tokens[index];
|
|
433
|
-
|
|
682
|
+
const type = arena.dataTypes[curr];
|
|
683
|
+
if (type === EnumToken.WhitespaceTokenType) {
|
|
434
684
|
if (trimWhiteSpace.includes(arena.dataTypes[tokens[index - 1]]) ||
|
|
435
685
|
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]]])) {
|
|
686
|
+
isCombinatorString(arena, tokens[index - 1]) ||
|
|
687
|
+
isCombinatorString(arena, tokens[index + 1])) {
|
|
440
688
|
continue;
|
|
441
689
|
}
|
|
442
690
|
}
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
691
|
+
if (type === EnumToken.CommaTokenType) {
|
|
692
|
+
selectors = addSelectorCodes(count, selectors);
|
|
693
|
+
count = 0;
|
|
694
|
+
partBuilder.reset();
|
|
695
|
+
continue;
|
|
447
696
|
}
|
|
448
|
-
|
|
449
|
-
|
|
697
|
+
const start = partBuilder.length;
|
|
698
|
+
appendSelectorToken(arena, curr, partBuilder);
|
|
699
|
+
// a token rendered as `,`
|
|
700
|
+
if (partBuilder.length - start === 1 && partBuilder.codes[start] === 0x2c) {
|
|
701
|
+
partBuilder.length = start;
|
|
702
|
+
selectors = addSelectorCodes(count, selectors);
|
|
703
|
+
count = 0;
|
|
704
|
+
partBuilder.reset();
|
|
705
|
+
continue;
|
|
450
706
|
}
|
|
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
|
-
}
|
|
707
|
+
if ((count + 1) * 2 > partOffsets.length) {
|
|
708
|
+
const offsets = new Int32Array(partOffsets.length * 2);
|
|
709
|
+
offsets.set(partOffsets);
|
|
710
|
+
partOffsets = offsets;
|
|
473
711
|
}
|
|
712
|
+
partOffsets[count * 2] = start;
|
|
713
|
+
partOffsets[count * 2 + 1] = partBuilder.length;
|
|
714
|
+
count++;
|
|
474
715
|
}
|
|
475
|
-
selectors
|
|
716
|
+
addSelectorCodes(count, selectors);
|
|
717
|
+
return arena.internBuilder(selectorBuilder);
|
|
476
718
|
}
|
|
477
719
|
|
|
478
720
|
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.2.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.2.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;
|