summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/DOM/HTMLCollection.cpp
blob: 86b26a087c26859c5cbdf1b6a431f153110c2d0c (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
/*
 * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibWeb/DOM/Element.h>
#include <LibWeb/DOM/HTMLCollection.h>
#include <LibWeb/DOM/ParentNode.h>

namespace Web::DOM {

HTMLCollection::HTMLCollection(ParentNode& root, Function<bool(Element const&)> filter)
    : m_root(root)
    , m_filter(move(filter))
{
}

HTMLCollection::~HTMLCollection()
{
}

Vector<NonnullRefPtr<Element>> HTMLCollection::collect_matching_elements()
{
    Vector<NonnullRefPtr<Element>> elements;
    m_root->for_each_in_inclusive_subtree_of_type<Element>([&](auto& element) {
        if (m_filter(element))
            elements.append(element);
        return IterationDecision::Continue;
    });
    return elements;
}

size_t HTMLCollection::length()
{
    return collect_matching_elements().size();
}

Element* HTMLCollection::item(size_t index)
{
    auto elements = collect_matching_elements();
    if (index >= elements.size())
        return nullptr;
    return elements[index];
}

Element* HTMLCollection::named_item(FlyString const& name)
{
    if (name.is_null())
        return nullptr;
    auto elements = collect_matching_elements();
    // First look for an "id" attribute match
    if (auto it = elements.find_if([&](auto& entry) { return entry->attribute(HTML::AttributeNames::id) == name; }); it != elements.end())
        return *it;
    // Then look for a "name" attribute match
    if (auto it = elements.find_if([&](auto& entry) { return entry->name() == name; }); it != elements.end())
        return *it;
    return nullptr;
}

}