1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:17:36 +00:00

LibWeb: Add an "undefined" state to Length

A default-constructed Length now gives you an undefined length value,
which can be used to signify the absence of a value.
This commit is contained in:
Andreas Kling 2020-06-24 11:24:00 +02:00
parent f742b245b7
commit 90edaabc4b
2 changed files with 7 additions and 1 deletions

View file

@ -53,6 +53,8 @@ const char* Length::unit_name() const
return "rem"; return "rem";
case Type::Auto: case Type::Auto:
return "auto"; return "auto";
case Type::Undefined:
return "undefined";
} }
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
} }

View file

@ -34,12 +34,14 @@ namespace Web {
class Length { class Length {
public: public:
enum class Type { enum class Type {
Undefined,
Auto, Auto,
Px, Px,
Em, Em,
Rem, Rem,
}; };
Length() { }
Length(int value, Type type) Length(int value, Type type)
: m_type(type) : m_type(type)
, m_value(value) , m_value(value)
@ -54,6 +56,7 @@ public:
static Length make_auto() { return Length(0, Type::Auto); } static Length make_auto() { return Length(0, Type::Auto); }
static Length make_px(float value) { return Length(value, Type::Px); } static Length make_px(float value) { return Length(value, Type::Px); }
bool is_undefined() const { return m_type == Type::Undefined; }
bool is_auto() const { return m_type == Type::Auto; } bool is_auto() const { return m_type == Type::Auto; }
bool is_absolute() const { return m_type == Type::Px; } bool is_absolute() const { return m_type == Type::Px; }
bool is_relative() const { return m_type == Type::Em || m_type == Type::Rem; } bool is_relative() const { return m_type == Type::Em || m_type == Type::Rem; }
@ -68,6 +71,7 @@ public:
return 0; return 0;
case Type::Px: case Type::Px:
return m_value; return m_value;
case Type::Undefined:
default: default:
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
} }
@ -85,7 +89,7 @@ private:
const char* unit_name() const; const char* unit_name() const;
Type m_type { Type::Auto }; Type m_type { Type::Undefined };
float m_value { 0 }; float m_value { 0 };
}; };