summaryrefslogtreecommitdiff
path: root/Libraries/LibHTML/CSS/SelectorEngine.cpp
blob: f33018607d95d5191bcfd4d01620651594a2091b (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
#include <LibHTML/CSS/SelectorEngine.h>
#include <LibHTML/DOM/Document.h>
#include <LibHTML/DOM/Element.h>

namespace SelectorEngine {

static bool matches_hover_pseudo_class(const Element& element)
{
    auto* hovered_node = element.document().hovered_node();
    if (!hovered_node)
        return false;
    if (&element == hovered_node)
        return true;
    return element.is_ancestor_of(*hovered_node);
}

bool matches(const Selector::SimpleSelector& component, const Element& element)
{
    switch (component.pseudo_class) {
    case Selector::SimpleSelector::PseudoClass::None:
        break;
    case Selector::SimpleSelector::PseudoClass::Link:
        if (!element.is_link())
            return false;
        break;
    case Selector::SimpleSelector::PseudoClass::Hover:
        if (!matches_hover_pseudo_class(element))
            return false;
        break;
    }

    switch (component.attribute_match_type) {
    case Selector::SimpleSelector::AttributeMatchType::HasAttribute:
        if (!element.has_attribute(component.attribute_name))
            return false;
        break;
    case Selector::SimpleSelector::AttributeMatchType::ExactValueMatch:
        if (element.attribute(component.attribute_name) != component.attribute_value)
            return false;
        break;
    default:
        break;
    }

    switch (component.type) {
    case Selector::SimpleSelector::Type::Universal:
        return true;
    case Selector::SimpleSelector::Type::Id:
        return component.value == element.attribute("id");
    case Selector::SimpleSelector::Type::Class:
        return element.has_class(component.value);
    case Selector::SimpleSelector::Type::TagName:
        return component.value == element.tag_name();
    default:
        ASSERT_NOT_REACHED();
    }
}

bool matches(const Selector& selector, int component_list_index, const Element& element)
{
    auto& component_list = selector.complex_selectors()[component_list_index];
    for (auto& component : component_list.compound_selector) {
        if (!matches(component, element))
            return false;
    }
    switch (component_list.relation) {
    case Selector::ComplexSelector::Relation::None:
        return true;
    case Selector::ComplexSelector::Relation::Descendant:
        ASSERT(component_list_index != 0);
        for (auto* ancestor = element.parent(); ancestor; ancestor = ancestor->parent()) {
            if (!is<Element>(*ancestor))
                continue;
            if (matches(selector, component_list_index - 1, to<Element>(*ancestor)))
                return true;
        }
        return false;
    case Selector::ComplexSelector::Relation::ImmediateChild:
        ASSERT(component_list_index != 0);
        if (!element.parent() || !is<Element>(*element.parent()))
            return false;
        return matches(selector, component_list_index - 1, to<Element>(*element.parent()));
    case Selector::ComplexSelector::Relation::AdjacentSibling:
        ASSERT(component_list_index != 0);
        if (auto* sibling = element.previous_element_sibling())
            return matches(selector, component_list_index - 1, *sibling);
        return false;
    case Selector::ComplexSelector::Relation::GeneralSibling:
        ASSERT(component_list_index != 0);
        for (auto* sibling = element.previous_element_sibling(); sibling; sibling = sibling->previous_element_sibling()) {
            if (matches(selector, component_list_index - 1, *sibling))
                return true;
        }
        return false;
    }
    ASSERT_NOT_REACHED();
}

bool matches(const Selector& selector, const Element& element)
{
    ASSERT(!selector.complex_selectors().is_empty());
    return matches(selector, selector.complex_selectors().size() - 1, element);
}

}