Skip to content

Commit b85c1b9

Browse files
fix: auto margin should absorb only positive space
1 parent b796469 commit b85c1b9

File tree

10 files changed

+498
-21
lines changed

10 files changed

+498
-21
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<div
2+
id="grid_auto_margin_overflow_block"
3+
style="display: grid; grid-template-rows: 100px; width: 100px; height: 100px">
4+
<div
5+
style="
6+
width: 50px;
7+
height: 200px;
8+
margin-top: auto;
9+
margin-bottom: auto;
10+
"></div>
11+
</div>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<div
2+
id="grid_auto_margin_overflow_inline"
3+
style="display: grid; grid-template-columns: 100px; width: 100px">
4+
<div
5+
style="
6+
width: 200px;
7+
height: 50px;
8+
margin-left: auto;
9+
margin-right: auto;
10+
"></div>
11+
</div>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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<<d255ed6d3817e05f22d492e0c95e9645>>
8+
* generated by gentest/gentest-driver.ts from gentest/fixtures/grid_auto_margin_overflow_block.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_auto_margin_overflow_block {
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_auto_margin_overflow_block() {
32+
YogaConfig config = YogaConfigFactory.create();
33+
34+
final YogaNode root = createNode(config);
35+
root.setPositionType(YogaPositionType.ABSOLUTE);
36+
root.setWidth(100f);
37+
root.setHeight(100f);
38+
root.setDisplay(YogaDisplay.GRID);
39+
YogaGridTrackList rootGridTemplateRows = new YogaGridTrackList();
40+
rootGridTemplateRows.addTrack(YogaGridTrackValue.auto());
41+
rootGridTemplateRows.addTrack(YogaGridTrackValue.auto());
42+
rootGridTemplateRows.addTrack(YogaGridTrackValue.auto());
43+
rootGridTemplateRows.addTrack(YogaGridTrackValue.auto());
44+
rootGridTemplateRows.addTrack(YogaGridTrackValue.auto());
45+
root.setGridTemplateRows(rootGridTemplateRows);
46+
47+
final YogaNode root_child0 = createNode(config);
48+
root_child0.setMarginAuto(YogaEdge.TOP);
49+
root_child0.setMarginAuto(YogaEdge.BOTTOM);
50+
root_child0.setWidth(50f);
51+
root_child0.setHeight(200f);
52+
root.addChildAt(root_child0, 0);
53+
root.setDirection(YogaDirection.LTR);
54+
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);
55+
56+
assertEquals(0f, root.getLayoutX(), 0.0f);
57+
assertEquals(0f, root.getLayoutY(), 0.0f);
58+
assertEquals(100f, root.getLayoutWidth(), 0.0f);
59+
assertEquals(100f, root.getLayoutHeight(), 0.0f);
60+
61+
assertEquals(0f, root_child0.getLayoutX(), 0.0f);
62+
assertEquals(0f, root_child0.getLayoutY(), 0.0f);
63+
assertEquals(50f, root_child0.getLayoutWidth(), 0.0f);
64+
assertEquals(200f, root_child0.getLayoutHeight(), 0.0f);
65+
66+
root.setDirection(YogaDirection.RTL);
67+
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);
68+
69+
assertEquals(0f, root.getLayoutX(), 0.0f);
70+
assertEquals(0f, root.getLayoutY(), 0.0f);
71+
assertEquals(100f, root.getLayoutWidth(), 0.0f);
72+
assertEquals(100f, root.getLayoutHeight(), 0.0f);
73+
74+
assertEquals(50f, root_child0.getLayoutX(), 0.0f);
75+
assertEquals(0f, root_child0.getLayoutY(), 0.0f);
76+
assertEquals(50f, root_child0.getLayoutWidth(), 0.0f);
77+
assertEquals(200f, root_child0.getLayoutHeight(), 0.0f);
78+
}
79+
80+
private YogaNode createNode(YogaConfig config) {
81+
return mNodeFactory.create(config);
82+
}
83+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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<<de7cd96816cf100e87f895b2116f7bc6>>
8+
* generated by gentest/gentest-driver.ts from gentest/fixtures/grid_auto_margin_overflow_inline.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_auto_margin_overflow_inline {
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_auto_margin_overflow_inline() {
32+
YogaConfig config = YogaConfigFactory.create();
33+
34+
final YogaNode root = createNode(config);
35+
root.setPositionType(YogaPositionType.ABSOLUTE);
36+
root.setWidth(100f);
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+
root.setGridTemplateColumns(rootGridTemplateColumns);
45+
46+
final YogaNode root_child0 = createNode(config);
47+
root_child0.setMarginAuto(YogaEdge.LEFT);
48+
root_child0.setMarginAuto(YogaEdge.RIGHT);
49+
root_child0.setWidth(200f);
50+
root_child0.setHeight(50f);
51+
root.addChildAt(root_child0, 0);
52+
root.setDirection(YogaDirection.LTR);
53+
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);
54+
55+
assertEquals(0f, root.getLayoutX(), 0.0f);
56+
assertEquals(0f, root.getLayoutY(), 0.0f);
57+
assertEquals(100f, root.getLayoutWidth(), 0.0f);
58+
assertEquals(50f, root.getLayoutHeight(), 0.0f);
59+
60+
assertEquals(0f, root_child0.getLayoutX(), 0.0f);
61+
assertEquals(0f, root_child0.getLayoutY(), 0.0f);
62+
assertEquals(200f, root_child0.getLayoutWidth(), 0.0f);
63+
assertEquals(50f, root_child0.getLayoutHeight(), 0.0f);
64+
65+
root.setDirection(YogaDirection.RTL);
66+
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);
67+
68+
assertEquals(0f, root.getLayoutX(), 0.0f);
69+
assertEquals(0f, root.getLayoutY(), 0.0f);
70+
assertEquals(100f, root.getLayoutWidth(), 0.0f);
71+
assertEquals(50f, root.getLayoutHeight(), 0.0f);
72+
73+
assertEquals(-100f, root_child0.getLayoutX(), 0.0f);
74+
assertEquals(0f, root_child0.getLayoutY(), 0.0f);
75+
assertEquals(200f, root_child0.getLayoutWidth(), 0.0f);
76+
assertEquals(50f, root_child0.getLayoutHeight(), 0.0f);
77+
}
78+
79+
private YogaNode createNode(YogaConfig config) {
80+
return mNodeFactory.create(config);
81+
}
82+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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<<9485428880cf366ae4749aa1a2b51296>>
8+
* generated by gentest/gentest-driver.ts from gentest/fixtures/grid_auto_margin_overflow_block.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_auto_margin_overflow_block', () => {
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(100);
40+
root.setHeight(100);
41+
root.setDisplay(Display.Grid);
42+
const rootGridTemplateRows = [];
43+
rootGridTemplateRows.push({type: GridTrackType.Points, value: 100});
44+
root.setGridTemplateRows(rootGridTemplateRows);
45+
46+
const root_child0 = Yoga.Node.create(config);
47+
root_child0.setMargin(Edge.Top, 'auto');
48+
root_child0.setMargin(Edge.Bottom, 'auto');
49+
root_child0.setWidth(50);
50+
root_child0.setHeight(200);
51+
root.insertChild(root_child0, 0);
52+
root.calculateLayout(undefined, undefined, Direction.LTR);
53+
54+
expect(root.getComputedLeft()).toBe(0);
55+
expect(root.getComputedTop()).toBe(0);
56+
expect(root.getComputedWidth()).toBe(100);
57+
expect(root.getComputedHeight()).toBe(100);
58+
59+
expect(root_child0.getComputedLeft()).toBe(0);
60+
expect(root_child0.getComputedTop()).toBe(0);
61+
expect(root_child0.getComputedWidth()).toBe(50);
62+
expect(root_child0.getComputedHeight()).toBe(200);
63+
64+
root.calculateLayout(undefined, undefined, Direction.RTL);
65+
66+
expect(root.getComputedLeft()).toBe(0);
67+
expect(root.getComputedTop()).toBe(0);
68+
expect(root.getComputedWidth()).toBe(100);
69+
expect(root.getComputedHeight()).toBe(100);
70+
71+
expect(root_child0.getComputedLeft()).toBe(50);
72+
expect(root_child0.getComputedTop()).toBe(0);
73+
expect(root_child0.getComputedWidth()).toBe(50);
74+
expect(root_child0.getComputedHeight()).toBe(200);
75+
} finally {
76+
if (typeof root !== 'undefined') {
77+
root.freeRecursive();
78+
}
79+
80+
config.free();
81+
}
82+
});
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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<<db6cfd6ddd1cdb8395a6a8d2c0c47815>>
8+
* generated by gentest/gentest-driver.ts from gentest/fixtures/grid_auto_margin_overflow_inline.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_auto_margin_overflow_inline', () => {
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(100);
40+
root.setDisplay(Display.Grid);
41+
const rootGridTemplateColumns = [];
42+
rootGridTemplateColumns.push({type: GridTrackType.Points, value: 100});
43+
root.setGridTemplateColumns(rootGridTemplateColumns);
44+
45+
const root_child0 = Yoga.Node.create(config);
46+
root_child0.setMargin(Edge.Left, 'auto');
47+
root_child0.setMargin(Edge.Right, 'auto');
48+
root_child0.setWidth(200);
49+
root_child0.setHeight(50);
50+
root.insertChild(root_child0, 0);
51+
root.calculateLayout(undefined, undefined, Direction.LTR);
52+
53+
expect(root.getComputedLeft()).toBe(0);
54+
expect(root.getComputedTop()).toBe(0);
55+
expect(root.getComputedWidth()).toBe(100);
56+
expect(root.getComputedHeight()).toBe(50);
57+
58+
expect(root_child0.getComputedLeft()).toBe(0);
59+
expect(root_child0.getComputedTop()).toBe(0);
60+
expect(root_child0.getComputedWidth()).toBe(200);
61+
expect(root_child0.getComputedHeight()).toBe(50);
62+
63+
root.calculateLayout(undefined, undefined, Direction.RTL);
64+
65+
expect(root.getComputedLeft()).toBe(0);
66+
expect(root.getComputedTop()).toBe(0);
67+
expect(root.getComputedWidth()).toBe(100);
68+
expect(root.getComputedHeight()).toBe(50);
69+
70+
expect(root_child0.getComputedLeft()).toBe(-100);
71+
expect(root_child0.getComputedTop()).toBe(0);
72+
expect(root_child0.getComputedWidth()).toBe(200);
73+
expect(root_child0.getComputedHeight()).toBe(50);
74+
} finally {
75+
if (typeof root !== 'undefined') {
76+
root.freeRecursive();
77+
}
78+
79+
config.free();
80+
}
81+
});

0 commit comments

Comments
 (0)