@solidtv/solid 1.7.3 → 1.8.0-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 (73) hide show
  1. package/MIGRATION-1.8.md +270 -0
  2. package/dist/src/core/dom-renderer/dashedBorderStyle.d.ts +20 -0
  3. package/dist/src/core/dom-renderer/dashedBorderStyle.js +104 -0
  4. package/dist/src/core/dom-renderer/dashedBorderStyle.js.map +1 -0
  5. package/dist/src/core/dom-renderer/domRenderer.d.ts +14 -0
  6. package/dist/src/core/dom-renderer/domRenderer.js +194 -12
  7. package/dist/src/core/dom-renderer/domRenderer.js.map +1 -1
  8. package/dist/src/core/dom-renderer/domRendererTypes.d.ts +12 -2
  9. package/dist/src/core/elementNode.d.ts +176 -9
  10. package/dist/src/core/elementNode.js +1906 -411
  11. package/dist/src/core/elementNode.js.map +1 -1
  12. package/dist/src/core/flexLayout.d.ts +6 -0
  13. package/dist/src/core/flexLayout.js +535 -218
  14. package/dist/src/core/flexLayout.js.map +1 -1
  15. package/dist/src/core/focusManager.js +70 -23
  16. package/dist/src/core/focusManager.js.map +1 -1
  17. package/dist/src/core/fontLoaded.d.ts +10 -0
  18. package/dist/src/core/fontLoaded.js +26 -0
  19. package/dist/src/core/fontLoaded.js.map +1 -0
  20. package/dist/src/core/intrinsicTypes.d.ts +2 -1
  21. package/dist/src/core/lightningInit.js +8 -1
  22. package/dist/src/core/lightningInit.js.map +1 -1
  23. package/dist/src/core/shaders.d.ts +5 -2
  24. package/dist/src/core/shaders.js +6 -0
  25. package/dist/src/core/shaders.js.map +1 -1
  26. package/dist/src/core/states.d.ts +2 -0
  27. package/dist/src/core/states.js +26 -7
  28. package/dist/src/core/states.js.map +1 -1
  29. package/dist/src/core/stylePlan.d.ts +39 -0
  30. package/dist/src/core/stylePlan.js +159 -0
  31. package/dist/src/core/stylePlan.js.map +1 -0
  32. package/dist/src/core/utils.js +2 -2
  33. package/dist/src/primitives/Grid.d.ts +7 -1
  34. package/dist/src/primitives/Grid.jsx +48 -4
  35. package/dist/src/primitives/Grid.jsx.map +1 -1
  36. package/dist/src/primitives/Virtual.jsx +16 -6
  37. package/dist/src/primitives/Virtual.jsx.map +1 -1
  38. package/dist/src/primitives/useMouse.d.ts +10 -5
  39. package/dist/src/primitives/useMouse.js +42 -10
  40. package/dist/src/primitives/useMouse.js.map +1 -1
  41. package/dist/src/primitives/utils/handleNavigation.js +29 -6
  42. package/dist/src/primitives/utils/handleNavigation.js.map +1 -1
  43. package/dist/src/primitives/utils/withScrolling.js +2 -1
  44. package/dist/src/primitives/utils/withScrolling.js.map +1 -1
  45. package/dist/src/solidOpts.js +22 -8
  46. package/dist/src/solidOpts.js.map +1 -1
  47. package/dist/tsconfig.tsbuildinfo +1 -1
  48. package/docs/AI_CODING_GUIDELINES.md +7 -6
  49. package/docs/AI_UPGRADE_1.8.md +290 -0
  50. package/package.json +5 -3
  51. package/src/core/dom-renderer/dashedBorderStyle.ts +128 -0
  52. package/src/core/dom-renderer/domRenderer.ts +201 -10
  53. package/src/core/dom-renderer/domRendererTypes.ts +12 -0
  54. package/src/core/elementNode.ts +2124 -459
  55. package/src/core/flexLayout.ts +583 -231
  56. package/src/core/focusManager.ts +86 -27
  57. package/src/core/fontLoaded.ts +29 -0
  58. package/src/core/intrinsicTypes.ts +2 -0
  59. package/src/core/lightningInit.ts +8 -1
  60. package/src/core/shaders.ts +12 -0
  61. package/src/core/states.ts +29 -9
  62. package/src/core/stylePlan.ts +195 -0
  63. package/src/core/utils.ts +2 -2
  64. package/src/primitives/Grid.tsx +66 -14
  65. package/src/primitives/Virtual.tsx +20 -6
  66. package/src/primitives/useMouse.ts +73 -6
  67. package/src/primitives/utils/handleNavigation.ts +40 -6
  68. package/src/primitives/utils/withScrolling.ts +3 -2
  69. package/src/solidOpts.ts +23 -8
  70. package/dist/src/core/flex.d.ts +0 -2
  71. package/dist/src/core/flex.js +0 -285
  72. package/dist/src/core/flex.js.map +0 -1
  73. package/src/core/flex.ts +0 -332
@@ -1,5 +1,37 @@
1
+ import { Config } from './config.js';
1
2
  import { isTextNode, isElementText } from './utils.js';
2
- function getArrayValue(val, index, defaultValue = 0) {
3
+ // One flex pass over one container. It allocates nothing in the steady
4
+ // state: the per-child values live in module-level typed arrays that grow on
5
+ // demand and are reused by every pass, and the children are reached through
6
+ // an index array (`order`), sorted by `flexOrder` only when a child has one.
7
+ // A prop is written only when it must be, so a relayout that moves nothing
8
+ // writes nothing (and starts no transition). A prop with a transition reads
9
+ // back its current, animated value, not its target, so for those the last
10
+ // value this engine wrote is kept on the node (`_flexX`, `_flexY`, `_flexW`,
11
+ // `_flexH`) and the write is skipped only when both match.
12
+ /** Slot flags. */
13
+ const FROM_RECORD = 1; // the main size came from the flex record (B8)
14
+ const GROWN = 2;
15
+ const SHRUNK = 4;
16
+ let capacity = 0;
17
+ let childIndex = new Int32Array(0); // slot -> index in node.children
18
+ let order = new Int32Array(0); // layout position -> slot
19
+ let flags = new Uint8Array(0);
20
+ let mainSize = new Float64Array(0);
21
+ let mainRead = new Float64Array(0); // the main size read from the child
22
+ let ownSize = new Float64Array(0); // the size before flexGrow/flexShrink (B8)
23
+ let marginStart = new Float64Array(0);
24
+ let marginEnd = new Float64Array(0);
25
+ let totalMain = new Float64Array(0);
26
+ let crossSize = new Float64Array(0);
27
+ let marginCrossStart = new Float64Array(0);
28
+ let marginCrossEnd = new Float64Array(0);
29
+ let growFactor = new Float64Array(0);
30
+ let shrinkFactor = new Float64Array(0);
31
+ let orderKey = new Float64Array(0);
32
+ let lineStart = new Float64Array(0);
33
+ /** A `padding`/`margin` side from a number or a 2-4 value array. */
34
+ export function getArrayValue(val, index, defaultValue = 0) {
3
35
  if (val === undefined)
4
36
  return defaultValue;
5
37
  if (typeof val === 'number')
@@ -17,12 +49,162 @@ function getArrayValue(val, index, defaultValue = 0) {
17
49
  }
18
50
  return result ?? defaultValue;
19
51
  }
52
+ /**
53
+ * Whether a write of `name` (or its alias) on `c` starts a transition, as
54
+ * ElementNode's animatable setter decides it.
55
+ */
56
+ function animates(c, name, alias) {
57
+ const t = c.transition;
58
+ if (!t || c.rendered !== true || !Config.animationsEnabled) {
59
+ return false;
60
+ }
61
+ return t === true || !!t[name] || !!t[alias];
62
+ }
63
+ function setSize(c, isWidth, v) {
64
+ if (isWidth) {
65
+ c.width = v;
66
+ }
67
+ else {
68
+ c.height = v;
69
+ }
70
+ }
71
+ /**
72
+ * Writes a size the engine computed, unless `current` (read from the node)
73
+ * is already `v` and, with a transition on it (`current` is then the
74
+ * animated value), `v` is also what this engine last wrote. Returns whether
75
+ * it wrote. Callers test `current !== v || c.transition` first, so the
76
+ * common no-write case passes no doubles to a call.
77
+ */
78
+ function writeSize(c, isWidth, current, v) {
79
+ const animated = c.transition
80
+ ? isWidth
81
+ ? animates(c, 'w', 'width')
82
+ : animates(c, 'h', 'height')
83
+ : false;
84
+ if (current === v && (!animated || (isWidth ? c._flexW : c._flexH) === v)) {
85
+ return false;
86
+ }
87
+ if (animated) {
88
+ if (isWidth) {
89
+ c._flexW = v;
90
+ }
91
+ else {
92
+ c._flexH = v;
93
+ }
94
+ }
95
+ setSize(c, isWidth, v);
96
+ return true;
97
+ }
98
+ /** Writes x or y as writeSize writes a size. */
99
+ function setPos(c, isX, v) {
100
+ if (isX) {
101
+ const animated = c.transition ? animates(c, 'x', 'x') : false;
102
+ if (c.x === v && (!animated || c._flexX === v))
103
+ return;
104
+ if (animated)
105
+ c._flexX = v;
106
+ c.x = v;
107
+ }
108
+ else {
109
+ const animated = c.transition ? animates(c, 'y', 'y') : false;
110
+ if (c.y === v && (!animated || c._flexY === v))
111
+ return;
112
+ if (animated)
113
+ c._flexY = v;
114
+ c.y = v;
115
+ }
116
+ }
117
+ /**
118
+ * Places child `c` (slot `k`) on the cross axis of a line that starts at
119
+ * `start` and is `size` long.
120
+ */
121
+ function alignCross(c, k, isRow, align, start, size) {
122
+ const alignSelf = c.alignSelf || align;
123
+ if (alignSelf === 'flexStart') {
124
+ setPos(c, !isRow, start + marginCrossStart[k]);
125
+ }
126
+ else if (alignSelf === 'center') {
127
+ setPos(c, !isRow, start + (size - crossSize[k]) / 2 + marginCrossStart[k]);
128
+ }
129
+ else if (alignSelf === 'flexEnd') {
130
+ setPos(c, !isRow, start + size - crossSize[k] - marginCrossEnd[k]);
131
+ }
132
+ }
133
+ let passDepth = 0;
134
+ /**
135
+ * Lays out the children of a `display: 'flex'` container. Returns true when
136
+ * the container's own size changed, so its parent must be laid out again.
137
+ */
20
138
  export default function (node) {
139
+ if (passDepth === 0) {
140
+ passDepth = 1;
141
+ try {
142
+ return layout(node);
143
+ }
144
+ finally {
145
+ passDepth = 0;
146
+ }
147
+ }
148
+ return nestedLayout(node);
149
+ }
150
+ /**
151
+ * A pass started while another runs (from an app callback a write fired,
152
+ * such as onAnimation calling updateLayout): it gets its own scratch, so
153
+ * the outer pass's is intact when it resumes. Allocates; not the hot path.
154
+ */
155
+ function nestedLayout(node) {
156
+ const sCapacity = capacity;
157
+ const sChildIndex = childIndex;
158
+ const sOrder = order;
159
+ const sFlags = flags;
160
+ const sMainSize = mainSize;
161
+ const sMainRead = mainRead;
162
+ const sOwnSize = ownSize;
163
+ const sMarginStart = marginStart;
164
+ const sMarginEnd = marginEnd;
165
+ const sTotalMain = totalMain;
166
+ const sCrossSize = crossSize;
167
+ const sMarginCrossStart = marginCrossStart;
168
+ const sMarginCrossEnd = marginCrossEnd;
169
+ const sGrowFactor = growFactor;
170
+ const sShrinkFactor = shrinkFactor;
171
+ const sOrderKey = orderKey;
172
+ const sLineStart = lineStart;
173
+ capacity = 0;
174
+ passDepth++;
175
+ try {
176
+ return layout(node);
177
+ }
178
+ finally {
179
+ passDepth--;
180
+ capacity = sCapacity;
181
+ childIndex = sChildIndex;
182
+ order = sOrder;
183
+ flags = sFlags;
184
+ mainSize = sMainSize;
185
+ mainRead = sMainRead;
186
+ ownSize = sOwnSize;
187
+ marginStart = sMarginStart;
188
+ marginEnd = sMarginEnd;
189
+ totalMain = sTotalMain;
190
+ crossSize = sCrossSize;
191
+ marginCrossStart = sMarginCrossStart;
192
+ marginCrossEnd = sMarginCrossEnd;
193
+ growFactor = sGrowFactor;
194
+ shrinkFactor = sShrinkFactor;
195
+ orderKey = sOrderKey;
196
+ lineStart = sLineStart;
197
+ }
198
+ }
199
+ function layout(node) {
200
+ const children = node.children;
201
+ const numChildren = children.length;
202
+ if (numChildren === 0) {
203
+ return false;
204
+ }
21
205
  const direction = node.flexDirection || 'row';
22
206
  const isRow = direction === 'row' || direction === 'row-reverse';
23
207
  const isReverse = direction === 'row-reverse' || direction === 'column-reverse';
24
- const dimension = isRow ? 'width' : 'height';
25
- const crossDimension = isRow ? 'height' : 'width';
26
208
  // padding order: Top, Right, Bottom, Left
27
209
  const nodePadding = node.padding;
28
210
  const paddingTop = node.paddingTop ?? getArrayValue(nodePadding, 0);
@@ -34,321 +216,456 @@ export default function (node) {
34
216
  const paddingCrossStart = isRow ? paddingTop : paddingLeft;
35
217
  const paddingCrossEnd = isRow ? paddingBottom : paddingRight;
36
218
  const nodePaddingTotal = paddingStart + paddingEnd;
37
- const minDimension = isRow ? 'minWidth' : 'minHeight';
38
- const crossMinDimension = isRow ? 'minHeight' : 'minWidth';
39
- const children = node.children;
40
- const numChildren = children.length;
41
- if (numChildren === 0) {
42
- return false;
219
+ const flexWrap = node.flexWrap;
220
+ const isWrapReverse = flexWrap === 'wrap-reverse';
221
+ // B10: wrap-reverse wraps too.
222
+ const wrapping = flexWrap === 'wrap' || isWrapReverse;
223
+ const align = node.alignItems || (flexWrap ? 'flexStart' : undefined);
224
+ // Scratch for numChildren slots. Written out here, not a helper: terser's
225
+ // default `reduce_funcs` makes a function with one call site a closure
226
+ // created per call, and one created in this cold branch is no better: in
227
+ // Chrome 141 the function holding it (Maglev) then boxes this pass's
228
+ // doubles (virtual-grid: 2 to 5.5 KiB per press).
229
+ if (numChildren > capacity) {
230
+ let c = capacity * 2;
231
+ if (c < numChildren) {
232
+ c = numChildren < 16 ? 16 : numChildren;
233
+ }
234
+ capacity = c;
235
+ childIndex = new Int32Array(c);
236
+ order = new Int32Array(c);
237
+ flags = new Uint8Array(c);
238
+ mainSize = new Float64Array(c);
239
+ mainRead = new Float64Array(c);
240
+ ownSize = new Float64Array(c);
241
+ marginStart = new Float64Array(c);
242
+ marginEnd = new Float64Array(c);
243
+ totalMain = new Float64Array(c);
244
+ crossSize = new Float64Array(c);
245
+ marginCrossStart = new Float64Array(c);
246
+ marginCrossEnd = new Float64Array(c);
247
+ growFactor = new Float64Array(c);
248
+ shrinkFactor = new Float64Array(c);
249
+ orderKey = new Float64Array(c);
250
+ lineStart = new Float64Array(c);
43
251
  }
44
- // Optimize arrays caching
45
- const processableChildrenIndices = [];
252
+ let n = 0;
46
253
  let hasOrder = false;
47
254
  let totalFlexGrow = 0;
48
255
  let totalFlexShrink = 0;
256
+ let sizeWork = false;
49
257
  for (let i = 0; i < numChildren; i++) {
50
258
  const c = children[i];
51
- if (isElementText(c) && c.text && !(c.width || c.height)) {
52
- return false; // specific text layout constraint
53
- }
259
+ // B5: a child that is not a flex item is skipped before the text check,
260
+ // so an unsized text with flexItem={false} does not block the pass.
54
261
  if (isTextNode(c) || c.flexItem === false) {
55
262
  continue;
56
263
  }
57
- if (c.flexOrder !== undefined) {
264
+ const w = c.width;
265
+ const h = c.height;
266
+ if (isElementText(c) && c.text && !(w || h)) {
267
+ return false; // specific text layout constraint
268
+ }
269
+ const flexOrder = c.flexOrder;
270
+ if (flexOrder !== undefined) {
58
271
  hasOrder = true;
59
272
  }
60
273
  const flexGrow = c.flexGrow;
61
- if (flexGrow !== undefined && flexGrow > 0) {
62
- totalFlexGrow += flexGrow;
63
- }
274
+ const grow = flexGrow !== undefined && flexGrow > 0 ? flexGrow : 0;
275
+ totalFlexGrow += grow;
64
276
  const flexShrink = c.flexShrink;
65
277
  if (flexShrink !== undefined && flexShrink > 0) {
66
278
  totalFlexShrink += flexShrink;
67
279
  }
68
- if (c[minDimension] && (c[dimension] || 0) < c[minDimension]) {
69
- c[dimension] = c[minDimension];
280
+ let main = isRow ? w : h;
281
+ let cross = isRow ? h : w;
282
+ const minMain = isRow ? c.minWidth : c.minHeight;
283
+ if (minMain && (main || 0) < minMain) {
284
+ setSize(c, isRow, minMain);
285
+ main = isRow ? c.width : c.height;
286
+ }
287
+ const minCross = isRow ? c.minHeight : c.minWidth;
288
+ if (minCross && (cross || 0) < minCross) {
289
+ setSize(c, !isRow, minCross);
290
+ cross = isRow ? c.height : c.width;
70
291
  }
71
- if (c[crossMinDimension] &&
72
- (c[crossDimension] || 0) < c[crossMinDimension]) {
73
- c[crossDimension] = c[crossMinDimension];
292
+ // B8: flexGrow and flexShrink start from the item's own size, not from
293
+ // the size the last pass grew or shrank it to. The record holds both; a
294
+ // size written since (anything but the flexed one) is the new own size.
295
+ // With a transition the read is the animated value, so the record is
296
+ // kept as it is.
297
+ const read = main || 0;
298
+ let own = read;
299
+ let flag = 0;
300
+ const grown = c._flexGrown;
301
+ if (grown !== undefined) {
302
+ if ((grow > 0 || (flexShrink !== undefined && flexShrink > 0)) &&
303
+ (read === grown ||
304
+ (isRow
305
+ ? animates(c, 'w', 'width')
306
+ : animates(c, 'h', 'height')))) {
307
+ own = c._flexBase;
308
+ flag = FROM_RECORD;
309
+ sizeWork = true;
310
+ }
311
+ else {
312
+ c._flexGrown = undefined;
313
+ }
314
+ }
315
+ const flexBasis = c.flexBasis;
316
+ const base = flexBasis === undefined || flexBasis === 'auto'
317
+ ? own
318
+ : Math.max(flexBasis, minMain || 0);
319
+ // index mappings for margins: Top: 0, Right: 1, Bottom: 2, Left: 3
320
+ const margin = c.margin;
321
+ let mStart;
322
+ let mEnd;
323
+ let mCrossStart;
324
+ let mCrossEnd;
325
+ if (isRow) {
326
+ mStart = c.marginLeft || getArrayValue(margin, 3);
327
+ mEnd = c.marginRight || getArrayValue(margin, 1);
328
+ mCrossStart = c.marginTop || getArrayValue(margin, 0);
329
+ mCrossEnd = c.marginBottom || getArrayValue(margin, 2);
330
+ }
331
+ else {
332
+ mStart = c.marginTop || getArrayValue(margin, 0);
333
+ mEnd = c.marginBottom || getArrayValue(margin, 2);
334
+ mCrossStart = c.marginLeft || getArrayValue(margin, 3);
335
+ mCrossEnd = c.marginRight || getArrayValue(margin, 1);
74
336
  }
75
- processableChildrenIndices.push(i);
337
+ const k = n++;
338
+ childIndex[k] = i;
339
+ flags[k] = flag;
340
+ mainSize[k] = base;
341
+ mainRead[k] = read;
342
+ ownSize[k] = own;
343
+ marginStart[k] = mStart;
344
+ marginEnd[k] = mEnd;
345
+ totalMain[k] = base + mStart + mEnd;
346
+ crossSize[k] = cross || 0;
347
+ marginCrossStart[k] = mCrossStart;
348
+ marginCrossEnd[k] = mCrossEnd;
349
+ growFactor[k] = grow;
350
+ shrinkFactor[k] = flexShrink || 0;
351
+ orderKey[k] = flexOrder || 0;
352
+ }
353
+ if (n === 0) {
354
+ return false;
355
+ }
356
+ for (let p = 0; p < n; p++) {
357
+ order[p] = p;
76
358
  }
77
359
  if (hasOrder) {
78
- processableChildrenIndices.sort((aIdx, bIdx) => {
79
- const a = children[aIdx];
80
- const b = children[bIdx];
81
- return (a.flexOrder || 0) - (b.flexOrder || 0);
82
- });
360
+ // Stable insertion sort by flexOrder (a missing one counts as 0).
361
+ for (let p = 1; p < n; p++) {
362
+ const k = order[p];
363
+ const key = orderKey[k];
364
+ let q = p - 1;
365
+ while (q >= 0 && orderKey[order[q]] > key) {
366
+ order[q + 1] = order[q];
367
+ q--;
368
+ }
369
+ order[q + 1] = k;
370
+ }
83
371
  }
84
372
  // Apply reverse layout ordering
85
373
  if (isReverse || node.direction === 'rtl') {
86
- processableChildrenIndices.reverse();
87
- }
88
- const numProcessedChildren = processableChildrenIndices.length;
89
- if (numProcessedChildren === 0) {
90
- return false;
374
+ for (let lo = 0, hi = n - 1; lo < hi; lo++, hi--) {
375
+ const t = order[lo];
376
+ order[lo] = order[hi];
377
+ order[hi] = t;
378
+ }
91
379
  }
92
- const prop = isRow ? 'x' : 'y';
93
- const crossProp = isRow ? 'y' : 'x';
94
- const containerSize = Math.max(node[dimension] || 0, node[minDimension] || 0, 0);
95
- let containerCrossSize = Math.max(node[crossDimension] || 0, node[crossMinDimension] || 0, 0);
96
- const isWrapReverse = node.flexWrap === 'wrap-reverse';
380
+ const nodeWidth = node.width;
381
+ const nodeHeight = node.height;
382
+ const nodeMain = isRow ? nodeWidth : nodeHeight;
383
+ const containerSize = Math.max(nodeMain || 0, (isRow ? node.minWidth : node.minHeight) || 0, 0);
384
+ let containerCrossSize = Math.max((isRow ? nodeHeight : nodeWidth) || 0, (isRow ? node.minHeight : node.minWidth) || 0, 0);
97
385
  const gap = node.gap || 0;
98
386
  const justify = node.justifyContent || 'flexStart';
99
- const align = node.alignItems || (node.flexWrap ? 'flexStart' : undefined);
100
387
  let containerUpdated = false;
101
- // Resolve sizes matching old processed calculation
102
- const childMainSizes = new Float32Array(numProcessedChildren);
103
- const childMarginStarts = new Float32Array(numProcessedChildren);
104
- const childMarginEnds = new Float32Array(numProcessedChildren);
105
- const childTotalMainSizes = new Float32Array(numProcessedChildren);
106
- const childCrossSizes = new Float32Array(numProcessedChildren);
107
- const childMarginCrossStarts = new Float32Array(numProcessedChildren);
108
- const childMarginCrossEnds = new Float32Array(numProcessedChildren);
109
388
  let sumOfFlexBaseSizesWithMargins = 0;
110
- for (let idx = 0; idx < numProcessedChildren; idx++) {
111
- const c = children[processableChildrenIndices[idx]];
112
- const marginArray = c.margin;
113
- // index mappings for margins: Top: 0, Right: 1, Bottom: 2, Left: 3
114
- // if row: main = left/right (3/1),
115
- const flexBasis = c.flexBasis;
116
- const isBasisAuto = flexBasis === undefined || flexBasis === 'auto';
117
- const computedBasis = isBasisAuto
118
- ? c[dimension] || 0
119
- : flexBasis;
120
- const baseMainSize = isBasisAuto
121
- ? computedBasis
122
- : Math.max(computedBasis, c[minDimension] || 0);
123
- const marginStart = isRow
124
- ? c.marginLeft || getArrayValue(marginArray, 3)
125
- : c.marginTop || getArrayValue(marginArray, 0);
126
- const marginEnd = isRow
127
- ? c.marginRight || getArrayValue(marginArray, 1)
128
- : c.marginBottom || getArrayValue(marginArray, 2);
129
- const marginCrossStart = isRow
130
- ? c.marginTop || getArrayValue(marginArray, 0)
131
- : c.marginLeft || getArrayValue(marginArray, 3);
132
- const marginCrossEnd = isRow
133
- ? c.marginBottom || getArrayValue(marginArray, 2)
134
- : c.marginRight || getArrayValue(marginArray, 1);
135
- childMainSizes[idx] = baseMainSize;
136
- childMarginStarts[idx] = marginStart;
137
- childMarginEnds[idx] = marginEnd;
138
- childTotalMainSizes[idx] = baseMainSize + marginStart + marginEnd;
139
- childCrossSizes[idx] = c[crossDimension] || 0;
140
- childMarginCrossStarts[idx] = marginCrossStart;
141
- childMarginCrossEnds[idx] = marginCrossEnd;
142
- sumOfFlexBaseSizesWithMargins += childTotalMainSizes[idx];
143
- }
144
- if ((totalFlexGrow > 0 || totalFlexShrink > 0) && numProcessedChildren > 1) {
145
- node.flexBoundary = node.flexBoundary || 'fixed';
146
- const totalGapSpace = numProcessedChildren > 0 ? gap * (numProcessedChildren - 1) : 0;
147
- const availableSpace = containerSize - sumOfFlexBaseSizesWithMargins - totalGapSpace;
389
+ for (let p = 0; p < n; p++) {
390
+ sumOfFlexBaseSizesWithMargins += totalMain[order[p]];
391
+ }
392
+ if ((totalFlexGrow > 0 || totalFlexShrink > 0) && n > 1) {
393
+ if (!node.flexBoundary) {
394
+ node.flexBoundary = 'fixed';
395
+ }
396
+ // The space inside the padding.
397
+ const availableSpace = containerSize -
398
+ nodePaddingTotal -
399
+ sumOfFlexBaseSizesWithMargins -
400
+ gap * (n - 1);
148
401
  if (availableSpace > 0 && totalFlexGrow > 0) {
149
- for (let idx = 0; idx < numProcessedChildren; idx++) {
150
- const c = children[processableChildrenIndices[idx]];
151
- const flexGrowValue = c.flexGrow || 0;
152
- if (flexGrowValue > 0) {
153
- const shareOfSpace = (flexGrowValue / totalFlexGrow) * availableSpace;
154
- const newMainSize = childMainSizes[idx] + shareOfSpace;
155
- c[dimension] = newMainSize;
156
- childMainSizes[idx] = newMainSize;
157
- childTotalMainSizes[idx] =
158
- newMainSize + childMarginStarts[idx] + childMarginEnds[idx];
402
+ for (let p = 0; p < n; p++) {
403
+ const k = order[p];
404
+ const grow = growFactor[k];
405
+ if (grow > 0) {
406
+ const newMainSize = mainSize[k] + (grow / totalFlexGrow) * availableSpace;
407
+ mainSize[k] = newMainSize;
408
+ totalMain[k] = newMainSize + marginStart[k] + marginEnd[k];
409
+ flags[k] = flags[k] | GROWN;
159
410
  }
160
411
  }
161
- node._containsFlexGrow = node._containsFlexGrow ? null : true;
412
+ sizeWork = true;
162
413
  }
163
414
  else if (availableSpace < 0 && totalFlexShrink > 0) {
164
415
  // Flex Shrink Phase
165
416
  let totalScaledShrinkFactor = 0;
166
- for (let idx = 0; idx < numProcessedChildren; idx++) {
167
- const c = children[processableChildrenIndices[idx]];
168
- const flexShrinkValue = c.flexShrink || 0;
169
- totalScaledShrinkFactor += flexShrinkValue * childMainSizes[idx];
417
+ for (let p = 0; p < n; p++) {
418
+ const k = order[p];
419
+ totalScaledShrinkFactor += shrinkFactor[k] * mainSize[k];
170
420
  }
171
421
  if (totalScaledShrinkFactor > 0) {
172
- for (let idx = 0; idx < numProcessedChildren; idx++) {
173
- const c = children[processableChildrenIndices[idx]];
174
- const flexShrinkValue = c.flexShrink || 0;
175
- if (flexShrinkValue > 0) {
176
- const shrinkRatio = (flexShrinkValue * childMainSizes[idx]) /
177
- totalScaledShrinkFactor;
178
- const sizeReduction = shrinkRatio * Math.abs(availableSpace);
179
- let newMainSize = childMainSizes[idx] - sizeReduction;
422
+ for (let p = 0; p < n; p++) {
423
+ const k = order[p];
424
+ const shrink = shrinkFactor[k];
425
+ if (shrink > 0) {
426
+ const shrinkRatio = (shrink * mainSize[k]) / totalScaledShrinkFactor;
427
+ let newMainSize = mainSize[k] - shrinkRatio * Math.abs(availableSpace);
180
428
  // Constrain by min width/height
181
- const minBound = c[minDimension] || 0;
429
+ const c = children[childIndex[k]];
430
+ const minBound = (isRow ? c.minWidth : c.minHeight) || 0;
182
431
  if (newMainSize < minBound) {
183
432
  newMainSize = minBound;
184
433
  }
185
- c[dimension] = newMainSize;
186
- childMainSizes[idx] = newMainSize;
187
- childTotalMainSizes[idx] =
188
- newMainSize + childMarginStarts[idx] + childMarginEnds[idx];
434
+ mainSize[k] = newMainSize;
435
+ totalMain[k] = newMainSize + marginStart[k] + marginEnd[k];
436
+ flags[k] = flags[k] | SHRUNK;
437
+ sizeWork = true;
189
438
  }
190
439
  }
191
440
  }
192
- node._containsFlexGrow = node._containsFlexGrow ? null : true;
193
441
  }
194
- else if (node._containsFlexGrow) {
195
- node._containsFlexGrow = null;
442
+ }
443
+ // Whether this pass changed a child's size: updateLayout then lays the
444
+ // flex ones out again (`_containsFlexGrow`). A relayout that changes none
445
+ // asks for nothing, so a grown child that sizes itself to its content is
446
+ // not shrunk and regrown on every relayout.
447
+ let resized = false;
448
+ if (sizeWork) {
449
+ for (let p = 0; p < n; p++) {
450
+ const k = order[p];
451
+ const flag = flags[k];
452
+ if (flag === 0) {
453
+ continue;
454
+ }
455
+ const c = children[childIndex[k]];
456
+ const read = mainRead[k];
457
+ if ((flag & (GROWN | SHRUNK)) !== 0) {
458
+ const size = mainSize[k];
459
+ if ((size !== read || c.transition) &&
460
+ writeSize(c, isRow, read, size)) {
461
+ resized = true;
462
+ }
463
+ const own = ownSize[k];
464
+ if (c._flexBase !== own)
465
+ c._flexBase = own;
466
+ if (c._flexGrown !== size)
467
+ c._flexGrown = size;
468
+ }
469
+ else {
470
+ // B8: grown or shrunk by an earlier pass, not by this one: back to
471
+ // its own size.
472
+ const size = ownSize[k];
473
+ if ((size !== read || c.transition) &&
474
+ writeSize(c, isRow, read, size)) {
475
+ resized = true;
476
+ }
477
+ c._flexGrown = undefined;
478
+ }
196
479
  }
197
480
  }
481
+ if (resized) {
482
+ node._containsFlexGrow = true;
483
+ }
484
+ else if (node._containsFlexGrow) {
485
+ node._containsFlexGrow = null;
486
+ }
198
487
  let totalItemSize = 0;
199
488
  if (justify === 'center' ||
200
489
  justify === 'spaceBetween' ||
201
490
  justify === 'spaceEvenly' ||
202
491
  justify === 'spaceAround') {
203
- for (let idx = 0; idx < numProcessedChildren; idx++) {
204
- totalItemSize += childTotalMainSizes[idx];
492
+ for (let p = 0; p < n; p++) {
493
+ totalItemSize += totalMain[order[p]];
205
494
  }
206
495
  }
207
- const doCrossAlign = containerCrossSize
208
- ? (c, idx, crossCurrentPos = 0) => {
209
- const alignSelf = c.alignSelf || align;
210
- if (!alignSelf) {
211
- return;
212
- }
213
- if (alignSelf === 'flexStart') {
214
- c[crossProp] = crossCurrentPos + childMarginCrossStarts[idx];
215
- }
216
- else if (alignSelf === 'center') {
217
- c[crossProp] =
218
- crossCurrentPos +
219
- (containerCrossSize - childCrossSizes[idx]) / 2 +
220
- childMarginCrossStarts[idx];
221
- }
222
- else if (alignSelf === 'flexEnd') {
223
- c[crossProp] =
224
- crossCurrentPos +
225
- containerCrossSize -
226
- childCrossSizes[idx] -
227
- childMarginCrossEnds[idx];
228
- }
229
- }
230
- : (_c, _idx, _crossCurrentPos = 0) => {
231
- /* no-op */
232
- };
496
+ // Decided before the row's height is fitted below (as before 1.7).
497
+ const crossAlign = containerCrossSize > 0;
233
498
  if (isRow && node._calcHeight && !node.flexCrossBoundary) {
499
+ // An item alignCross places sits inside the cross padding, so the row
500
+ // fits it with that padding; one it leaves where it is, without.
501
+ const crossPadding = paddingCrossStart + paddingCrossEnd;
234
502
  let maxHeight = 0;
235
- for (let idx = 0; idx < numProcessedChildren; idx++) {
236
- if (childCrossSizes[idx] > maxHeight)
237
- maxHeight = childCrossSizes[idx];
503
+ for (let p = 0; p < n; p++) {
504
+ const k = order[p];
505
+ let size = crossSize[k];
506
+ if (size > 0 &&
507
+ crossAlign &&
508
+ (align || children[childIndex[k]].alignSelf)) {
509
+ size += crossPadding;
510
+ }
511
+ if (size > maxHeight)
512
+ maxHeight = size;
238
513
  }
239
- const newHeight = maxHeight || node.height;
240
- if (newHeight !== node.height) {
514
+ const newHeight = maxHeight || nodeHeight;
515
+ if ((newHeight !== nodeHeight || node.transition) &&
516
+ writeSize(node, false, nodeHeight, newHeight)) {
241
517
  containerUpdated = true;
242
- node.height = containerCrossSize = newHeight;
518
+ containerCrossSize = newHeight;
243
519
  }
244
520
  }
521
+ // B7: center and flexEnd align inside the padded box, not the full size.
522
+ const crossBox = containerCrossSize - paddingCrossStart - paddingCrossEnd;
245
523
  let currentPos = paddingStart;
246
524
  if (justify === 'flexStart') {
247
- if (node.flexWrap === 'wrap') {
248
- const childCrossSizeVar = numProcessedChildren > 0 ? childCrossSizes[0] : containerCrossSize;
249
- let crossCurrentPos = isWrapReverse
250
- ? containerCrossSize - paddingCrossEnd - childCrossSizeVar
251
- : paddingCrossStart;
525
+ if (wrapping) {
526
+ const lineSize = crossSize[order[0]];
252
527
  const crossGap = isRow ? (node.columnGap ?? gap) : (node.rowGap ?? gap);
253
- for (let idx = 0; idx < numProcessedChildren; idx++) {
254
- const c = children[processableChildrenIndices[idx]];
255
- if (currentPos + childTotalMainSizes[idx] > containerSize &&
528
+ let line = paddingCrossStart;
529
+ for (let p = 0; p < n; p++) {
530
+ const k = order[p];
531
+ if (currentPos + totalMain[k] > containerSize &&
256
532
  currentPos > paddingStart) {
257
533
  currentPos = paddingStart;
258
- crossCurrentPos += isWrapReverse
259
- ? -(childCrossSizeVar + crossGap)
260
- : childCrossSizeVar + crossGap;
534
+ line += lineSize + crossGap;
535
+ }
536
+ setPos(children[childIndex[k]], isRow, currentPos + marginStart[k]);
537
+ currentPos += totalMain[k] + gap;
538
+ lineStart[k] = line;
539
+ }
540
+ // Each item is aligned from its line's start; center and flexEnd
541
+ // measure against the container's cross size (B7: less its padding),
542
+ // as before 1.7. B9: a container with no cross size places its items
543
+ // too, against the line. B10: wrap-reverse mirrors the lines, so the
544
+ // first one is at the end.
545
+ const alignSize = crossAlign ? crossBox : lineSize;
546
+ for (let p = 0; p < n; p++) {
547
+ const k = order[p];
548
+ alignCross(children[childIndex[k]], k, isRow, align, isWrapReverse
549
+ ? line + paddingCrossStart - lineStart[k]
550
+ : lineStart[k], alignSize);
551
+ }
552
+ const finalCrossSize = line + lineSize + paddingCrossEnd;
553
+ if (isRow) {
554
+ const height = node.height;
555
+ if ((height !== finalCrossSize || node.transition) &&
556
+ writeSize(node, false, height, finalCrossSize)) {
557
+ node.preFlexheight = height;
558
+ containerUpdated = true;
261
559
  }
262
- c[prop] = currentPos + childMarginStarts[idx];
263
- currentPos += childTotalMainSizes[idx] + gap;
264
- doCrossAlign(c, idx, crossCurrentPos);
265
560
  }
266
- const finalCrossSize = isWrapReverse
267
- ? containerCrossSize - crossCurrentPos + paddingCrossStart
268
- : crossCurrentPos + childCrossSizeVar + paddingCrossEnd;
269
- if (node[crossDimension] !== finalCrossSize) {
270
- node[`preFlex${crossDimension}`] = node[crossDimension];
271
- node[crossDimension] = finalCrossSize;
272
- containerUpdated = true;
561
+ else {
562
+ const width = node.width;
563
+ if ((width !== finalCrossSize || node.transition) &&
564
+ writeSize(node, true, width, finalCrossSize)) {
565
+ node.preFlexwidth = width;
566
+ containerUpdated = true;
567
+ }
273
568
  }
274
569
  }
275
570
  else {
276
- for (let idx = 0; idx < numProcessedChildren; idx++) {
277
- const c = children[processableChildrenIndices[idx]];
278
- c[prop] = currentPos + childMarginStarts[idx];
279
- currentPos += childTotalMainSizes[idx] + gap;
280
- doCrossAlign(c, idx, paddingCrossStart);
571
+ for (let p = 0; p < n; p++) {
572
+ const k = order[p];
573
+ const c = children[childIndex[k]];
574
+ setPos(c, isRow, currentPos + marginStart[k]);
575
+ currentPos += totalMain[k] + gap;
576
+ if (crossAlign) {
577
+ alignCross(c, k, isRow, align, paddingCrossStart, crossBox);
578
+ }
281
579
  }
282
580
  }
283
581
  // Update container size
284
- if (node.flexBoundary !== 'fixed' && node.flexWrap !== 'wrap') {
582
+ if (node.flexBoundary !== 'fixed' && !wrapping) {
285
583
  let calculatedSize = currentPos - gap + paddingEnd;
286
- const minSize = node[minDimension] || 0;
584
+ const minSize = (isRow ? node.minWidth : node.minHeight) || 0;
287
585
  if (calculatedSize < minSize) {
288
586
  calculatedSize = minSize;
289
587
  }
290
- if (calculatedSize !== (node[dimension] || 0)) {
291
- node[`preFlex${dimension}`] = containerSize;
292
- node[dimension] = calculatedSize;
588
+ const current = nodeMain || 0;
589
+ if ((calculatedSize !== current || node.transition) &&
590
+ writeSize(node, isRow, current, calculatedSize)) {
591
+ if (isRow) {
592
+ node.preFlexwidth = containerSize;
593
+ }
594
+ else {
595
+ node.preFlexheight = containerSize;
596
+ }
293
597
  return true;
294
598
  }
295
599
  }
296
600
  }
297
601
  else if (justify === 'flexEnd') {
298
602
  currentPos = containerSize - paddingEnd;
299
- for (let idx = numProcessedChildren - 1; idx >= 0; idx--) {
300
- const c = children[processableChildrenIndices[idx]];
301
- c[prop] = currentPos - childMainSizes[idx] - childMarginEnds[idx];
302
- currentPos -= childTotalMainSizes[idx] + gap;
303
- doCrossAlign(c, idx, paddingCrossStart);
603
+ for (let p = n - 1; p >= 0; p--) {
604
+ const k = order[p];
605
+ const c = children[childIndex[k]];
606
+ setPos(c, isRow, currentPos - mainSize[k] - marginEnd[k]);
607
+ currentPos -= totalMain[k] + gap;
608
+ if (crossAlign) {
609
+ alignCross(c, k, isRow, align, paddingCrossStart, crossBox);
610
+ }
304
611
  }
305
612
  }
306
613
  else if (justify === 'center') {
614
+ // B6: centred inside the padded box.
307
615
  currentPos =
308
- (containerSize - (totalItemSize + gap * (numProcessedChildren - 1))) / 2 +
616
+ (containerSize -
617
+ paddingStart -
618
+ paddingEnd -
619
+ (totalItemSize + gap * (n - 1))) /
620
+ 2 +
309
621
  paddingStart;
310
- for (let idx = 0; idx < numProcessedChildren; idx++) {
311
- const c = children[processableChildrenIndices[idx]];
312
- c[prop] = currentPos + childMarginStarts[idx];
313
- currentPos += childTotalMainSizes[idx] + gap;
314
- doCrossAlign(c, idx, paddingCrossStart);
622
+ for (let p = 0; p < n; p++) {
623
+ const k = order[p];
624
+ const c = children[childIndex[k]];
625
+ setPos(c, isRow, currentPos + marginStart[k]);
626
+ currentPos += totalMain[k] + gap;
627
+ if (crossAlign) {
628
+ alignCross(c, k, isRow, align, paddingCrossStart, crossBox);
629
+ }
315
630
  }
316
631
  }
317
632
  else if (justify === 'spaceBetween') {
318
- const spaceBetween = numProcessedChildren > 1
319
- ? (containerSize - totalItemSize - nodePaddingTotal) /
320
- (numProcessedChildren - 1)
321
- : 0;
633
+ const spaceBetween = n > 1 ? (containerSize - totalItemSize - nodePaddingTotal) / (n - 1) : 0;
322
634
  currentPos = paddingStart;
323
- for (let idx = 0; idx < numProcessedChildren; idx++) {
324
- const c = children[processableChildrenIndices[idx]];
325
- c[prop] = currentPos + childMarginStarts[idx];
326
- currentPos += childTotalMainSizes[idx] + spaceBetween;
327
- doCrossAlign(c, idx, paddingCrossStart);
635
+ for (let p = 0; p < n; p++) {
636
+ const k = order[p];
637
+ const c = children[childIndex[k]];
638
+ setPos(c, isRow, currentPos + marginStart[k]);
639
+ currentPos += totalMain[k] + spaceBetween;
640
+ if (crossAlign) {
641
+ alignCross(c, k, isRow, align, paddingCrossStart, crossBox);
642
+ }
328
643
  }
329
644
  }
330
645
  else if (justify === 'spaceAround') {
331
- const spaceAround = numProcessedChildren > 0
332
- ? (containerSize - totalItemSize - nodePaddingTotal) /
333
- numProcessedChildren
334
- : 0;
646
+ const spaceAround = (containerSize - totalItemSize - nodePaddingTotal) / n;
335
647
  currentPos = paddingStart + spaceAround / 2;
336
- for (let idx = 0; idx < numProcessedChildren; idx++) {
337
- const c = children[processableChildrenIndices[idx]];
338
- c[prop] = currentPos + childMarginStarts[idx];
339
- currentPos += childTotalMainSizes[idx] + spaceAround;
340
- doCrossAlign(c, idx, paddingCrossStart);
648
+ for (let p = 0; p < n; p++) {
649
+ const k = order[p];
650
+ const c = children[childIndex[k]];
651
+ setPos(c, isRow, currentPos + marginStart[k]);
652
+ currentPos += totalMain[k] + spaceAround;
653
+ if (crossAlign) {
654
+ alignCross(c, k, isRow, align, paddingCrossStart, crossBox);
655
+ }
341
656
  }
342
657
  }
343
658
  else if (justify === 'spaceEvenly') {
344
- const spaceEvenly = (containerSize - totalItemSize - nodePaddingTotal) /
345
- (numProcessedChildren + 1);
659
+ const spaceEvenly = (containerSize - totalItemSize - nodePaddingTotal) / (n + 1);
346
660
  currentPos = spaceEvenly + paddingStart;
347
- for (let idx = 0; idx < numProcessedChildren; idx++) {
348
- const c = children[processableChildrenIndices[idx]];
349
- c[prop] = currentPos + childMarginStarts[idx];
350
- currentPos += childTotalMainSizes[idx] + spaceEvenly;
351
- doCrossAlign(c, idx, paddingCrossStart);
661
+ for (let p = 0; p < n; p++) {
662
+ const k = order[p];
663
+ const c = children[childIndex[k]];
664
+ setPos(c, isRow, currentPos + marginStart[k]);
665
+ currentPos += totalMain[k] + spaceEvenly;
666
+ if (crossAlign) {
667
+ alignCross(c, k, isRow, align, paddingCrossStart, crossBox);
668
+ }
352
669
  }
353
670
  }
354
671
  return containerUpdated;