1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 21:57:43 +00:00

LibWeb: Move width into LayoutStyle

This patch also adds the ability for Length to contain percentage
values. This is a little off-spec, but will make storing and dealing
with lengths a lot easier.

To resolve a Length to a px-or-auto Length, there are now helpers
for that. After calling them, you no longer have to think about
em, rem, %, and such things.
This commit is contained in:
Andreas Kling 2020-06-24 15:38:21 +02:00
parent 959464fce4
commit ecacab8618
6 changed files with 53 additions and 29 deletions

View file

@ -35,6 +35,7 @@ class Length {
public:
enum class Type {
Undefined,
Percentage,
Auto,
Px,
Em,
@ -56,7 +57,24 @@ public:
static Length make_auto() { return Length(0, Type::Auto); }
static Length make_px(float value) { return Length(value, Type::Px); }
Length resolved(const Length& fallback_for_undefined, const LayoutNode& layout_node, float reference_for_percent) const
{
if (is_undefined())
return fallback_for_undefined;
if (is_percentage())
return make_px(raw_value() / 100.0 * reference_for_percent);
if (is_relative())
return make_px(to_px(layout_node));
return *this;
}
Length resolved_or_auto(const LayoutNode& layout_node, float reference_for_percent) const
{
return resolved(make_auto(), layout_node, reference_for_percent);
}
bool is_undefined() const { return m_type == Type::Undefined; }
bool is_percentage() const { return m_type == Type::Percentage; }
bool is_auto() const { return m_type == Type::Auto; }
bool is_absolute() const { return m_type == Type::Px; }
bool is_relative() const { return m_type == Type::Em || m_type == Type::Rem; }
@ -72,6 +90,7 @@ public:
case Type::Px:
return m_value;
case Type::Undefined:
case Type::Percentage:
default:
ASSERT_NOT_REACHED();
}