Fix stale layout when a child switches to display: contents after insertion - #2028
Merged
meta-codesync[bot] merged 1 commit intoSep 28, 2026
Merged
Conversation
…ertion Each node caches how many of its children use `display: contents`, but the count is only updated when children are inserted, removed or replaced. Setting `display: contents` on a child that is already attached left the count at zero, so the owner skipped `cleanupContentsNodesRecursively` and the contents node stayed dirty after layout. Dirty marking from its children then stopped at that node and never reached the root, so later style changes were ignored. Recompute the owner's count in `YGNodeStyleSetDisplay` when a node with an owner switches to or from `display: contents`.
cipolleschi
approved these changes
Sep 28, 2026
|
@cipolleschi has imported this pull request. If you are a Meta employee, you can view this in D122122520. |
meta-codesync Bot
pushed a commit
to react/react-native
that referenced
this pull request
Sep 28, 2026
…nsertion (#58721) Summary: Pull Request resolved: #58721 Each node caches how many of its children use `display: contents` (`contentsChildrenCount_`, added in react/yoga#1726). The count is updated in `insertChild`, `removeChild`, `replaceChild` and `setChildren`, but not when the display of a child that is already attached changes. So if `YGNodeStyleSetDisplay(child, YGDisplayContents)` is called after the child was inserted, the owner still reports `hasContentsChildren() == false`. The layout algorithm then skips `cleanupContentsNodesRecursively` for it, and the contents node keeps `isDirty == true` after layout. When a descendant of that node changes later, `markDirtyAndPropagate` stops at the contents node because it is already dirty, the root is never marked dirty, and the next `YGNodeCalculateLayout` returns the cached layout. Minimal repro: ```cpp YGNodeRef root = YGNodeNew(); YGNodeStyleSetWidth(root, 100); YGNodeStyleSetHeight(root, 100); YGNodeRef child = YGNodeNew(); YGNodeInsertChild(root, child, 0); YGNodeRef grandchild = YGNodeNew(); YGNodeStyleSetWidth(grandchild, 10); YGNodeStyleSetHeight(grandchild, 10); YGNodeInsertChild(child, grandchild, 0); YGNodeStyleSetDisplay(child, YGDisplayContents); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); // YGNodeIsDirty(child) is still true YGNodeStyleSetWidth(grandchild, 20); // YGNodeIsDirty(root) is false YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); // YGNodeLayoutGetWidth(grandchild) is 10, expected 20 ``` This affects any binding that mutates nodes in place (the JS and Java bindings both go through `YGNodeStyleSetDisplay`). Setting the display before inserting the child works, which is what the existing tests do. The fix recomputes the owner's count in `YGNodeStyleSetDisplay` when a node that has an owner switches to or from `display: contents`. Changelog: [General][Fixed] - Fix stale layout when a child switches to `display: contents` after being inserted X-link: react/yoga#2028 Test Plan: Added `dirty_propagation_through_child_set_to_display_contents` to `tests/YGDirtyMarkingTest.cpp`. Without the fix, all three of its assertions fail (the contents node stays dirty, the root is not marked dirty, and the grandchild keeps width 10). With the fix it passes. Ran the full C++ suite (`unit_tests.bat`, MSVC 2022 + Ninja): 855 tests passed. Checked formatting of the changed files with the repo's clang-format version (21.1.2): `clang-format --dry-run --Werror yoga/YGNodeStyle.cpp tests/YGDirtyMarkingTest.cpp` reports no changes. Reviewed By: javache Differential Revision: D122122520 Pulled By: cipolleschi fbshipit-source-id: 79433cfb8112c9e8596f1ae604de56055860d814
kwy404
added a commit
to kwy404/kwy404
that referenced
this pull request
Sep 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Each node caches how many of its children use
display: contents(contentsChildrenCount_, added in #1726). The count is updated ininsertChild,removeChild,replaceChildandsetChildren, but not when the display of a child that is already attached changes.So if
YGNodeStyleSetDisplay(child, YGDisplayContents)is called after the child was inserted, the owner still reportshasContentsChildren() == false. The layout algorithm then skipscleanupContentsNodesRecursivelyfor it, and the contents node keepsisDirty == trueafter layout. When a descendant of that node changes later,markDirtyAndPropagatestops at the contents node because it is already dirty, the root is never marked dirty, and the nextYGNodeCalculateLayoutreturns the cached layout.Minimal repro:
This affects any binding that mutates nodes in place (the JS and Java bindings both go through
YGNodeStyleSetDisplay). Setting the display before inserting the child works, which is what the existing tests do.The fix recomputes the owner's count in
YGNodeStyleSetDisplaywhen a node that has an owner switches to or fromdisplay: contents.Changelog: [General][Fixed] - Fix stale layout when a child switches to
display: contentsafter being insertedTest Plan
Added
dirty_propagation_through_child_set_to_display_contentstotests/YGDirtyMarkingTest.cpp. Without the fix, all three of its assertions fail (the contents node stays dirty, the root is not marked dirty, and the grandchild keeps width 10). With the fix it passes.Ran the full C++ suite (
unit_tests.bat, MSVC 2022 + Ninja): 855 tests passed.Checked formatting of the changed files with the repo's clang-format version (21.1.2):
clang-format --dry-run --Werror yoga/YGNodeStyle.cpp tests/YGDirtyMarkingTest.cppreports no changes.