1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:38:11 +00:00

LibHTML: Add LengthStyleValue and create those when parsing number values.

This commit is contained in:
Andreas Kling 2019-07-03 07:55:22 +02:00
parent 4b82ac3c8e
commit 8ac2b30de7
3 changed files with 43 additions and 7 deletions

View file

@ -1,5 +1,7 @@
#pragma once
#include <AK/AKString.h>
class Length {
public:
enum class Type {
@ -20,6 +22,13 @@ public:
int value() const { return m_value; }
String to_string() const
{
if (is_auto())
return "auto";
return String::format("%d [Length/Absolute]", m_value);
}
private:
Type m_type { Type::Auto };
int m_value { 0 };

View file

@ -11,5 +11,15 @@ StyleValue::~StyleValue()
NonnullRefPtr<StyleValue> StyleValue::parse(const StringView& str)
{
return adopt(*new PrimitiveStyleValue(str));
String string(str);
bool ok;
int as_int = string.to_int(ok);
if (ok)
return adopt(*new LengthStyleValue(Length(as_int, Length::Type::Absolute)));
unsigned as_uint = string.to_uint(ok);
if (ok)
return adopt(*new LengthStyleValue(Length(as_uint, Length::Type::Absolute)));
if (string == "auto")
return adopt(*new LengthStyleValue(Length()));
return adopt(*new StringStyleValue(str));
}

View file

@ -4,6 +4,7 @@
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/StringView.h>
#include <LibHTML/CSS/Length.h>
class StyleValue : public RefCounted<StyleValue> {
public:
@ -11,11 +12,12 @@ public:
virtual ~StyleValue();
enum Type {
enum class Type {
Invalid,
Inherit,
Initial,
Primitive,
String,
Length,
};
Type type() const { return m_type; }
@ -29,11 +31,11 @@ private:
Type m_type { Type::Invalid };
};
class PrimitiveStyleValue : public StyleValue {
class StringStyleValue : public StyleValue {
public:
virtual ~PrimitiveStyleValue() override {}
PrimitiveStyleValue(const String& string)
: StyleValue(Type::Primitive)
virtual ~StringStyleValue() override {}
StringStyleValue(const String& string)
: StyleValue(Type::String)
, m_string(string)
{
}
@ -43,3 +45,18 @@ public:
private:
String m_string;
};
class LengthStyleValue : public StyleValue {
public:
virtual ~LengthStyleValue() override {}
LengthStyleValue(const Length& length)
: StyleValue(Type::Length)
, m_length(length)
{
}
String to_string() const override { return m_length.to_string(); }
private:
Length m_length;
};