diff --git a/LibHTML/CSS/Length.h b/LibHTML/CSS/Length.h
index d0483c6fdf1..0dc8c5a54ae 100644
--- a/LibHTML/CSS/Length.h
+++ b/LibHTML/CSS/Length.h
@@ -1,5 +1,7 @@
#pragma once
+#include
+
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 };
diff --git a/LibHTML/CSS/StyleValue.cpp b/LibHTML/CSS/StyleValue.cpp
index 4549613bcbe..1d19d602f95 100644
--- a/LibHTML/CSS/StyleValue.cpp
+++ b/LibHTML/CSS/StyleValue.cpp
@@ -11,5 +11,15 @@ StyleValue::~StyleValue()
NonnullRefPtr 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));
}
diff --git a/LibHTML/CSS/StyleValue.h b/LibHTML/CSS/StyleValue.h
index 3fc1930d42d..e195ce05940 100644
--- a/LibHTML/CSS/StyleValue.h
+++ b/LibHTML/CSS/StyleValue.h
@@ -4,6 +4,7 @@
#include
#include
#include
+#include
class StyleValue : public RefCounted {
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;
+};