mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 09:58:11 +00:00
LibHTML: Add LengthStyleValue and create those when parsing number values.
This commit is contained in:
parent
4b82ac3c8e
commit
8ac2b30de7
3 changed files with 43 additions and 7 deletions
|
@ -1,5 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/AKString.h>
|
||||||
|
|
||||||
class Length {
|
class Length {
|
||||||
public:
|
public:
|
||||||
enum class Type {
|
enum class Type {
|
||||||
|
@ -20,6 +22,13 @@ public:
|
||||||
|
|
||||||
int value() const { return m_value; }
|
int value() const { return m_value; }
|
||||||
|
|
||||||
|
String to_string() const
|
||||||
|
{
|
||||||
|
if (is_auto())
|
||||||
|
return "auto";
|
||||||
|
return String::format("%d [Length/Absolute]", m_value);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Type m_type { Type::Auto };
|
Type m_type { Type::Auto };
|
||||||
int m_value { 0 };
|
int m_value { 0 };
|
||||||
|
|
|
@ -11,5 +11,15 @@ StyleValue::~StyleValue()
|
||||||
|
|
||||||
NonnullRefPtr<StyleValue> StyleValue::parse(const StringView& str)
|
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));
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#include <AK/RefCounted.h>
|
#include <AK/RefCounted.h>
|
||||||
#include <AK/RefPtr.h>
|
#include <AK/RefPtr.h>
|
||||||
#include <AK/StringView.h>
|
#include <AK/StringView.h>
|
||||||
|
#include <LibHTML/CSS/Length.h>
|
||||||
|
|
||||||
class StyleValue : public RefCounted<StyleValue> {
|
class StyleValue : public RefCounted<StyleValue> {
|
||||||
public:
|
public:
|
||||||
|
@ -11,11 +12,12 @@ public:
|
||||||
|
|
||||||
virtual ~StyleValue();
|
virtual ~StyleValue();
|
||||||
|
|
||||||
enum Type {
|
enum class Type {
|
||||||
Invalid,
|
Invalid,
|
||||||
Inherit,
|
Inherit,
|
||||||
Initial,
|
Initial,
|
||||||
Primitive,
|
String,
|
||||||
|
Length,
|
||||||
};
|
};
|
||||||
|
|
||||||
Type type() const { return m_type; }
|
Type type() const { return m_type; }
|
||||||
|
@ -29,11 +31,11 @@ private:
|
||||||
Type m_type { Type::Invalid };
|
Type m_type { Type::Invalid };
|
||||||
};
|
};
|
||||||
|
|
||||||
class PrimitiveStyleValue : public StyleValue {
|
class StringStyleValue : public StyleValue {
|
||||||
public:
|
public:
|
||||||
virtual ~PrimitiveStyleValue() override {}
|
virtual ~StringStyleValue() override {}
|
||||||
PrimitiveStyleValue(const String& string)
|
StringStyleValue(const String& string)
|
||||||
: StyleValue(Type::Primitive)
|
: StyleValue(Type::String)
|
||||||
, m_string(string)
|
, m_string(string)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -43,3 +45,18 @@ public:
|
||||||
private:
|
private:
|
||||||
String m_string;
|
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;
|
||||||
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue