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

LibWeb: Add basic support for CSS percentages

Many properties can now have percentage values that get resolved in
layout. The reference value (what is this a percentage *of*?) differs
per property, so I've added a helper where you provide a reference
value as an added parameter to the existing length_or_fallback().
This commit is contained in:
Andreas Kling 2020-05-11 23:04:59 +02:00
parent 8ffdcce0d0
commit 5f9d80d8bc
7 changed files with 117 additions and 31 deletions

View file

@ -70,6 +70,7 @@ public:
Initial,
String,
Length,
Percentage,
Color,
Identifier,
Image,
@ -85,6 +86,7 @@ public:
bool is_image() const { return type() == Type::Image; }
bool is_string() const { return type() == Type::String; }
bool is_length() const { return type() == Type::Length; }
bool is_percentage() const { return type() == Type::Percentage; }
bool is_position() const { return type() == Type::Position; }
virtual String to_string() const = 0;
@ -145,6 +147,31 @@ private:
Length m_length;
};
class PercentageStyleValue : public StyleValue {
public:
static NonnullRefPtr<PercentageStyleValue> create(float percentage)
{
return adopt(*new PercentageStyleValue(percentage));
}
virtual ~PercentageStyleValue() override {}
virtual String to_string() const override { return String::format("%g%%", m_percentage); }
Length to_length(float reference) const { return Length((m_percentage / 100.0f) * reference, Length::Type::Absolute); }
private:
virtual Length to_length() const override { return {}; }
virtual bool is_auto() const override { return false; }
explicit PercentageStyleValue(float percentage)
: StyleValue(Type::Percentage)
, m_percentage(percentage)
{
}
float m_percentage { 0 };
};
class InitialStyleValue final : public StyleValue {
public:
static NonnullRefPtr<InitialStyleValue> create() { return adopt(*new InitialStyleValue); }