@tbela99/css-parser 1.8.2 → 2.0.1
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 -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/cache.js +333 -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/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,11 +2,11 @@
|
|
|
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.1
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
|
-
import { PropertyList } from '../../parser/declaration/list.js';
|
|
9
|
-
import { EnumToken } from '../types.js';
|
|
8
|
+
import { DeclarationMemo, PropertyList } from '../../parser/declaration/list.js';
|
|
9
|
+
import { EnumToken, EnumAstNodeStatus } from '../types.js';
|
|
10
10
|
import { FeatureWalkMode } from './type.js';
|
|
11
11
|
|
|
12
12
|
class ComputeShorthandFeature {
|
|
@@ -27,65 +27,59 @@ class ComputeShorthandFeature {
|
|
|
27
27
|
options.features.push(new ComputeShorthandFeature(options));
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
|
-
run(ast, options) {
|
|
31
|
-
if (!(
|
|
30
|
+
run(arena, ast, options, _parent, context) {
|
|
31
|
+
if (!(arena.flags[ast] & EnumAstNodeStatus.HasChildren) || arena.firstChild[ast] == 0) {
|
|
32
32
|
return null;
|
|
33
33
|
}
|
|
34
|
-
//
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
let l;
|
|
38
|
-
const rules = [];
|
|
39
|
-
const declarations = [];
|
|
34
|
+
// the keys of the declarations are shared by the rules of the minification
|
|
35
|
+
const memo = context == null ? null : (context.declarations ??= new DeclarationMemo());
|
|
36
|
+
const list = new PropertyList(arena, options, memo);
|
|
40
37
|
let doNotSort = false;
|
|
41
|
-
|
|
42
|
-
|
|
38
|
+
let hasDeclarations = false;
|
|
39
|
+
if (arena.dataTypes[ast] == EnumToken.AtRuleNodeType) {
|
|
40
|
+
switch (arena.strings[arena.name[ast]]) {
|
|
43
41
|
case "function":
|
|
44
42
|
case "mixin":
|
|
45
43
|
doNotSort = true;
|
|
46
44
|
break;
|
|
47
45
|
}
|
|
48
46
|
}
|
|
49
|
-
//
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
47
|
+
// the declarations (with their leading comments) are moved into the property list,
|
|
48
|
+
// the rules (with their leading comments) stay in place
|
|
49
|
+
let child = arena.firstChild[ast];
|
|
50
|
+
while (child != 0) {
|
|
51
|
+
let last = child;
|
|
52
|
+
if (!doNotSort) {
|
|
53
|
+
// capture comments with the next node
|
|
54
|
+
while (arena.nextSibling[last] != 0 && arena.dataTypes[last] == EnumToken.CommentNodeType) {
|
|
55
|
+
last = arena.nextSibling[last];
|
|
56
|
+
}
|
|
53
57
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
if (node.typ == EnumToken.CommentNodeType) {
|
|
64
|
-
l++;
|
|
65
|
-
continue;
|
|
66
|
-
}
|
|
67
|
-
break;
|
|
58
|
+
const next = arena.nextSibling[last];
|
|
59
|
+
// https://drafts.csswg.org/css-mixins/#:~:text=The%20value%20of%20the%20result%20descriptor%20is%20always%2016px%20in%20the%20above%20example
|
|
60
|
+
if (doNotSort || arena.dataTypes[last] == EnumToken.DeclarationNodeType) {
|
|
61
|
+
hasDeclarations = true;
|
|
62
|
+
for (let node = child; node != next;) {
|
|
63
|
+
const nextNode = arena.nextSibling[node];
|
|
64
|
+
arena.detach(node);
|
|
65
|
+
list.addDeclaration(node);
|
|
66
|
+
node = nextNode;
|
|
68
67
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
68
|
+
}
|
|
69
|
+
child = next;
|
|
70
|
+
}
|
|
71
|
+
if (hasDeclarations) {
|
|
72
|
+
// the declarations are inserted before the rules
|
|
73
|
+
const first = arena.firstChild[ast];
|
|
74
|
+
for (const node of list) {
|
|
75
|
+
if (first == 0) {
|
|
76
|
+
arena.appendChild(ast, node);
|
|
75
77
|
}
|
|
76
78
|
else {
|
|
77
|
-
|
|
78
|
-
rules.push(ast.chi[m]);
|
|
79
|
-
}
|
|
79
|
+
arena.insertBefore(first, node);
|
|
80
80
|
}
|
|
81
|
-
k = l;
|
|
82
81
|
}
|
|
83
82
|
}
|
|
84
|
-
if (declarations.length > 0) {
|
|
85
|
-
ast.chi.length = 0;
|
|
86
|
-
// @ts-expect-error
|
|
87
|
-
ast.chi.push(...new PropertyList(options).add(declarations), ...rules);
|
|
88
|
-
}
|
|
89
83
|
return ast;
|
|
90
84
|
}
|
|
91
85
|
}
|
|
@@ -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.1
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { EnumToken, EnumAstNodeStatus } from '../types.js';
|
|
@@ -10,8 +10,11 @@ import { compute } from '../transform/compute.js';
|
|
|
10
10
|
import { filterValues, renderValue } from '../../printer/render.js';
|
|
11
11
|
import { minifyTransformFunctions, eqMatrix } from '../transform/minify.js';
|
|
12
12
|
import { FeatureWalkMode } from './type.js';
|
|
13
|
-
import { STATE } from '../../syntax/constants.js';
|
|
14
13
|
|
|
14
|
+
/**
|
|
15
|
+
* transform property and its prefixed versions
|
|
16
|
+
*/
|
|
17
|
+
const transformProperty = /^(-[a-z]+-)?transform$/;
|
|
15
18
|
class TransformCssFeature {
|
|
16
19
|
accept = new Set([
|
|
17
20
|
EnumToken.RuleNodeType,
|
|
@@ -31,52 +34,49 @@ class TransformCssFeature {
|
|
|
31
34
|
options.features.push(new TransformCssFeature());
|
|
32
35
|
}
|
|
33
36
|
}
|
|
34
|
-
run(ast) {
|
|
35
|
-
if (ast
|
|
37
|
+
run(arena, ast) {
|
|
38
|
+
if (!(arena.flags[ast] & EnumAstNodeStatus.HasChildren)) {
|
|
36
39
|
return null;
|
|
37
40
|
}
|
|
38
|
-
let i = 0;
|
|
39
41
|
let node;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
// @ts-ignore
|
|
43
|
-
node = ast.chi[i];
|
|
44
|
-
if (node[STATE] & (EnumAstNodeStatus.Invalid | EnumAstNodeStatus.ValidationFailed)) {
|
|
42
|
+
for (node = arena.firstChild[ast]; node != 0; node = arena.nextSibling[node]) {
|
|
43
|
+
if (arena.flags[node] & (EnumAstNodeStatus.Invalid | EnumAstNodeStatus.ValidationFailed)) {
|
|
45
44
|
continue;
|
|
46
45
|
}
|
|
47
|
-
if (node
|
|
48
|
-
!
|
|
46
|
+
if (arena.dataTypes[node] != EnumToken.DeclarationNodeType ||
|
|
47
|
+
!transformProperty.test(arena.strings[arena.name[node]])) {
|
|
49
48
|
continue;
|
|
50
49
|
}
|
|
51
50
|
const children = [];
|
|
52
|
-
for (const child of node
|
|
53
|
-
children.push(child
|
|
54
|
-
? minifyTransformFunctions(child)
|
|
51
|
+
for (const child of arena.tokens(node)) {
|
|
52
|
+
children.push(arena.dataTypes[child] == EnumToken.TransformFunctionTokenType
|
|
53
|
+
? minifyTransformFunctions(arena, child)
|
|
55
54
|
: child);
|
|
56
55
|
}
|
|
57
|
-
let { matrix, cumulative, minified } = compute(children) ?? {
|
|
56
|
+
let { matrix, cumulative, minified } = compute(arena, children) ?? {
|
|
58
57
|
matrix: null,
|
|
59
58
|
cumulative: null,
|
|
60
59
|
minified: null,
|
|
61
60
|
};
|
|
62
61
|
if (matrix == null || cumulative == null || minified == null) {
|
|
63
|
-
node
|
|
62
|
+
arena.setTokenList(node, children);
|
|
64
63
|
continue;
|
|
65
64
|
}
|
|
66
|
-
let r = [filterValues(children)];
|
|
67
|
-
if (eqMatrix(matrix, cumulative)) {
|
|
65
|
+
let r = [filterValues(arena, children)];
|
|
66
|
+
if (eqMatrix(arena, matrix, cumulative)) {
|
|
68
67
|
r.push(cumulative);
|
|
69
68
|
}
|
|
70
|
-
if (eqMatrix(matrix, minified)) {
|
|
69
|
+
if (eqMatrix(arena, matrix, minified)) {
|
|
71
70
|
r.push(minified);
|
|
72
71
|
}
|
|
73
|
-
const l = renderValue(matrix).length;
|
|
74
|
-
node
|
|
75
|
-
|
|
72
|
+
const l = renderValue(arena, matrix).length;
|
|
73
|
+
// inlined: arena.setTokens(node, ...r.reduce((acc: Token[], curr: Token[]): Token[] => { if (curr.reduce((acc: string, t: Token) => acc + renderValue(arena, t), "").length < l) { return curr; } return acc; }, [matrix]))
|
|
74
|
+
arena.setTokenList(node, r.reduce((acc, curr) => {
|
|
75
|
+
if (curr.reduce((acc, t) => acc + renderValue(arena, t), "").length < l) {
|
|
76
76
|
return curr;
|
|
77
77
|
}
|
|
78
78
|
return acc;
|
|
79
|
-
}, [matrix]);
|
|
79
|
+
}, [matrix]));
|
|
80
80
|
}
|
|
81
81
|
return null;
|
|
82
82
|
}
|