LibWeb: Retain size definiteness when transferred through aspect ratio

If a box has definite width and a preferred aspect ratio, we can
determine its height without performing layout. Hence, its height should
also be considered definite. And the other way around for width from
height as well.
This commit is contained in:
Andreas Kling 2024-03-27 08:23:12 +01:00
parent ca53eefe11
commit 1cfd8b3ac0
Notes: sideshowbarker 2024-07-16 21:39:23 +09:00

View file

@ -482,6 +482,23 @@ void LayoutState::UsedValues::set_node(NodeWithStyle& node, UsedValues const* co
m_has_definite_width = is_definite_size(computed_values.width(), m_content_width, true);
m_has_definite_height = is_definite_size(computed_values.height(), m_content_height, false);
// For boxes with a preferred aspect ratio and one definite size, we can infer the other size
// and consider it definite since this did not require performing layout.
if (is<Box>(node)) {
auto const& box = static_cast<Box const&>(node);
if (auto aspect_ratio = box.preferred_aspect_ratio(); aspect_ratio.has_value()) {
if (m_has_definite_width && m_has_definite_height) {
// Both width and height are definite.
} else if (m_has_definite_width) {
m_content_height = m_content_width / *aspect_ratio;
m_has_definite_height = true;
} else if (m_has_definite_height) {
m_content_width = m_content_height * *aspect_ratio;
m_has_definite_width = true;
}
}
}
if (m_has_definite_width) {
if (has_definite_min_width)
m_content_width = max(min_width, m_content_width);