From 8ac2b30de7550ae42a4660802dc9e5ec1223ac70 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 3 Jul 2019 07:55:22 +0200 Subject: [PATCH] LibHTML: Add LengthStyleValue and create those when parsing number values. --- LibHTML/CSS/Length.h | 9 +++++++++ LibHTML/CSS/StyleValue.cpp | 12 +++++++++++- LibHTML/CSS/StyleValue.h | 29 +++++++++++++++++++++++------ 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/LibHTML/CSS/Length.h b/LibHTML/CSS/Length.h index d0483c6fdf..0dc8c5a54a 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 4549613bcb..1d19d602f9 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 3fc1930d42..e195ce0594 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; +};