1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 22:37:35 +00:00

LibHTML: Implement basic 'max-width' and 'min-width' support

These CSS properties constrain the computed width of a block-level box
to a maximum, followed by a minimum value.

This makes the "better mother fricken website" look more like it's
intended to (which helps makes the author's point, I suppose.)
This commit is contained in:
Andreas Kling 2019-11-18 12:21:37 +01:00
parent df16c9676b
commit b08de46480

View file

@ -157,18 +157,26 @@ void LayoutBlock::compute_width()
auto auto_value = Length();
auto zero_value = Length(0, Length::Type::Absolute);
auto width = style.length_or_fallback(CSS::PropertyID::Width, auto_value);
auto margin_left = style.length_or_fallback(CSS::PropertyID::MarginLeft, zero_value);
auto margin_right = style.length_or_fallback(CSS::PropertyID::MarginRight, zero_value);
auto border_left = style.length_or_fallback(CSS::PropertyID::BorderLeftWidth, zero_value);
auto border_right = style.length_or_fallback(CSS::PropertyID::BorderRightWidth, zero_value);
auto padding_left = style.length_or_fallback(CSS::PropertyID::PaddingLeft, zero_value);
auto padding_right = style.length_or_fallback(CSS::PropertyID::PaddingRight, zero_value);
Length margin_left;
Length margin_right;
Length border_left;
Length border_right;
Length padding_left;
Length padding_right;
auto try_compute_width = [&](const auto& a_width) {
Length width = a_width;
#ifdef HTML_DEBUG
dbg() << " Left: " << margin_left << "+" << border_left << "+" << padding_left;
dbg() << "Right: " << margin_right << "+" << border_right << "+" << padding_right;
#endif
margin_left = style.length_or_fallback(CSS::PropertyID::MarginLeft, zero_value);
margin_right = style.length_or_fallback(CSS::PropertyID::MarginRight, zero_value);
border_left = style.length_or_fallback(CSS::PropertyID::BorderLeftWidth, zero_value);
border_right = style.length_or_fallback(CSS::PropertyID::BorderRightWidth, zero_value);
padding_left = style.length_or_fallback(CSS::PropertyID::PaddingLeft, zero_value);
padding_right = style.length_or_fallback(CSS::PropertyID::PaddingRight, zero_value);
int total_px = 0;
for (auto& value : { margin_left, border_left, padding_left, width, padding_right, border_right, margin_right }) {
@ -215,8 +223,33 @@ void LayoutBlock::compute_width()
margin_right = half_of_the_underflow;
}
}
return width;
};
rect().set_width(width.to_px());
auto specified_width = style.length_or_fallback(CSS::PropertyID::Width, auto_value);
// 1. The tentative used width is calculated (without 'min-width' and 'max-width')
auto used_width = try_compute_width(specified_width);
// 2. The tentative used width is greater than 'max-width', the rules above are applied again,
// but this time using the computed value of 'max-width' as the computed value for 'width'.
auto specified_max_width = style.length_or_fallback(CSS::PropertyID::MaxWidth, auto_value);
if (!specified_max_width.is_auto()) {
if (used_width.to_px() > specified_max_width.to_px()) {
used_width = try_compute_width(specified_max_width);
}
}
// 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
// but this time using the value of 'min-width' as the computed value for 'width'.
auto specified_min_width = style.length_or_fallback(CSS::PropertyID::MinWidth, auto_value);
if (!specified_min_width.is_auto()) {
if (used_width.to_px() < specified_min_width.to_px()) {
used_width = try_compute_width(specified_min_width);
}
}
rect().set_width(used_width.to_px());
box_model().margin().left = margin_left;
box_model().margin().right = margin_right;
box_model().border().left = border_left;