@tbela99/css-parser 1.1.1 → 1.3.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.
Files changed (107) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/README.md +53 -6
  3. package/dist/index-umd-web.js +5503 -5037
  4. package/dist/index.cjs +5510 -5044
  5. package/dist/index.d.ts +148 -41
  6. package/dist/lib/ast/expand.js +81 -65
  7. package/dist/lib/ast/features/calc.js +14 -36
  8. package/dist/lib/ast/features/inlinecssvariables.js +6 -17
  9. package/dist/lib/ast/features/prefix.js +22 -19
  10. package/dist/lib/ast/features/shorthand.js +1 -1
  11. package/dist/lib/ast/features/transform.js +17 -2
  12. package/dist/lib/ast/math/expression.js +74 -172
  13. package/dist/lib/ast/math/math.js +24 -22
  14. package/dist/lib/ast/minify.js +249 -199
  15. package/dist/lib/ast/transform/compute.js +18 -41
  16. package/dist/lib/ast/transform/matrix.js +36 -36
  17. package/dist/lib/ast/transform/minify.js +37 -59
  18. package/dist/lib/ast/transform/perspective.js +1 -1
  19. package/dist/lib/ast/transform/rotate.js +13 -13
  20. package/dist/lib/ast/transform/scale.js +8 -8
  21. package/dist/lib/ast/transform/skew.js +4 -4
  22. package/dist/lib/ast/transform/translate.js +8 -8
  23. package/dist/lib/ast/transform/utils.js +80 -28
  24. package/dist/lib/ast/types.js +122 -2
  25. package/dist/lib/fs/resolve.js +1 -14
  26. package/dist/lib/parser/declaration/list.js +1 -1
  27. package/dist/lib/parser/declaration/map.js +1 -1
  28. package/dist/lib/parser/declaration/set.js +3 -3
  29. package/dist/lib/parser/parse.js +53 -107
  30. package/dist/lib/parser/tokenize.js +29 -53
  31. package/dist/lib/parser/utils/declaration.js +1 -1
  32. package/dist/lib/parser/utils/type.js +2 -2
  33. package/dist/lib/renderer/render.js +79 -194
  34. package/dist/lib/{renderer → syntax}/color/a98rgb.js +2 -2
  35. package/dist/lib/syntax/color/cmyk.js +104 -0
  36. package/dist/lib/{renderer → syntax}/color/color-mix.js +31 -33
  37. package/dist/lib/syntax/color/color.js +581 -0
  38. package/dist/lib/syntax/color/hex.js +179 -0
  39. package/dist/lib/syntax/color/hsl.js +201 -0
  40. package/dist/lib/syntax/color/hwb.js +204 -0
  41. package/dist/lib/syntax/color/lab.js +262 -0
  42. package/dist/lib/syntax/color/lch.js +194 -0
  43. package/dist/lib/syntax/color/oklab.js +237 -0
  44. package/dist/lib/syntax/color/oklch.js +166 -0
  45. package/dist/lib/{renderer → syntax}/color/p3.js +3 -3
  46. package/dist/lib/{renderer → syntax}/color/rec2020.js +11 -11
  47. package/dist/lib/{renderer → syntax}/color/relativecolor.js +54 -41
  48. package/dist/lib/syntax/color/rgb.js +140 -0
  49. package/dist/lib/{renderer → syntax}/color/srgb.js +58 -46
  50. package/dist/lib/syntax/color/utils/components.js +46 -0
  51. package/dist/lib/{renderer → syntax}/color/utils/constants.js +6 -33
  52. package/dist/lib/syntax/color/utils/distance.js +40 -0
  53. package/dist/lib/{renderer → syntax}/color/xyz.js +27 -14
  54. package/dist/lib/{renderer → syntax}/color/xyzd50.js +8 -8
  55. package/dist/lib/syntax/syntax.js +78 -77
  56. package/dist/lib/syntax/utils.js +70 -0
  57. package/dist/lib/validation/at-rules/container.js +1 -1
  58. package/dist/lib/validation/at-rules/counter-style.js +1 -1
  59. package/dist/lib/validation/at-rules/custom-media.js +1 -1
  60. package/dist/lib/validation/at-rules/document.js +2 -2
  61. package/dist/lib/validation/at-rules/font-feature-values.js +2 -2
  62. package/dist/lib/validation/at-rules/import.js +5 -5
  63. package/dist/lib/validation/at-rules/keyframes.js +3 -13
  64. package/dist/lib/validation/at-rules/layer.js +1 -1
  65. package/dist/lib/validation/at-rules/media.js +1 -1
  66. package/dist/lib/validation/at-rules/namespace.js +1 -1
  67. package/dist/lib/validation/at-rules/page-margin-box.js +1 -1
  68. package/dist/lib/validation/at-rules/page.js +1 -1
  69. package/dist/lib/validation/at-rules/supports.js +7 -7
  70. package/dist/lib/validation/at-rules/when.js +1 -1
  71. package/dist/lib/validation/atrule.js +2 -2
  72. package/dist/lib/validation/config.js +0 -3
  73. package/dist/lib/validation/config.json.js +1 -1
  74. package/dist/lib/validation/parser/parse.js +8 -11
  75. package/dist/lib/validation/selector.js +1 -9
  76. package/dist/lib/validation/syntax.js +67 -137
  77. package/dist/lib/validation/syntaxes/complex-selector-list.js +2 -19
  78. package/dist/lib/validation/syntaxes/complex-selector.js +1 -1
  79. package/dist/lib/validation/syntaxes/compound-selector.js +5 -24
  80. package/dist/lib/validation/syntaxes/family-name.js +5 -40
  81. package/dist/lib/validation/syntaxes/keyframe-selector.js +3 -22
  82. package/dist/lib/validation/syntaxes/layer-name.js +1 -1
  83. package/dist/lib/validation/syntaxes/relative-selector-list.js +1 -25
  84. package/dist/lib/validation/syntaxes/relative-selector.js +1 -1
  85. package/dist/lib/validation/syntaxes/url.js +3 -34
  86. package/dist/lib/validation/utils/list.js +2 -9
  87. package/dist/lib/validation/utils/whitespace.js +1 -1
  88. package/dist/node/index.js +4 -2
  89. package/dist/web/index.js +4 -2
  90. package/package.json +4 -4
  91. package/.editorconfig +0 -484
  92. package/dist/lib/ast/transform/convert.js +0 -33
  93. package/dist/lib/ast/utils/utils.js +0 -104
  94. package/dist/lib/renderer/color/color.js +0 -654
  95. package/dist/lib/renderer/color/hex.js +0 -105
  96. package/dist/lib/renderer/color/hsl.js +0 -125
  97. package/dist/lib/renderer/color/hwb.js +0 -103
  98. package/dist/lib/renderer/color/lab.js +0 -148
  99. package/dist/lib/renderer/color/lch.js +0 -90
  100. package/dist/lib/renderer/color/oklab.js +0 -131
  101. package/dist/lib/renderer/color/oklch.js +0 -75
  102. package/dist/lib/renderer/color/rgb.js +0 -50
  103. package/dist/lib/renderer/color/utils/components.js +0 -34
  104. package/dist/lib/validation/syntaxes/keyframe-block-list.js +0 -28
  105. package/dist/lib/{renderer → syntax}/color/hsv.js +0 -0
  106. package/dist/lib/{renderer → syntax}/color/prophotorgb.js +1 -1
  107. /package/dist/lib/{renderer → syntax}/color/utils/matrix.js +0 -0
@@ -1,14 +1,14 @@
1
- import { multiply, identity } from './utils.js';
1
+ import { multiply, toZero, identity } from './utils.js';
2
2
  import { EnumToken } from '../types.js';
3
- import { length2Px } from './convert.js';
4
3
  import { transformFunctions } from '../../syntax/syntax.js';
4
+ import { length2Px } from '../../syntax/utils.js';
5
5
  import '../minify.js';
6
6
  import '../walk.js';
7
7
  import '../../parser/parse.js';
8
8
  import '../../parser/tokenize.js';
9
9
  import '../../parser/utils/config.js';
10
- import { getNumber, getAngle } from '../../renderer/color/color.js';
11
- import '../../renderer/color/utils/constants.js';
10
+ import { getNumber, getAngle } from '../../syntax/color/color.js';
11
+ import '../../syntax/color/utils/constants.js';
12
12
  import '../../renderer/sourcemap/lib/encode.js';
13
13
  import { stripCommaToken } from '../../validation/utils/list.js';
14
14
  import { translateX, translateY, translateZ, translate, translate3d } from './translate.js';
@@ -22,9 +22,6 @@ import { perspective } from './perspective.js';
22
22
  function compute(transformLists) {
23
23
  transformLists = transformLists.slice();
24
24
  stripCommaToken(transformLists);
25
- if (transformLists.length == 0) {
26
- return null;
27
- }
28
25
  let matrix = identity();
29
26
  let mat;
30
27
  const cumulative = [];
@@ -49,8 +46,10 @@ function compute(transformLists) {
49
46
  });
50
47
  }
51
48
  }
49
+ // console.error({matrix});
50
+ // matrix = toZero(matrix) as Matrix;
52
51
  return {
53
- matrix: serialize(matrix),
52
+ matrix: serialize(toZero(matrix)),
54
53
  cumulative,
55
54
  minified: minify(matrix) ?? [serialized]
56
55
  };
@@ -60,9 +59,6 @@ function computeMatrix(transformList, matrixVar) {
60
59
  let val;
61
60
  let i = 0;
62
61
  for (; i < transformList.length; i++) {
63
- if (transformList[i].typ == EnumToken.WhitespaceTokenType) {
64
- continue;
65
- }
66
62
  if (transformList[i].typ != EnumToken.FunctionTokenType || !transformFunctions.includes(transformList[i].val)) {
67
63
  return null;
68
64
  }
@@ -75,24 +71,16 @@ function computeMatrix(transformList, matrixVar) {
75
71
  {
76
72
  values.length = 0;
77
73
  const children = stripCommaToken(transformList[i].chi.slice());
78
- if (children == null || children.length == 0) {
79
- return null;
80
- }
81
74
  const valCount = transformList[i].val == 'translate3d' || transformList[i].val == 'translate' ? 3 : 1;
82
- if (children.length == 1 && children[0].typ == EnumToken.IdenTokenType && children[0].val == 'none') {
83
- values.fill(0, 0, valCount);
84
- }
85
- else {
86
- for (let j = 0; j < children.length; j++) {
87
- if (children[j].typ == EnumToken.WhitespaceTokenType) {
88
- continue;
89
- }
90
- val = length2Px(children[j]);
91
- if (typeof val != 'number' || Number.isNaN(val)) {
92
- return null;
93
- }
94
- values.push(val);
75
+ for (let j = 0; j < children.length; j++) {
76
+ if (children[j].typ == EnumToken.WhitespaceTokenType) {
77
+ continue;
95
78
  }
79
+ val = length2Px(children[j]);
80
+ if (val == null) {
81
+ return null;
82
+ }
83
+ values.push(val);
96
84
  }
97
85
  if (values.length == 0 || values.length > valCount) {
98
86
  return null;
@@ -128,9 +116,6 @@ function computeMatrix(transformList, matrixVar) {
128
116
  let values = [];
129
117
  let valuesCount = transformList[i].val == 'rotate3d' ? 4 : 1;
130
118
  for (const child of stripCommaToken(transformList[i].chi.slice())) {
131
- if (child.typ == EnumToken.WhitespaceTokenType) {
132
- continue;
133
- }
134
119
  values.push(child);
135
120
  if (transformList[i].val == 'rotateX') {
136
121
  x = 1;
@@ -173,17 +158,11 @@ function computeMatrix(transformList, matrixVar) {
173
158
  const children = stripCommaToken(transformList[i].chi.slice());
174
159
  for (let k = 0; k < children.length; k++) {
175
160
  child = children[k];
176
- if (child.typ == EnumToken.CommentTokenType || child.typ == EnumToken.WhitespaceTokenType) {
177
- continue;
178
- }
179
161
  if (child.typ != EnumToken.NumberTokenType) {
180
162
  return null;
181
163
  }
182
164
  values.push(getNumber(child));
183
165
  }
184
- if (values.length == 0) {
185
- return null;
186
- }
187
166
  if (transformList[i].val == 'scale3d') {
188
167
  if (values.length != 3) {
189
168
  return null;
@@ -221,13 +200,12 @@ function computeMatrix(transformList, matrixVar) {
221
200
  let value;
222
201
  for (let k = 0; k < transformList[i].chi.length; k++) {
223
202
  child = transformList[i].chi[k];
224
- if (child.typ == EnumToken.CommentTokenType || child.typ == EnumToken.WhitespaceTokenType) {
203
+ if (child.typ == EnumToken.CommentTokenType ||
204
+ child.typ == EnumToken.CommaTokenType ||
205
+ child.typ == EnumToken.WhitespaceTokenType) {
225
206
  continue;
226
207
  }
227
208
  value = getAngle(child);
228
- if (value == null) {
229
- return null;
230
- }
231
209
  values.push(value * 2 * Math.PI);
232
210
  }
233
211
  if (values.length == 0 || (values.length > (transformList[i].val == 'skew' ? 2 : 1))) {
@@ -268,7 +246,6 @@ function computeMatrix(transformList, matrixVar) {
268
246
  }
269
247
  break;
270
248
  case 'matrix3d':
271
- // return null;
272
249
  case 'matrix':
273
250
  {
274
251
  const values = [];
@@ -1,14 +1,14 @@
1
- import { toZero, is2DMatrix, identity } from './utils.js';
1
+ import { is2DMatrix, identity } from './utils.js';
2
2
  import { EnumToken } from '../types.js';
3
- import { reduceNumber } from '../../renderer/render.js';
4
3
  import { eq } from '../../parser/utils/eq.js';
5
- import { getNumber } from '../../renderer/color/color.js';
6
- import '../../renderer/color/utils/constants.js';
4
+ import { getNumber } from '../../syntax/color/color.js';
5
+ import '../../syntax/color/utils/constants.js';
7
6
  import '../minify.js';
8
7
  import '../walk.js';
9
8
  import '../../parser/parse.js';
10
9
  import '../../parser/tokenize.js';
11
10
  import '../../parser/utils/config.js';
11
+ import '../../renderer/sourcemap/lib/encode.js';
12
12
 
13
13
  function parseMatrix(mat) {
14
14
  if (mat.typ == EnumToken.IdenTokenType) {
@@ -30,30 +30,30 @@ function parseMatrix(mat) {
30
30
  function matrix(values) {
31
31
  const matrix = identity();
32
32
  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];
39
39
  }
40
40
  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];
57
57
  }
58
58
  else {
59
59
  return null;
@@ -61,7 +61,7 @@ function matrix(values) {
61
61
  return matrix;
62
62
  }
63
63
  function serialize(matrix) {
64
- matrix = matrix.map(t => toZero(t.slice()));
64
+ matrix = matrix.slice();
65
65
  // @ts-ignore
66
66
  if (eq(matrix, identity())) {
67
67
  return {
@@ -75,19 +75,19 @@ function serialize(matrix) {
75
75
  typ: EnumToken.FunctionTokenType,
76
76
  val: 'matrix',
77
77
  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]
84
84
  ].reduce((acc, t) => {
85
85
  if (acc.length > 0) {
86
86
  acc.push({ typ: EnumToken.CommaTokenType });
87
87
  }
88
88
  acc.push({
89
89
  typ: EnumToken.NumberTokenType,
90
- val: reduceNumber(t)
90
+ val: t
91
91
  });
92
92
  return acc;
93
93
  }, [])
@@ -96,13 +96,13 @@ function serialize(matrix) {
96
96
  return {
97
97
  typ: EnumToken.FunctionTokenType,
98
98
  val: 'matrix3d',
99
- chi: matrix.flat().reduce((acc, curr) => {
99
+ chi: matrix.reduce((acc, curr) => {
100
100
  if (acc.length > 0) {
101
101
  acc.push({ typ: EnumToken.CommaTokenType });
102
102
  }
103
103
  acc.push({
104
104
  typ: EnumToken.NumberTokenType,
105
- val: reduceNumber(curr)
105
+ val: curr
106
106
  });
107
107
  return acc;
108
108
  }, [])
@@ -36,25 +36,12 @@ function minify(matrix) {
36
36
  coordinates.delete(i == 0 ? 'x' : i == 1 ? 'y' : 'z');
37
37
  }
38
38
  }
39
- if (coordinates.size == 3) {
40
- result.push({
41
- typ: EnumToken.FunctionTokenType,
42
- val: 'translate3d',
43
- chi: [
44
- { typ: EnumToken.LengthTokenType, val: round(decomposed.translate[0]) + '', unit: 'px' },
45
- { typ: EnumToken.CommaTokenType },
46
- { typ: EnumToken.LengthTokenType, val: round(decomposed.translate[1]) + '', unit: 'px' },
47
- { typ: EnumToken.CommaTokenType },
48
- { typ: EnumToken.LengthTokenType, val: round(decomposed.translate[2]) + '', unit: 'px' }
49
- ]
50
- });
51
- }
52
- else if (coordinates.size == 1) {
39
+ if (coordinates.size == 1) {
53
40
  if (coordinates.has('x')) {
54
41
  result.push({
55
42
  typ: EnumToken.FunctionTokenType,
56
43
  val: 'translate',
57
- chi: [{ typ: EnumToken.LengthTokenType, val: round(decomposed.translate[0]) + '', unit: 'px' }]
44
+ chi: [{ typ: EnumToken.LengthTokenType, val: round(decomposed.translate[0]), unit: 'px' }]
58
45
  });
59
46
  }
60
47
  else {
@@ -63,7 +50,7 @@ function minify(matrix) {
63
50
  result.push({
64
51
  typ: EnumToken.FunctionTokenType,
65
52
  val: 'translate' + axis.toUpperCase(),
66
- chi: [{ typ: EnumToken.LengthTokenType, val: round(decomposed.translate[index]) + '', unit: 'px' }]
53
+ chi: [{ typ: EnumToken.LengthTokenType, val: round(decomposed.translate[index]), unit: 'px' }]
67
54
  });
68
55
  }
69
56
  }
@@ -74,15 +61,15 @@ function minify(matrix) {
74
61
  chi: [
75
62
  decomposed.translate[0] == 0 ? {
76
63
  typ: EnumToken.NumberTokenType,
77
- 'val': '0'
78
- } : { typ: EnumToken.LengthTokenType, val: round(decomposed.translate[0]) + '', unit: 'px' },
64
+ val: 0
65
+ } : { typ: EnumToken.LengthTokenType, val: round(decomposed.translate[0]), unit: 'px' },
79
66
  { typ: EnumToken.CommaTokenType },
80
67
  decomposed.translate[1] == 0 ? {
81
68
  typ: EnumToken.NumberTokenType,
82
- 'val': '0'
83
- } : { typ: EnumToken.LengthTokenType, val: round(decomposed.translate[1]) + '', unit: 'px' },
69
+ val: 0
70
+ } : { typ: EnumToken.LengthTokenType, val: round(decomposed.translate[1]), unit: 'px' },
84
71
  { typ: EnumToken.CommaTokenType },
85
- { typ: EnumToken.LengthTokenType, val: round(decomposed.translate[2]) + '', unit: 'px' }
72
+ { typ: EnumToken.LengthTokenType, val: round(decomposed.translate[2]), unit: 'px' }
86
73
  ]
87
74
  });
88
75
  }
@@ -91,15 +78,9 @@ function minify(matrix) {
91
78
  typ: EnumToken.FunctionTokenType,
92
79
  val: 'translate',
93
80
  chi: [
94
- decomposed.translate[0] == 0 ? {
95
- typ: EnumToken.NumberTokenType,
96
- 'val': '0'
97
- } : { typ: EnumToken.LengthTokenType, val: round(decomposed.translate[0]) + '', unit: 'px' },
81
+ { typ: EnumToken.LengthTokenType, val: round(decomposed.translate[0]), unit: 'px' },
98
82
  { typ: EnumToken.CommaTokenType },
99
- decomposed.translate[1] == 0 ? {
100
- typ: EnumToken.NumberTokenType,
101
- 'val': '0'
102
- } : { typ: EnumToken.LengthTokenType, val: round(decomposed.translate[1]) + '', unit: 'px' }
83
+ { typ: EnumToken.LengthTokenType, val: round(decomposed.translate[1]), unit: 'px' }
103
84
  ]
104
85
  });
105
86
  }
@@ -113,7 +94,7 @@ function minify(matrix) {
113
94
  chi: [
114
95
  {
115
96
  typ: EnumToken.AngleTokenType,
116
- val: '' + round(angle),
97
+ val: round(angle),
117
98
  unit: 'deg'
118
99
  }
119
100
  ]
@@ -126,7 +107,7 @@ function minify(matrix) {
126
107
  chi: [
127
108
  {
128
109
  typ: EnumToken.AngleTokenType,
129
- val: '' + round(angle),
110
+ val: round(angle),
130
111
  unit: 'deg'
131
112
  }
132
113
  ]
@@ -139,7 +120,7 @@ function minify(matrix) {
139
120
  chi: [
140
121
  {
141
122
  typ: EnumToken.AngleTokenType,
142
- val: '' + round(angle),
123
+ val: round(angle),
143
124
  unit: 'deg'
144
125
  }
145
126
  ]
@@ -152,22 +133,22 @@ function minify(matrix) {
152
133
  chi: [
153
134
  {
154
135
  typ: EnumToken.NumberTokenType,
155
- val: '' + round(x)
136
+ val: round(x)
156
137
  },
157
138
  { typ: EnumToken.CommaTokenType },
158
139
  {
159
140
  typ: EnumToken.NumberTokenType,
160
- val: '' + round(y)
141
+ val: round(y)
161
142
  },
162
143
  { typ: EnumToken.CommaTokenType },
163
144
  {
164
145
  typ: EnumToken.NumberTokenType,
165
- val: '' + round(z)
146
+ val: round(z)
166
147
  },
167
148
  { typ: EnumToken.CommaTokenType },
168
149
  {
169
150
  typ: EnumToken.AngleTokenType,
170
- val: '' + round(angle),
151
+ val: round(angle),
171
152
  unit: 'deg'
172
153
  }
173
154
  ]
@@ -175,9 +156,10 @@ function minify(matrix) {
175
156
  }
176
157
  }
177
158
  if (transforms.has('skew')) {
178
- if (round(decomposed.skew[0]) == 0) {
179
- skew.delete('x');
180
- }
159
+ // if (round(decomposed.skew[0]) == 0) {
160
+ //
161
+ // skew.delete('x');
162
+ // }
181
163
  if (round(decomposed.skew[1]) == 0) {
182
164
  skew.delete('y');
183
165
  }
@@ -189,7 +171,7 @@ function minify(matrix) {
189
171
  typ: EnumToken.FunctionTokenType,
190
172
  val: 'skew' + (skew.has('x') ? '' : 'Y'),
191
173
  chi: [
192
- { typ: EnumToken.AngleTokenType, val: '' + round(decomposed.skew[0]), unit: 'deg' }
174
+ { typ: EnumToken.AngleTokenType, val: round(decomposed.skew[0]), unit: 'deg' }
193
175
  ]
194
176
  });
195
177
  }
@@ -198,9 +180,9 @@ function minify(matrix) {
198
180
  typ: EnumToken.FunctionTokenType,
199
181
  val: 'skew',
200
182
  chi: [
201
- { typ: EnumToken.AngleTokenType, val: '' + round(decomposed.skew[0]), unit: 'deg' },
183
+ { typ: EnumToken.AngleTokenType, val: round(decomposed.skew[0]), unit: 'deg' },
202
184
  { typ: EnumToken.CommaTokenType },
203
- { typ: EnumToken.AngleTokenType, val: '' + round(decomposed.skew[1]), unit: 'deg' }
185
+ { typ: EnumToken.AngleTokenType, val: round(decomposed.skew[1]), unit: 'deg' }
204
186
  ]
205
187
  });
206
188
  }
@@ -222,7 +204,7 @@ function minify(matrix) {
222
204
  typ: EnumToken.FunctionTokenType,
223
205
  val: 'scale' + prefix,
224
206
  chi: [
225
- { typ: EnumToken.NumberTokenType, val: '' + round(prefix == 'Z' ? sz : prefix == 'Y' ? sy : sx) }
207
+ { typ: EnumToken.NumberTokenType, val: round(prefix == 'Z' ? sz : prefix == 'Y' ? sy : sx) }
226
208
  ]
227
209
  });
228
210
  }
@@ -231,9 +213,9 @@ function minify(matrix) {
231
213
  typ: EnumToken.FunctionTokenType,
232
214
  val: 'scale',
233
215
  chi: [
234
- { typ: EnumToken.NumberTokenType, val: '' + round(sx) },
216
+ { typ: EnumToken.NumberTokenType, val: round(sx) },
235
217
  { typ: EnumToken.CommaTokenType },
236
- { typ: EnumToken.NumberTokenType, val: '' + round(sy) },
218
+ { typ: EnumToken.NumberTokenType, val: round(sy) },
237
219
  ]
238
220
  });
239
221
  }
@@ -242,24 +224,15 @@ function minify(matrix) {
242
224
  typ: EnumToken.FunctionTokenType,
243
225
  val: 'scale3d',
244
226
  chi: [
245
- { typ: EnumToken.NumberTokenType, val: '' + round(sx) },
227
+ { typ: EnumToken.NumberTokenType, val: round(sx) },
246
228
  { typ: EnumToken.CommaTokenType },
247
- { typ: EnumToken.NumberTokenType, val: '' + round(sy) },
229
+ { typ: EnumToken.NumberTokenType, val: round(sy) },
248
230
  { typ: EnumToken.CommaTokenType },
249
- { typ: EnumToken.NumberTokenType, val: '' + round(sz) }
231
+ { typ: EnumToken.NumberTokenType, val: round(sz) }
250
232
  ]
251
233
  });
252
234
  }
253
235
  }
254
- if (transforms.has('perspective')) {
255
- result.push({
256
- typ: EnumToken.FunctionTokenType,
257
- val: 'perspective',
258
- chi: [
259
- { typ: EnumToken.Length, val: '' + round(1 / decomposed.perspective[2]), unit: 'px' },
260
- ]
261
- });
262
- }
263
236
  // identity
264
237
  return result.length == 0 || (result.length == 1 && eqMatrix(identity(), result)) ? [
265
238
  {
@@ -269,10 +242,13 @@ function minify(matrix) {
269
242
  ] : result;
270
243
  }
271
244
  function eqMatrix(a, b) {
245
+ // console.error(JSON.stringify({a, b}, null, 1));
272
246
  let mat = identity();
273
247
  let tmp = identity();
274
248
  // @ts-ignore
275
- const data = Array.isArray(a) ? a : parseMatrix(a);
249
+ const data = (Array.isArray(a) ? a : parseMatrix(a));
250
+ // toZero(data);
251
+ // console.error({data});
276
252
  for (const transform of b) {
277
253
  tmp = computeMatrix([transform], identity());
278
254
  if (tmp == null) {
@@ -280,12 +256,14 @@ function eqMatrix(a, b) {
280
256
  }
281
257
  mat = multiply(mat, tmp);
282
258
  }
259
+ // toZero(mat);
260
+ // console.error({mat});
283
261
  if (mat == null) {
284
262
  return false;
285
263
  }
286
264
  for (let i = 0; i < 4; i++) {
287
265
  for (let j = 0; j < 4; j++) {
288
- if (Math.abs(mat[i][j] - data[i][j]) > epsilon) {
266
+ if (Math.abs(mat[i * 4 + j] - data[i * 4 + j]) > epsilon) {
289
267
  return false;
290
268
  }
291
269
  }
@@ -3,7 +3,7 @@ import { identity, multiply } from './utils.js';
3
3
  function perspective(x, from) {
4
4
  const matrix = identity();
5
5
  // @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;
7
7
  return multiply(from, matrix);
8
8
  }
9
9
 
@@ -17,23 +17,23 @@ function rotate3D(angle, x, y, z, from) {
17
17
  x *= unit;
18
18
  y *= unit;
19
19
  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;
29
29
  return multiply(from, matrix);
30
30
  }
31
31
  function rotate(angle, from) {
32
32
  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);
37
37
  return multiply(from, matrix);
38
38
  }
39
39
 
@@ -2,30 +2,30 @@ import { identity, multiply } from './utils.js';
2
2
 
3
3
  function scaleX(x, from) {
4
4
  const matrix = identity();
5
- matrix[0][0] = x;
5
+ matrix[0 * 4 + 0] = x;
6
6
  return multiply(from, matrix);
7
7
  }
8
8
  function scaleY(y, from) {
9
9
  const matrix = identity();
10
- matrix[1][1] = y;
10
+ matrix[1 * 4 + 1] = y;
11
11
  return multiply(from, matrix);
12
12
  }
13
13
  function scaleZ(z, from) {
14
14
  const matrix = identity();
15
- matrix[2][2] = z;
15
+ matrix[2 * 4 + 2] = z;
16
16
  return multiply(from, matrix);
17
17
  }
18
18
  function scale(x, y, from) {
19
19
  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;
22
22
  return multiply(from, matrix);
23
23
  }
24
24
  function scale3d(x, y, z, from) {
25
25
  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;
29
29
  return multiply(from, matrix);
30
30
  }
31
31
 
@@ -2,20 +2,20 @@ import { identity, multiply } from './utils.js';
2
2
 
3
3
  function skewX(x, from) {
4
4
  const matrix = identity();
5
- matrix[1][0] = Math.tan(x);
5
+ matrix[1 * 4 + 0] = Math.tan(x);
6
6
  return multiply(from, matrix);
7
7
  }
8
8
  function skewY(y, from) {
9
9
  const matrix = identity();
10
- matrix[0][1] = Math.tan(y);
10
+ matrix[0 * 4 + 1] = Math.tan(y);
11
11
  return multiply(from, matrix);
12
12
  }
13
13
  // convert angle to radian
14
14
  function skew(values, from) {
15
15
  const matrix = identity();
16
- matrix[1][0] = Math.tan(values[0]);
16
+ matrix[1 * 4 + 0] = Math.tan(values[0]);
17
17
  if (values.length > 1) {
18
- matrix[0][1] = Math.tan(values[1]);
18
+ matrix[0 * 4 + 1] = Math.tan(values[1]);
19
19
  }
20
20
  return multiply(from, matrix);
21
21
  }
@@ -2,30 +2,30 @@ import { identity, multiply } from './utils.js';
2
2
 
3
3
  function translateX(x, from) {
4
4
  const matrix = identity();
5
- matrix[3][0] = x;
5
+ matrix[3 * 4 + 0] = x;
6
6
  return multiply(from, matrix);
7
7
  }
8
8
  function translateY(y, from) {
9
9
  const matrix = identity();
10
- matrix[3][1] = y;
10
+ matrix[3 * 4 + 1] = y;
11
11
  return multiply(from, matrix);
12
12
  }
13
13
  function translateZ(z, from) {
14
14
  const matrix = identity();
15
- matrix[3][2] = z;
15
+ matrix[3 * 4 + 2] = z;
16
16
  return multiply(from, matrix);
17
17
  }
18
18
  function translate(translate, from) {
19
19
  const matrix = identity();
20
- matrix[3][0] = translate[0];
21
- matrix[3][1] = translate[1] ?? 0;
20
+ matrix[3 * 4 + 0] = translate[0];
21
+ matrix[3 * 4 + 1] = translate[1] ?? 0;
22
22
  return multiply(from, matrix);
23
23
  }
24
24
  function translate3d(translate, from) {
25
25
  const matrix = identity();
26
- matrix[3][0] = translate[0];
27
- matrix[3][1] = translate[1];
28
- matrix[3][2] = translate[2];
26
+ matrix[3 * 4 + 0] = translate[0];
27
+ matrix[3 * 4 + 1] = translate[1];
28
+ matrix[3 * 4 + 2] = translate[2];
29
29
  return multiply(from, matrix);
30
30
  }
31
31