@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
|
@@ -0,0 +1,560 @@
|
|
|
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 { setIdentity, multiplyInto, toZero } from './utils.js';
|
|
9
|
+
import { EnumToken } from '../types.js';
|
|
10
|
+
import { translateX, translateY, translateZ, translate, translate3d } from './translate.js';
|
|
11
|
+
import { getAngle, getNumber } from '../../syntax/color/color.js';
|
|
12
|
+
import { rotate, rotate3D } from './rotate.js';
|
|
13
|
+
import { scale3d, scale, scaleX, scaleY, scaleZ } from './scale.js';
|
|
14
|
+
import { minify } from './minify.js';
|
|
15
|
+
import { skew, skewX, skewY } from './skew.js';
|
|
16
|
+
import { serialize, setMatrixValues } from './matrix.js';
|
|
17
|
+
import { perspective } from './perspective.js';
|
|
18
|
+
import { length2Px } from '../../syntax/syntax.js';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* kind of a transform function name, and family of the name (see {@link transformKind})
|
|
22
|
+
*/
|
|
23
|
+
var TransformKind;
|
|
24
|
+
(function (TransformKind) {
|
|
25
|
+
TransformKind[TransformKind["None"] = 0] = "None";
|
|
26
|
+
TransformKind[TransformKind["Translate"] = 1] = "Translate";
|
|
27
|
+
TransformKind[TransformKind["TranslateX"] = 2] = "TranslateX";
|
|
28
|
+
TransformKind[TransformKind["TranslateY"] = 3] = "TranslateY";
|
|
29
|
+
TransformKind[TransformKind["TranslateZ"] = 4] = "TranslateZ";
|
|
30
|
+
TransformKind[TransformKind["Translate3d"] = 5] = "Translate3d";
|
|
31
|
+
TransformKind[TransformKind["Rotate"] = 6] = "Rotate";
|
|
32
|
+
TransformKind[TransformKind["RotateX"] = 7] = "RotateX";
|
|
33
|
+
TransformKind[TransformKind["RotateY"] = 8] = "RotateY";
|
|
34
|
+
TransformKind[TransformKind["RotateZ"] = 9] = "RotateZ";
|
|
35
|
+
TransformKind[TransformKind["Rotate3d"] = 10] = "Rotate3d";
|
|
36
|
+
TransformKind[TransformKind["Scale"] = 11] = "Scale";
|
|
37
|
+
TransformKind[TransformKind["ScaleX"] = 12] = "ScaleX";
|
|
38
|
+
TransformKind[TransformKind["ScaleY"] = 13] = "ScaleY";
|
|
39
|
+
TransformKind[TransformKind["ScaleZ"] = 14] = "ScaleZ";
|
|
40
|
+
TransformKind[TransformKind["Scale3d"] = 15] = "Scale3d";
|
|
41
|
+
TransformKind[TransformKind["Skew"] = 16] = "Skew";
|
|
42
|
+
TransformKind[TransformKind["SkewX"] = 17] = "SkewX";
|
|
43
|
+
TransformKind[TransformKind["SkewY"] = 18] = "SkewY";
|
|
44
|
+
TransformKind[TransformKind["Perspective"] = 19] = "Perspective";
|
|
45
|
+
TransformKind[TransformKind["Matrix"] = 20] = "Matrix";
|
|
46
|
+
TransformKind[TransformKind["Matrix3d"] = 21] = "Matrix3d";
|
|
47
|
+
/**
|
|
48
|
+
* the name of the `transform` property or of a prefixed version (`-webkit-transform`)
|
|
49
|
+
*/
|
|
50
|
+
TransformKind[TransformKind["Property"] = 22] = "Property";
|
|
51
|
+
/**
|
|
52
|
+
* mask of the kind
|
|
53
|
+
*/
|
|
54
|
+
TransformKind[TransformKind["KindMask"] = 255] = "KindMask";
|
|
55
|
+
/**
|
|
56
|
+
* the name starts with `translate`
|
|
57
|
+
*/
|
|
58
|
+
TransformKind[TransformKind["TranslateFamily"] = 256] = "TranslateFamily";
|
|
59
|
+
/**
|
|
60
|
+
* the name starts with `rotate`
|
|
61
|
+
*/
|
|
62
|
+
TransformKind[TransformKind["RotateFamily"] = 512] = "RotateFamily";
|
|
63
|
+
/**
|
|
64
|
+
* the name starts with `scale`
|
|
65
|
+
*/
|
|
66
|
+
TransformKind[TransformKind["ScaleFamily"] = 768] = "ScaleFamily";
|
|
67
|
+
/**
|
|
68
|
+
* mask of the family
|
|
69
|
+
*/
|
|
70
|
+
TransformKind[TransformKind["FamilyMask"] = 768] = "FamilyMask";
|
|
71
|
+
})(TransformKind || (TransformKind = {}));
|
|
72
|
+
/**
|
|
73
|
+
* the transform functions, by name (case-sensitive)
|
|
74
|
+
*/
|
|
75
|
+
const transformKinds = new Map([
|
|
76
|
+
["translate", 1 /* TransformKind.Translate */],
|
|
77
|
+
["translateX", 2 /* TransformKind.TranslateX */],
|
|
78
|
+
["translateY", 3 /* TransformKind.TranslateY */],
|
|
79
|
+
["translateZ", 4 /* TransformKind.TranslateZ */],
|
|
80
|
+
["translate3d", 5 /* TransformKind.Translate3d */],
|
|
81
|
+
["rotate", 6 /* TransformKind.Rotate */],
|
|
82
|
+
["rotateX", 7 /* TransformKind.RotateX */],
|
|
83
|
+
["rotateY", 8 /* TransformKind.RotateY */],
|
|
84
|
+
["rotateZ", 9 /* TransformKind.RotateZ */],
|
|
85
|
+
["rotate3d", 10 /* TransformKind.Rotate3d */],
|
|
86
|
+
["scale", 11 /* TransformKind.Scale */],
|
|
87
|
+
["scaleX", 12 /* TransformKind.ScaleX */],
|
|
88
|
+
["scaleY", 13 /* TransformKind.ScaleY */],
|
|
89
|
+
["scaleZ", 14 /* TransformKind.ScaleZ */],
|
|
90
|
+
["scale3d", 15 /* TransformKind.Scale3d */],
|
|
91
|
+
["skew", 16 /* TransformKind.Skew */],
|
|
92
|
+
["skewX", 17 /* TransformKind.SkewX */],
|
|
93
|
+
["skewY", 18 /* TransformKind.SkewY */],
|
|
94
|
+
["perspective", 19 /* TransformKind.Perspective */],
|
|
95
|
+
["matrix", 20 /* TransformKind.Matrix */],
|
|
96
|
+
["matrix3d", 21 /* TransformKind.Matrix3d */],
|
|
97
|
+
]);
|
|
98
|
+
/**
|
|
99
|
+
* the transform property and its prefixed versions
|
|
100
|
+
*/
|
|
101
|
+
const transformProperty = /^(-[a-z]+-)?transform$/;
|
|
102
|
+
/**
|
|
103
|
+
* the arena of the strings read by {@link computeTransformKind}: the function passed to the string table is not a
|
|
104
|
+
* closure (no allocation per call)
|
|
105
|
+
*/
|
|
106
|
+
let kindArena = null;
|
|
107
|
+
/**
|
|
108
|
+
* see {@link transformKind}
|
|
109
|
+
* @param id
|
|
110
|
+
*/
|
|
111
|
+
function computeTransformKind(id) {
|
|
112
|
+
const name = kindArena.resolveString(id);
|
|
113
|
+
let kind = transformKinds.get(name) ?? 0 /* TransformKind.None */;
|
|
114
|
+
if (kind === 0 /* TransformKind.None */ && transformProperty.test(name)) {
|
|
115
|
+
return 22 /* TransformKind.Property */;
|
|
116
|
+
}
|
|
117
|
+
if (name.startsWith("translate")) {
|
|
118
|
+
kind |= 256 /* TransformKind.TranslateFamily */;
|
|
119
|
+
}
|
|
120
|
+
else if (name.startsWith("rotate")) {
|
|
121
|
+
kind |= 512 /* TransformKind.RotateFamily */;
|
|
122
|
+
}
|
|
123
|
+
else if (name.startsWith("scale")) {
|
|
124
|
+
kind |= 768 /* TransformKind.ScaleFamily */;
|
|
125
|
+
}
|
|
126
|
+
return kind;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* the kind of a string ({@link TransformKind.KindMask}: transform function or transform property) and the family of a
|
|
130
|
+
* transform function name ({@link TransformKind.FamilyMask}). computed once per interned string
|
|
131
|
+
* @param arena
|
|
132
|
+
* @param id interned string id
|
|
133
|
+
*/
|
|
134
|
+
function transformKind(arena, id) {
|
|
135
|
+
kindArena = arena;
|
|
136
|
+
return arena.stringDerived(id, 2 /* StringDerived.TransformKind */, computeTransformKind);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* numeric arguments of a transform function (the first 16 arguments), reused
|
|
140
|
+
*/
|
|
141
|
+
const args = new Float64Array(16);
|
|
142
|
+
/**
|
|
143
|
+
* the matrix of the values of `matrix()` and `matrix3d()`
|
|
144
|
+
*/
|
|
145
|
+
const valuesMatrix = new Float64Array(16);
|
|
146
|
+
/**
|
|
147
|
+
* multiply a matrix in place by the matrix of a transform function
|
|
148
|
+
* @param arena
|
|
149
|
+
* @param token
|
|
150
|
+
* @param matrixVar
|
|
151
|
+
* @returns false if the matrix cannot be computed. the matrix is then partially updated
|
|
152
|
+
*/
|
|
153
|
+
function computeTransform(arena, token, matrixVar) {
|
|
154
|
+
if (arena.dataTypes[token] !== EnumToken.TransformFunctionTokenType) {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
const kind = transformKind(arena, arena.stringValue[token]) & 255 /* TransformKind.KindMask */;
|
|
158
|
+
// number of arguments
|
|
159
|
+
let count = 0;
|
|
160
|
+
let child;
|
|
161
|
+
let typ;
|
|
162
|
+
switch (kind) {
|
|
163
|
+
case 1 /* TransformKind.Translate */:
|
|
164
|
+
case 2 /* TransformKind.TranslateX */:
|
|
165
|
+
case 3 /* TransformKind.TranslateY */:
|
|
166
|
+
case 4 /* TransformKind.TranslateZ */:
|
|
167
|
+
case 5 /* TransformKind.Translate3d */: {
|
|
168
|
+
let val;
|
|
169
|
+
for (child = arena.firstToken[token]; child !== 0; child = arena.nextSibling[child]) {
|
|
170
|
+
typ = arena.dataTypes[child];
|
|
171
|
+
if (typ === EnumToken.CommentTokenType ||
|
|
172
|
+
typ === EnumToken.CommaTokenType ||
|
|
173
|
+
typ === EnumToken.WhitespaceTokenType) {
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
val = length2Px(arena, child);
|
|
177
|
+
if (val == null) {
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
if (count < 16) {
|
|
181
|
+
args[count] = val;
|
|
182
|
+
}
|
|
183
|
+
count++;
|
|
184
|
+
}
|
|
185
|
+
if (count === 0 ||
|
|
186
|
+
count > (kind === 5 /* TransformKind.Translate3d */ ? 3 : kind === 1 /* TransformKind.Translate */ ? 2 : 1)) {
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
if (kind === 2 /* TransformKind.TranslateX */) {
|
|
190
|
+
translateX(args[0], matrixVar);
|
|
191
|
+
}
|
|
192
|
+
else if (kind === 3 /* TransformKind.TranslateY */) {
|
|
193
|
+
translateY(args[0], matrixVar);
|
|
194
|
+
}
|
|
195
|
+
else if (kind === 4 /* TransformKind.TranslateZ */) {
|
|
196
|
+
translateZ(args[0], matrixVar);
|
|
197
|
+
}
|
|
198
|
+
else if (kind === 1 /* TransformKind.Translate */) {
|
|
199
|
+
translate(args[0], count > 1 ? args[1] : undefined, matrixVar);
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
translate3d(args[0], count > 1 ? args[1] : undefined, count > 2 ? args[2] : undefined, matrixVar);
|
|
203
|
+
}
|
|
204
|
+
return true;
|
|
205
|
+
}
|
|
206
|
+
case 6 /* TransformKind.Rotate */:
|
|
207
|
+
case 7 /* TransformKind.RotateX */:
|
|
208
|
+
case 8 /* TransformKind.RotateY */:
|
|
209
|
+
case 9 /* TransformKind.RotateZ */:
|
|
210
|
+
case 10 /* TransformKind.Rotate3d */: {
|
|
211
|
+
let x = 0;
|
|
212
|
+
let y = 0;
|
|
213
|
+
let z = 0;
|
|
214
|
+
// the first three arguments and the last argument
|
|
215
|
+
let first = 0;
|
|
216
|
+
let second = 0;
|
|
217
|
+
let third = 0;
|
|
218
|
+
let last = 0;
|
|
219
|
+
// the whitespace tokens are arguments
|
|
220
|
+
for (child = arena.firstToken[token]; child !== 0; child = arena.nextSibling[child]) {
|
|
221
|
+
typ = arena.dataTypes[child];
|
|
222
|
+
if (typ === EnumToken.CommentTokenType || typ === EnumToken.CommaTokenType) {
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
if (count === 0) {
|
|
226
|
+
first = child;
|
|
227
|
+
}
|
|
228
|
+
else if (count === 1) {
|
|
229
|
+
second = child;
|
|
230
|
+
}
|
|
231
|
+
else if (count === 2) {
|
|
232
|
+
third = child;
|
|
233
|
+
}
|
|
234
|
+
last = child;
|
|
235
|
+
count++;
|
|
236
|
+
}
|
|
237
|
+
if (count !== (kind === 10 /* TransformKind.Rotate3d */ ? 4 : 1)) {
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
if (kind === 7 /* TransformKind.RotateX */) {
|
|
241
|
+
x = 1;
|
|
242
|
+
}
|
|
243
|
+
else if (kind === 8 /* TransformKind.RotateY */) {
|
|
244
|
+
y = 1;
|
|
245
|
+
}
|
|
246
|
+
else if (kind === 6 /* TransformKind.Rotate */ || kind === 9 /* TransformKind.RotateZ */) {
|
|
247
|
+
z = 1;
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
x = getNumber(arena, first);
|
|
251
|
+
y = getNumber(arena, second);
|
|
252
|
+
z = getNumber(arena, third);
|
|
253
|
+
}
|
|
254
|
+
const angle = getAngle(arena, last);
|
|
255
|
+
if (Number.isNaN(x) || Number.isNaN(y) || Number.isNaN(z) || Number.isNaN(angle)) {
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
258
|
+
if (kind === 6 /* TransformKind.Rotate */ || kind === 9 /* TransformKind.RotateZ */) {
|
|
259
|
+
rotate(angle * 2 * Math.PI, matrixVar);
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
rotate3D(angle * 2 * Math.PI, x, y, z, matrixVar);
|
|
263
|
+
}
|
|
264
|
+
return true;
|
|
265
|
+
}
|
|
266
|
+
case 11 /* TransformKind.Scale */:
|
|
267
|
+
case 12 /* TransformKind.ScaleX */:
|
|
268
|
+
case 13 /* TransformKind.ScaleY */:
|
|
269
|
+
case 14 /* TransformKind.ScaleZ */:
|
|
270
|
+
case 15 /* TransformKind.Scale3d */:
|
|
271
|
+
for (child = arena.firstToken[token]; child !== 0; child = arena.nextSibling[child]) {
|
|
272
|
+
typ = arena.dataTypes[child];
|
|
273
|
+
if (typ === EnumToken.CommentTokenType || typ === EnumToken.CommaTokenType) {
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
// whitespace tokens are not allowed
|
|
277
|
+
if (typ !== EnumToken.NumberTokenType && typ !== EnumToken.PercentageTokenType) {
|
|
278
|
+
return false;
|
|
279
|
+
}
|
|
280
|
+
if (count < 16) {
|
|
281
|
+
args[count] = getNumber(arena, child);
|
|
282
|
+
}
|
|
283
|
+
count++;
|
|
284
|
+
}
|
|
285
|
+
if (kind === 15 /* TransformKind.Scale3d */) {
|
|
286
|
+
if (count !== 3) {
|
|
287
|
+
return false;
|
|
288
|
+
}
|
|
289
|
+
scale3d(args[0], args[1], args[2], matrixVar);
|
|
290
|
+
}
|
|
291
|
+
else if (kind === 11 /* TransformKind.Scale */) {
|
|
292
|
+
if (count !== 1 && count !== 2) {
|
|
293
|
+
return false;
|
|
294
|
+
}
|
|
295
|
+
scale(args[0], count > 1 ? args[1] : args[0], matrixVar);
|
|
296
|
+
}
|
|
297
|
+
else if (count !== 1) {
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
else if (kind === 12 /* TransformKind.ScaleX */) {
|
|
301
|
+
scaleX(args[0], matrixVar);
|
|
302
|
+
}
|
|
303
|
+
else if (kind === 13 /* TransformKind.ScaleY */) {
|
|
304
|
+
scaleY(args[0], matrixVar);
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
scaleZ(args[0], matrixVar);
|
|
308
|
+
}
|
|
309
|
+
return true;
|
|
310
|
+
case 16 /* TransformKind.Skew */:
|
|
311
|
+
case 17 /* TransformKind.SkewX */:
|
|
312
|
+
case 18 /* TransformKind.SkewY */:
|
|
313
|
+
for (child = arena.firstToken[token]; child !== 0; child = arena.nextSibling[child]) {
|
|
314
|
+
typ = arena.dataTypes[child];
|
|
315
|
+
if (typ === EnumToken.CommentTokenType ||
|
|
316
|
+
typ === EnumToken.CommaTokenType ||
|
|
317
|
+
typ === EnumToken.WhitespaceTokenType) {
|
|
318
|
+
continue;
|
|
319
|
+
}
|
|
320
|
+
if (count < 16) {
|
|
321
|
+
args[count] = getAngle(arena, child) * 2 * Math.PI;
|
|
322
|
+
}
|
|
323
|
+
count++;
|
|
324
|
+
}
|
|
325
|
+
if (count === 0 || count > (kind === 16 /* TransformKind.Skew */ ? 2 : 1)) {
|
|
326
|
+
return false;
|
|
327
|
+
}
|
|
328
|
+
if (kind === 16 /* TransformKind.Skew */) {
|
|
329
|
+
skew(args[0], count > 1 ? args[1] : undefined, matrixVar);
|
|
330
|
+
}
|
|
331
|
+
else if (kind === 17 /* TransformKind.SkewX */) {
|
|
332
|
+
skewX(args[0], matrixVar);
|
|
333
|
+
}
|
|
334
|
+
else {
|
|
335
|
+
skewY(args[0], matrixVar);
|
|
336
|
+
}
|
|
337
|
+
return true;
|
|
338
|
+
case 19 /* TransformKind.Perspective */: {
|
|
339
|
+
// null for `none`
|
|
340
|
+
let distance = null;
|
|
341
|
+
let val;
|
|
342
|
+
for (child = arena.firstToken[token]; child !== 0; child = arena.nextSibling[child]) {
|
|
343
|
+
typ = arena.dataTypes[child];
|
|
344
|
+
if (typ === EnumToken.CommentTokenType || typ === EnumToken.WhitespaceTokenType) {
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
count++;
|
|
348
|
+
if (typ === EnumToken.IdenTokenType && arena.resolveString(arena.stringValue[child]) === "none") {
|
|
349
|
+
distance = null;
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
val = length2Px(arena, child);
|
|
353
|
+
if (val == null) {
|
|
354
|
+
return false;
|
|
355
|
+
}
|
|
356
|
+
distance = val;
|
|
357
|
+
}
|
|
358
|
+
if (count !== 1) {
|
|
359
|
+
return false;
|
|
360
|
+
}
|
|
361
|
+
perspective(distance, matrixVar);
|
|
362
|
+
return true;
|
|
363
|
+
}
|
|
364
|
+
case 20 /* TransformKind.Matrix */:
|
|
365
|
+
case 21 /* TransformKind.Matrix3d */:
|
|
366
|
+
for (child = arena.firstToken[token]; child !== 0; child = arena.nextSibling[child]) {
|
|
367
|
+
typ = arena.dataTypes[child];
|
|
368
|
+
if (typ === EnumToken.WhitespaceTokenType ||
|
|
369
|
+
typ === EnumToken.CommentTokenType ||
|
|
370
|
+
typ === EnumToken.CommaTokenType) {
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
373
|
+
if (count < 16) {
|
|
374
|
+
args[count] = getNumber(arena, child);
|
|
375
|
+
}
|
|
376
|
+
count++;
|
|
377
|
+
}
|
|
378
|
+
if (count !== (kind === 20 /* TransformKind.Matrix */ ? 6 : 16)) {
|
|
379
|
+
return false;
|
|
380
|
+
}
|
|
381
|
+
multiplyInto(matrixVar, setMatrixValues(args, count, valuesMatrix), matrixVar);
|
|
382
|
+
return true;
|
|
383
|
+
}
|
|
384
|
+
return false;
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* multiply a matrix in place by the matrices of a list of transform functions
|
|
388
|
+
* @param arena
|
|
389
|
+
* @param tokens
|
|
390
|
+
* @param start index of the first function
|
|
391
|
+
* @param end index after the last function
|
|
392
|
+
* @param matrixVar
|
|
393
|
+
* @returns false if the matrix cannot be computed. the matrix is then partially updated
|
|
394
|
+
*/
|
|
395
|
+
function computeMatrix(arena, tokens, start, end, matrixVar) {
|
|
396
|
+
for (let i = start; i < end; i++) {
|
|
397
|
+
if (!computeTransform(arena, tokens[i], matrixVar)) {
|
|
398
|
+
return false;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
return true;
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* the matrix of the transform list
|
|
405
|
+
*/
|
|
406
|
+
const transformMatrix = new Float64Array(16);
|
|
407
|
+
/**
|
|
408
|
+
* the matrix of a run of functions of the transform list
|
|
409
|
+
*/
|
|
410
|
+
const runMatrix = new Float64Array(16);
|
|
411
|
+
/**
|
|
412
|
+
* the transform functions of the runs: index of the first token of each run, then the end of the last run. the arrays
|
|
413
|
+
* are reused: their length is not reset (see {@link runCount} and {@link tokenCount})
|
|
414
|
+
*/
|
|
415
|
+
const runs = [];
|
|
416
|
+
/**
|
|
417
|
+
* the tokens of the transform list without the comments and the whitespace tokens, see {@link splitTransformList}
|
|
418
|
+
*/
|
|
419
|
+
const tokens = [];
|
|
420
|
+
/**
|
|
421
|
+
* number of values of {@link runs}
|
|
422
|
+
*/
|
|
423
|
+
let runCount = 0;
|
|
424
|
+
/**
|
|
425
|
+
* number of values of {@link tokens}
|
|
426
|
+
*/
|
|
427
|
+
let tokenCount = 0;
|
|
428
|
+
/**
|
|
429
|
+
* the result of the computation, reused
|
|
430
|
+
*/
|
|
431
|
+
const computed = {
|
|
432
|
+
values: transformMatrix,
|
|
433
|
+
matrix: 0,
|
|
434
|
+
cumulative: [],
|
|
435
|
+
minified: [],
|
|
436
|
+
created: true,
|
|
437
|
+
};
|
|
438
|
+
/**
|
|
439
|
+
* compute the matrix of a transform list
|
|
440
|
+
* @param arena
|
|
441
|
+
* @param transformList the tokens of the value
|
|
442
|
+
* @returns null if the matrix cannot be computed
|
|
443
|
+
*/
|
|
444
|
+
function compute(arena, transformList) {
|
|
445
|
+
let token;
|
|
446
|
+
let child;
|
|
447
|
+
let kind;
|
|
448
|
+
let typ;
|
|
449
|
+
// all transform function arguments must be 0 or scale(1)
|
|
450
|
+
for (let i = 0; i < transformList.length; i++) {
|
|
451
|
+
token = transformList[i];
|
|
452
|
+
kind = transformKind(arena, arena.stringValue[token]) & 255 /* TransformKind.KindMask */;
|
|
453
|
+
if (kind === 6 /* TransformKind.Rotate */ ||
|
|
454
|
+
kind === 7 /* TransformKind.RotateX */ ||
|
|
455
|
+
kind === 8 /* TransformKind.RotateY */ ||
|
|
456
|
+
kind === 9 /* TransformKind.RotateZ */ ||
|
|
457
|
+
kind === 10 /* TransformKind.Rotate3d */) {
|
|
458
|
+
for (child = arena.firstToken[token]; child !== 0; child = arena.nextSibling[child]) {
|
|
459
|
+
typ = arena.dataTypes[child];
|
|
460
|
+
if (typ === EnumToken.WhitespaceTokenType || typ === EnumToken.CommaTokenType) {
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
if ((typ !== EnumToken.AngleTokenType &&
|
|
464
|
+
typ !== EnumToken.NumberTokenType &&
|
|
465
|
+
typ !== EnumToken.PercentageTokenType) ||
|
|
466
|
+
// angle >= 360deg, do not transform
|
|
467
|
+
Math.abs(getAngle(arena, child)) >= 1) {
|
|
468
|
+
return null;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
splitTransformList(arena, transformList);
|
|
474
|
+
// a new array: it may become the value of the declaration
|
|
475
|
+
const cumulative = [];
|
|
476
|
+
const matrix = setIdentity(transformMatrix);
|
|
477
|
+
let minified;
|
|
478
|
+
// the runs produced tokens: `none` is the value of an empty list
|
|
479
|
+
let empty = true;
|
|
480
|
+
computed.created = true;
|
|
481
|
+
for (let run = 0; run < runCount - 1; run++) {
|
|
482
|
+
const start = runs[run];
|
|
483
|
+
const end = runs[run + 1];
|
|
484
|
+
if (!computeMatrix(arena, tokens, start, end, setIdentity(runMatrix))) {
|
|
485
|
+
return null;
|
|
486
|
+
}
|
|
487
|
+
multiplyInto(matrix, runMatrix, matrix);
|
|
488
|
+
minified = minify(arena, runMatrix);
|
|
489
|
+
if (minified == null) {
|
|
490
|
+
computed.created = false;
|
|
491
|
+
empty = false;
|
|
492
|
+
for (let i = start; i < end; i++) {
|
|
493
|
+
pushTransform(arena, cumulative, tokens[i]);
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
else {
|
|
497
|
+
empty &&= minified.length === 0;
|
|
498
|
+
for (let i = 0; i < minified.length; i++) {
|
|
499
|
+
pushTransform(arena, cumulative, minified[i]);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
const serialized = serialize(arena, matrix);
|
|
504
|
+
if (!empty && cumulative.length === 0) {
|
|
505
|
+
cumulative.push(arena.createToken(EnumToken.IdenTokenType, "none"));
|
|
506
|
+
}
|
|
507
|
+
computed.cumulative = cumulative;
|
|
508
|
+
computed.matrix = serialize(arena, toZero(matrix));
|
|
509
|
+
computed.minified = minify(arena, matrix) ?? [serialized];
|
|
510
|
+
return computed;
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* append a token to a transform list. `none` is omitted
|
|
514
|
+
* @param arena
|
|
515
|
+
* @param list
|
|
516
|
+
* @param token
|
|
517
|
+
*/
|
|
518
|
+
function pushTransform(arena, list, token) {
|
|
519
|
+
if (arena.dataTypes[token] !== EnumToken.IdenTokenType ||
|
|
520
|
+
arena.resolveString(arena.stringValue[token]) !== "none") {
|
|
521
|
+
list.push(token);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* split a transform list into runs (see {@link runs} and {@link tokens}): the consecutive translations, rotations and
|
|
526
|
+
* scales are grouped, the other tokens are alone. the comments and the whitespace tokens are ignored
|
|
527
|
+
* @param arena
|
|
528
|
+
* @param transformList
|
|
529
|
+
*/
|
|
530
|
+
function splitTransformList(arena, transformList) {
|
|
531
|
+
// family of the current run
|
|
532
|
+
let pattern = 0;
|
|
533
|
+
let family;
|
|
534
|
+
let typ;
|
|
535
|
+
let token;
|
|
536
|
+
runCount = 0;
|
|
537
|
+
// the runs index the list without the ignored tokens
|
|
538
|
+
tokenCount = 0;
|
|
539
|
+
for (let i = 0; i < transformList.length; i++) {
|
|
540
|
+
token = transformList[i];
|
|
541
|
+
typ = arena.dataTypes[token];
|
|
542
|
+
if (typ === EnumToken.CommentTokenType || typ === EnumToken.WhitespaceTokenType) {
|
|
543
|
+
continue;
|
|
544
|
+
}
|
|
545
|
+
if (typ === EnumToken.TransformFunctionTokenType) {
|
|
546
|
+
family = transformKind(arena, arena.stringValue[token]) & 768 /* TransformKind.FamilyMask */;
|
|
547
|
+
if (pattern !== 0 && family === pattern) {
|
|
548
|
+
// continue the current run
|
|
549
|
+
tokens[tokenCount++] = token;
|
|
550
|
+
continue;
|
|
551
|
+
}
|
|
552
|
+
pattern = family;
|
|
553
|
+
}
|
|
554
|
+
runs[runCount++] = tokenCount;
|
|
555
|
+
tokens[tokenCount++] = token;
|
|
556
|
+
}
|
|
557
|
+
runs[runCount++] = tokenCount;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
export { TransformKind, compute, computeMatrix, computeTransform, transformKind };
|
|
@@ -0,0 +1,67 @@
|
|
|
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 { isIdentity, is2DMatrix } from './utils.js';
|
|
9
|
+
import { EnumToken } from '../types.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* set the values of a `matrix()` (6 values) or of a `matrix3d()` (16 values) function, column-major order. the other
|
|
13
|
+
* values of a `matrix()` are the values of the identity matrix
|
|
14
|
+
* @param values
|
|
15
|
+
* @param count number of values: 6 or 16
|
|
16
|
+
* @param matrix the matrix that receives the values
|
|
17
|
+
*/
|
|
18
|
+
function setMatrixValues(values, count, matrix) {
|
|
19
|
+
if (count === 6) {
|
|
20
|
+
matrix[0] = values[0];
|
|
21
|
+
matrix[1] = values[1];
|
|
22
|
+
matrix[2] = 0;
|
|
23
|
+
matrix[3] = 0;
|
|
24
|
+
matrix[4] = values[2];
|
|
25
|
+
matrix[4 + 1] = values[3];
|
|
26
|
+
matrix[4 + 2] = 0;
|
|
27
|
+
matrix[4 + 3] = 0;
|
|
28
|
+
matrix[2 * 4] = 0;
|
|
29
|
+
matrix[2 * 4 + 1] = 0;
|
|
30
|
+
matrix[2 * 4 + 2] = 1;
|
|
31
|
+
matrix[2 * 4 + 3] = 0;
|
|
32
|
+
matrix[3 * 4] = values[4];
|
|
33
|
+
matrix[3 * 4 + 1] = values[5];
|
|
34
|
+
matrix[3 * 4 + 2] = 0;
|
|
35
|
+
matrix[3 * 4 + 3] = 1;
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
for (let i = 0; i < 16; i++) {
|
|
39
|
+
matrix[i] = values[i];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return matrix;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* the tokens of a matrix: `none`, `matrix()` if it is a 2d matrix, `matrix3d()` otherwise
|
|
46
|
+
* @param arena
|
|
47
|
+
* @param matrix
|
|
48
|
+
*/
|
|
49
|
+
function serialize(arena, matrix) {
|
|
50
|
+
if (isIdentity(matrix)) {
|
|
51
|
+
return arena.createToken(EnumToken.IdenTokenType, "none");
|
|
52
|
+
}
|
|
53
|
+
// https://drafts.csswg.org/css-transforms-2/#two-dimensional-subset
|
|
54
|
+
const is2D = is2DMatrix(matrix);
|
|
55
|
+
const count = is2D ? 6 : 16;
|
|
56
|
+
const tokens = new Array(count * 2 - 1);
|
|
57
|
+
for (let i = 0; i < count; i++) {
|
|
58
|
+
if (i > 0) {
|
|
59
|
+
tokens[i * 2 - 1] = arena.createToken(EnumToken.CommaTokenType);
|
|
60
|
+
}
|
|
61
|
+
// matrix(a, b, c, d, e, f): values 0, 1, 4, 5, 12, 13
|
|
62
|
+
tokens[i * 2] = arena.createToken(EnumToken.NumberTokenType, matrix[is2D ? (i < 2 ? i : i < 4 ? i + 2 : i + 8) : i]);
|
|
63
|
+
}
|
|
64
|
+
return arena.createFunction(EnumToken.TransformFunctionTokenType, is2D ? "matrix" : "matrix3d", tokens);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export { serialize, setMatrixValues };
|