diff --git a/Libraries/LibWeb/CSS/Length.h b/Libraries/LibWeb/CSS/Length.h index 7ec690b08b1..4423271af11 100644 --- a/Libraries/LibWeb/CSS/Length.h +++ b/Libraries/LibWeb/CSS/Length.h @@ -251,6 +251,48 @@ private: double m_value { 0 }; }; +class LengthOrAuto { +public: + LengthOrAuto(Length length) + { + if (length.is_auto()) + m_length = {}; + else + m_length = move(length); + } + + static LengthOrAuto make_auto() { return LengthOrAuto { OptionalNone {} }; } + + bool is_length() const { return m_length.has_value(); } + bool is_auto() const { return !m_length.has_value(); } + + Length const& length() const { return m_length.value(); } + + String to_string(SerializationMode mode = SerializationMode::Normal) const + { + if (is_auto()) + return "auto"_string; + return m_length->to_string(mode); + } + + CSSPixels to_px_or_zero(Layout::Node const& node) const + { + if (is_auto()) + return 0; + return m_length->to_px(node); + } + + bool operator==(LengthOrAuto const&) const = default; + +private: + explicit LengthOrAuto(Optional maybe_length) + : m_length(move(maybe_length)) + { + } + + Optional m_length; +}; + } template<> @@ -260,3 +302,11 @@ struct AK::Formatter : Formatter { return Formatter::format(builder, length.to_string()); } }; + +template<> +struct AK::Formatter : Formatter { + ErrorOr format(FormatBuilder& builder, Web::CSS::LengthOrAuto const& length_or_auto) + { + return Formatter::format(builder, length_or_auto.to_string()); + } +}; diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.h index a5daa91a602..3a7531a6ae8 100644 --- a/Libraries/LibWeb/Forward.h +++ b/Libraries/LibWeb/Forward.h @@ -310,6 +310,7 @@ class InvalidationSet; class KeywordStyleValue; class Length; class LengthBox; +class LengthOrAuto; class LengthOrCalculated; class LengthPercentage; class LengthStyleValue;