@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,325 @@
|
|
|
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, decompose, round, toZeroValue, identityMatrix } from './utils.js';
|
|
9
|
+
import { epsilon } from '../../syntax/constants.js';
|
|
10
|
+
import { EnumToken } from '../types.js';
|
|
11
|
+
import { transformKind, computeTransform } from './compute.js';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* the decomposition of the matrix being minified, reused
|
|
15
|
+
*/
|
|
16
|
+
const decomposed = {
|
|
17
|
+
translateX: 0,
|
|
18
|
+
translateY: 0,
|
|
19
|
+
translateZ: 0,
|
|
20
|
+
scaleX: 1,
|
|
21
|
+
scaleY: 1,
|
|
22
|
+
scaleZ: 1,
|
|
23
|
+
skewXY: 0,
|
|
24
|
+
skewXZ: 0,
|
|
25
|
+
skewYZ: 0,
|
|
26
|
+
rotateX: 0,
|
|
27
|
+
rotateY: 0,
|
|
28
|
+
rotateZ: 0,
|
|
29
|
+
rotateAngle: 0,
|
|
30
|
+
perspectiveX: 0,
|
|
31
|
+
perspectiveY: 0,
|
|
32
|
+
perspectiveZ: 0,
|
|
33
|
+
perspectiveW: 1,
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* a length in pixels
|
|
37
|
+
* @param arena
|
|
38
|
+
* @param value
|
|
39
|
+
*/
|
|
40
|
+
function px(arena, value) {
|
|
41
|
+
return arena.createToken(EnumToken.LengthTokenType, value, "px");
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* an angle in degrees
|
|
45
|
+
* @param arena
|
|
46
|
+
* @param value
|
|
47
|
+
*/
|
|
48
|
+
function deg(arena, value) {
|
|
49
|
+
return arena.createToken(EnumToken.AngleTokenType, value, "deg");
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* a number
|
|
53
|
+
* @param arena
|
|
54
|
+
* @param value
|
|
55
|
+
*/
|
|
56
|
+
function num(arena, value) {
|
|
57
|
+
return arena.createToken(EnumToken.NumberTokenType, value);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* a comma
|
|
61
|
+
* @param arena
|
|
62
|
+
*/
|
|
63
|
+
function comma(arena) {
|
|
64
|
+
return arena.createToken(EnumToken.CommaTokenType);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* the shortest transform functions of a matrix: translate → rotate → skew → scale. the matrix is not modified
|
|
68
|
+
* @param arena
|
|
69
|
+
* @param matrix
|
|
70
|
+
* @returns null if the matrix cannot be decomposed
|
|
71
|
+
*/
|
|
72
|
+
function minify(arena, matrix) {
|
|
73
|
+
if (!decompose(matrix, decomposed)) {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
const result = [];
|
|
77
|
+
// check identity
|
|
78
|
+
if (!(round(decomposed.translateX) === 0 &&
|
|
79
|
+
round(decomposed.translateY) === 0 &&
|
|
80
|
+
round(decomposed.translateZ) === 0)) {
|
|
81
|
+
const x = round(decomposed.translateX) !== 0;
|
|
82
|
+
const y = round(decomposed.translateY) !== 0;
|
|
83
|
+
const z = round(decomposed.translateZ) !== 0;
|
|
84
|
+
if (+x + +y + +z === 1) {
|
|
85
|
+
if (x) {
|
|
86
|
+
result.push(arena.createFunction(EnumToken.TransformFunctionTokenType, "translate", [
|
|
87
|
+
px(arena, round(decomposed.translateX)),
|
|
88
|
+
]));
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
result.push(arena.createFunction(EnumToken.TransformFunctionTokenType, y ? "translateY" : "translateZ", [
|
|
92
|
+
px(arena, round(y ? decomposed.translateY : decomposed.translateZ)),
|
|
93
|
+
]));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
else if (z) {
|
|
97
|
+
result.push(arena.createFunction(EnumToken.TransformFunctionTokenType, "translate3d", [
|
|
98
|
+
decomposed.translateX === 0 ? num(arena, 0) : px(arena, round(decomposed.translateX)),
|
|
99
|
+
comma(arena),
|
|
100
|
+
decomposed.translateY === 0 ? num(arena, 0) : px(arena, round(decomposed.translateY)),
|
|
101
|
+
comma(arena),
|
|
102
|
+
px(arena, round(decomposed.translateZ)),
|
|
103
|
+
]));
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
result.push(arena.createFunction(EnumToken.TransformFunctionTokenType, "translate", [
|
|
107
|
+
px(arena, round(decomposed.translateX)),
|
|
108
|
+
comma(arena),
|
|
109
|
+
px(arena, round(decomposed.translateY)),
|
|
110
|
+
]));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (round(decomposed.rotateAngle) !== 0) {
|
|
114
|
+
const x = decomposed.rotateX;
|
|
115
|
+
const y = decomposed.rotateY;
|
|
116
|
+
const z = decomposed.rotateZ;
|
|
117
|
+
const angle = decomposed.rotateAngle;
|
|
118
|
+
if (y === 0 && z === 0) {
|
|
119
|
+
result.push(arena.createFunction(EnumToken.TransformFunctionTokenType, "rotateX", [deg(arena, round(angle))]));
|
|
120
|
+
}
|
|
121
|
+
else if (x === 0 && z === 0) {
|
|
122
|
+
result.push(arena.createFunction(EnumToken.TransformFunctionTokenType, "rotateY", [deg(arena, round(angle))]));
|
|
123
|
+
}
|
|
124
|
+
else if (x === 0 && y === 0) {
|
|
125
|
+
result.push(arena.createFunction(EnumToken.TransformFunctionTokenType, "rotate", [deg(arena, round(angle))]));
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
result.push(arena.createFunction(EnumToken.TransformFunctionTokenType, "rotate3d", [
|
|
129
|
+
num(arena, round(x)),
|
|
130
|
+
comma(arena),
|
|
131
|
+
num(arena, round(y)),
|
|
132
|
+
comma(arena),
|
|
133
|
+
num(arena, round(z)),
|
|
134
|
+
comma(arena),
|
|
135
|
+
deg(arena, round(angle)),
|
|
136
|
+
]));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (!(round(decomposed.skewXY) === 0 && round(decomposed.skewXZ) === 0)) {
|
|
140
|
+
// a single angle
|
|
141
|
+
const single = round(decomposed.skewXZ) === 0;
|
|
142
|
+
const skewX = round((Math.atan(decomposed.skewXY) * 180) / Math.PI);
|
|
143
|
+
const skewY = round((Math.atan(decomposed.skewXZ) * 180) / Math.PI);
|
|
144
|
+
if (single) {
|
|
145
|
+
result.push(arena.createFunction(EnumToken.TransformFunctionTokenType, "skew", [deg(arena, round(skewX))]));
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
result.push(arena.createFunction(EnumToken.TransformFunctionTokenType, "skew", [
|
|
149
|
+
deg(arena, round(skewX)),
|
|
150
|
+
comma(arena),
|
|
151
|
+
deg(arena, round(skewY)),
|
|
152
|
+
]));
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (!(round(decomposed.scaleX) === 1 && round(decomposed.scaleY) === 1 && round(decomposed.scaleZ) === 1)) {
|
|
156
|
+
const sx = toZeroValue(decomposed.scaleX);
|
|
157
|
+
const sy = toZeroValue(decomposed.scaleY);
|
|
158
|
+
const sz = toZeroValue(decomposed.scaleZ);
|
|
159
|
+
const x = sx !== 1;
|
|
160
|
+
const y = sy !== 1;
|
|
161
|
+
const z = sz !== 1;
|
|
162
|
+
if (+x + +y + +z === 1) {
|
|
163
|
+
result.push(arena.createFunction(EnumToken.TransformFunctionTokenType, x ? "scaleX" : y ? "scaleY" : "scaleZ", [num(arena, round(x ? sx : y ? sy : sz))]));
|
|
164
|
+
}
|
|
165
|
+
else if (!z) {
|
|
166
|
+
result.push(arena.createFunction(EnumToken.TransformFunctionTokenType, "scale", [
|
|
167
|
+
num(arena, round(sx)),
|
|
168
|
+
comma(arena),
|
|
169
|
+
num(arena, round(sy)),
|
|
170
|
+
]));
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
result.push(arena.createFunction(EnumToken.TransformFunctionTokenType, "scale3d", [
|
|
174
|
+
num(arena, round(sx)),
|
|
175
|
+
comma(arena),
|
|
176
|
+
num(arena, round(sy)),
|
|
177
|
+
comma(arena),
|
|
178
|
+
num(arena, round(sz)),
|
|
179
|
+
]));
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
// identity
|
|
183
|
+
if (result.length === 0 || (result.length === 1 && eqMatrix(arena, identityMatrix, result))) {
|
|
184
|
+
result.length = 0;
|
|
185
|
+
result.push(arena.createToken(EnumToken.IdenTokenType, "none"));
|
|
186
|
+
}
|
|
187
|
+
return result;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* the product of the matrices of the functions compared by {@link eqMatrix}
|
|
191
|
+
*/
|
|
192
|
+
const productMatrix = new Float64Array(16);
|
|
193
|
+
/**
|
|
194
|
+
* the matrix of a function compared by {@link eqMatrix}
|
|
195
|
+
*/
|
|
196
|
+
const functionMatrix = new Float64Array(16);
|
|
197
|
+
/**
|
|
198
|
+
* true if a list of transform functions is equivalent to a matrix (the values differ by at most `epsilon`)
|
|
199
|
+
* @param arena
|
|
200
|
+
* @param data
|
|
201
|
+
* @param b
|
|
202
|
+
*/
|
|
203
|
+
function eqMatrix(arena, data, b) {
|
|
204
|
+
const mat = setIdentity(productMatrix);
|
|
205
|
+
for (let i = 0; i < b.length; i++) {
|
|
206
|
+
if (!computeTransform(arena, b[i], setIdentity(functionMatrix))) {
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
multiplyInto(mat, functionMatrix, mat);
|
|
210
|
+
}
|
|
211
|
+
for (let i = 0; i < 16; i++) {
|
|
212
|
+
if (Math.abs(mat[i] - data[i]) > epsilon) {
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return true;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* shorter form of a transform function: `translate3d(0, 10px, 0)` → `translateY(10px)`, `scale3d(2, 1, 1)` →
|
|
220
|
+
* `scaleX(2)`, `skewX()` → `skew()`. the percentages of the scale functions are converted to numbers
|
|
221
|
+
* @param arena
|
|
222
|
+
* @param transform
|
|
223
|
+
* @returns a new function, or the same function (possibly modified)
|
|
224
|
+
*/
|
|
225
|
+
function minifyTransformFunctions(arena, transform) {
|
|
226
|
+
const kind = transformKind(arena, arena.stringValue[transform]) & 255 /* TransformKind.KindMask */;
|
|
227
|
+
if (kind === 17 /* TransformKind.SkewX */) {
|
|
228
|
+
// inlined: arena.setStr(transform, "skew")
|
|
229
|
+
arena.stringValue[transform] = arena.internString("skew");
|
|
230
|
+
return transform;
|
|
231
|
+
}
|
|
232
|
+
if (kind !== 1 /* TransformKind.Translate */ &&
|
|
233
|
+
kind !== 5 /* TransformKind.Translate3d */ &&
|
|
234
|
+
kind !== 11 /* TransformKind.Scale */ &&
|
|
235
|
+
kind !== 15 /* TransformKind.Scale3d */) {
|
|
236
|
+
return transform;
|
|
237
|
+
}
|
|
238
|
+
const isScale = kind === 11 /* TransformKind.Scale */ || kind === 15 /* TransformKind.Scale3d */;
|
|
239
|
+
let typ;
|
|
240
|
+
// the first three arguments
|
|
241
|
+
let first = 0;
|
|
242
|
+
let second = 0;
|
|
243
|
+
let third = 0;
|
|
244
|
+
let count = 0;
|
|
245
|
+
for (let token = arena.firstToken[transform]; token !== 0; token = arena.nextSibling[token]) {
|
|
246
|
+
typ = arena.dataTypes[token];
|
|
247
|
+
if (typ === EnumToken.CommentTokenType ||
|
|
248
|
+
typ === EnumToken.WhitespaceTokenType ||
|
|
249
|
+
typ === EnumToken.CommaTokenType) {
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
if (typ !== EnumToken.NumberTokenType &&
|
|
253
|
+
typ !== EnumToken.LengthTokenType &&
|
|
254
|
+
typ !== EnumToken.AngleTokenType &&
|
|
255
|
+
typ !== EnumToken.PercentageTokenType) {
|
|
256
|
+
return transform;
|
|
257
|
+
}
|
|
258
|
+
if (typ === EnumToken.PercentageTokenType && isScale && !arena.hasFraction(token)) {
|
|
259
|
+
arena.dataTypes[token] = EnumToken.NumberTokenType;
|
|
260
|
+
arena.numericValue[token] /= 100;
|
|
261
|
+
}
|
|
262
|
+
if (count === 0) {
|
|
263
|
+
first = token;
|
|
264
|
+
}
|
|
265
|
+
else if (count === 1) {
|
|
266
|
+
second = token;
|
|
267
|
+
}
|
|
268
|
+
else if (count === 2) {
|
|
269
|
+
third = token;
|
|
270
|
+
}
|
|
271
|
+
count++;
|
|
272
|
+
}
|
|
273
|
+
if (count === 0) {
|
|
274
|
+
return transform;
|
|
275
|
+
}
|
|
276
|
+
if ((kind === 1 /* TransformKind.Translate */ || kind === 11 /* TransformKind.Scale */) && count > 2) {
|
|
277
|
+
return transform;
|
|
278
|
+
}
|
|
279
|
+
const ignoredValue = isScale ? 1 : 0;
|
|
280
|
+
// the axes whose value is not the ignored value
|
|
281
|
+
const x = arena.numericValue[first] !== ignoredValue;
|
|
282
|
+
const y = count > 1 && arena.numericValue[second] !== ignoredValue;
|
|
283
|
+
const z = count > 2 && arena.numericValue[third] !== ignoredValue;
|
|
284
|
+
const size = +x + +y + +z;
|
|
285
|
+
if (size === 2 && z) {
|
|
286
|
+
return transform;
|
|
287
|
+
}
|
|
288
|
+
if (!isScale) {
|
|
289
|
+
if (size === 0) {
|
|
290
|
+
return arena.createFunction(EnumToken.TransformFunctionTokenType, "translate", [
|
|
291
|
+
arena.createToken(EnumToken.NumberTokenType, 0),
|
|
292
|
+
]);
|
|
293
|
+
}
|
|
294
|
+
if (size === 1) {
|
|
295
|
+
return arena.createFunction(EnumToken.TransformFunctionTokenType, x ? "translate" : y ? "translateY" : "translateZ", [x ? first : y ? second : third]);
|
|
296
|
+
}
|
|
297
|
+
if (size === 2) {
|
|
298
|
+
return arena.createFunction(EnumToken.TransformFunctionTokenType, "translate", [
|
|
299
|
+
first,
|
|
300
|
+
arena.createToken(EnumToken.CommaTokenType),
|
|
301
|
+
second,
|
|
302
|
+
]);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
else {
|
|
306
|
+
if (size === 0) {
|
|
307
|
+
return arena.createFunction(EnumToken.TransformFunctionTokenType, "scale", [
|
|
308
|
+
arena.createToken(EnumToken.NumberTokenType, 1),
|
|
309
|
+
]);
|
|
310
|
+
}
|
|
311
|
+
if (size === 1) {
|
|
312
|
+
return arena.createFunction(EnumToken.TransformFunctionTokenType, x ? "scaleX" : y ? "scaleY" : "scaleZ", [x ? first : y ? second : third]);
|
|
313
|
+
}
|
|
314
|
+
if (size === 2) {
|
|
315
|
+
return arena.createFunction(EnumToken.TransformFunctionTokenType, "scale", [
|
|
316
|
+
first,
|
|
317
|
+
arena.createToken(EnumToken.CommaTokenType),
|
|
318
|
+
second,
|
|
319
|
+
]);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
return transform;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
export { eqMatrix, minify, minifyTransformFunctions };
|
|
@@ -0,0 +1,24 @@
|
|
|
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 } from './utils.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* the perspective matrix. the function multiplies the matrix it receives in place: `from = from × perspective`
|
|
12
|
+
*/
|
|
13
|
+
const matrix = new Float64Array(16);
|
|
14
|
+
/**
|
|
15
|
+
* @param x distance, `null` for `none`
|
|
16
|
+
* @param from
|
|
17
|
+
*/
|
|
18
|
+
function perspective(x, from) {
|
|
19
|
+
setIdentity(matrix);
|
|
20
|
+
matrix[2 * 4 + 3] = x === null ? 0 : x === 0 ? Number.NEGATIVE_INFINITY : -1 / x;
|
|
21
|
+
return multiplyInto(from, matrix, from);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { perspective };
|
|
@@ -1,12 +1,16 @@
|
|
|
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
|
-
import {
|
|
8
|
+
import { setIdentity, multiplyInto } from './utils.js';
|
|
9
9
|
|
|
10
|
+
/**
|
|
11
|
+
* the rotation matrix. the functions multiply the matrix they receive in place: `from = from × rotation`
|
|
12
|
+
*/
|
|
13
|
+
const matrix = new Float64Array(16);
|
|
10
14
|
/**
|
|
11
15
|
* angle in radian
|
|
12
16
|
* @param angle
|
|
@@ -16,7 +20,7 @@ import { identity, multiply } from './utils.js';
|
|
|
16
20
|
* @param from
|
|
17
21
|
*/
|
|
18
22
|
function rotate3D(angle, x, y, z, from) {
|
|
19
|
-
|
|
23
|
+
setIdentity(matrix);
|
|
20
24
|
const sc = Math.sin(angle / 2) * Math.cos(angle / 2);
|
|
21
25
|
const sq = Math.sin(angle / 2) * Math.sin(angle / 2);
|
|
22
26
|
const norm = Math.sqrt(x * x + y * y + z * z);
|
|
@@ -33,15 +37,20 @@ function rotate3D(angle, x, y, z, from) {
|
|
|
33
37
|
matrix[2 * 4] = 2 * (x * z * sq + y * sc);
|
|
34
38
|
matrix[2 * 4 + 1] = 2 * (y * z * sq - x * sc);
|
|
35
39
|
matrix[2 * 4 + 2] = 1 - 2 * (x * x + y * y) * sq;
|
|
36
|
-
return
|
|
40
|
+
return multiplyInto(from, matrix, from);
|
|
37
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* angle in radian
|
|
44
|
+
* @param angle
|
|
45
|
+
* @param from
|
|
46
|
+
*/
|
|
38
47
|
function rotate(angle, from) {
|
|
39
|
-
|
|
48
|
+
setIdentity(matrix);
|
|
40
49
|
matrix[0] = Math.cos(angle);
|
|
41
50
|
matrix[1] = Math.sin(angle);
|
|
42
51
|
matrix[4] = -Math.sin(angle);
|
|
43
52
|
matrix[4 + 1] = Math.cos(angle);
|
|
44
|
-
return
|
|
53
|
+
return multiplyInto(from, matrix, from);
|
|
45
54
|
}
|
|
46
55
|
|
|
47
56
|
export { rotate, rotate3D };
|
|
@@ -0,0 +1,43 @@
|
|
|
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 } from './utils.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* the scale matrix. the functions multiply the matrix they receive in place: `from = from × scale`
|
|
12
|
+
*/
|
|
13
|
+
const matrix = new Float64Array(16);
|
|
14
|
+
function scaleX(x, from) {
|
|
15
|
+
setIdentity(matrix);
|
|
16
|
+
matrix[0] = x;
|
|
17
|
+
return multiplyInto(from, matrix, from);
|
|
18
|
+
}
|
|
19
|
+
function scaleY(y, from) {
|
|
20
|
+
setIdentity(matrix);
|
|
21
|
+
matrix[4 + 1] = y;
|
|
22
|
+
return multiplyInto(from, matrix, from);
|
|
23
|
+
}
|
|
24
|
+
function scaleZ(z, from) {
|
|
25
|
+
setIdentity(matrix);
|
|
26
|
+
matrix[2 * 4 + 2] = z;
|
|
27
|
+
return multiplyInto(from, matrix, from);
|
|
28
|
+
}
|
|
29
|
+
function scale(x, y, from) {
|
|
30
|
+
setIdentity(matrix);
|
|
31
|
+
matrix[0] = x;
|
|
32
|
+
matrix[4 + 1] = y;
|
|
33
|
+
return multiplyInto(from, matrix, from);
|
|
34
|
+
}
|
|
35
|
+
function scale3d(x, y, z, from) {
|
|
36
|
+
setIdentity(matrix);
|
|
37
|
+
matrix[0] = x;
|
|
38
|
+
matrix[4 + 1] = y;
|
|
39
|
+
matrix[2 * 4 + 2] = z;
|
|
40
|
+
return multiplyInto(from, matrix, from);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export { scale, scale3d, scaleX, scaleY, scaleZ };
|
|
@@ -0,0 +1,38 @@
|
|
|
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 } from './utils.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* the skew matrix. the functions multiply the matrix they receive in place: `from = from × skew`. angles in radian
|
|
12
|
+
*/
|
|
13
|
+
const matrix = new Float64Array(16);
|
|
14
|
+
function skewX(x, from) {
|
|
15
|
+
setIdentity(matrix);
|
|
16
|
+
matrix[4] = Math.tan(x);
|
|
17
|
+
return multiplyInto(from, matrix, from);
|
|
18
|
+
}
|
|
19
|
+
function skewY(y, from) {
|
|
20
|
+
setIdentity(matrix);
|
|
21
|
+
matrix[1] = Math.tan(y);
|
|
22
|
+
return multiplyInto(from, matrix, from);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* @param x
|
|
26
|
+
* @param y ignored if undefined
|
|
27
|
+
* @param from
|
|
28
|
+
*/
|
|
29
|
+
function skew(x, y, from) {
|
|
30
|
+
setIdentity(matrix);
|
|
31
|
+
matrix[4] = Math.tan(x);
|
|
32
|
+
if (y !== undefined) {
|
|
33
|
+
matrix[1] = Math.tan(y);
|
|
34
|
+
}
|
|
35
|
+
return multiplyInto(from, matrix, from);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { skew, skewX, skewY };
|
|
@@ -0,0 +1,56 @@
|
|
|
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 } from './utils.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* the translation matrix. the functions multiply the matrix they receive in place: `from = from × translation`
|
|
12
|
+
*/
|
|
13
|
+
const matrix = new Float64Array(16);
|
|
14
|
+
function translateX(x, from) {
|
|
15
|
+
setIdentity(matrix);
|
|
16
|
+
matrix[3 * 4] = x;
|
|
17
|
+
return multiplyInto(from, matrix, from);
|
|
18
|
+
}
|
|
19
|
+
function translateY(y, from) {
|
|
20
|
+
setIdentity(matrix);
|
|
21
|
+
matrix[3 * 4 + 1] = y;
|
|
22
|
+
return multiplyInto(from, matrix, from);
|
|
23
|
+
}
|
|
24
|
+
function translateZ(z, from) {
|
|
25
|
+
setIdentity(matrix);
|
|
26
|
+
matrix[3 * 4 + 2] = z;
|
|
27
|
+
return multiplyInto(from, matrix, from);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* @param x
|
|
31
|
+
* @param y 0 if undefined
|
|
32
|
+
* @param from
|
|
33
|
+
*/
|
|
34
|
+
function translate(x, y, from) {
|
|
35
|
+
setIdentity(matrix);
|
|
36
|
+
matrix[3 * 4] = x;
|
|
37
|
+
matrix[3 * 4 + 1] = y ?? 0;
|
|
38
|
+
return multiplyInto(from, matrix, from);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* @param x
|
|
42
|
+
* @param y NaN if undefined
|
|
43
|
+
* @param z NaN if undefined
|
|
44
|
+
* @param from
|
|
45
|
+
*/
|
|
46
|
+
function translate3d(x, y, z, from) {
|
|
47
|
+
setIdentity(matrix);
|
|
48
|
+
matrix[3 * 4] = x;
|
|
49
|
+
// @ts-ignore undefined is stored as NaN
|
|
50
|
+
matrix[3 * 4 + 1] = y;
|
|
51
|
+
// @ts-ignore undefined is stored as NaN
|
|
52
|
+
matrix[3 * 4 + 2] = z;
|
|
53
|
+
return multiplyInto(from, matrix, from);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export { translate, translate3d, translateX, translateY, translateZ };
|