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

LibHTML: Implement enough of the CSS parser to parse the default stylesheet.

This commit is contained in:
Andreas Kling 2019-06-22 21:48:21 +02:00
parent 7e1cb86da7
commit 4573eb226e
3 changed files with 71 additions and 5 deletions

View file

@ -0,0 +1,15 @@
#include "StyleValue.h"
StyleValue::StyleValue(Type type)
: m_type(type)
{
}
StyleValue::~StyleValue()
{
}
NonnullRefPtr<StyleValue> StyleValue::parse(const StringView& str)
{
return adopt(*new PrimitiveStyleValue(str));
}

View file

@ -1,9 +1,14 @@
#pragma once #pragma once
#include <AK/AKString.h>
#include <AK/RefCounted.h> #include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/StringView.h>
class StyleValue : public RefCounted<StyleValue> { class StyleValue : public RefCounted<StyleValue> {
public: public:
static NonnullRefPtr<StyleValue> parse(const StringView&);
virtual ~StyleValue(); virtual ~StyleValue();
enum Type { enum Type {
@ -21,3 +26,18 @@ protected:
private: private:
Type m_type { Type::Invalid }; Type m_type { Type::Invalid };
}; };
class PrimitiveStyleValue : public StyleValue {
public:
virtual ~PrimitiveStyleValue() override {}
PrimitiveStyleValue(const String& string)
: StyleValue(Type::Primitive)
, m_string(string)
{
}
String to_string() const { return m_string; }
private:
String m_string;
};

View file

@ -16,7 +16,7 @@ NonnullRefPtr<StyleSheet> parse_css(const String& css)
struct CurrentRule { struct CurrentRule {
Vector<Selector> selectors; Vector<Selector> selectors;
Vector<StyleDeclaration> declarations; Vector<NonnullRefPtr<StyleDeclaration>> declarations;
}; };
CurrentRule current_rule; CurrentRule current_rule;
@ -25,8 +25,8 @@ NonnullRefPtr<StyleSheet> parse_css(const String& css)
int index = 0; int index = 0;
auto peek = [&]() -> char { auto peek = [&]() -> char {
if (index + 1 < css.length()) if (index < css.length())
return css[index + 1]; return css[index];
return 0; return 0;
}; };
@ -62,8 +62,12 @@ NonnullRefPtr<StyleSheet> parse_css(const String& css)
while (is_valid_selector_char(peek())) while (is_valid_selector_char(peek()))
buffer.append(consume_one()); buffer.append(consume_one());
ASSERT(!buffer.is_null());
auto component_string = String::copy(buffer);
Vector<Selector::Component> components; Vector<Selector::Component> components;
components.append({ type, String::copy(buffer) }); components.append({ type, component_string });
buffer.clear(); buffer.clear();
current_rule.selectors.append(Selector(move(components))); current_rule.selectors.append(Selector(move(components)));
}; };
@ -71,20 +75,46 @@ NonnullRefPtr<StyleSheet> parse_css(const String& css)
auto parse_selector_list = [&] { auto parse_selector_list = [&] {
for (;;) { for (;;) {
parse_selector(); parse_selector();
if (peek() == ',') consume_whitespace();
if (peek() == ',') {
consume_one();
continue; continue;
}
if (peek() == '{') if (peek() == '{')
break; break;
} }
}; };
auto is_valid_property_name_char = [](char ch) {
return !isspace(ch) && ch != ':';
};
auto is_valid_property_value_char = [](char ch) {
return !isspace(ch) && ch != ';';
};
auto parse_declaration = [&] { auto parse_declaration = [&] {
consume_whitespace(); consume_whitespace();
buffer.clear();
while (is_valid_property_name_char(peek()))
buffer.append(consume_one());
auto property_name = String::copy(buffer);
buffer.clear();
consume_whitespace();
consume_specific(':');
consume_whitespace();
while (is_valid_property_value_char(peek()))
buffer.append(consume_one());
auto property_value = String::copy(buffer);
buffer.clear();
consume_specific(';');
current_rule.declarations.append(StyleDeclaration::create(property_name, StyleValue::parse(property_value)));
}; };
auto parse_declarations = [&] { auto parse_declarations = [&] {
for (;;) { for (;;) {
parse_declaration(); parse_declaration();
consume_whitespace();
if (peek() == '}') if (peek() == '}')
break; break;
} }
@ -95,6 +125,7 @@ NonnullRefPtr<StyleSheet> parse_css(const String& css)
consume_specific('{'); consume_specific('{');
parse_declarations(); parse_declarations();
consume_specific('}'); consume_specific('}');
rules.append(StyleRule::create(move(current_rule.selectors), move(current_rule.declarations)));
}; };
while (index < css.length()) { while (index < css.length()) {