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

LibHTML: Move CSS value parsing to CSSParser.

This commit is contained in:
Andreas Kling 2019-07-04 15:49:16 +02:00
parent 55a5c46253
commit 63814ffebf
3 changed files with 34 additions and 26 deletions

View file

@ -8,18 +8,3 @@ StyleValue::StyleValue(Type type)
StyleValue::~StyleValue() StyleValue::~StyleValue()
{ {
} }
NonnullRefPtr<StyleValue> StyleValue::parse(const StringView& 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

@ -8,8 +8,6 @@
class StyleValue : public RefCounted<StyleValue> { class StyleValue : public RefCounted<StyleValue> {
public: public:
static NonnullRefPtr<StyleValue> parse(const StringView&);
virtual ~StyleValue(); virtual ~StyleValue();
enum class Type { enum class Type {
@ -33,30 +31,40 @@ private:
class StringStyleValue : public StyleValue { class StringStyleValue : public StyleValue {
public: public:
static NonnullRefPtr<StringStyleValue> create(const String& string)
{
return adopt(*new StringStyleValue(string));
}
virtual ~StringStyleValue() override {} virtual ~StringStyleValue() override {}
StringStyleValue(const String& string)
String to_string() const override { return m_string; }
private:
explicit StringStyleValue(const String& string)
: StyleValue(Type::String) : StyleValue(Type::String)
, m_string(string) , m_string(string)
{ {
} }
String to_string() const override { return m_string; }
private:
String m_string; String m_string;
}; };
class LengthStyleValue : public StyleValue { class LengthStyleValue : public StyleValue {
public: public:
static NonnullRefPtr<LengthStyleValue> create(const Length& length)
{
return adopt(*new LengthStyleValue(length));
}
virtual ~LengthStyleValue() override {} virtual ~LengthStyleValue() override {}
LengthStyleValue(const Length& length)
String to_string() const override { return m_length.to_string(); }
private:
explicit LengthStyleValue(const Length& length)
: StyleValue(Type::Length) : StyleValue(Type::Length)
, m_length(length) , m_length(length)
{ {
} }
String to_string() const override { return m_length.to_string(); }
private:
Length m_length; Length m_length;
}; };

View file

@ -3,6 +3,21 @@
#include <ctype.h> #include <ctype.h>
#include <stdio.h> #include <stdio.h>
NonnullRefPtr<StyleValue> parse_css_value(const StringView& view)
{
String string(view);
bool ok;
int as_int = string.to_int(ok);
if (ok)
return LengthStyleValue::create(Length(as_int, Length::Type::Absolute));
unsigned as_uint = string.to_uint(ok);
if (ok)
return LengthStyleValue::create(Length(as_uint, Length::Type::Absolute));
if (string == "auto")
return LengthStyleValue::create(Length());
return StringStyleValue::create(string);
}
NonnullRefPtr<StyleSheet> parse_css(const String& css) NonnullRefPtr<StyleSheet> parse_css(const String& css)
{ {
NonnullRefPtrVector<StyleRule> rules; NonnullRefPtrVector<StyleRule> rules;
@ -108,7 +123,7 @@ NonnullRefPtr<StyleSheet> parse_css(const String& css)
auto property_value = String::copy(buffer); auto property_value = String::copy(buffer);
buffer.clear(); buffer.clear();
consume_specific(';'); consume_specific(';');
current_rule.declarations.append(StyleDeclaration::create(property_name, StyleValue::parse(property_value))); current_rule.declarations.append(StyleDeclaration::create(property_name, parse_css_value(property_value)));
}; };
auto parse_declarations = [&] { auto parse_declarations = [&] {