Serenity Operating System
1/*
2 * Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
3 * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8#include <LibWeb/Bindings/Intrinsics.h>
9#include <LibWeb/DOM/Element.h>
10#include <LibWeb/DOM/HTMLCollection.h>
11#include <LibWeb/DOM/ParentNode.h>
12#include <LibWeb/Namespace.h>
13
14namespace Web::DOM {
15
16WebIDL::ExceptionOr<JS::NonnullGCPtr<HTMLCollection>> HTMLCollection::create(ParentNode& root, Function<bool(Element const&)> filter)
17{
18 return MUST_OR_THROW_OOM(root.heap().allocate<HTMLCollection>(root.realm(), root, move(filter)));
19}
20
21HTMLCollection::HTMLCollection(ParentNode& root, Function<bool(Element const&)> filter)
22 : LegacyPlatformObject(root.realm())
23 , m_root(root)
24 , m_filter(move(filter))
25{
26}
27
28HTMLCollection::~HTMLCollection() = default;
29
30JS::ThrowCompletionOr<void> HTMLCollection::initialize(JS::Realm& realm)
31{
32 MUST_OR_THROW_OOM(Base::initialize(realm));
33 set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLCollectionPrototype>(realm, "HTMLCollection"));
34
35 return {};
36}
37
38void HTMLCollection::visit_edges(Cell::Visitor& visitor)
39{
40 Base::visit_edges(visitor);
41 visitor.visit(m_root.ptr());
42}
43
44JS::MarkedVector<Element*> HTMLCollection::collect_matching_elements() const
45{
46 JS::MarkedVector<Element*> elements(m_root->heap());
47 m_root->for_each_in_inclusive_subtree_of_type<Element>([&](auto& element) {
48 if (m_filter(element))
49 elements.append(const_cast<Element*>(&element));
50 return IterationDecision::Continue;
51 });
52 return elements;
53}
54
55// https://dom.spec.whatwg.org/#dom-htmlcollection-length
56size_t HTMLCollection::length()
57{
58 // The length getter steps are to return the number of nodes represented by the collection.
59 return collect_matching_elements().size();
60}
61
62// https://dom.spec.whatwg.org/#dom-htmlcollection-item
63Element* HTMLCollection::item(size_t index) const
64{
65 // The item(index) method steps are to return the indexth element in the collection. If there is no indexth element in the collection, then the method must return null.
66 auto elements = collect_matching_elements();
67 if (index >= elements.size())
68 return nullptr;
69 return elements[index];
70}
71
72// https://dom.spec.whatwg.org/#dom-htmlcollection-nameditem-key
73Element* HTMLCollection::named_item(DeprecatedFlyString const& name) const
74{
75 // 1. If key is the empty string, return null.
76 if (name.is_empty())
77 return nullptr;
78 auto elements = collect_matching_elements();
79 // 2. Return the first element in the collection for which at least one of the following is true:
80 // - it has an ID which is key;
81 if (auto it = elements.find_if([&](auto& entry) { return entry->attribute(HTML::AttributeNames::id) == name; }); it != elements.end())
82 return *it;
83 // - it is in the HTML namespace and has a name attribute whose value is key;
84 if (auto it = elements.find_if([&](auto& entry) { return entry->namespace_() == Namespace::HTML && entry->name() == name; }); it != elements.end())
85 return *it;
86 // or null if there is no such element.
87 return nullptr;
88}
89
90// https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-names
91Vector<DeprecatedString> HTMLCollection::supported_property_names() const
92{
93 // 1. Let result be an empty list.
94 Vector<DeprecatedString> result;
95
96 // 2. For each element represented by the collection, in tree order:
97 auto elements = collect_matching_elements();
98
99 for (auto& element : elements) {
100 // 1. If element has an ID which is not in result, append element’s ID to result.
101 if (element->has_attribute(HTML::AttributeNames::id)) {
102 auto id = element->attribute(HTML::AttributeNames::id);
103
104 if (!result.contains_slow(id))
105 result.append(id);
106 }
107
108 // 2. If element is in the HTML namespace and has a name attribute whose value is neither the empty string nor is in result, append element’s name attribute value to result.
109 if (element->namespace_() == Namespace::HTML && element->has_attribute(HTML::AttributeNames::name)) {
110 auto name = element->attribute(HTML::AttributeNames::name);
111
112 if (!name.is_empty() && !result.contains_slow(name))
113 result.append(name);
114 }
115 }
116
117 // 3. Return result.
118 return result;
119}
120
121// https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-indices%E2%91%A1
122bool HTMLCollection::is_supported_property_index(u32 index) const
123{
124 // The object’s supported property indices are the numbers in the range zero to one less than the number of elements represented by the collection.
125 // If there are no such elements, then there are no supported property indices.
126 auto elements = collect_matching_elements();
127 if (elements.is_empty())
128 return false;
129
130 return index < elements.size();
131}
132
133WebIDL::ExceptionOr<JS::Value> HTMLCollection::item_value(size_t index) const
134{
135 auto* element = item(index);
136 if (!element)
137 return JS::js_undefined();
138 return const_cast<Element*>(element);
139}
140
141WebIDL::ExceptionOr<JS::Value> HTMLCollection::named_item_value(DeprecatedFlyString const& index) const
142{
143 auto* element = named_item(index);
144 if (!element)
145 return JS::js_undefined();
146 return const_cast<Element*>(element);
147}
148}