Repository navigation
Measure a leaf's min-content height at the width it is given (#2105) - #60513
Merged
Merged
Conversation
|
@javache has exported this pull request. If you are a Meta employee, you can view the originating Diff in D135502567. |
) Summary: * Proposal: measure a leaf's min-content height at the width it is given Why === A flex item does not shrink below its min-content size (css-flexbox-1 §4.5). In a column that size is a height, and for text it is the height of the lines it wraps to at the box's width (css-sizing-3 §2.1: a block container's min-content block size equals its max-content block size). Yoga's min-content walk probes a measured leaf with at most zero on the probed axis and no limit on the other, so a text measurer answers with zero or with one unbroken line, and the text overflows its box: ```html <div style="display: flex; flex-direction: column; width: 50px; height: 20px"> <div style="flex-shrink: 1">aaaa aaaa aaaa</div> <!-- three lines. Chrome: 30px. Yoga: 10px --> <div style="height: 10px; flex-shrink: 0"></div> </div> ``` This is a proposal because it changes the arguments a host's measure function is called with. How === With the experimental feature `MinContentHeightAtGivenWidth`, the walk measures the leaf at its content width (`AtMost`) with no limit on its height. Without the feature nothing changes. The walk finds the width on the way down from the flex container: a box's stated width, or its owner's content box less its margins, within its minimum and maximum. That is the box's width when it is stretched in a column; a narrower box that is not stretched may wrap to more lines than were measured. A row's children are walked against no width, so text below a row counts as unwrapped unless it, or a box between it and the row, states a width. In both cases the item can shrink further than its content needs. A wrapping column is treated as a column, since Chrome wraps its text at the column's width (`text_in_wrapping_column_keeps_wrapped_lines`). In a height walk, a percentage padding below the item resolves against this width. The leaf's height now depends on the owner's width, so its memo and those above it on that axis are keyed on `minContentOwnerWidth` (the existing `minContentResolvesAgainstOwnerWidth` flag). A new width measures the leaf again. The gate is the open question. The layout change exists only with `MinSizeUndefinedInsteadOfAuto` off; what needs a gate is the callback contract. `YGMinContentMeasureFunc`, and a leaf's `YGMeasureFunc` when it has none, is documented as called with `AtMost 0` on the probed axis, and a host that recognizes a height probe by those arguments would stop recognizing it. An experimental feature lets each host turn the fix on when its measure functions are ready. The alternative is an erratum that keeps the old probe, set by default like `MinSizeUndefinedInsteadOfAuto`: one switch fewer for a host that asks for `YGErrataNone`, but it changes the callback arguments for every such host at once. I will switch to it if you prefer. `YGNode.h` documents the new arguments either way. React Native's `MinContentTest` cases (`packages/react-native/ReactCommon/yoga/test/BaselineTest.cpp`) assert that a new width leaves a clean leaf shared. With the feature on, three of them hold only if the leaf's height is static (`YGNodeSetMinContentHeight`). Test Plan ========= `python3 enums.py` regenerates the enum files with no change, `yarn gentest -f YGAutoMinSizeTest` generates the new fixtures from Chrome, and `cmake --build tests/build && tests/build/yogatests` passes with them and with the hand-written tests for the measure function's arguments and the memo. The height of the shrinking item, in Chrome and with the change to `CalculateLayout.cpp` reverted: | Fixture | Chrome | Without the change | | --- | --- | --- | | `text_keeps_wrapped_lines` | 30 | 10 | | `nested_text_keeps_wrapped_lines` | 35 | 10 | | `text_wraps_inside_margin_and_padding` | 30 | 10 | | `text_wraps_at_stated_width` | 30 | 10 | | `text_wraps_at_stated_content_box_width` | 20 | 5 | | `text_wraps_inside_padded_parent` | 30 | 10 | | `text_wraps_at_max_width` | 30 | 10 | | `text_in_wrapping_column_keeps_wrapped_lines` | 30 | 5 | `text_fits_wider_parent`, `text_fits_min_width` and `text_below_row_is_not_wrapped` keep text wider than the container on one line (10). Without the change they give 5, and measuring at the container's width instead of the box's would give 30. `min_content_height_follows_given_width` gets 16 where it expects 32 and then 48, and `min_content_height_without_owner_width_is_unconstrained` fails on the measure modes. `min_content_height_probe_asks_for_at_most_zero` pins the contract with the feature off, and passes with and without the change. `yarn gentest -h` leaves the tree clean, and `node ./scripts/clang-format.js --check` passes. The generated Java and JavaScript tests were not run. Changelog: [General][Added] - Add the Yoga experimental feature MinContentHeightAtGivenWidth, which keeps wrapped text in a column from shrinking below its lines when automatic minimum sizes are on * Resolve percentage margins in the min-content height walk against the parent's content width The walk gave each child its parent's content width for its padding, but its margins still used the grandparent's width. * Wrap text at its max-width when the min-content height walk has no width Below a row the walk passes no width, so a box's max-width was never applied and its text was measured on one line. --------- Differential Revision: D135502567
javache
force-pushed
the
export-D135502567
branch
from
December 3, 2026 22:10
477885f to
38867b4
Compare
Collaborator
|
This pull request was successfully merged by @javache in 20eae72 When will my fix make it into a release? | How to file a pick request? |
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:
Why
A flex item does not shrink below its min-content size (css-flexbox-1 §4.5). In a column that size is a height, and for text it is the height of the lines it wraps to at the box's width (css-sizing-3 §2.1: a block container's min-content block size equals its max-content block size). Yoga's min-content walk probes a measured leaf with at most zero on the probed axis and no limit on the other, so a text measurer answers with zero or with one unbroken line, and the text overflows its box:
This is a proposal because it changes the arguments a host's measure function is called with.
How
With the experimental feature
MinContentHeightAtGivenWidth, the walk measures the leaf at its content width (AtMost) with no limit on its height. Without the feature nothing changes.The walk finds the width on the way down from the flex container: a box's stated width, or its owner's content box less its margins, within its minimum and maximum. That is the box's width when it is stretched in a column; a narrower box that is not stretched may wrap to more lines than were measured. A row's children are walked against no width, so text below a row counts as unwrapped unless it, or a box between it and the row, states a width. In both cases the item can shrink further than its content needs. A wrapping column is treated as a column, since Chrome wraps its text at the column's width (
text_in_wrapping_column_keeps_wrapped_lines). In a height walk, a percentage padding below the item resolves against this width.The leaf's height now depends on the owner's width, so its memo and those above it on that axis are keyed on
minContentOwnerWidth(the existingminContentResolvesAgainstOwnerWidthflag). A new width measures the leaf again.The gate is the open question. The layout change exists only with
MinSizeUndefinedInsteadOfAutooff; what needs a gate is the callback contract.YGMinContentMeasureFunc, and a leaf'sYGMeasureFuncwhen it has none, is documented as called withAtMost 0on the probed axis, and a host that recognizes a height probe by those arguments would stop recognizing it. An experimental feature lets each host turn the fix on when its measure functions are ready. The alternative is an erratum that keeps the old probe, set by default likeMinSizeUndefinedInsteadOfAuto: one switch fewer for a host that asks forYGErrataNone, but it changes the callback arguments for every such host at once. I will switch to it if you prefer.YGNode.hdocuments the new arguments either way.React Native's
MinContentTestcases (packages/react-native/ReactCommon/yoga/test/BaselineTest.cpp) assert that a new width leaves a clean leaf shared. With the feature on, three of them hold only if the leaf's height is static (YGNodeSetMinContentHeight).Test Plan
python3 enums.pyregenerates the enum files with no change,yarn gentest -f YGAutoMinSizeTestgenerates the new fixtures from Chrome, andcmake --build tests/build && tests/build/yogatestspasses with them and with the hand-written tests for the measure function's arguments and the memo.The height of the shrinking item, in Chrome and with the change to
CalculateLayout.cppreverted:text_keeps_wrapped_linesnested_text_keeps_wrapped_linestext_wraps_inside_margin_and_paddingtext_wraps_at_stated_widthtext_wraps_at_stated_content_box_widthtext_wraps_inside_padded_parenttext_wraps_at_max_widthtext_in_wrapping_column_keeps_wrapped_linestext_fits_wider_parent,text_fits_min_widthandtext_below_row_is_not_wrappedkeep text wider than the container on one line (10). Without the change they give 5, and measuring at the container's width instead of the box's would give 30.min_content_height_follows_given_widthgets 16 where it expects 32 and then 48, andmin_content_height_without_owner_width_is_unconstrainedfails on the measure modes.min_content_height_probe_asks_for_at_most_zeropins the contract with the feature off, and passes with and without the change.yarn gentest -hleaves the tree clean, andnode ./scripts/clang-format.js --checkpasses. The generated Java and JavaScript tests were not run.Changelog:
[General][Added] - Add the Yoga experimental feature MinContentHeightAtGivenWidth, which keeps wrapped text in a column from shrinking below its lines when automatic minimum sizes are on
The walk gave each child its parent's content width for its padding, but its
margins still used the grandparent's width.
Below a row the walk passes no width, so a box's max-width was never applied
and its text was measured on one line.
Differential Revision: D135502567