From 1a3246b5600dd17516ca7622e4d69641987d6028 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 27 Aug 2025 15:42:33 +0100 Subject: [PATCH] LibWeb/CSS: Stop allowing auto in ComputedProperties::length_percentage We only had one user of this which allows `auto`, which is length_box(). So, inline the code there, remove the `auto` branch from length_percentage(), and then remove length_percentage_or_fallback() entirely as nobody uses it any more. --- Libraries/LibWeb/CSS/ComputedProperties.cpp | 54 +++++++++++++++------ Libraries/LibWeb/CSS/ComputedProperties.h | 1 - 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/Libraries/LibWeb/CSS/ComputedProperties.cpp b/Libraries/LibWeb/CSS/ComputedProperties.cpp index 30bfe37104a..c0ba3b49ef4 100644 --- a/Libraries/LibWeb/CSS/ComputedProperties.cpp +++ b/Libraries/LibWeb/CSS/ComputedProperties.cpp @@ -222,11 +222,6 @@ Size ComputedProperties::size_value(PropertyID id) const return Size::make_auto(); } -LengthPercentage ComputedProperties::length_percentage_or_fallback(PropertyID id, Layout::NodeWithStyle const& layout_node, ClampNegativeLengths disallow_negative_lengths, LengthPercentage const& fallback) const -{ - return length_percentage(id, layout_node, disallow_negative_lengths).value_or(fallback); -} - Optional ComputedProperties::length_percentage(PropertyID id, Layout::NodeWithStyle const& layout_node, ClampNegativeLengths disallow_negative_lengths) const { auto const& value = property(id); @@ -256,9 +251,6 @@ Optional ComputedProperties::length_percentage(PropertyID id, return length; } - if (value.has_auto()) - return LengthPercentage { Length::make_auto() }; - return {}; } @@ -269,12 +261,46 @@ Length ComputedProperties::length(PropertyID property_id) const LengthBox ComputedProperties::length_box(PropertyID left_id, PropertyID top_id, PropertyID right_id, PropertyID bottom_id, Layout::NodeWithStyle const& layout_node, ClampNegativeLengths disallow_negative_lengths, Length const& default_value) const { - LengthBox box; - box.left() = length_percentage_or_fallback(left_id, layout_node, disallow_negative_lengths, default_value); - box.top() = length_percentage_or_fallback(top_id, layout_node, disallow_negative_lengths, default_value); - box.right() = length_percentage_or_fallback(right_id, layout_node, disallow_negative_lengths, default_value); - box.bottom() = length_percentage_or_fallback(bottom_id, layout_node, disallow_negative_lengths, default_value); - return box; + auto length_box_side = [&](PropertyID id) -> LengthPercentage { + auto const& value = property(id); + + if (value.is_calculated()) + return LengthPercentage { value.as_calculated() }; + + if (value.is_percentage()) { + auto percentage = value.as_percentage().percentage(); + + // FIXME: This value can be negative as interpolation does not yet clamp values to allowed ranges - remove this + // once we do that. + if (disallow_negative_lengths == ClampNegativeLengths::Yes && percentage.as_fraction() < 0) + return default_value; + + return percentage; + } + + if (value.is_length()) { + auto length = value.as_length().length(); + + // FIXME: This value can be negative as interpolation does not yet clamp values to allowed ranges - remove this + // once we do that. + if (disallow_negative_lengths == ClampNegativeLengths::Yes && length.to_px(layout_node) < 0) + return default_value; + + return value.as_length().length(); + } + + if (value.has_auto()) + return LengthPercentage { Length::make_auto() }; + + return default_value; + }; + + return LengthBox { + length_box_side(top_id), + length_box_side(right_id), + length_box_side(bottom_id), + length_box_side(left_id) + }; } Color ComputedProperties::color_or_fallback(PropertyID id, ColorResolutionContext color_resolution_context, Color fallback) const diff --git a/Libraries/LibWeb/CSS/ComputedProperties.h b/Libraries/LibWeb/CSS/ComputedProperties.h index cad4171509f..c4d43f23209 100644 --- a/Libraries/LibWeb/CSS/ComputedProperties.h +++ b/Libraries/LibWeb/CSS/ComputedProperties.h @@ -82,7 +82,6 @@ public: No, Yes, }; - LengthPercentage length_percentage_or_fallback(PropertyID, Layout::NodeWithStyle const&, ClampNegativeLengths, LengthPercentage const& fallback) const; Optional length_percentage(PropertyID, Layout::NodeWithStyle const&, ClampNegativeLengths) const; LengthBox length_box(PropertyID left_id, PropertyID top_id, PropertyID right_id, PropertyID bottom_id, Layout::NodeWithStyle const&, ClampNegativeLengths, Length const& default_value) const; Color color_or_fallback(PropertyID, ColorResolutionContext, Color fallback) const;