From 60fc23e9165f6d293394e87c25f7ba0ca90fc941 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 28 Aug 2025 12:29:58 +0100 Subject: [PATCH] LibWeb/CSS: Use Optional instead of auto lengths in Size Any type of Size which has no LengthPercentage value now uses an empty optional instead of making an auto Length as before. We also now serialize a `fit-content` Size as `fit-content` instead of `fit-content(auto)`, though this doesn't affect test results and I didn't identify where it's actually used. --- Libraries/LibWeb/CSS/CSSStyleProperties.cpp | 7 ++++-- Libraries/LibWeb/CSS/Size.cpp | 27 ++++++++++++--------- Libraries/LibWeb/CSS/Size.h | 16 ++++++------ 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/Libraries/LibWeb/CSS/CSSStyleProperties.cpp b/Libraries/LibWeb/CSS/CSSStyleProperties.cpp index 8f549c69418..856080c5ead 100644 --- a/Libraries/LibWeb/CSS/CSSStyleProperties.cpp +++ b/Libraries/LibWeb/CSS/CSSStyleProperties.cpp @@ -346,8 +346,11 @@ static NonnullRefPtr style_value_for_size(Size const& size) return KeywordStyleValue::create(Keyword::MinContent); if (size.is_max_content()) return KeywordStyleValue::create(Keyword::MaxContent); - if (size.is_fit_content()) - return FitContentStyleValue::create(size.fit_content_available_space()); + if (size.is_fit_content()) { + if (auto available_space = size.fit_content_available_space(); available_space.has_value()) + return FitContentStyleValue::create(available_space.release_value()); + return FitContentStyleValue::create(); + } TODO(); } diff --git a/Libraries/LibWeb/CSS/Size.cpp b/Libraries/LibWeb/CSS/Size.cpp index 67848ad82ad..1de91309088 100644 --- a/Libraries/LibWeb/CSS/Size.cpp +++ b/Libraries/LibWeb/CSS/Size.cpp @@ -8,7 +8,7 @@ namespace Web::CSS { -Size::Size(Type type, LengthPercentage length_percentage) +Size::Size(Type type, Optional length_percentage) : m_type(type) , m_length_percentage(move(length_percentage)) { @@ -16,17 +16,19 @@ Size::Size(Type type, LengthPercentage length_percentage) CSSPixels Size::to_px(Layout::Node const& node, CSSPixels reference_value) const { - return m_length_percentage.resolved(node, reference_value).to_px(node); + if (!m_length_percentage.has_value()) + return 0; + return m_length_percentage->resolved(node, reference_value).to_px(node); } Size Size::make_auto() { - return Size { Type::Auto, Length::make_auto() }; + return Size { Type::Auto }; } Size Size::make_px(CSSPixels px) { - return make_length(CSS::Length::make_px(px)); + return make_length(Length::make_px(px)); } Size Size::make_length(Length length) @@ -58,12 +60,12 @@ Size Size::make_length_percentage(LengthPercentage const& length_percentage) Size Size::make_min_content() { - return Size { Type::MinContent, Length::make_auto() }; + return Size { Type::MinContent }; } Size Size::make_max_content() { - return Size { Type::MaxContent, Length::make_auto() }; + return Size { Type::MaxContent }; } Size Size::make_fit_content(LengthPercentage available_space) @@ -73,13 +75,12 @@ Size Size::make_fit_content(LengthPercentage available_space) Size Size::make_fit_content() { - // NOTE: We use "auto" as a stand-in for "stretch" here. - return Size { Type::FitContent, Length::make_auto() }; + return Size { Type::FitContent }; } Size Size::make_none() { - return Size { Type::None, Length::make_auto() }; + return Size { Type::None }; } bool Size::contains_percentage() const @@ -95,7 +96,7 @@ bool Size::contains_percentage() const // but we have to update a lot of code to handle this. return false; default: - return m_length_percentage.contains_percentage(); + return m_length_percentage->contains_percentage(); } } @@ -107,13 +108,15 @@ String Size::to_string(SerializationMode mode) const case Type::Calculated: case Type::Length: case Type::Percentage: - return m_length_percentage.to_string(mode); + return m_length_percentage->to_string(mode); case Type::MinContent: return "min-content"_string; case Type::MaxContent: return "max-content"_string; case Type::FitContent: - return MUST(String::formatted("fit-content({})", m_length_percentage.to_string(mode))); + if (!m_length_percentage.has_value()) + return "fit-content"_string; + return MUST(String::formatted("fit-content({})", m_length_percentage->to_string(mode))); case Type::None: return "none"_string; } diff --git a/Libraries/LibWeb/CSS/Size.h b/Libraries/LibWeb/CSS/Size.h index 1cc5a8c6dcc..a66aabf6a2b 100644 --- a/Libraries/LibWeb/CSS/Size.h +++ b/Libraries/LibWeb/CSS/Size.h @@ -55,22 +55,22 @@ public: CalculatedStyleValue const& calculated() const { VERIFY(is_calculated()); - return m_length_percentage.calculated(); + return m_length_percentage->calculated(); } - CSS::Length const& length() const + Length const& length() const { VERIFY(is_length()); - return m_length_percentage.length(); + return m_length_percentage->length(); } - CSS::Percentage const& percentage() const + Percentage const& percentage() const { VERIFY(is_percentage()); - return m_length_percentage.percentage(); + return m_length_percentage->percentage(); } - CSS::LengthPercentage const& fit_content_available_space() const + Optional const& fit_content_available_space() const { VERIFY(is_fit_content()); return m_length_percentage; @@ -80,10 +80,10 @@ public: bool operator==(Size const&) const = default; private: - Size(Type type, LengthPercentage); + explicit Size(Type type, Optional = {}); Type m_type {}; - CSS::LengthPercentage m_length_percentage; + Optional m_length_percentage; }; }