From 55ad9591dfbc8896c48d3c85f5b74adced867213 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 14 Sep 2022 11:28:15 +0200 Subject: [PATCH] LibWeb: Add a way to ask if a PercentageOr value contains percentages Values that contain percentages require special treatment in various parts of layout. Previously we had no way of peeking into calc() values to see if their expression contains one or more percentages. That's the bulk of what we're adding here. --- Userland/Libraries/LibWeb/CSS/Percentage.h | 9 ++++ Userland/Libraries/LibWeb/CSS/StyleValue.cpp | 47 ++++++++++++++++++++ Userland/Libraries/LibWeb/CSS/StyleValue.h | 9 ++++ 3 files changed, 65 insertions(+) diff --git a/Userland/Libraries/LibWeb/CSS/Percentage.h b/Userland/Libraries/LibWeb/CSS/Percentage.h index 68bb465728d..e0716407eed 100644 --- a/Userland/Libraries/LibWeb/CSS/Percentage.h +++ b/Userland/Libraries/LibWeb/CSS/Percentage.h @@ -77,6 +77,15 @@ public: bool is_percentage() const { return m_value.template has(); } bool is_calculated() const { return m_value.template has>(); } + bool contains_percentage() const + { + if (is_percentage()) + return true; + if (is_calculated()) + return calculated()->contains_percentage(); + return false; + } + Percentage const& percentage() const { VERIFY(is_percentage()); diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp index 897209b0d3d..b51e5cfd918 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp @@ -2054,4 +2054,51 @@ NonnullRefPtr BorderRadiusStyleValue::absolutized(Gfx::IntRect const return BorderRadiusStyleValue::create(absolutized_horizontal_radius, absolutized_vertical_radius); } +bool CalculatedStyleValue::contains_percentage() const +{ + return m_expression->contains_percentage(); +} + +bool CalculatedStyleValue::CalcSum::contains_percentage() const +{ + if (first_calc_product->contains_percentage()) + return true; + for (auto& part : zero_or_more_additional_calc_products) { + if (part.contains_percentage()) + return true; + } + return false; +} + +bool CalculatedStyleValue::CalcSumPartWithOperator::contains_percentage() const +{ + return value->contains_percentage(); +} + +bool CalculatedStyleValue::CalcProduct::contains_percentage() const +{ + if (first_calc_value.contains_percentage()) + return true; + for (auto& part : zero_or_more_additional_calc_values) { + if (part.contains_percentage()) + return true; + } + return false; +} + +bool CalculatedStyleValue::CalcProductPartWithOperator::contains_percentage() const +{ + return value.visit( + [](CalcValue const& value) { return value.contains_percentage(); }, + [](CalcNumberValue const&) { return false; }); +} + +bool CalculatedStyleValue::CalcValue::contains_percentage() const +{ + return value.visit( + [](Percentage const&) { return true; }, + [](NonnullOwnPtr const& sum) { return sum->contains_percentage(); }, + [](auto const&) { return false; }); +} + } diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValue.h index 5118c7a26a0..ccfe444dbc4 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValue.h +++ b/Userland/Libraries/LibWeb/CSS/StyleValue.h @@ -609,6 +609,7 @@ public: String to_string() const; Optional resolved_type() const; CalculationResult resolve(Layout::Node const*, PercentageBasis const& percentage_basis) const; + bool contains_percentage() const; }; // This represents that: https://www.w3.org/TR/css-values-3/#calc-syntax @@ -623,6 +624,8 @@ public: String to_string() const; Optional resolved_type() const; CalculationResult resolve(Layout::Node const*, PercentageBasis const& percentage_basis) const; + + bool contains_percentage() const; }; struct CalcNumberSum { @@ -645,6 +648,7 @@ public: String to_string() const; Optional resolved_type() const; CalculationResult resolve(Layout::Node const*, PercentageBasis const& percentage_basis) const; + bool contains_percentage() const; }; struct CalcSumPartWithOperator { @@ -658,6 +662,7 @@ public: String to_string() const; Optional resolved_type() const; CalculationResult resolve(Layout::Node const*, PercentageBasis const& percentage_basis) const; + bool contains_percentage() const; }; struct CalcProductPartWithOperator { @@ -667,6 +672,8 @@ public: String to_string() const; Optional resolved_type() const; CalculationResult resolve(Layout::Node const*, PercentageBasis const& percentage_basis) const; + + bool contains_percentage() const; }; struct CalcNumberProduct { @@ -734,6 +741,8 @@ public: Optional resolve_number(); Optional resolve_integer(); + bool contains_percentage() const; + private: explicit CalculatedStyleValue(NonnullOwnPtr calc_sum, ResolvedType resolved_type) : StyleValue(Type::Calculated)