LibWeb/CSS: Introduce a LengthOrAuto type

`clip: rect()` in particular wants this for its parameters.
This commit is contained in:
Sam Atkins 2025-08-28 13:40:39 +01:00
commit f663c0a72d
Notes: github-actions[bot] 2025-09-04 12:34:08 +00:00
2 changed files with 51 additions and 0 deletions

View file

@ -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<Length> maybe_length)
: m_length(move(maybe_length))
{
}
Optional<Length> m_length;
};
}
template<>
@ -260,3 +302,11 @@ struct AK::Formatter<Web::CSS::Length> : Formatter<StringView> {
return Formatter<StringView>::format(builder, length.to_string());
}
};
template<>
struct AK::Formatter<Web::CSS::LengthOrAuto> : Formatter<StringView> {
ErrorOr<void> format(FormatBuilder& builder, Web::CSS::LengthOrAuto const& length_or_auto)
{
return Formatter<StringView>::format(builder, length_or_auto.to_string());
}
};

View file

@ -310,6 +310,7 @@ class InvalidationSet;
class KeywordStyleValue;
class Length;
class LengthBox;
class LengthOrAuto;
class LengthOrCalculated;
class LengthPercentage;
class LengthStyleValue;