mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-23 17:52:26 -05:00
LibHTML: Move CSS value parsing to CSSParser.
This commit is contained in:
parent
55a5c46253
commit
63814ffebf
Notes:
sideshowbarker
2024-07-19 13:23:28 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/63814ffebf1
3 changed files with 34 additions and 26 deletions
|
@ -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));
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
|
@ -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 = [&] {
|
||||
|
|
Loading…
Add table
Reference in a new issue