summaryrefslogtreecommitdiff
path: root/Libraries/LibHTML/Parser/CSSParser.cpp
blob: 518ac990cf7024fd6bb098bfacfc931d7e785d88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <LibHTML/CSS/StyleSheet.h>
#include <LibHTML/Parser/CSSParser.h>
#include <ctype.h>
#include <stdio.h>

static Optional<Color> parse_css_color(const StringView& view)
{
    auto color = Color::from_string(view);
    if (color.has_value())
        return color;

    // FIXME: Parse all valid color strings :^)
    return {};
}

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 == "inherit")
        return InheritStyleValue::create();
    if (string == "initial")
        return InitialStyleValue::create();
    if (string == "auto")
        return LengthStyleValue::create(Length());

    auto color = parse_css_color(view);
    if (color.has_value())
        return ColorStyleValue::create(color.value());

    return StringStyleValue::create(string);
}

NonnullRefPtr<StyleSheet> parse_css(const String& css)
{
    NonnullRefPtrVector<StyleRule> rules;

    enum class State {
        Free,
        InSelectorComponent,
        InPropertyName,
        InPropertyValue,
    };

    struct CurrentRule {
        Vector<Selector> selectors;
        NonnullRefPtrVector<StyleDeclaration> declarations;
    };

    CurrentRule current_rule;
    Vector<char> buffer;

    int index = 0;

    auto peek = [&]() -> char {
        if (index < css.length())
            return css[index];
        return 0;
    };

    auto consume_specific = [&](char ch) {
        ASSERT(peek() == ch);
        ++index;
        return ch;
    };

    auto consume_one = [&]() -> char {
        return css[index++];
    };

    auto consume_whitespace = [&] {
        while (isspace(peek()))
            ++index;
    };

    auto is_valid_selector_char = [](char ch) {
        return isalnum(ch) || ch == '-' || ch == '_';
    };

    auto parse_selector = [&] {
        consume_whitespace();
        Selector::Component::Type type;
        if (peek() == '.')
            type = Selector::Component::Type::Class;
        else if (peek() == '#')
            type = Selector::Component::Type::Id;
        else
            type = Selector::Component::Type::TagName;

        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, component_string });
        buffer.clear();
        current_rule.selectors.append(Selector(move(components)));
    };

    auto parse_selector_list = [&] {
        for (;;) {
            parse_selector();
            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, parse_css_value(property_value)));
    };

    auto parse_declarations = [&] {
        for (;;) {
            parse_declaration();
            consume_whitespace();
            if (peek() == '}')
                break;
        }
    };

    auto parse_rule = [&] {
        parse_selector_list();
        consume_specific('{');
        parse_declarations();
        consume_specific('}');
        rules.append(StyleRule::create(move(current_rule.selectors), move(current_rule.declarations)));
    };

    while (index < css.length()) {
        parse_rule();
    }

    return StyleSheet::create(move(rules));
}