Skip to content

Commit 65c4aa0

Browse files
mrobinsonGae24
authored andcommitted
layout: Improve documentation and code structure in FlexItemBox::automatic_min_size (servo#32911)
This change add specification text to comments and restructres the code a bit to better match the specification. In addition, a the `establishes_scroll_container()` helper is used instead of looking at overflow directly. It should not change behavior at all. Signed-off-by: Martin Robinson <mrobinson@igalia.com>
1 parent 73021e2 commit 65c4aa0

File tree

1 file changed

+42
-31
lines changed

1 file changed

+42
-31
lines changed

components/layout_2020/flexbox/layout.rs

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,16 +1978,21 @@ impl FlexItemBox {
19781978
if self
19791979
.independent_formatting_context
19801980
.style()
1981-
.get_box()
1982-
.overflow_x
1983-
.is_scrollable()
1981+
.establishes_scroll_container()
19841982
{
19851983
return Au::zero();
19861984
}
19871985

19881986
if cross_axis_is_item_block_axis {
1989-
let specified_size_suggestion = content_box_size.inline;
1990-
1987+
// > **specified size suggestion**
1988+
// > If the item’s preferred main size is definite and not automatic, then the specified
1989+
// > size suggestion is that size. It is otherwise undefined.
1990+
let specified_size_suggestion = content_box_size.inline.non_auto();
1991+
1992+
// > **transferred size suggestion**
1993+
// > If the item has a preferred aspect ratio and its preferred cross size is definite, then the
1994+
// > transferred size suggestion is that size (clamped by its minimum and maximum cross sizes if they
1995+
// > are definite), converted through the aspect ratio. It is otherwise undefined.
19911996
let transferred_size_suggestion = match self.independent_formatting_context {
19921997
IndependentFormattingContext::NonReplaced(_) => None,
19931998
IndependentFormattingContext::Replaced(ref bfc) => {
@@ -2009,40 +2014,46 @@ impl FlexItemBox {
20092014
},
20102015
};
20112016

2017+
// > **content size suggestion**
2018+
// > The content size suggestion is the min-content size in the main axis, clamped, if it has a
2019+
// > preferred aspect ratio, by any definite minimum and maximum cross sizes converted through the
2020+
// > aspect ratio.
20122021
let inline_content_size = self
20132022
.independent_formatting_context
20142023
.inline_content_sizes(layout_context)
20152024
.min_content;
2016-
let content_size_suggestion = match self.independent_formatting_context {
2017-
IndependentFormattingContext::NonReplaced(_) => inline_content_size,
2018-
IndependentFormattingContext::Replaced(ref replaced) => {
2019-
if let Some(ratio) = replaced
2025+
let (is_replaced, aspect_ratio) = match self.independent_formatting_context {
2026+
IndependentFormattingContext::NonReplaced(_) => (false, None),
2027+
IndependentFormattingContext::Replaced(ref replaced) => (
2028+
true,
2029+
replaced
20202030
.contents
20212031
.inline_size_over_block_size_intrinsic_ratio(
20222032
self.independent_formatting_context.style(),
2023-
)
2024-
{
2025-
inline_content_size.clamp_between_extremums(
2026-
min_size.block.auto_is(Au::zero).scale_by(ratio),
2027-
max_size.block.map(|l| l.scale_by(ratio)),
2028-
)
2029-
} else {
2030-
inline_content_size
2031-
}
2032-
},
2033-
};
2034-
2035-
let result = match specified_size_suggestion {
2036-
AuOrAuto::LengthPercentage(l) => l.min(content_size_suggestion),
2037-
AuOrAuto::Auto => {
2038-
if let Some(l) = transferred_size_suggestion {
2039-
l.min(content_size_suggestion)
2040-
} else {
2041-
content_size_suggestion
2042-
}
2043-
},
2033+
),
2034+
),
20442035
};
2045-
result.clamp_below_max(max_size.inline)
2036+
let content_size_suggestion = aspect_ratio
2037+
.map(|aspect_ratio| {
2038+
inline_content_size.clamp_between_extremums(
2039+
min_size.block.auto_is(Au::zero).scale_by(aspect_ratio),
2040+
max_size.block.map(|l| l.scale_by(aspect_ratio)),
2041+
)
2042+
})
2043+
.unwrap_or(inline_content_size);
2044+
2045+
// > The content-based minimum size of a flex item is the smaller of its specified size
2046+
// > suggestion and its content size suggestion if its specified size suggestion exists;
2047+
// > otherwise, the smaller of its transferred size suggestion and its content size
2048+
// > suggestion if the element is replaced and its transferred size suggestion exists;
2049+
// > otherwise its content size suggestion. In all cases, the size is clamped by the maximum
2050+
// > main size if it’s definite.
2051+
match (specified_size_suggestion, transferred_size_suggestion) {
2052+
(Some(specified), _) => specified.min(content_size_suggestion),
2053+
(_, Some(transferred)) if is_replaced => transferred.min(content_size_suggestion),
2054+
_ => content_size_suggestion,
2055+
}
2056+
.clamp_below_max(max_size.inline)
20462057
} else {
20472058
// FIXME(stshine): Implement this when main axis is item's block axis.
20482059
Au::zero()

0 commit comments

Comments
 (0)