Skip to content

Commit

Permalink
LibHTML: Add LengthStyleValue and create those when parsing number va…
Browse files Browse the repository at this point in the history
…lues.
  • Loading branch information
awesomekling committed Jul 3, 2019
1 parent 4b82ac3 commit 8ac2b30
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
9 changes: 9 additions & 0 deletions LibHTML/CSS/Length.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <AK/AKString.h>

class Length {
public:
enum class Type {
Expand All @@ -20,6 +22,13 @@ class Length {

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 };
Expand Down
12 changes: 11 additions & 1 deletion LibHTML/CSS/StyleValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
29 changes: 23 additions & 6 deletions LibHTML/CSS/StyleValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/StringView.h>
#include <LibHTML/CSS/Length.h>

class StyleValue : public RefCounted<StyleValue> {
public:
static NonnullRefPtr<StyleValue> parse(const StringView&);

virtual ~StyleValue();

enum Type {
enum class Type {
Invalid,
Inherit,
Initial,
Primitive,
String,
Length,
};

Type type() const { return m_type; }
Expand All @@ -29,11 +31,11 @@ class StyleValue : public RefCounted<StyleValue> {
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)
{
}
Expand All @@ -43,3 +45,18 @@ class PrimitiveStyleValue : public StyleValue {
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;
};

0 comments on commit 8ac2b30

Please sign in to comment.