From 0c14103025737bd5e033c4198882f5bab13732e3 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 30 Mar 2023 17:13:37 +0100 Subject: [PATCH] LibWeb: Move PercentageOr and subclasses into PercentageOr.{h,cpp} This solves an awkward dependency cycle, where CalculatedStyleValue needs the definition of Percentage, but including that would also pull in PercentageOr, which in turn needs CalculatedStyleValue. Many places that previously included StyleValue.h no longer need to. :^) --- Userland/Libraries/LibWeb/CMakeLists.txt | 2 +- .../Libraries/LibWeb/CSS/ComputedValues.h | 3 +- .../Libraries/LibWeb/CSS/GridTrackSize.cpp | 3 - Userland/Libraries/LibWeb/CSS/LengthBox.cpp | 1 - Userland/Libraries/LibWeb/CSS/LengthBox.h | 2 +- Userland/Libraries/LibWeb/CSS/MediaQuery.h | 3 +- Userland/Libraries/LibWeb/CSS/Percentage.h | 228 ---------------- .../CSS/{Percentage.cpp => PercentageOr.cpp} | 5 +- Userland/Libraries/LibWeb/CSS/PercentageOr.h | 247 ++++++++++++++++++ Userland/Libraries/LibWeb/CSS/Position.cpp | 1 - Userland/Libraries/LibWeb/CSS/Position.h | 2 +- Userland/Libraries/LibWeb/CSS/Resolution.cpp | 1 - Userland/Libraries/LibWeb/CSS/Size.cpp | 2 - Userland/Libraries/LibWeb/CSS/Size.h | 2 +- .../Libraries/LibWeb/CSS/StyleProperties.h | 1 - Userland/Libraries/LibWeb/CSS/StyleValue.cpp | 5 - .../CSS/StyleValues/AbstractImageStyleValue.h | 1 + .../StyleValues/BackgroundSizeStyleValue.h | 2 +- .../CSS/StyleValues/BorderRadiusStyleValue.h | 2 +- .../CSS/StyleValues/ConicGradientStyleValue.h | 1 + .../StyleValues/FilterValueListStyleValue.h | 3 +- .../StyleValues/LinearGradientStyleValue.h | 2 + .../CSS/StyleValues/PositionStyleValue.h | 2 +- .../Libraries/LibWeb/HTML/HTMLBodyElement.cpp | 1 - .../Libraries/LibWeb/HTML/HTMLFontElement.cpp | 1 - .../LibWeb/Painting/GradientPainting.cpp | 1 - 26 files changed, 265 insertions(+), 259 deletions(-) rename Userland/Libraries/LibWeb/CSS/{Percentage.cpp => PercentageOr.cpp} (88%) create mode 100644 Userland/Libraries/LibWeb/CSS/PercentageOr.h diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index f7f07dde622..77c13a040b1 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -51,7 +51,7 @@ set(SOURCES CSS/Parser/Rule.cpp CSS/Parser/Token.cpp CSS/Parser/Tokenizer.cpp - CSS/Percentage.cpp + CSS/PercentageOr.cpp CSS/Position.cpp CSS/PreferredColorScheme.cpp CSS/Ratio.cpp diff --git a/Userland/Libraries/LibWeb/CSS/ComputedValues.h b/Userland/Libraries/LibWeb/CSS/ComputedValues.h index 48bb3e206d2..f742d79bceb 100644 --- a/Userland/Libraries/LibWeb/CSS/ComputedValues.h +++ b/Userland/Libraries/LibWeb/CSS/ComputedValues.h @@ -8,13 +8,14 @@ #include #include +#include #include #include #include #include #include +#include #include -#include #include #include #include diff --git a/Userland/Libraries/LibWeb/CSS/GridTrackSize.cpp b/Userland/Libraries/LibWeb/CSS/GridTrackSize.cpp index 128651f1afa..64396ade9d1 100644 --- a/Userland/Libraries/LibWeb/CSS/GridTrackSize.cpp +++ b/Userland/Libraries/LibWeb/CSS/GridTrackSize.cpp @@ -6,9 +6,6 @@ #include "GridTrackSize.h" #include -#include -#include -#include namespace Web::CSS { diff --git a/Userland/Libraries/LibWeb/CSS/LengthBox.cpp b/Userland/Libraries/LibWeb/CSS/LengthBox.cpp index f6ced27f35f..7d271763e3d 100644 --- a/Userland/Libraries/LibWeb/CSS/LengthBox.cpp +++ b/Userland/Libraries/LibWeb/CSS/LengthBox.cpp @@ -6,7 +6,6 @@ */ #include "LengthBox.h" -#include namespace Web::CSS { diff --git a/Userland/Libraries/LibWeb/CSS/LengthBox.h b/Userland/Libraries/LibWeb/CSS/LengthBox.h index 6d5bdd8e67c..87f6d3cc051 100644 --- a/Userland/Libraries/LibWeb/CSS/LengthBox.h +++ b/Userland/Libraries/LibWeb/CSS/LengthBox.h @@ -6,7 +6,7 @@ #pragma once -#include +#include namespace Web::CSS { diff --git a/Userland/Libraries/LibWeb/CSS/MediaQuery.h b/Userland/Libraries/LibWeb/CSS/MediaQuery.h index 99a95808b69..942b8296b2e 100644 --- a/Userland/Libraries/LibWeb/CSS/MediaQuery.h +++ b/Userland/Libraries/LibWeb/CSS/MediaQuery.h @@ -11,9 +11,10 @@ #include #include #include +#include #include #include -#include +#include namespace Web::CSS { diff --git a/Userland/Libraries/LibWeb/CSS/Percentage.h b/Userland/Libraries/LibWeb/CSS/Percentage.h index 48fbc067f93..6769ccf1ca2 100644 --- a/Userland/Libraries/LibWeb/CSS/Percentage.h +++ b/Userland/Libraries/LibWeb/CSS/Percentage.h @@ -42,232 +42,4 @@ private: float m_value; }; -bool calculated_style_value_contains_percentage(CalculatedStyleValue const&); - -template -class PercentageOr { -public: - PercentageOr(T t) - : m_value(move(t)) - { - } - - PercentageOr(Percentage percentage) - : m_value(move(percentage)) - { - } - - PercentageOr(NonnullRefPtr calculated) - : m_value(move(calculated)) - { - } - - virtual ~PercentageOr() = default; - - PercentageOr& operator=(T t) - { - m_value = move(t); - return *this; - } - - PercentageOr& operator=(Percentage percentage) - { - m_value = move(percentage); - return *this; - } - - bool is_percentage() const { return m_value.template has(); } - bool is_calculated() const { return m_value.template has>(); } - - bool contains_percentage() const - { - return m_value.visit( - [&](T const& t) { - if constexpr (requires { t.is_calculated(); }) { - if (t.is_calculated()) - return calculated_style_value_contains_percentage(*t.calculated_style_value()); - } - return false; - }, - [&](Percentage const&) { - return true; - }, - [&](NonnullRefPtr const& calculated) { - return calculated_style_value_contains_percentage(*calculated); - }); - } - - Percentage const& percentage() const - { - VERIFY(is_percentage()); - return m_value.template get(); - } - - NonnullRefPtr const& calculated() const - { - VERIFY(is_calculated()); - return m_value.template get>(); - } - - virtual T resolve_calculated(NonnullRefPtr const&, [[maybe_unused]] Layout::Node const&, [[maybe_unused]] T const& reference_value) const - { - VERIFY_NOT_REACHED(); - } - - T resolved(Layout::Node const& layout_node, T const& reference_value) const - { - return m_value.visit( - [&](T const& t) { - if constexpr (requires { t.is_calculated(); }) { - if (t.is_calculated()) - return resolve_calculated(t.calculated_style_value(), layout_node, reference_value); - } - - return t; - }, - [&](Percentage const& percentage) { - return reference_value.percentage_of(percentage); - }, - [&](NonnullRefPtr const& calculated) { - return resolve_calculated(calculated, layout_node, reference_value); - }); - } - - ErrorOr to_string() const - { - if (is_percentage()) - return m_value.template get().to_string(); - - return m_value.template get().to_string(); - } - - bool operator==(PercentageOr const& other) const - { - if (is_calculated()) - return false; - if (is_percentage() != other.is_percentage()) - return false; - if (is_percentage()) - return (m_value.template get() == other.m_value.template get()); - return (m_value.template get() == other.m_value.template get()); - } - -protected: - bool is_t() const { return m_value.template has(); } - T const& get_t() const { return m_value.template get(); } - -private: - Variant> m_value; -}; - -template -bool operator==(PercentageOr const& percentage_or, T const& t) -{ - return percentage_or == PercentageOr { t }; } - -template -bool operator==(T const& t, PercentageOr const& percentage_or) -{ - return t == percentage_or; -} - -template -bool operator==(PercentageOr const& percentage_or, Percentage const& percentage) -{ - return percentage_or == PercentageOr { percentage }; -} - -template -bool operator==(Percentage const& percentage, PercentageOr const& percentage_or) -{ - return percentage == percentage_or; -} - -class AnglePercentage : public PercentageOr { -public: - using PercentageOr::PercentageOr; - - bool is_angle() const { return is_t(); } - Angle const& angle() const { return get_t(); } - virtual Angle resolve_calculated(NonnullRefPtr const&, Layout::Node const&, Angle const& reference_value) const override; -}; - -class FrequencyPercentage : public PercentageOr { -public: - using PercentageOr::PercentageOr; - - bool is_frequency() const { return is_t(); } - Frequency const& frequency() const { return get_t(); } - virtual Frequency resolve_calculated(NonnullRefPtr const&, Layout::Node const&, Frequency const& reference_value) const override; -}; - -class LengthPercentage : public PercentageOr { -public: - using PercentageOr::PercentageOr; - - bool is_auto() const { return is_length() && length().is_auto(); } - - bool is_length() const { return is_t(); } - Length const& length() const { return get_t(); } - virtual Length resolve_calculated(NonnullRefPtr const&, Layout::Node const&, Length const& reference_value) const override; -}; - -class TimePercentage : public PercentageOr