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
Notes: sideshowbarker 2024-07-19 13:23:28 +09:00
3 changed files with 34 additions and 26 deletions

View file

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

View file

@ -3,6 +3,21 @@
#include <ctype.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)
{
NonnullRefPtrVector<StyleRule> rules;
@ -108,7 +123,7 @@ NonnullRefPtr<StyleSheet> parse_css(const String& css)
auto property_value = String::copy(buffer);
buffer.clear();
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 = [&] {