Skip to content

Commit

Permalink
Fix crash when you layout multiple absolute nodes in the same static …
Browse files Browse the repository at this point in the history
…subtree (#1686)

Summary:
X-link: facebook/react-native#45952

Pull Request resolved: #1686

https://en.wikipedia.org/wiki/Short-circuit_evaluation 🫠

Changelog: [Internal]

Reviewed By: NickGerleman

Differential Revision: D60997231

fbshipit-source-id: 11d70086eecfb5481c578477f288138370016a83
  • Loading branch information
joevilches authored and facebook-github-bot committed Aug 9, 2024
1 parent 5009f5c commit ae8ede9
Show file tree
Hide file tree
Showing 6 changed files with 450 additions and 7 deletions.
17 changes: 17 additions & 0 deletions gentest/fixtures/YGStaticPositionTest.html
Original file line number Diff line number Diff line change
Expand Up @@ -693,3 +693,20 @@
style="height: 50%; width: 50%; position: absolute; border-width: 3px 2px 1px 4px; padding: 7px 5px 4px 3px; margin: 11px 15px 1px 12px">
</div>
</div>

<div id="static_position_absolute_child_multiple">
<div style="width: 400px; height: 400px; padding: 100px; position: relative">
<div style="height:100px; width: 100px; position: static">
<div style="height: 50px; width: 10%; position: absolute">
</div>
</div>
<div style="height:100px; width: 100px; position: static">
<div style="height: 50px; width: 50%; position: absolute">
</div>
<div style="height: 50px; width: 50%; position: absolute">
</div>
</div>
<div style="height: 50px; width: 25px; position: absolute">
</div>
</div>
</div>
140 changes: 139 additions & 1 deletion java/tests/generated/com/facebook/yoga/YGStaticPositionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<cc85b1a4863eb810093395adeee3fa44>>
* @generated SignedSource<<997d12880828a3c2881898b769618b43>>
* generated by gentest/gentest-driver.ts from gentest/fixtures/YGStaticPositionTest.html
*/

Expand Down Expand Up @@ -5887,6 +5887,144 @@ public void test_static_position_static_root() {
assertEquals(100f, root_child0.getLayoutHeight(), 0.0f);
}

@Test
public void test_static_position_absolute_child_multiple() {
YogaConfig config = YogaConfigFactory.create();

final YogaNode root = createNode(config);
root.setPositionType(YogaPositionType.ABSOLUTE);

final YogaNode root_child0 = createNode(config);
root_child0.setPadding(YogaEdge.LEFT, 100);
root_child0.setPadding(YogaEdge.TOP, 100);
root_child0.setPadding(YogaEdge.RIGHT, 100);
root_child0.setPadding(YogaEdge.BOTTOM, 100);
root_child0.setWidth(400f);
root_child0.setHeight(400f);
root.addChildAt(root_child0, 0);

final YogaNode root_child0_child0 = createNode(config);
root_child0_child0.setPositionType(YogaPositionType.STATIC);
root_child0_child0.setWidth(100f);
root_child0_child0.setHeight(100f);
root_child0.addChildAt(root_child0_child0, 0);

final YogaNode root_child0_child0_child0 = createNode(config);
root_child0_child0_child0.setPositionType(YogaPositionType.ABSOLUTE);
root_child0_child0_child0.setWidthPercent(10f);
root_child0_child0_child0.setHeight(50f);
root_child0_child0.addChildAt(root_child0_child0_child0, 0);

final YogaNode root_child0_child1 = createNode(config);
root_child0_child1.setPositionType(YogaPositionType.STATIC);
root_child0_child1.setWidth(100f);
root_child0_child1.setHeight(100f);
root_child0.addChildAt(root_child0_child1, 1);

final YogaNode root_child0_child1_child0 = createNode(config);
root_child0_child1_child0.setPositionType(YogaPositionType.ABSOLUTE);
root_child0_child1_child0.setWidthPercent(50f);
root_child0_child1_child0.setHeight(50f);
root_child0_child1.addChildAt(root_child0_child1_child0, 0);

final YogaNode root_child0_child1_child1 = createNode(config);
root_child0_child1_child1.setPositionType(YogaPositionType.ABSOLUTE);
root_child0_child1_child1.setWidthPercent(50f);
root_child0_child1_child1.setHeight(50f);
root_child0_child1.addChildAt(root_child0_child1_child1, 1);

final YogaNode root_child0_child2 = createNode(config);
root_child0_child2.setPositionType(YogaPositionType.ABSOLUTE);
root_child0_child2.setWidth(25f);
root_child0_child2.setHeight(50f);
root_child0.addChildAt(root_child0_child2, 2);
root.setDirection(YogaDirection.LTR);
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);

assertEquals(0f, root.getLayoutX(), 0.0f);
assertEquals(0f, root.getLayoutY(), 0.0f);
assertEquals(400f, root.getLayoutWidth(), 0.0f);
assertEquals(400f, root.getLayoutHeight(), 0.0f);

assertEquals(0f, root_child0.getLayoutX(), 0.0f);
assertEquals(0f, root_child0.getLayoutY(), 0.0f);
assertEquals(400f, root_child0.getLayoutWidth(), 0.0f);
assertEquals(400f, root_child0.getLayoutHeight(), 0.0f);

assertEquals(100f, root_child0_child0.getLayoutX(), 0.0f);
assertEquals(100f, root_child0_child0.getLayoutY(), 0.0f);
assertEquals(100f, root_child0_child0.getLayoutWidth(), 0.0f);
assertEquals(100f, root_child0_child0.getLayoutHeight(), 0.0f);

assertEquals(0f, root_child0_child0_child0.getLayoutX(), 0.0f);
assertEquals(0f, root_child0_child0_child0.getLayoutY(), 0.0f);
assertEquals(40f, root_child0_child0_child0.getLayoutWidth(), 0.0f);
assertEquals(50f, root_child0_child0_child0.getLayoutHeight(), 0.0f);

assertEquals(100f, root_child0_child1.getLayoutX(), 0.0f);
assertEquals(200f, root_child0_child1.getLayoutY(), 0.0f);
assertEquals(100f, root_child0_child1.getLayoutWidth(), 0.0f);
assertEquals(100f, root_child0_child1.getLayoutHeight(), 0.0f);

assertEquals(0f, root_child0_child1_child0.getLayoutX(), 0.0f);
assertEquals(0f, root_child0_child1_child0.getLayoutY(), 0.0f);
assertEquals(200f, root_child0_child1_child0.getLayoutWidth(), 0.0f);
assertEquals(50f, root_child0_child1_child0.getLayoutHeight(), 0.0f);

assertEquals(0f, root_child0_child1_child1.getLayoutX(), 0.0f);
assertEquals(0f, root_child0_child1_child1.getLayoutY(), 0.0f);
assertEquals(200f, root_child0_child1_child1.getLayoutWidth(), 0.0f);
assertEquals(50f, root_child0_child1_child1.getLayoutHeight(), 0.0f);

assertEquals(100f, root_child0_child2.getLayoutX(), 0.0f);
assertEquals(100f, root_child0_child2.getLayoutY(), 0.0f);
assertEquals(25f, root_child0_child2.getLayoutWidth(), 0.0f);
assertEquals(50f, root_child0_child2.getLayoutHeight(), 0.0f);

root.setDirection(YogaDirection.RTL);
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);

assertEquals(0f, root.getLayoutX(), 0.0f);
assertEquals(0f, root.getLayoutY(), 0.0f);
assertEquals(400f, root.getLayoutWidth(), 0.0f);
assertEquals(400f, root.getLayoutHeight(), 0.0f);

assertEquals(0f, root_child0.getLayoutX(), 0.0f);
assertEquals(0f, root_child0.getLayoutY(), 0.0f);
assertEquals(400f, root_child0.getLayoutWidth(), 0.0f);
assertEquals(400f, root_child0.getLayoutHeight(), 0.0f);

assertEquals(200f, root_child0_child0.getLayoutX(), 0.0f);
assertEquals(100f, root_child0_child0.getLayoutY(), 0.0f);
assertEquals(100f, root_child0_child0.getLayoutWidth(), 0.0f);
assertEquals(100f, root_child0_child0.getLayoutHeight(), 0.0f);

assertEquals(60f, root_child0_child0_child0.getLayoutX(), 0.0f);
assertEquals(0f, root_child0_child0_child0.getLayoutY(), 0.0f);
assertEquals(40f, root_child0_child0_child0.getLayoutWidth(), 0.0f);
assertEquals(50f, root_child0_child0_child0.getLayoutHeight(), 0.0f);

assertEquals(200f, root_child0_child1.getLayoutX(), 0.0f);
assertEquals(200f, root_child0_child1.getLayoutY(), 0.0f);
assertEquals(100f, root_child0_child1.getLayoutWidth(), 0.0f);
assertEquals(100f, root_child0_child1.getLayoutHeight(), 0.0f);

assertEquals(-100f, root_child0_child1_child0.getLayoutX(), 0.0f);
assertEquals(0f, root_child0_child1_child0.getLayoutY(), 0.0f);
assertEquals(200f, root_child0_child1_child0.getLayoutWidth(), 0.0f);
assertEquals(50f, root_child0_child1_child0.getLayoutHeight(), 0.0f);

assertEquals(-100f, root_child0_child1_child1.getLayoutX(), 0.0f);
assertEquals(0f, root_child0_child1_child1.getLayoutY(), 0.0f);
assertEquals(200f, root_child0_child1_child1.getLayoutWidth(), 0.0f);
assertEquals(50f, root_child0_child1_child1.getLayoutHeight(), 0.0f);

assertEquals(275f, root_child0_child2.getLayoutX(), 0.0f);
assertEquals(100f, root_child0_child2.getLayoutY(), 0.0f);
assertEquals(25f, root_child0_child2.getLayoutWidth(), 0.0f);
assertEquals(50f, root_child0_child2.getLayoutHeight(), 0.0f);
}

private YogaNode createNode(YogaConfig config) {
return mNodeFactory.create(config);
}
Expand Down
145 changes: 144 additions & 1 deletion javascript/tests/generated/YGStaticPositionTest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<fd288594fce8fa8e2399df5f5d303440>>
* @generated SignedSource<<ce99ac91b3467951bb539796415198ee>>
* generated by gentest/gentest-driver.ts from gentest/fixtures/YGStaticPositionTest.html
*/

Expand Down Expand Up @@ -6192,3 +6192,146 @@ test('static_position_static_root', () => {
config.free();
}
});
test('static_position_absolute_child_multiple', () => {
const config = Yoga.Config.create();
let root;

try {
root = Yoga.Node.create(config);
root.setPositionType(PositionType.Absolute);

const root_child0 = Yoga.Node.create(config);
root_child0.setPadding(Edge.Left, 100);
root_child0.setPadding(Edge.Top, 100);
root_child0.setPadding(Edge.Right, 100);
root_child0.setPadding(Edge.Bottom, 100);
root_child0.setWidth(400);
root_child0.setHeight(400);
root.insertChild(root_child0, 0);

const root_child0_child0 = Yoga.Node.create(config);
root_child0_child0.setPositionType(PositionType.Static);
root_child0_child0.setWidth(100);
root_child0_child0.setHeight(100);
root_child0.insertChild(root_child0_child0, 0);

const root_child0_child0_child0 = Yoga.Node.create(config);
root_child0_child0_child0.setPositionType(PositionType.Absolute);
root_child0_child0_child0.setWidth("10%");
root_child0_child0_child0.setHeight(50);
root_child0_child0.insertChild(root_child0_child0_child0, 0);

const root_child0_child1 = Yoga.Node.create(config);
root_child0_child1.setPositionType(PositionType.Static);
root_child0_child1.setWidth(100);
root_child0_child1.setHeight(100);
root_child0.insertChild(root_child0_child1, 1);

const root_child0_child1_child0 = Yoga.Node.create(config);
root_child0_child1_child0.setPositionType(PositionType.Absolute);
root_child0_child1_child0.setWidth("50%");
root_child0_child1_child0.setHeight(50);
root_child0_child1.insertChild(root_child0_child1_child0, 0);

const root_child0_child1_child1 = Yoga.Node.create(config);
root_child0_child1_child1.setPositionType(PositionType.Absolute);
root_child0_child1_child1.setWidth("50%");
root_child0_child1_child1.setHeight(50);
root_child0_child1.insertChild(root_child0_child1_child1, 1);

const root_child0_child2 = Yoga.Node.create(config);
root_child0_child2.setPositionType(PositionType.Absolute);
root_child0_child2.setWidth(25);
root_child0_child2.setHeight(50);
root_child0.insertChild(root_child0_child2, 2);
root.calculateLayout(undefined, undefined, Direction.LTR);

expect(root.getComputedLeft()).toBe(0);
expect(root.getComputedTop()).toBe(0);
expect(root.getComputedWidth()).toBe(400);
expect(root.getComputedHeight()).toBe(400);

expect(root_child0.getComputedLeft()).toBe(0);
expect(root_child0.getComputedTop()).toBe(0);
expect(root_child0.getComputedWidth()).toBe(400);
expect(root_child0.getComputedHeight()).toBe(400);

expect(root_child0_child0.getComputedLeft()).toBe(100);
expect(root_child0_child0.getComputedTop()).toBe(100);
expect(root_child0_child0.getComputedWidth()).toBe(100);
expect(root_child0_child0.getComputedHeight()).toBe(100);

expect(root_child0_child0_child0.getComputedLeft()).toBe(0);
expect(root_child0_child0_child0.getComputedTop()).toBe(0);
expect(root_child0_child0_child0.getComputedWidth()).toBe(40);
expect(root_child0_child0_child0.getComputedHeight()).toBe(50);

expect(root_child0_child1.getComputedLeft()).toBe(100);
expect(root_child0_child1.getComputedTop()).toBe(200);
expect(root_child0_child1.getComputedWidth()).toBe(100);
expect(root_child0_child1.getComputedHeight()).toBe(100);

expect(root_child0_child1_child0.getComputedLeft()).toBe(0);
expect(root_child0_child1_child0.getComputedTop()).toBe(0);
expect(root_child0_child1_child0.getComputedWidth()).toBe(200);
expect(root_child0_child1_child0.getComputedHeight()).toBe(50);

expect(root_child0_child1_child1.getComputedLeft()).toBe(0);
expect(root_child0_child1_child1.getComputedTop()).toBe(0);
expect(root_child0_child1_child1.getComputedWidth()).toBe(200);
expect(root_child0_child1_child1.getComputedHeight()).toBe(50);

expect(root_child0_child2.getComputedLeft()).toBe(100);
expect(root_child0_child2.getComputedTop()).toBe(100);
expect(root_child0_child2.getComputedWidth()).toBe(25);
expect(root_child0_child2.getComputedHeight()).toBe(50);

root.calculateLayout(undefined, undefined, Direction.RTL);

expect(root.getComputedLeft()).toBe(0);
expect(root.getComputedTop()).toBe(0);
expect(root.getComputedWidth()).toBe(400);
expect(root.getComputedHeight()).toBe(400);

expect(root_child0.getComputedLeft()).toBe(0);
expect(root_child0.getComputedTop()).toBe(0);
expect(root_child0.getComputedWidth()).toBe(400);
expect(root_child0.getComputedHeight()).toBe(400);

expect(root_child0_child0.getComputedLeft()).toBe(200);
expect(root_child0_child0.getComputedTop()).toBe(100);
expect(root_child0_child0.getComputedWidth()).toBe(100);
expect(root_child0_child0.getComputedHeight()).toBe(100);

expect(root_child0_child0_child0.getComputedLeft()).toBe(60);
expect(root_child0_child0_child0.getComputedTop()).toBe(0);
expect(root_child0_child0_child0.getComputedWidth()).toBe(40);
expect(root_child0_child0_child0.getComputedHeight()).toBe(50);

expect(root_child0_child1.getComputedLeft()).toBe(200);
expect(root_child0_child1.getComputedTop()).toBe(200);
expect(root_child0_child1.getComputedWidth()).toBe(100);
expect(root_child0_child1.getComputedHeight()).toBe(100);

expect(root_child0_child1_child0.getComputedLeft()).toBe(-100);
expect(root_child0_child1_child0.getComputedTop()).toBe(0);
expect(root_child0_child1_child0.getComputedWidth()).toBe(200);
expect(root_child0_child1_child0.getComputedHeight()).toBe(50);

expect(root_child0_child1_child1.getComputedLeft()).toBe(-100);
expect(root_child0_child1_child1.getComputedTop()).toBe(0);
expect(root_child0_child1_child1.getComputedWidth()).toBe(200);
expect(root_child0_child1_child1.getComputedHeight()).toBe(50);

expect(root_child0_child2.getComputedLeft()).toBe(275);
expect(root_child0_child2.getComputedTop()).toBe(100);
expect(root_child0_child2.getComputedWidth()).toBe(25);
expect(root_child0_child2.getComputedHeight()).toBe(50);
} finally {
if (typeof root !== 'undefined') {
root.freeRecursive();
}

config.free();
}
});
8 changes: 7 additions & 1 deletion tests/YGRelayoutTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,17 @@ TEST(YogaTest, has_new_layout_flag_set_static) {
YGNodeStyleSetHeight(root_child0, 10);
YGNodeInsertChild(root, root_child0, 0);

YGNodeRef root_child0_child1 = YGNodeNew();
YGNodeStyleSetPositionType(root_child0_child1, YGPositionTypeAbsolute);
YGNodeStyleSetWidth(root_child0_child1, 5);
YGNodeStyleSetHeight(root_child0_child1, 5);
YGNodeInsertChild(root_child0, root_child0_child1, 0);

YGNodeRef root_child0_child0 = YGNodeNew();
YGNodeStyleSetPositionType(root_child0_child0, YGPositionTypeStatic);
YGNodeStyleSetWidth(root_child0_child0, 5);
YGNodeStyleSetHeight(root_child0_child0, 5);
YGNodeInsertChild(root_child0, root_child0_child0, 0);
YGNodeInsertChild(root_child0, root_child0_child0, 1);

YGNodeRef root_child0_child0_child0 = YGNodeNew();
YGNodeStyleSetPositionType(root_child0_child0_child0, YGPositionTypeAbsolute);
Expand Down
Loading

0 comments on commit ae8ede9

Please sign in to comment.