summaryrefslogtreecommitdiff
path: root/Libraries/LibHTML/CSS/StyledNode.h
blob: 1681b6c9c76537200ba1b3f2160231de48fb45ef (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
#pragma once

#include <AK/String.h>
#include <AK/HashMap.h>
#include <AK/NonnullRefPtr.h>
#include <AK/Optional.h>
#include <LibHTML/CSS/StyleValue.h>
#include <LibHTML/TreeNode.h>

class Node;

enum class Display {
    None,
    Block,
    Inline,
};

class StyledNode : public TreeNode<StyledNode> {
public:
    static NonnullRefPtr<StyledNode> create(const Node& node)
    {
        return adopt(*new StyledNode(&node));
    }
    ~StyledNode();

    const Node* node() const { return m_node; }

    template<typename Callback>
    inline void for_each_child(Callback callback) const
    {
        for (auto* node = first_child(); node; node = node->next_sibling())
            callback(*node);
    }

    template<typename Callback>
    inline void for_each_child(Callback callback)
    {
        for (auto* node = first_child(); node; node = node->next_sibling())
            callback(*node);
    }

    template<typename Callback>
    inline void for_each_property(Callback callback) const
    {
        for (auto& it : m_property_values)
            callback(it.key, *it.value);
    }

    void set_property(const String& name, NonnullRefPtr<StyleValue> value)
    {
        m_property_values.set(name, move(value));
    }

    Optional<NonnullRefPtr<StyleValue>> property(const String& name) const
    {
        auto it = m_property_values.find(name);
        if (it == m_property_values.end())
            return {};
        return it->value;
    }

    Display display() const;

    Length length_or_fallback(const StringView& property_name, const Length& fallback)
    {
        auto value = property(property_name);
        if (!value.has_value())
            return fallback;
        return value.value()->to_length();
    }

protected:
    explicit StyledNode(const Node*);

private:
    const Node* m_node { nullptr };
    HashMap<String, NonnullRefPtr<StyleValue>> m_property_values;
};