From 76622392c81d6fb485170c4d1ae737adc8fb3d43 Mon Sep 17 00:00:00 2001 From: GitHub Workflows Support Date: Sat, 5 Dec 2026 00:26:49 -0800 Subject: [PATCH] Lay out a baseline-aligned item before a measured row reads its baseline (#2153) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Why === A row with baseline-aligned items takes each line's cross size from the items' baselines ([css-flexbox-1 ยง9.4, step 8](https://www.w3.org/TR/css-flexbox-1/#algo-cross-line)). `calculateBaseline` reads an item's baseline from the positions of its descendants, and only a layout pass sets positions. When the row is measured and not laid out, as it is for its flex basis in a column, its items are only measured too: an item with a definite size is sized by `measureNodeWithFixedSize` without a visit to its descendants, and any other item is measured without positions. The row's height then comes from whatever positions the descendants last had, which on a first layout is none: ```html
``` The first item's baseline is 10 below its top and the second's is 50, so the line is 90 high in Chrome and the last box is at 90. Yoga measures the row as 50 high, lays it out at that height, and puts the last box at 50, over the first item. How === When a node that is being measured sizes a baseline-aligned item in the second free-space pass, it now lays the item out instead of measuring it, so that the baseline it reads next is the item's own. It does so only for an item whose baseline comes from descendants (one with children and no baseline function). It also does so only when the node is dirty. A layout pass writes the positions and sizes that the embedder reads, and sets `hasNewLayout`, on the item's subtree. That is safe when the node's own layout pass follows in the same `YGNodeCalculateLayout`, where it lays the item out at its final size and visits everything the measurement touched. A dirty node always gets that pass, because every node above a dirty node in the tree being laid out is dirty too, so none of them can answer from its layout cache: `markDirtyAndPropagate` dirties a node's owners within one tree, and React Native marks a parent dirty whenever one of its children is dirty when it clones the parent (`updateYogaChildren`). A clean node that is measured again may not get one. Widening a parent, for example, measures a content-sized child again and then finds the child's cached layout still valid; a layout written during that measurement would stay, with `hasNewLayout` set under a node that has none and a padding resolved against the wrong width. A clean node's items therefore keep the layout they have, and the baselines read from it are those of the last layout, as before this change. No erratum gates this. The size of a measured baseline row already depended on what its items' descendants held from an earlier layout, so a first layout and a later one of the same tree could differ, and no application can rely on the first. Cost, counted in passes with the `LayoutPassEnd` event for a first layout of baseline rows nested eight deep in a 400-wide column, each row's first item a column that holds the next row: | Tree | Layouts and measures, before | After | | --- | --- | --- | | A 10 by 10 leaf in every item and a 10 by 10 sibling in every row | 33 and 1880 | 41 and 2207 | | A leaf in every item, no sibling | 25 and 602 | 33 and 684 | | A leaf in the innermost item only, no sibling | 18 and 108 | 26 and 117 | Where the result does not change, as in these three, a first layout costs about a quarter to two fifths more layouts and up to 17% more measures. These are pass counts; no time was measured. Limits, all of which read the baselines of an earlier layout, as `main` does everywhere: - A clean row measured at a size its items were not last laid out at. A resize does this: a row whose item has a percentage padding above its baseline is 70 high in a fresh layout at one width, and keeps the height of the earlier width after a resize to it. - A baseline row inside a baseline-aligned item, on a first layout. The outer row's measurement lays the item out, which clears the dirty flags below it, so the inner row is clean when it is measured again at another size. - On `main` the line's size also counts the baselines of items that are not baseline-aligned, which this change does not lay out; sizing a line from its baseline-aligned items alone is a separate change. Removing the first two needs a baseline that does not come from a layout: one recorded with each cached measurement, for the constraints it was measured at. That is a larger change, to the cache. Test Plan ========= `yarn gentest -f YGAlignItemsTest` adds two fixtures with Chrome's values, and `cmake --build tests/build && tests/build/yogatests` passes. With `yoga/algorithm/CalculateLayout.cpp` reverted, both fail: in `align_baseline_row_of_fixed_size_items_in_column` (the markup above) the row is 50 high where Chrome has 90, and in `align_baseline_row_of_content_sized_items_in_column`, whose first item has no height of its own, the row is 50 high where Chrome has 60. The hand-written `measuring_clean_baseline_row_keeps_layout_of_its_items` in `YGAlignBaselineTest.cpp` covers the check that the node is dirty: it widens a parent so that a clean row is measured again while its owner's cached layout holds. Without the check, the row's item is left with `hasNewLayout` set and its child 30 lower than the layout the tree keeps. The two checks on the item (children, no baseline function) only avoid work. `yarn format-check`, `yarn lint`, `yarn tsc` and `yarn gentest-validate` pass. Not run: the generated Java and JavaScript tests. Changelog: [General][Fixed] - Fix Yoga making a row with baseline-aligned items too short, so that it overlaps what follows it, when the row's height comes from its content Reviewed By: javache Differential Revision: D135550892 --- .../yoga/yoga/algorithm/CalculateLayout.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp index 8abeaa76408..85870e59db5 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp @@ -1645,7 +1645,18 @@ static float distributeFreeSpaceSecondPass( const SizingMode childHeightSizingMode = !isMainAxisRow ? childMainSizingMode : childCrossSizingMode; - const bool isLayoutPass = performLayout && !requiresStretchLayout; + // A baseline-aligned item's baseline comes from the positions of its + // descendants, which only a layout pass sets, and the line's cross size + // comes from that baseline. Lay the item out even when the node is only + // measured, if the node is dirty: its own layout pass then follows in this + // layout and visits the item again. A clean node may never get one, so its + // items keep the layout, and the baselines, they have. + const bool needsLayoutForBaseline = node->isDirty() && + resolveChildAlignment(node, currentLineChild) == Align::Baseline && + !currentLineChild->hasBaselineFunc() && + currentLineChild->getLayoutChildCount() != 0; + const bool isLayoutPass = + (performLayout && !requiresStretchLayout) || needsLayoutForBaseline; // Recursively call the layout algorithm for this child with the updated // main size. calculateLayoutInternal(