Skip to content

Commit b796469

Browse files
fix flexible intrinsic space distribution and optimise item containing block calculation
1 parent 28573da commit b796469

File tree

8 files changed

+416
-32
lines changed

8 files changed

+416
-32
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<div
2+
id="grid_distribute_space_flex_auto"
3+
style="display: grid; grid-template-columns: auto 1fr auto; width: 300px">
4+
<div
5+
style="
6+
grid-column-start: 1;
7+
grid-column-end: 4;
8+
width: 200px;
9+
height: 50px;
10+
"></div>
11+
<div style="width: 50px; height: 30px"></div>
12+
<div style="height: 30px"></div>
13+
<div style="width: 50px; height: 30px"></div>
14+
</div>
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @generated SignedSource<<b6fd86b68e2b4b22e8479246a3c5a6ac>>
8+
* generated by gentest/gentest-driver.ts from gentest/fixtures/grid_distribute_space_flex_auto.html
9+
*/
10+
11+
package com.facebook.yoga;
12+
13+
import static org.junit.Assert.assertEquals;
14+
15+
import org.junit.Ignore;
16+
import org.junit.Test;
17+
import org.junit.runner.RunWith;
18+
import org.junit.runners.Parameterized;
19+
import com.facebook.yoga.utils.TestUtils;
20+
21+
@RunWith(Parameterized.class)
22+
public class grid_distribute_space_flex_auto {
23+
@Parameterized.Parameters(name = "{0}")
24+
public static Iterable<TestParametrization.NodeFactory> nodeFactories() {
25+
return TestParametrization.nodeFactories();
26+
}
27+
28+
@Parameterized.Parameter public TestParametrization.NodeFactory mNodeFactory;
29+
30+
@Test
31+
public void test_grid_distribute_space_flex_auto() {
32+
YogaConfig config = YogaConfigFactory.create();
33+
34+
final YogaNode root = createNode(config);
35+
root.setPositionType(YogaPositionType.ABSOLUTE);
36+
root.setWidth(300f);
37+
root.setDisplay(YogaDisplay.GRID);
38+
YogaGridTrackList rootGridTemplateColumns = new YogaGridTrackList();
39+
rootGridTemplateColumns.addTrack(YogaGridTrackValue.auto());
40+
rootGridTemplateColumns.addTrack(YogaGridTrackValue.auto());
41+
rootGridTemplateColumns.addTrack(YogaGridTrackValue.auto());
42+
rootGridTemplateColumns.addTrack(YogaGridTrackValue.auto());
43+
rootGridTemplateColumns.addTrack(YogaGridTrackValue.auto());
44+
rootGridTemplateColumns.addTrack(YogaGridTrackValue.auto());
45+
rootGridTemplateColumns.addTrack(YogaGridTrackValue.auto());
46+
rootGridTemplateColumns.addTrack(YogaGridTrackValue.auto());
47+
rootGridTemplateColumns.addTrack(YogaGridTrackValue.auto());
48+
rootGridTemplateColumns.addTrack(YogaGridTrackValue.auto());
49+
rootGridTemplateColumns.addTrack(YogaGridTrackValue.auto());
50+
rootGridTemplateColumns.addTrack(YogaGridTrackValue.auto());
51+
rootGridTemplateColumns.addTrack(YogaGridTrackValue.auto());
52+
root.setGridTemplateColumns(rootGridTemplateColumns);
53+
54+
final YogaNode root_child0 = createNode(config);
55+
root_child0.setWidth(200f);
56+
root_child0.setHeight(50f);
57+
root_child0.setGridColumnStart(1);
58+
root_child0.setGridColumnEnd(4);
59+
root.addChildAt(root_child0, 0);
60+
61+
final YogaNode root_child1 = createNode(config);
62+
root_child1.setWidth(50f);
63+
root_child1.setHeight(30f);
64+
root.addChildAt(root_child1, 1);
65+
66+
final YogaNode root_child2 = createNode(config);
67+
root_child2.setHeight(30f);
68+
root.addChildAt(root_child2, 2);
69+
70+
final YogaNode root_child3 = createNode(config);
71+
root_child3.setWidth(50f);
72+
root_child3.setHeight(30f);
73+
root.addChildAt(root_child3, 3);
74+
root.setDirection(YogaDirection.LTR);
75+
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);
76+
77+
assertEquals(0f, root.getLayoutX(), 0.0f);
78+
assertEquals(0f, root.getLayoutY(), 0.0f);
79+
assertEquals(300f, root.getLayoutWidth(), 0.0f);
80+
assertEquals(80f, root.getLayoutHeight(), 0.0f);
81+
82+
assertEquals(0f, root_child0.getLayoutX(), 0.0f);
83+
assertEquals(0f, root_child0.getLayoutY(), 0.0f);
84+
assertEquals(200f, root_child0.getLayoutWidth(), 0.0f);
85+
assertEquals(50f, root_child0.getLayoutHeight(), 0.0f);
86+
87+
assertEquals(0f, root_child1.getLayoutX(), 0.0f);
88+
assertEquals(50f, root_child1.getLayoutY(), 0.0f);
89+
assertEquals(50f, root_child1.getLayoutWidth(), 0.0f);
90+
assertEquals(30f, root_child1.getLayoutHeight(), 0.0f);
91+
92+
assertEquals(50f, root_child2.getLayoutX(), 0.0f);
93+
assertEquals(50f, root_child2.getLayoutY(), 0.0f);
94+
assertEquals(200f, root_child2.getLayoutWidth(), 0.0f);
95+
assertEquals(30f, root_child2.getLayoutHeight(), 0.0f);
96+
97+
assertEquals(250f, root_child3.getLayoutX(), 0.0f);
98+
assertEquals(50f, root_child3.getLayoutY(), 0.0f);
99+
assertEquals(50f, root_child3.getLayoutWidth(), 0.0f);
100+
assertEquals(30f, root_child3.getLayoutHeight(), 0.0f);
101+
102+
root.setDirection(YogaDirection.RTL);
103+
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);
104+
105+
assertEquals(0f, root.getLayoutX(), 0.0f);
106+
assertEquals(0f, root.getLayoutY(), 0.0f);
107+
assertEquals(300f, root.getLayoutWidth(), 0.0f);
108+
assertEquals(80f, root.getLayoutHeight(), 0.0f);
109+
110+
assertEquals(100f, root_child0.getLayoutX(), 0.0f);
111+
assertEquals(0f, root_child0.getLayoutY(), 0.0f);
112+
assertEquals(200f, root_child0.getLayoutWidth(), 0.0f);
113+
assertEquals(50f, root_child0.getLayoutHeight(), 0.0f);
114+
115+
assertEquals(250f, root_child1.getLayoutX(), 0.0f);
116+
assertEquals(50f, root_child1.getLayoutY(), 0.0f);
117+
assertEquals(50f, root_child1.getLayoutWidth(), 0.0f);
118+
assertEquals(30f, root_child1.getLayoutHeight(), 0.0f);
119+
120+
assertEquals(50f, root_child2.getLayoutX(), 0.0f);
121+
assertEquals(50f, root_child2.getLayoutY(), 0.0f);
122+
assertEquals(200f, root_child2.getLayoutWidth(), 0.0f);
123+
assertEquals(30f, root_child2.getLayoutHeight(), 0.0f);
124+
125+
assertEquals(0f, root_child3.getLayoutX(), 0.0f);
126+
assertEquals(50f, root_child3.getLayoutY(), 0.0f);
127+
assertEquals(50f, root_child3.getLayoutWidth(), 0.0f);
128+
assertEquals(30f, root_child3.getLayoutHeight(), 0.0f);
129+
}
130+
131+
private YogaNode createNode(YogaConfig config) {
132+
return mNodeFactory.create(config);
133+
}
134+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @generated SignedSource<<ab955ae897fcd61bea156be6d320314d>>
8+
* generated by gentest/gentest-driver.ts from gentest/fixtures/grid_distribute_space_flex_auto.html
9+
*/
10+
11+
import { instrinsicSizeMeasureFunc } from '../tools/utils.ts'
12+
import Yoga from 'yoga-layout';
13+
import {
14+
Align,
15+
BoxSizing,
16+
Direction,
17+
Display,
18+
Edge,
19+
Errata,
20+
ExperimentalFeature,
21+
FlexDirection,
22+
GridTrackType,
23+
Gutter,
24+
Justify,
25+
MeasureMode,
26+
Overflow,
27+
PositionType,
28+
Unit,
29+
Wrap,
30+
} from 'yoga-layout';
31+
32+
test('grid_distribute_space_flex_auto', () => {
33+
const config = Yoga.Config.create();
34+
let root;
35+
36+
try {
37+
root = Yoga.Node.create(config);
38+
root.setPositionType(PositionType.Absolute);
39+
root.setWidth(300);
40+
root.setDisplay(Display.Grid);
41+
const rootGridTemplateColumns = [];
42+
rootGridTemplateColumns.push({type: GridTrackType.Auto});
43+
rootGridTemplateColumns.push({type: GridTrackType.Fr, value: 1});
44+
rootGridTemplateColumns.push({type: GridTrackType.Auto});
45+
root.setGridTemplateColumns(rootGridTemplateColumns);
46+
47+
const root_child0 = Yoga.Node.create(config);
48+
root_child0.setWidth(200);
49+
root_child0.setHeight(50);
50+
root_child0.setGridColumnStart(1);
51+
root_child0.setGridColumnEnd(4);
52+
root.insertChild(root_child0, 0);
53+
54+
const root_child1 = Yoga.Node.create(config);
55+
root_child1.setWidth(50);
56+
root_child1.setHeight(30);
57+
root.insertChild(root_child1, 1);
58+
59+
const root_child2 = Yoga.Node.create(config);
60+
root_child2.setHeight(30);
61+
root.insertChild(root_child2, 2);
62+
63+
const root_child3 = Yoga.Node.create(config);
64+
root_child3.setWidth(50);
65+
root_child3.setHeight(30);
66+
root.insertChild(root_child3, 3);
67+
root.calculateLayout(undefined, undefined, Direction.LTR);
68+
69+
expect(root.getComputedLeft()).toBe(0);
70+
expect(root.getComputedTop()).toBe(0);
71+
expect(root.getComputedWidth()).toBe(300);
72+
expect(root.getComputedHeight()).toBe(80);
73+
74+
expect(root_child0.getComputedLeft()).toBe(0);
75+
expect(root_child0.getComputedTop()).toBe(0);
76+
expect(root_child0.getComputedWidth()).toBe(200);
77+
expect(root_child0.getComputedHeight()).toBe(50);
78+
79+
expect(root_child1.getComputedLeft()).toBe(0);
80+
expect(root_child1.getComputedTop()).toBe(50);
81+
expect(root_child1.getComputedWidth()).toBe(50);
82+
expect(root_child1.getComputedHeight()).toBe(30);
83+
84+
expect(root_child2.getComputedLeft()).toBe(50);
85+
expect(root_child2.getComputedTop()).toBe(50);
86+
expect(root_child2.getComputedWidth()).toBe(200);
87+
expect(root_child2.getComputedHeight()).toBe(30);
88+
89+
expect(root_child3.getComputedLeft()).toBe(250);
90+
expect(root_child3.getComputedTop()).toBe(50);
91+
expect(root_child3.getComputedWidth()).toBe(50);
92+
expect(root_child3.getComputedHeight()).toBe(30);
93+
94+
root.calculateLayout(undefined, undefined, Direction.RTL);
95+
96+
expect(root.getComputedLeft()).toBe(0);
97+
expect(root.getComputedTop()).toBe(0);
98+
expect(root.getComputedWidth()).toBe(300);
99+
expect(root.getComputedHeight()).toBe(80);
100+
101+
expect(root_child0.getComputedLeft()).toBe(100);
102+
expect(root_child0.getComputedTop()).toBe(0);
103+
expect(root_child0.getComputedWidth()).toBe(200);
104+
expect(root_child0.getComputedHeight()).toBe(50);
105+
106+
expect(root_child1.getComputedLeft()).toBe(250);
107+
expect(root_child1.getComputedTop()).toBe(50);
108+
expect(root_child1.getComputedWidth()).toBe(50);
109+
expect(root_child1.getComputedHeight()).toBe(30);
110+
111+
expect(root_child2.getComputedLeft()).toBe(50);
112+
expect(root_child2.getComputedTop()).toBe(50);
113+
expect(root_child2.getComputedWidth()).toBe(200);
114+
expect(root_child2.getComputedHeight()).toBe(30);
115+
116+
expect(root_child3.getComputedLeft()).toBe(0);
117+
expect(root_child3.getComputedTop()).toBe(50);
118+
expect(root_child3.getComputedWidth()).toBe(50);
119+
expect(root_child3.getComputedHeight()).toBe(30);
120+
} finally {
121+
if (typeof root !== 'undefined') {
122+
root.freeRecursive();
123+
}
124+
125+
config.free();
126+
}
127+
});
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* clang-format off
8+
* @generated SignedSource<<37d652da10b673403933fa09616a5e2b>>
9+
* generated by gentest/gentest-driver.ts from gentest/fixtures/grid_distribute_space_flex_auto.html
10+
*/
11+
12+
#include <gtest/gtest.h>
13+
#include <yoga/Yoga.h>
14+
#include "../util/TestUtil.h"
15+
16+
TEST(YogaTest, grid_distribute_space_flex_auto) {
17+
YGConfigRef config = YGConfigNew();
18+
19+
YGNodeRef root = YGNodeNewWithConfig(config);
20+
YGNodeStyleSetPositionType(root, YGPositionTypeAbsolute);
21+
YGNodeStyleSetWidth(root, 300);
22+
YGNodeStyleSetDisplay(root, YGDisplayGrid);
23+
auto root_gridTemplateColumns = YGGridTrackListCreate();
24+
YGGridTrackListAddTrack(root_gridTemplateColumns, YGAuto());
25+
YGGridTrackListAddTrack(root_gridTemplateColumns, YGFr(1));
26+
YGGridTrackListAddTrack(root_gridTemplateColumns, YGAuto());
27+
YGNodeStyleSetGridTemplateColumns(root, root_gridTemplateColumns);
28+
YGGridTrackListFree(root_gridTemplateColumns);
29+
30+
YGNodeRef root_child0 = YGNodeNewWithConfig(config);
31+
YGNodeStyleSetWidth(root_child0, 200);
32+
YGNodeStyleSetHeight(root_child0, 50);
33+
YGNodeStyleSetGridColumnStart(root_child0, 1);
34+
YGNodeStyleSetGridColumnEnd(root_child0, 4);
35+
YGNodeInsertChild(root, root_child0, 0);
36+
37+
YGNodeRef root_child1 = YGNodeNewWithConfig(config);
38+
YGNodeStyleSetWidth(root_child1, 50);
39+
YGNodeStyleSetHeight(root_child1, 30);
40+
YGNodeInsertChild(root, root_child1, 1);
41+
42+
YGNodeRef root_child2 = YGNodeNewWithConfig(config);
43+
YGNodeStyleSetHeight(root_child2, 30);
44+
YGNodeInsertChild(root, root_child2, 2);
45+
46+
YGNodeRef root_child3 = YGNodeNewWithConfig(config);
47+
YGNodeStyleSetWidth(root_child3, 50);
48+
YGNodeStyleSetHeight(root_child3, 30);
49+
YGNodeInsertChild(root, root_child3, 3);
50+
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
51+
52+
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root));
53+
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root));
54+
ASSERT_FLOAT_EQ(300, YGNodeLayoutGetWidth(root));
55+
ASSERT_FLOAT_EQ(80, YGNodeLayoutGetHeight(root));
56+
57+
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0));
58+
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child0));
59+
ASSERT_FLOAT_EQ(200, YGNodeLayoutGetWidth(root_child0));
60+
ASSERT_FLOAT_EQ(50, YGNodeLayoutGetHeight(root_child0));
61+
62+
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child1));
63+
ASSERT_FLOAT_EQ(50, YGNodeLayoutGetTop(root_child1));
64+
ASSERT_FLOAT_EQ(50, YGNodeLayoutGetWidth(root_child1));
65+
ASSERT_FLOAT_EQ(30, YGNodeLayoutGetHeight(root_child1));
66+
67+
ASSERT_FLOAT_EQ(50, YGNodeLayoutGetLeft(root_child2));
68+
ASSERT_FLOAT_EQ(50, YGNodeLayoutGetTop(root_child2));
69+
ASSERT_FLOAT_EQ(200, YGNodeLayoutGetWidth(root_child2));
70+
ASSERT_FLOAT_EQ(30, YGNodeLayoutGetHeight(root_child2));
71+
72+
ASSERT_FLOAT_EQ(250, YGNodeLayoutGetLeft(root_child3));
73+
ASSERT_FLOAT_EQ(50, YGNodeLayoutGetTop(root_child3));
74+
ASSERT_FLOAT_EQ(50, YGNodeLayoutGetWidth(root_child3));
75+
ASSERT_FLOAT_EQ(30, YGNodeLayoutGetHeight(root_child3));
76+
77+
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionRTL);
78+
79+
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root));
80+
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root));
81+
ASSERT_FLOAT_EQ(300, YGNodeLayoutGetWidth(root));
82+
ASSERT_FLOAT_EQ(80, YGNodeLayoutGetHeight(root));
83+
84+
ASSERT_FLOAT_EQ(100, YGNodeLayoutGetLeft(root_child0));
85+
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child0));
86+
ASSERT_FLOAT_EQ(200, YGNodeLayoutGetWidth(root_child0));
87+
ASSERT_FLOAT_EQ(50, YGNodeLayoutGetHeight(root_child0));
88+
89+
ASSERT_FLOAT_EQ(250, YGNodeLayoutGetLeft(root_child1));
90+
ASSERT_FLOAT_EQ(50, YGNodeLayoutGetTop(root_child1));
91+
ASSERT_FLOAT_EQ(50, YGNodeLayoutGetWidth(root_child1));
92+
ASSERT_FLOAT_EQ(30, YGNodeLayoutGetHeight(root_child1));
93+
94+
ASSERT_FLOAT_EQ(50, YGNodeLayoutGetLeft(root_child2));
95+
ASSERT_FLOAT_EQ(50, YGNodeLayoutGetTop(root_child2));
96+
ASSERT_FLOAT_EQ(200, YGNodeLayoutGetWidth(root_child2));
97+
ASSERT_FLOAT_EQ(30, YGNodeLayoutGetHeight(root_child2));
98+
99+
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child3));
100+
ASSERT_FLOAT_EQ(50, YGNodeLayoutGetTop(root_child3));
101+
ASSERT_FLOAT_EQ(50, YGNodeLayoutGetWidth(root_child3));
102+
ASSERT_FLOAT_EQ(30, YGNodeLayoutGetHeight(root_child3));
103+
104+
YGNodeFreeRecursive(root);
105+
106+
YGConfigFree(config);
107+
}

0 commit comments

Comments
 (0)