1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 08:57:34 +00:00

LibWeb: Start fleshing out support for relative CSS units

This patch introduces support for more than just "absolute px" units in
our Length class. It now also supports "em" and "rem", which are units
relative to the font-size of the current layout node and the <html>
element's layout node respectively.
This commit is contained in:
Andreas Kling 2020-06-07 17:55:46 +02:00
parent d93bf78346
commit 731685468a
16 changed files with 163 additions and 92 deletions

View file

@ -27,6 +27,7 @@
#pragma once
#include <AK/String.h>
#include <LibWeb/Forward.h>
namespace Web {
@ -34,10 +35,12 @@ class Length {
public:
enum class Type {
Auto,
Absolute,
Px,
Em,
Rem,
};
Length() {}
Length() { }
Length(int value, Type type)
: m_type(type)
, m_value(value)
@ -48,28 +51,24 @@ public:
, m_value(value)
{
}
~Length() {}
bool is_auto() const { return m_type == Type::Auto; }
bool is_absolute() const { return m_type == Type::Absolute; }
bool is_absolute() const { return m_type == Type::Px; }
bool is_relative() const { return m_type == Type::Em || m_type == Type::Rem; }
float value() const { return m_value; }
float raw_value() const { return m_value; }
float to_px(const LayoutNode&) const;
String to_string() const
{
if (is_auto())
return "[Length/auto]";
return String::format("%g [Length/px]", m_value);
}
float to_px() const
{
if (is_auto())
return 0;
return m_value;
return "[auto]";
return String::format("[%g %s]", m_value, unit_name());
}
private:
const char* unit_name() const;
Type m_type { Type::Auto };
float m_value { 0 };
};