Files
yoga/tests/YGNodeChildTest.cpp
Yonah Karpandmeta-codesync[bot] 806d7f9e57 Yoga | dirty YGNode when removing it from parent (#1868)
Summary:
Pull Request resolved: https://github.com/facebook/yoga/pull/1868

X-link: https://github.com/facebook/react-native/pull/55411

## Context
When a child node is removed from its parent via YGNodeRemoveChild, the child's calculated layout is cleared (setLayout({})) but the child node is not marked dirty. This causes issues when the view is removed views and later reused with the same layout values. the layout values were cleared (resulting in NaN/0), but since the layout values are the same for the newly reattached node, it isn't dirtied. However, the user would expect it to be dirty, as:
a) a fully new node would be dirty by default.
b) They just set layout

**Example**:
- Node is removed from the view and it's layout is cleared.
- Node is reattatched and set with height/width that happens to be same as previous
- Checking if the view is dirty unexpectedly gives false, despite user setting a "new" value.
- Pulling height/width gives 0, as layout hasn't been recalculated yet

## This Diff
Marks the removed child node as dirty when clearing its layout in YGNodeRemoveChild, ensuring that subsequent layout calculations will properly recalculate the child's layout values

## Changelog:

[Internal] [Fixed] - Marks removed child node as dirty when clearing its layout in YGNodeRemoveChild, ensuring that subsequent layout calculations will properly recalculate the child's layout values

Reviewed By: NickGerleman

Differential Revision: D92280506

fbshipit-source-id: 6f1adb6f5b8cbe9d3461746b613e42f890b1e75e
2026-02-11 07:21:52 -08:00

69 lines
2.2 KiB
C++

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <gtest/gtest.h>
#include <yoga/Yoga.h>
TEST(YogaTest, reset_layout_when_child_removed) {
YGNodeRef root = YGNodeNew();
YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetWidth(root_child0, 100);
YGNodeStyleSetHeight(root_child0, 100);
YGNodeInsertChild(root, root_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
ASSERT_EQ(100, YGNodeLayoutGetWidth(root_child0));
ASSERT_EQ(100, YGNodeLayoutGetHeight(root_child0));
YGNodeRemoveChild(root, root_child0);
ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
ASSERT_TRUE(YGFloatIsUndefined(YGNodeLayoutGetWidth(root_child0)));
ASSERT_TRUE(YGFloatIsUndefined(YGNodeLayoutGetHeight(root_child0)));
YGNodeFreeRecursive(root);
YGNodeFreeRecursive(root_child0);
}
TEST(YogaTest, removed_child_can_be_reused_with_valid_layout) {
YGNodeRef root = YGNodeNew();
YGNodeStyleSetWidth(root, 200);
YGNodeStyleSetHeight(root, 200);
YGNodeRef child = YGNodeNew();
YGNodeStyleSetWidth(child, 100);
YGNodeStyleSetHeight(child, 100);
YGNodeInsertChild(root, child, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_EQ(100, YGNodeLayoutGetWidth(child));
ASSERT_EQ(100, YGNodeLayoutGetHeight(child));
// Remove child - layout should be cleared and child marked dirty
YGNodeRemoveChild(root, child);
ASSERT_TRUE(YGFloatIsUndefined(YGNodeLayoutGetWidth(child)));
ASSERT_TRUE(YGFloatIsUndefined(YGNodeLayoutGetHeight(child)));
ASSERT_TRUE(YGNodeIsDirty(child));
// Reinsert the child and recalculate - layout should be valid again
YGNodeInsertChild(root, child, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_EQ(100, YGNodeLayoutGetWidth(child));
ASSERT_EQ(100, YGNodeLayoutGetHeight(child));
ASSERT_FALSE(YGNodeIsDirty(child));
YGNodeFreeRecursive(root);
}