Skip to content

Commit 6f0cd53

Browse files
committed
change tokens to use numbers instead of strings. fix color conversion bug #98 #96
1 parent 875a074 commit 6f0cd53

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+800
-774
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## v1.3.0
44

55
- [x] numerical and dimension tokens use numbers instead of string
6+
- [x] fix bug when inlineCssVariables is disabled and computeCalcExpression is enabled
67

78
## V1.2.0
89

dist/index-umd-web.js

Lines changed: 169 additions & 173 deletions
Large diffs are not rendered by default.

dist/index.cjs

Lines changed: 169 additions & 173 deletions
Large diffs are not rendered by default.

dist/lib/ast/transform/compute.js

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { multiply, identity } from './utils.js';
1+
import { multiply, toZero, identity } from './utils.js';
22
import { EnumToken } from '../types.js';
33
import { transformFunctions } from '../../syntax/syntax.js';
44
import { length2Px } from '../../syntax/utils.js';
@@ -22,10 +22,6 @@ import { perspective } from './perspective.js';
2222
function compute(transformLists) {
2323
transformLists = transformLists.slice();
2424
stripCommaToken(transformLists);
25-
// if (transformLists.length == 0) {
26-
//
27-
// return null;
28-
// }
2925
let matrix = identity();
3026
let mat;
3127
const cumulative = [];
@@ -50,8 +46,10 @@ function compute(transformLists) {
5046
});
5147
}
5248
}
49+
// console.error({matrix});
50+
// matrix = toZero(matrix) as Matrix;
5351
return {
54-
matrix: serialize(matrix),
52+
matrix: serialize(toZero(matrix)),
5553
cumulative,
5654
minified: minify(matrix) ?? [serialized]
5755
};
@@ -61,10 +59,6 @@ function computeMatrix(transformList, matrixVar) {
6159
let val;
6260
let i = 0;
6361
for (; i < transformList.length; i++) {
64-
// if (transformList[i].typ == EnumToken.WhitespaceTokenType) {
65-
//
66-
// continue;
67-
// }
6862
if (transformList[i].typ != EnumToken.FunctionTokenType || !transformFunctions.includes(transformList[i].val)) {
6963
return null;
7064
}
@@ -77,16 +71,7 @@ function computeMatrix(transformList, matrixVar) {
7771
{
7872
values.length = 0;
7973
const children = stripCommaToken(transformList[i].chi.slice());
80-
// if (children == null || children.length == 0) {
81-
//
82-
// return null;
83-
// }
8474
const valCount = transformList[i].val == 'translate3d' || transformList[i].val == 'translate' ? 3 : 1;
85-
// if (children.length == 1 && children[0].typ == EnumToken.IdenTokenType && (children[0] as IdentToken).val == 'none') {
86-
//
87-
// values.fill(0, 0, valCount);
88-
//
89-
// } else {
9075
for (let j = 0; j < children.length; j++) {
9176
if (children[j].typ == EnumToken.WhitespaceTokenType) {
9277
continue;
@@ -97,7 +82,6 @@ function computeMatrix(transformList, matrixVar) {
9782
}
9883
values.push(val);
9984
}
100-
// }
10185
if (values.length == 0 || values.length > valCount) {
10286
return null;
10387
}
@@ -132,10 +116,6 @@ function computeMatrix(transformList, matrixVar) {
132116
let values = [];
133117
let valuesCount = transformList[i].val == 'rotate3d' ? 4 : 1;
134118
for (const child of stripCommaToken(transformList[i].chi.slice())) {
135-
// if (child.typ == EnumToken.WhitespaceTokenType) {
136-
//
137-
// continue;
138-
// }
139119
values.push(child);
140120
if (transformList[i].val == 'rotateX') {
141121
x = 1;
@@ -178,19 +158,11 @@ function computeMatrix(transformList, matrixVar) {
178158
const children = stripCommaToken(transformList[i].chi.slice());
179159
for (let k = 0; k < children.length; k++) {
180160
child = children[k];
181-
// if (child.typ == EnumToken.CommentTokenType || child.typ == EnumToken.WhitespaceTokenType) {
182-
//
183-
// continue;
184-
// }
185161
if (child.typ != EnumToken.NumberTokenType) {
186162
return null;
187163
}
188164
values.push(getNumber(child));
189165
}
190-
// if (values.length == 0) {
191-
//
192-
// return null;
193-
// }
194166
if (transformList[i].val == 'scale3d') {
195167
if (values.length != 3) {
196168
return null;
@@ -234,10 +206,6 @@ function computeMatrix(transformList, matrixVar) {
234206
continue;
235207
}
236208
value = getAngle(child);
237-
// if (value == null) {
238-
//
239-
// return null;
240-
// }
241209
values.push(value * 2 * Math.PI);
242210
}
243211
if (values.length == 0 || (values.length > (transformList[i].val == 'skew' ? 2 : 1))) {
@@ -278,7 +246,6 @@ function computeMatrix(transformList, matrixVar) {
278246
}
279247
break;
280248
case 'matrix3d':
281-
// return null;
282249
case 'matrix':
283250
{
284251
const values = [];

dist/lib/ast/transform/matrix.js

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { toZero, is2DMatrix, identity } from './utils.js';
1+
import { is2DMatrix, identity } from './utils.js';
22
import { EnumToken } from '../types.js';
33
import { eq } from '../../parser/utils/eq.js';
44
import { getNumber } from '../../syntax/color/color.js';
@@ -30,38 +30,38 @@ function parseMatrix(mat) {
3030
function matrix(values) {
3131
const matrix = identity();
3232
if (values.length === 6) {
33-
matrix[0][0] = values[0];
34-
matrix[0][1] = values[1];
35-
matrix[1][0] = values[2];
36-
matrix[1][1] = values[3];
37-
matrix[3][0] = values[4];
38-
matrix[3][1] = values[5];
33+
matrix[0 * 4 + 0] = values[0];
34+
matrix[0 * 4 + 1] = values[1];
35+
matrix[1 * 4 + 0] = values[2];
36+
matrix[1 * 4 + 1] = values[3];
37+
matrix[3 * 4 + 0] = values[4];
38+
matrix[3 * 4 + 1] = values[5];
3939
}
4040
else if (values.length === 16) {
41-
matrix[0][0] = values[0];
42-
matrix[0][1] = values[1];
43-
matrix[0][2] = values[2];
44-
matrix[0][3] = values[3];
45-
matrix[1][0] = values[4];
46-
matrix[1][1] = values[5];
47-
matrix[1][2] = values[6];
48-
matrix[1][3] = values[7];
49-
matrix[2][0] = values[8];
50-
matrix[2][1] = values[9];
51-
matrix[2][2] = values[10];
52-
matrix[2][3] = values[11];
53-
matrix[3][0] = values[12];
54-
matrix[3][1] = values[13];
55-
matrix[3][2] = values[14];
56-
matrix[3][3] = values[15];
41+
matrix[0 * 4 + 0] = values[0];
42+
matrix[0 * 4 + 1] = values[1];
43+
matrix[0 * 4 + 2] = values[2];
44+
matrix[0 * 4 + 3] = values[3];
45+
matrix[1 * 4 + 0] = values[4];
46+
matrix[1 * 4 + 1] = values[5];
47+
matrix[1 * 4 + 2] = values[6];
48+
matrix[1 * 4 + 3] = values[7];
49+
matrix[2 * 4 + 0] = values[8];
50+
matrix[2 * 4 + 1] = values[9];
51+
matrix[2 * 4 + 2] = values[10];
52+
matrix[2 * 4 + 3] = values[11];
53+
matrix[3 * 4 + 0] = values[12];
54+
matrix[3 * 4 + 1] = values[13];
55+
matrix[3 * 4 + 2] = values[14];
56+
matrix[3 * 4 + 3] = values[15];
5757
}
5858
else {
5959
return null;
6060
}
6161
return matrix;
6262
}
6363
function serialize(matrix) {
64-
matrix = matrix.map(t => toZero(t.slice()));
64+
matrix = matrix.slice();
6565
// @ts-ignore
6666
if (eq(matrix, identity())) {
6767
return {
@@ -75,12 +75,12 @@ function serialize(matrix) {
7575
typ: EnumToken.FunctionTokenType,
7676
val: 'matrix',
7777
chi: [
78-
matrix[0][0],
79-
matrix[0][1],
80-
matrix[1][0],
81-
matrix[1][1],
82-
matrix[3][0],
83-
matrix[3][1]
78+
matrix[0 * 4 + 0],
79+
matrix[0 * 4 + 1],
80+
matrix[1 * 4 + 0],
81+
matrix[1 * 4 + 1],
82+
matrix[3 * 4 + 0],
83+
matrix[3 * 4 + 1]
8484
].reduce((acc, t) => {
8585
if (acc.length > 0) {
8686
acc.push({ typ: EnumToken.CommaTokenType });
@@ -96,7 +96,7 @@ function serialize(matrix) {
9696
return {
9797
typ: EnumToken.FunctionTokenType,
9898
val: 'matrix3d',
99-
chi: matrix.flat().reduce((acc, curr) => {
99+
chi: matrix.reduce((acc, curr) => {
100100
if (acc.length > 0) {
101101
acc.push({ typ: EnumToken.CommaTokenType });
102102
}

dist/lib/ast/transform/minify.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,23 +242,28 @@ function minify(matrix) {
242242
] : result;
243243
}
244244
function eqMatrix(a, b) {
245+
// console.error(JSON.stringify({a, b}, null, 1));
245246
let mat = identity();
246247
let tmp = identity();
247248
// @ts-ignore
248-
const data = Array.isArray(a) ? a : parseMatrix(a);
249+
const data = (Array.isArray(a) ? a : parseMatrix(a));
250+
// toZero(data);
251+
// console.error({data});
249252
for (const transform of b) {
250253
tmp = computeMatrix([transform], identity());
251254
if (tmp == null) {
252255
return false;
253256
}
254257
mat = multiply(mat, tmp);
255258
}
259+
// toZero(mat);
260+
// console.error({mat});
256261
if (mat == null) {
257262
return false;
258263
}
259264
for (let i = 0; i < 4; i++) {
260265
for (let j = 0; j < 4; j++) {
261-
if (Math.abs(mat[i][j] - data[i][j]) > epsilon) {
266+
if (Math.abs(mat[i * 4 + j] - data[i * 4 + j]) > epsilon) {
262267
return false;
263268
}
264269
}

dist/lib/ast/transform/perspective.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { identity, multiply } from './utils.js';
33
function perspective(x, from) {
44
const matrix = identity();
55
// @ts-ignore
6-
matrix[2][3] = typeof x == 'object' && x.val == 'none' ? 0 : x == 0 ? Number.NEGATIVE_INFINITY : -1 / x;
6+
matrix[2 * 4 + 3] = typeof x == 'object' && x.val == 'none' ? 0 : x == 0 ? Number.NEGATIVE_INFINITY : -1 / x;
77
return multiply(from, matrix);
88
}
99

dist/lib/ast/transform/rotate.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,23 @@ function rotate3D(angle, x, y, z, from) {
1717
x *= unit;
1818
y *= unit;
1919
z *= unit;
20-
matrix[0][0] = 1 - 2 * (y * y + z * z) * sq;
21-
matrix[0][1] = 2 * (x * y * sq + z * sc);
22-
matrix[0][2] = 2 * (x * z * sq - y * sc);
23-
matrix[1][0] = 2 * (x * y * sq - z * sc);
24-
matrix[1][1] = 1 - 2 * (x * x + z * z) * sq;
25-
matrix[1][2] = 2 * (y * z * sq + x * sc);
26-
matrix[2][0] = 2 * (x * z * sq + y * sc);
27-
matrix[2][1] = 2 * (y * z * sq - x * sc);
28-
matrix[2][2] = 1 - 2 * (x * x + y * y) * sq;
20+
matrix[0 * 4 + 0] = 1 - 2 * (y * y + z * z) * sq;
21+
matrix[0 * 4 + 1] = 2 * (x * y * sq + z * sc);
22+
matrix[0 * 4 + 2] = 2 * (x * z * sq - y * sc);
23+
matrix[1 * 4 + 0] = 2 * (x * y * sq - z * sc);
24+
matrix[1 * 4 + 1] = 1 - 2 * (x * x + z * z) * sq;
25+
matrix[1 * 4 + 2] = 2 * (y * z * sq + x * sc);
26+
matrix[2 * 4 + 0] = 2 * (x * z * sq + y * sc);
27+
matrix[2 * 4 + 1] = 2 * (y * z * sq - x * sc);
28+
matrix[2 * 4 + 2] = 1 - 2 * (x * x + y * y) * sq;
2929
return multiply(from, matrix);
3030
}
3131
function rotate(angle, from) {
3232
const matrix = identity();
33-
matrix[0][0] = Math.cos(angle);
34-
matrix[0][1] = Math.sin(angle);
35-
matrix[1][0] = -Math.sin(angle);
36-
matrix[1][1] = Math.cos(angle);
33+
matrix[0 * 4 + 0] = Math.cos(angle);
34+
matrix[0 * 4 + 1] = Math.sin(angle);
35+
matrix[1 * 4 + 0] = -Math.sin(angle);
36+
matrix[1 * 4 + 1] = Math.cos(angle);
3737
return multiply(from, matrix);
3838
}
3939

dist/lib/ast/transform/scale.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,30 @@ import { identity, multiply } from './utils.js';
22

33
function scaleX(x, from) {
44
const matrix = identity();
5-
matrix[0][0] = x;
5+
matrix[0 * 4 + 0] = x;
66
return multiply(from, matrix);
77
}
88
function scaleY(y, from) {
99
const matrix = identity();
10-
matrix[1][1] = y;
10+
matrix[1 * 4 + 1] = y;
1111
return multiply(from, matrix);
1212
}
1313
function scaleZ(z, from) {
1414
const matrix = identity();
15-
matrix[2][2] = z;
15+
matrix[2 * 4 + 2] = z;
1616
return multiply(from, matrix);
1717
}
1818
function scale(x, y, from) {
1919
const matrix = identity();
20-
matrix[0][0] = x;
21-
matrix[1][1] = y;
20+
matrix[0 * 4 + 0] = x;
21+
matrix[1 * 4 + 1] = y;
2222
return multiply(from, matrix);
2323
}
2424
function scale3d(x, y, z, from) {
2525
const matrix = identity();
26-
matrix[0][0] = x;
27-
matrix[1][1] = y;
28-
matrix[2][2] = z;
26+
matrix[0 * 4 + 0] = x;
27+
matrix[1 * 4 + 1] = y;
28+
matrix[2 * 4 + 2] = z;
2929
return multiply(from, matrix);
3030
}
3131

dist/lib/ast/transform/skew.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ import { identity, multiply } from './utils.js';
22

33
function skewX(x, from) {
44
const matrix = identity();
5-
matrix[1][0] = Math.tan(x);
5+
matrix[1 * 4 + 0] = Math.tan(x);
66
return multiply(from, matrix);
77
}
88
function skewY(y, from) {
99
const matrix = identity();
10-
matrix[0][1] = Math.tan(y);
10+
matrix[0 * 4 + 1] = Math.tan(y);
1111
return multiply(from, matrix);
1212
}
1313
// convert angle to radian
1414
function skew(values, from) {
1515
const matrix = identity();
16-
matrix[1][0] = Math.tan(values[0]);
16+
matrix[1 * 4 + 0] = Math.tan(values[0]);
1717
if (values.length > 1) {
18-
matrix[0][1] = Math.tan(values[1]);
18+
matrix[0 * 4 + 1] = Math.tan(values[1]);
1919
}
2020
return multiply(from, matrix);
2121
}

0 commit comments

Comments
 (0)