@tbela99/css-parser 1.8.1 → 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 +66 -0
- package/README.md +129 -31
- package/dist/index-umd-web.js +27382 -24086
- 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 -1662
- 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 -35874
- 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,61 +2,61 @@
|
|
|
2
2
|
CSS parser, minifier and validator for node and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version:
|
|
5
|
+
Version: 2.0.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
|
-
import { EnumToken } from '../types.js';
|
|
9
|
-
import { walkValues } from '../walk.js';
|
|
8
|
+
import { EnumToken, EnumAstNodeStatus } from '../types.js';
|
|
10
9
|
import { renderValue } from '../../printer/render.js';
|
|
11
10
|
import { splitRule } from '../minify.js';
|
|
12
11
|
import { FeatureWalkMode } from './type.js';
|
|
13
|
-
import {
|
|
12
|
+
import { mathFuncs } from '../../syntax/constants.js';
|
|
13
|
+
import { TokenField } from '../arena.js';
|
|
14
|
+
import { ValueWalker } from '../walk.js';
|
|
14
15
|
|
|
15
|
-
|
|
16
|
+
/**
|
|
17
|
+
* inline a binary expression into a parens token: (l op r)
|
|
18
|
+
* @param arena
|
|
19
|
+
* @param token
|
|
20
|
+
*/
|
|
21
|
+
function inlineExpression(arena, token) {
|
|
16
22
|
const result = [];
|
|
17
|
-
if (token
|
|
18
|
-
const chi = inlineExpression(token.
|
|
19
|
-
chi.push(
|
|
20
|
-
for (const child of inlineExpression(token.
|
|
23
|
+
if (arena.dataTypes[token] == EnumToken.BinaryExpressionTokenType) {
|
|
24
|
+
const chi = inlineExpression(arena, arena.field(token, TokenField.L));
|
|
25
|
+
chi.push(arena.createToken(arena.numericValue[token]));
|
|
26
|
+
for (const child of inlineExpression(arena, arena.field(token, TokenField.R))) {
|
|
21
27
|
chi.push(child);
|
|
22
28
|
}
|
|
23
|
-
result.push(
|
|
24
|
-
typ: EnumToken.ParensTokenType,
|
|
25
|
-
chi,
|
|
26
|
-
});
|
|
29
|
+
result.push(arena.createList(EnumToken.ParensTokenType, chi));
|
|
27
30
|
}
|
|
28
31
|
else {
|
|
29
32
|
result.push(token);
|
|
30
33
|
}
|
|
31
34
|
return result;
|
|
32
35
|
}
|
|
33
|
-
function replace(node, variableScope) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
function replace(arena, node, variableScope) {
|
|
37
|
+
// inlined: arena.walkValues(node)
|
|
38
|
+
for (const value of new ValueWalker(arena, node)) {
|
|
39
|
+
// the parent of a detached token is the declaration
|
|
40
|
+
const parentValue = arena.getValueParent(value) || node;
|
|
41
|
+
if (arena.dataTypes[value] == EnumToken.BinaryExpressionTokenType &&
|
|
42
|
+
parentValue != null &&
|
|
43
|
+
parentValue != node &&
|
|
44
|
+
// the token belongs to the parent token list (function arguments)
|
|
45
|
+
arena.parents[value] == parentValue) {
|
|
46
|
+
arena.replaceToken(value, ...inlineExpression(arena, value));
|
|
38
47
|
}
|
|
39
48
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if (source[i] == value) {
|
|
52
|
-
source.splice(i, 1, ...info.node.val);
|
|
53
|
-
break;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
else {
|
|
58
|
-
node.val = info.node.val.slice();
|
|
59
|
-
}
|
|
49
|
+
// inlined: arena.walkValues(node)
|
|
50
|
+
for (const value of new ValueWalker(arena, node)) {
|
|
51
|
+
const typ = arena.dataTypes[value];
|
|
52
|
+
if (typ == EnumToken.MathFunctionTokenType ||
|
|
53
|
+
(typ == EnumToken.WildCardFunctionTokenType && arena.strings[arena.stringValue[value]] == "var")) {
|
|
54
|
+
const first = arena.firstToken[value];
|
|
55
|
+
if (first != 0 && arena.nextSibling[first] == 0 && arena.dataTypes[first] == EnumToken.DashedIdenTokenType) {
|
|
56
|
+
const info = (variableScope.get(arena.strings[arena.stringValue[first]]));
|
|
57
|
+
// the variable declaration may have been released
|
|
58
|
+
if (info?.replaceable && arena.name[info.node] != 0 && arena.parents[value] != 0) {
|
|
59
|
+
arena.replaceToken(value, ...arena.tokens(info.node).map((t) => arena.clone(t, true)));
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
}
|
|
@@ -76,78 +76,71 @@ class InlineCssVariablesFeature {
|
|
|
76
76
|
options.features.push(new InlineCssVariablesFeature());
|
|
77
77
|
}
|
|
78
78
|
}
|
|
79
|
-
run(ast,
|
|
80
|
-
if (!(
|
|
79
|
+
run(arena, ast, _options = {}, parent, context) {
|
|
80
|
+
if (!(arena.flags[ast] & EnumAstNodeStatus.HasChildren)) {
|
|
81
81
|
return null;
|
|
82
82
|
}
|
|
83
83
|
if (!("variableScope" in context)) {
|
|
84
84
|
context.variableScope = new Map();
|
|
85
85
|
}
|
|
86
86
|
// [':root', 'html']
|
|
87
|
-
const isRoot = parent
|
|
88
|
-
ast
|
|
89
|
-
(ast
|
|
87
|
+
const isRoot = arena.dataTypes[parent] == EnumToken.StyleSheetNodeType &&
|
|
88
|
+
arena.dataTypes[ast] == EnumToken.RuleNodeType &&
|
|
89
|
+
(arena.raw[ast] ?? splitRule(arena.strings[arena.name[ast]])).some((segment) => segment.some((s) => s == ":root" || s == "html"));
|
|
90
90
|
const variableScope = context.variableScope;
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
if (node.typ !== EnumToken.DeclarationNodeType) {
|
|
91
|
+
for (let node = arena.firstChild[ast]; node != 0; node = arena.nextSibling[node]) {
|
|
92
|
+
if (arena.dataTypes[node] !== EnumToken.DeclarationNodeType) {
|
|
94
93
|
continue;
|
|
95
94
|
}
|
|
95
|
+
const name = arena.strings[arena.name[node]];
|
|
96
96
|
// css variable
|
|
97
|
-
if (
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
}
|
|
120
|
-
if (recursive) {
|
|
121
|
-
replace(node, variableScope);
|
|
97
|
+
if (name.startsWith("--")) {
|
|
98
|
+
// the last declaration wins
|
|
99
|
+
const info = {
|
|
100
|
+
globalScope: isRoot,
|
|
101
|
+
// @ts-ignore
|
|
102
|
+
parent: new Set(),
|
|
103
|
+
declarationCount: 1,
|
|
104
|
+
replaceable: isRoot,
|
|
105
|
+
node: node,
|
|
106
|
+
values: arena.tokens(node).map((t) => arena.clone(t, true)),
|
|
107
|
+
};
|
|
108
|
+
info.parent.add(ast);
|
|
109
|
+
variableScope.set(name, info);
|
|
110
|
+
let recursive = false;
|
|
111
|
+
// inlined: arena.walkValues(node)
|
|
112
|
+
for (const value of new ValueWalker(arena, node)) {
|
|
113
|
+
if (arena.dataTypes[value] == EnumToken.MathFunctionTokenDefType ||
|
|
114
|
+
(arena.dataTypes[value] == EnumToken.WildCardFunctionTokenType &&
|
|
115
|
+
(mathFuncs.includes(arena.strings[arena.stringValue[value]]) ||
|
|
116
|
+
arena.strings[arena.stringValue[value]] === "var"))) {
|
|
117
|
+
recursive = true;
|
|
118
|
+
break;
|
|
122
119
|
}
|
|
123
120
|
}
|
|
121
|
+
if (recursive) {
|
|
122
|
+
replace(arena, node, variableScope);
|
|
123
|
+
}
|
|
124
124
|
}
|
|
125
125
|
else {
|
|
126
|
-
replace(node, variableScope);
|
|
126
|
+
replace(arena, node, variableScope);
|
|
127
127
|
}
|
|
128
128
|
}
|
|
129
129
|
return null;
|
|
130
130
|
}
|
|
131
|
-
cleanup(
|
|
131
|
+
cleanup(arena, _ast, _options = {}, context) {
|
|
132
132
|
const variableScope = context.variableScope;
|
|
133
133
|
if (variableScope == null) {
|
|
134
134
|
return;
|
|
135
135
|
}
|
|
136
136
|
for (const info of variableScope.values()) {
|
|
137
137
|
if (info.replaceable) {
|
|
138
|
-
let i;
|
|
139
138
|
// drop declarations from :root{}
|
|
140
139
|
for (const parent of info.parent) {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
parent.chi.splice(i, 1, {
|
|
146
|
-
typ: EnumToken.CommentTokenType,
|
|
147
|
-
val: `/* ${info.node.nam}: ${info.values.reduce((acc, curr) => acc + renderValue(curr, { convertColor: false }), "")} */`,
|
|
148
|
-
});
|
|
149
|
-
break;
|
|
150
|
-
}
|
|
140
|
+
if (arena.parents[info.node] == parent) {
|
|
141
|
+
const comment = arena.createComment(EnumToken.CommentTokenType, `/* ${arena.strings[arena.name[info.node]]}: ${info.values.reduce((acc, curr) => acc + renderValue(arena, curr, { convertColor: false }), "")} */`);
|
|
142
|
+
arena.replaceChild(info.node, comment);
|
|
143
|
+
arena.detach(info.node);
|
|
151
144
|
}
|
|
152
145
|
}
|
|
153
146
|
}
|