@tbela99/css-parser 2.0.1 → 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 +86 -1
- package/README.md +15 -7
- package/architecture.png +0 -0
- package/dist/{src/data → data}/properties.json.js +86 -2
- package/dist/{src/data → data}/validation.json.js +4848 -3750
- package/dist/index-umd-web.js +18234 -9498
- package/dist/index.d.ts +1251 -373
- package/dist/{src/lib → lib}/ast/arena-node.js +119 -57
- package/dist/{src/lib → lib}/ast/arena.js +1034 -655
- package/dist/{src/lib → lib}/ast/cache.js +36 -31
- package/dist/{src/lib → lib}/ast/features/calc.js +37 -27
- package/dist/{src/lib → lib}/ast/features/if.js +24 -19
- package/dist/{src/lib → lib}/ast/features/index.js +3 -2
- package/dist/{src/lib → lib}/ast/features/inlinecssvariables.js +42 -23
- package/dist/lib/ast/features/mixin.js +953 -0
- package/dist/{src/lib → lib}/ast/features/prefix.js +82 -69
- 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 +150 -150
- package/dist/{src/lib → lib}/ast/math/math.js +8 -8
- package/dist/{src/lib → lib}/ast/minify.js +932 -354
- 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 +7 -15
- package/dist/{src/lib → lib}/ast/walk.js +19 -19
- package/dist/{src/lib → lib}/fs/resolve.js +15 -15
- package/dist/{src/lib → lib}/parser/declaration/list.js +55 -26
- package/dist/{src/lib → lib}/parser/declaration/map.js +85 -72
- package/dist/{src/lib → lib}/parser/declaration/set.js +32 -27
- package/dist/lib/parser/input.js +85 -0
- package/dist/{src/lib → lib}/parser/linesmap.js +3 -16
- package/dist/{src/lib → lib}/parser/parse.js +852 -619
- package/dist/lib/parser/source.js +71 -0
- package/dist/{src/lib → lib}/parser/tokenize.js +886 -722
- package/dist/{src/lib → lib}/parser/utils/angle.js +3 -3
- package/dist/{src/lib → lib}/parser/utils/at-rule-container.js +68 -35
- 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 +31 -13
- package/dist/{src/lib → lib}/parser/utils/at-rule-import.js +29 -16
- package/dist/{src/lib → lib}/parser/utils/at-rule-media.js +77 -58
- package/dist/{src/lib → lib}/parser/utils/at-rule-support.js +18 -20
- package/dist/{src/lib → lib}/parser/utils/at-rule-when-else.js +9 -8
- 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 +67 -49
- package/dist/{src/lib → lib}/parser/utils/eq.js +14 -14
- package/dist/{src/lib → lib}/parser/utils/hash.js +4 -4
- package/dist/lib/parser/utils/selector.js +720 -0
- 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 +11 -9
- package/dist/{src/lib → lib}/printer/render.js +536 -243
- package/dist/lib/printer/result.js +148 -0
- package/dist/{src/lib → lib}/printer/sourcemap/lib/codec.js +5 -8
- package/dist/{src/lib → lib}/printer/sourcemap/sourcemap.js +13 -13
- package/dist/lib/printer/writer.js +252 -0
- package/dist/{src/lib → lib}/syntax/color/a98rgb.js +6 -6
- package/dist/{src/lib → lib}/syntax/color/alpha.js +12 -11
- package/dist/{src/lib → lib}/syntax/color/cmyk.js +11 -6
- package/dist/{src/lib → lib}/syntax/color/color-mix.js +78 -67
- package/dist/{src/lib → lib}/syntax/color/color.js +52 -47
- package/dist/{src/lib → lib}/syntax/color/hex.js +17 -17
- package/dist/{src/lib → lib}/syntax/color/hsl.js +8 -8
- package/dist/{src/lib → lib}/syntax/color/hwb.js +6 -7
- package/dist/{src/lib → lib}/syntax/color/lab.js +8 -8
- package/dist/{src/lib → lib}/syntax/color/lch.js +12 -11
- package/dist/{src/lib → lib}/syntax/color/oklab.js +9 -9
- package/dist/{src/lib → lib}/syntax/color/oklch.js +5 -5
- package/dist/{src/lib → lib}/syntax/color/p3.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/prophotorgb.js +4 -4
- package/dist/{src/lib → lib}/syntax/color/rec2020.js +3 -3
- package/dist/{src/lib → lib}/syntax/color/relative-color.js +41 -34
- package/dist/{src/lib → lib}/syntax/color/rgb.js +17 -17
- package/dist/{src/lib → lib}/syntax/color/srgb.js +20 -20
- package/dist/{src/lib → lib}/syntax/color/utils/color-type.js +6 -4
- package/dist/{src/lib → lib}/syntax/color/utils/colorspace.js +12 -14
- package/dist/{src/lib → lib}/syntax/color/utils/components.js +13 -13
- package/dist/{src/lib → lib}/syntax/color/utils/distance.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/utils/make-color.js +6 -4
- package/dist/{src/lib → lib}/syntax/color/utils/matrix.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/xyz.js +5 -10
- package/dist/{src/lib → lib}/syntax/color/xyzd50.js +3 -3
- package/dist/{src/lib → lib}/syntax/constants.js +30 -3
- package/dist/{src/lib → lib}/syntax/syntax.js +151 -116
- package/dist/lib/validation/config.js +173 -0
- package/dist/lib/validation/first.js +571 -0
- package/dist/{src/lib → lib}/validation/match.js +1256 -579
- package/dist/{src/lib → lib}/validation/parser/parse.js +85 -59
- package/dist/{src/lib → lib}/validation/parser/typedef.js +6 -2
- package/dist/{src/lib → lib}/validation/utils/list.js +3 -19
- package/dist/{src/node.js → node.js} +92 -80
- package/dist/{src/types.js → types.js} +2 -2
- package/dist/{src/utils → utils}/sync.js +13 -8
- package/dist/version.d.ts +4 -0
- package/dist/version.js +13 -0
- package/dist/{src/web.js → web.js} +80 -75
- package/package.json +13 -13
- package/dist/package.json.js +0 -10
- package/dist/src/lib/ast/features/shorthand.js +0 -87
- package/dist/src/lib/ast/features/transform.js +0 -85
- package/dist/src/lib/ast/transform/compute.js +0 -358
- 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/source.js +0 -127
- package/dist/src/lib/parser/utils/cache.js +0 -54
- package/dist/src/lib/parser/utils/selector.js +0 -440
- package/dist/src/lib/validation/config.js +0 -120
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/*
|
|
2
|
+
CSS parser, minifier and validator for node, deno, bun and the browser
|
|
3
|
+
|
|
4
|
+
Author(s): Thierry Bela
|
|
5
|
+
Version: 2.2.0
|
|
6
|
+
License: MIT
|
|
7
|
+
*/
|
|
8
|
+
import { EnumToken, EnumAstNodeStatus } from '../types.js';
|
|
9
|
+
import { transformKind, compute } from '../transform/compute.js';
|
|
10
|
+
import { filterValues, renderValue } from '../../printer/render.js';
|
|
11
|
+
import { minifyTransformFunctions, eqMatrix } from '../transform/minify.js';
|
|
12
|
+
import { FeatureWalkMode } from './type.js';
|
|
13
|
+
import { StructuralMemo } from '../cache.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* outcome of the minification of a transform value that is not a list of new tokens, see {@link TransformMemo}
|
|
17
|
+
*/
|
|
18
|
+
var Outcome;
|
|
19
|
+
(function (Outcome) {
|
|
20
|
+
/**
|
|
21
|
+
* the matrix cannot be computed: the transform functions are minified one by one
|
|
22
|
+
*/
|
|
23
|
+
Outcome[Outcome["Failed"] = 0] = "Failed";
|
|
24
|
+
/**
|
|
25
|
+
* the transform functions minified one by one are the shortest form
|
|
26
|
+
*/
|
|
27
|
+
Outcome[Outcome["Functions"] = 1] = "Functions";
|
|
28
|
+
/**
|
|
29
|
+
* the shortest form reuses tokens of the value: it is computed for every declaration
|
|
30
|
+
*/
|
|
31
|
+
Outcome[Outcome["Uncached"] = 2] = "Uncached";
|
|
32
|
+
})(Outcome || (Outcome = {}));
|
|
33
|
+
/**
|
|
34
|
+
* Results of the minification of the transform values, shared by the rules of a minification and valid for the arena
|
|
35
|
+
* they were computed for. Identical values are common (`transform: translate3d(0, 0, 0)`, the keyframes of the
|
|
36
|
+
* animation libraries, ...): the matrix of a value is computed once per distinct value. A value whose shortest form
|
|
37
|
+
* only contains new tokens (`none`, `matrix()`, the minified functions) is stored as detached tokens that are cloned.
|
|
38
|
+
*
|
|
39
|
+
* @internal
|
|
40
|
+
*/
|
|
41
|
+
class TransformMemo {
|
|
42
|
+
/**
|
|
43
|
+
* An instance is kept alive: the hidden class of the instances would otherwise be collected by a full garbage
|
|
44
|
+
* collection when no instance is alive, and the optimized code that depends on it discarded (deoptimized)
|
|
45
|
+
*/
|
|
46
|
+
static shape = new TransformMemo();
|
|
47
|
+
/**
|
|
48
|
+
* the outcome or the detached tokens of the shortest form, by value structure
|
|
49
|
+
*/
|
|
50
|
+
values = new StructuralMemo();
|
|
51
|
+
}
|
|
52
|
+
class TransformCssFeature {
|
|
53
|
+
accept = new Set([
|
|
54
|
+
EnumToken.RuleNodeType,
|
|
55
|
+
EnumToken.AtRuleNodeType,
|
|
56
|
+
EnumToken.KeyframesRuleNodeType,
|
|
57
|
+
]);
|
|
58
|
+
get ordering() {
|
|
59
|
+
return 4;
|
|
60
|
+
}
|
|
61
|
+
get processMode() {
|
|
62
|
+
return FeatureWalkMode.Post;
|
|
63
|
+
}
|
|
64
|
+
static register(options) {
|
|
65
|
+
// @ts-ignore
|
|
66
|
+
if (options.computeTransform) {
|
|
67
|
+
// @ts-ignore
|
|
68
|
+
options.features.push(new TransformCssFeature());
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
*
|
|
73
|
+
* @param arena
|
|
74
|
+
* @param ast
|
|
75
|
+
* @param _options
|
|
76
|
+
* @param _parent
|
|
77
|
+
* @param context minification context: the results are shared by the rules of the minification
|
|
78
|
+
*/
|
|
79
|
+
run(arena, ast, _options, _parent, context) {
|
|
80
|
+
if (!(arena.flags[ast] & EnumAstNodeStatus.HasChildren)) {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
let memo = null;
|
|
84
|
+
let node;
|
|
85
|
+
for (node = arena.firstChild[ast]; node !== 0; node = arena.nextSibling[node]) {
|
|
86
|
+
if (arena.flags[node] & (EnumAstNodeStatus.Invalid | EnumAstNodeStatus.ValidationFailed)) {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (arena.dataTypes[node] !== EnumToken.DeclarationNodeType ||
|
|
90
|
+
(transformKind(arena, arena.name[node]) & 255 /* TransformKind.KindMask */) !== 22 /* TransformKind.Property */) {
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
let slot = -1;
|
|
94
|
+
let outcome;
|
|
95
|
+
if (context != null) {
|
|
96
|
+
// created with the first transform declaration of the minification
|
|
97
|
+
if (memo === null) {
|
|
98
|
+
memo = (context.transforms ??= new TransformMemo()).values;
|
|
99
|
+
}
|
|
100
|
+
slot = memo.list(arena, arena.firstToken[node]);
|
|
101
|
+
// inlined: memo.get(slot)
|
|
102
|
+
outcome = memo.values[slot];
|
|
103
|
+
if (Array.isArray(outcome)) {
|
|
104
|
+
const tokens = new Array(outcome.length);
|
|
105
|
+
for (let i = 0; i < outcome.length; i++) {
|
|
106
|
+
tokens[i] = arena.clone(outcome[i], true);
|
|
107
|
+
}
|
|
108
|
+
arena.setTokenList(node, tokens);
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const children = [];
|
|
113
|
+
for (let child = arena.firstToken[node]; child !== 0; child = arena.nextSibling[child]) {
|
|
114
|
+
children.push(arena.dataTypes[child] === EnumToken.TransformFunctionTokenType
|
|
115
|
+
? minifyTransformFunctions(arena, child)
|
|
116
|
+
: child);
|
|
117
|
+
}
|
|
118
|
+
if (outcome === 0 /* Outcome.Failed */) {
|
|
119
|
+
arena.setTokenList(node, children);
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
if (outcome === 1 /* Outcome.Functions */) {
|
|
123
|
+
arena.setTokenList(node, filterValues(arena, children));
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
const computed = compute(arena, children);
|
|
127
|
+
if (computed == null) {
|
|
128
|
+
if (slot !== -1) {
|
|
129
|
+
memo.values[slot] = 0 /* Outcome.Failed */;
|
|
130
|
+
}
|
|
131
|
+
arena.setTokenList(node, children);
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
// the shortest form: the functions minified one by one, the cumulative list, the minified matrix. a
|
|
135
|
+
// candidate replaces the matrix if its rendered value is shorter
|
|
136
|
+
const length = renderValue(arena, computed.matrix).length;
|
|
137
|
+
let result = null;
|
|
138
|
+
if (renderedLength(arena, filterValues(arena, children)) < length) {
|
|
139
|
+
result = children;
|
|
140
|
+
}
|
|
141
|
+
if (eqMatrix(arena, computed.values, computed.cumulative) &&
|
|
142
|
+
renderedLength(arena, computed.cumulative) < length) {
|
|
143
|
+
result = computed.cumulative;
|
|
144
|
+
}
|
|
145
|
+
if (eqMatrix(arena, computed.values, computed.minified) &&
|
|
146
|
+
renderedLength(arena, computed.minified) < length) {
|
|
147
|
+
result = computed.minified;
|
|
148
|
+
}
|
|
149
|
+
if (slot !== -1) {
|
|
150
|
+
if (result === children) {
|
|
151
|
+
memo.values[slot] = 1 /* Outcome.Functions */;
|
|
152
|
+
}
|
|
153
|
+
else if (result === computed.cumulative && !computed.created) {
|
|
154
|
+
memo.values[slot] = 2 /* Outcome.Uncached */;
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
// detached copies: the tokens of the declaration may be modified by the next features
|
|
158
|
+
const template = result === null ? [arena.clone(computed.matrix, true)] : new Array(result.length);
|
|
159
|
+
for (let i = 0; result !== null && i < result.length; i++) {
|
|
160
|
+
template[i] = arena.clone(result[i], true);
|
|
161
|
+
}
|
|
162
|
+
memo.values[slot] = template;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (result === null) {
|
|
166
|
+
arena.setTokenList(node, [computed.matrix]);
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
arena.setTokenList(node, result);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* length of the rendered tokens
|
|
177
|
+
* @param arena
|
|
178
|
+
* @param tokens
|
|
179
|
+
*/
|
|
180
|
+
function renderedLength(arena, tokens) {
|
|
181
|
+
let length = 0;
|
|
182
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
183
|
+
length += renderValue(arena, tokens[i]).length;
|
|
184
|
+
}
|
|
185
|
+
return length;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export { TransformCssFeature };
|