mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
LibHTML: Implement enough of the CSS parser to parse the default stylesheet.
This commit is contained in:
parent
7e1cb86da7
commit
4573eb226e
Notes:
sideshowbarker
2024-07-19 13:30:50 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/4573eb226ed
3 changed files with 71 additions and 5 deletions
|
@ -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));
|
||||
}
|
|
@ -1,9 +1,14 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/AKString.h>
|
||||
#include <AK/RefCounted.h>
|
||||
#include <AK/RefPtr.h>
|
||||
#include <AK/StringView.h>
|
||||
|
||||
class StyleValue : public RefCounted<StyleValue> {
|
||||
public:
|
||||
static NonnullRefPtr<StyleValue> parse(const StringView&);
|
||||
|
||||
virtual ~StyleValue();
|
||||
|
||||
enum Type {
|
||||
|
@ -21,3 +26,18 @@ protected:
|
|||
private:
|
||||
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;
|
||||
};
|
||||
|
|
|
@ -16,7 +16,7 @@ NonnullRefPtr<StyleSheet> parse_css(const String& css)
|
|||
|
||||
struct CurrentRule {
|
||||
Vector<Selector> selectors;
|
||||
Vector<StyleDeclaration> declarations;
|
||||
Vector<NonnullRefPtr<StyleDeclaration>> declarations;
|
||||
};
|
||||
|
||||
CurrentRule current_rule;
|
||||
|
@ -25,8 +25,8 @@ NonnullRefPtr<StyleSheet> parse_css(const String& css)
|
|||
int index = 0;
|
||||
|
||||
auto peek = [&]() -> char {
|
||||
if (index + 1 < css.length())
|
||||
return css[index + 1];
|
||||
if (index < css.length())
|
||||
return css[index];
|
||||
return 0;
|
||||
};
|
||||
|
||||
|
@ -62,8 +62,12 @@ NonnullRefPtr<StyleSheet> parse_css(const String& css)
|
|||
while (is_valid_selector_char(peek()))
|
||||
buffer.append(consume_one());
|
||||
|
||||
ASSERT(!buffer.is_null());
|
||||
|
||||
auto component_string = String::copy(buffer);
|
||||
|
||||
Vector<Selector::Component> components;
|
||||
components.append({ type, String::copy(buffer) });
|
||||
components.append({ type, component_string });
|
||||
buffer.clear();
|
||||
current_rule.selectors.append(Selector(move(components)));
|
||||
};
|
||||
|
@ -71,20 +75,46 @@ NonnullRefPtr<StyleSheet> parse_css(const String& css)
|
|||
auto parse_selector_list = [&] {
|
||||
for (;;) {
|
||||
parse_selector();
|
||||
if (peek() == ',')
|
||||
consume_whitespace();
|
||||
if (peek() == ',') {
|
||||
consume_one();
|
||||
continue;
|
||||
}
|
||||
if (peek() == '{')
|
||||
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 = [&] {
|
||||
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 = [&] {
|
||||
for (;;) {
|
||||
parse_declaration();
|
||||
consume_whitespace();
|
||||
if (peek() == '}')
|
||||
break;
|
||||
}
|
||||
|
@ -95,6 +125,7 @@ NonnullRefPtr<StyleSheet> parse_css(const String& css)
|
|||
consume_specific('{');
|
||||
parse_declarations();
|
||||
consume_specific('}');
|
||||
rules.append(StyleRule::create(move(current_rule.selectors), move(current_rule.declarations)));
|
||||
};
|
||||
|
||||
while (index < css.length()) {
|
||||
|
|
Loading…
Reference in a new issue