Serenity Operating System
1/*
2 * Copyright (c) 2020, Luke Wilde <lukew@serenityos.org>
3 * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8#include <LibWeb/CSS/Parser/Parser.h>
9#include <LibWeb/CSS/SelectorEngine.h>
10#include <LibWeb/DOM/Document.h>
11#include <LibWeb/DOM/HTMLCollection.h>
12#include <LibWeb/DOM/NodeOperations.h>
13#include <LibWeb/DOM/ParentNode.h>
14#include <LibWeb/DOM/StaticNodeList.h>
15#include <LibWeb/Dump.h>
16#include <LibWeb/Namespace.h>
17
18namespace Web::DOM {
19
20WebIDL::ExceptionOr<JS::GCPtr<Element>> ParentNode::query_selector(StringView selector_text)
21{
22 auto maybe_selectors = parse_selector(CSS::Parser::ParsingContext(*this), selector_text);
23 if (!maybe_selectors.has_value())
24 return WebIDL::SyntaxError::create(realm(), "Failed to parse selector");
25
26 auto selectors = maybe_selectors.value();
27
28 JS::GCPtr<Element> result;
29 // FIXME: This should be shadow-including. https://drafts.csswg.org/selectors-4/#match-a-selector-against-a-tree
30 for_each_in_subtree_of_type<Element>([&](auto& element) {
31 for (auto& selector : selectors) {
32 if (SelectorEngine::matches(selector, element)) {
33 result = &element;
34 return IterationDecision::Break;
35 }
36 }
37 return IterationDecision::Continue;
38 });
39
40 return result;
41}
42
43WebIDL::ExceptionOr<JS::NonnullGCPtr<NodeList>> ParentNode::query_selector_all(StringView selector_text)
44{
45 auto maybe_selectors = parse_selector(CSS::Parser::ParsingContext(*this), selector_text);
46 if (!maybe_selectors.has_value())
47 return WebIDL::SyntaxError::create(realm(), "Failed to parse selector");
48
49 auto selectors = maybe_selectors.value();
50
51 Vector<JS::Handle<Node>> elements;
52 // FIXME: This should be shadow-including. https://drafts.csswg.org/selectors-4/#match-a-selector-against-a-tree
53 for_each_in_subtree_of_type<Element>([&](auto& element) {
54 for (auto& selector : selectors) {
55 if (SelectorEngine::matches(selector, element)) {
56 elements.append(&element);
57 }
58 }
59 return IterationDecision::Continue;
60 });
61
62 return StaticNodeList::create(realm(), move(elements));
63}
64
65JS::GCPtr<Element> ParentNode::first_element_child()
66{
67 return first_child_of_type<Element>();
68}
69
70JS::GCPtr<Element> ParentNode::last_element_child()
71{
72 return last_child_of_type<Element>();
73}
74
75// https://dom.spec.whatwg.org/#dom-parentnode-childelementcount
76u32 ParentNode::child_element_count() const
77{
78 u32 count = 0;
79 for (auto* child = first_child(); child; child = child->next_sibling()) {
80 if (is<Element>(child))
81 ++count;
82 }
83 return count;
84}
85
86void ParentNode::visit_edges(Cell::Visitor& visitor)
87{
88 Base::visit_edges(visitor);
89 visitor.visit(m_children);
90}
91
92// https://dom.spec.whatwg.org/#dom-parentnode-children
93JS::NonnullGCPtr<HTMLCollection> ParentNode::children()
94{
95 // The children getter steps are to return an HTMLCollection collection rooted at this matching only element children.
96 if (!m_children) {
97 m_children = HTMLCollection::create(*this, [this](Element const& element) {
98 return is_parent_of(element);
99 }).release_value_but_fixme_should_propagate_errors();
100 }
101 return *m_children;
102}
103
104// https://dom.spec.whatwg.org/#concept-getelementsbytagname
105// NOTE: This method is only exposed on Document and Element, but is in ParentNode to prevent code duplication.
106JS::NonnullGCPtr<HTMLCollection> ParentNode::get_elements_by_tag_name(DeprecatedFlyString const& qualified_name)
107{
108 // 1. If qualifiedName is "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches only descendant elements.
109 if (qualified_name == "*") {
110 return HTMLCollection::create(*this, [](Element const&) {
111 return true;
112 }).release_value_but_fixme_should_propagate_errors();
113 }
114
115 // 2. Otherwise, if root’s node document is an HTML document, return a HTMLCollection rooted at root, whose filter matches the following descendant elements:
116 if (root().document().document_type() == Document::Type::HTML) {
117 return HTMLCollection::create(*this, [qualified_name](Element const& element) {
118 // - Whose namespace is the HTML namespace and whose qualified name is qualifiedName, in ASCII lowercase.
119 if (element.namespace_() == Namespace::HTML)
120 return element.qualified_name().to_lowercase() == qualified_name.to_lowercase();
121
122 // - Whose namespace is not the HTML namespace and whose qualified name is qualifiedName.
123 return element.qualified_name() == qualified_name;
124 }).release_value_but_fixme_should_propagate_errors();
125 }
126
127 // 3. Otherwise, return a HTMLCollection rooted at root, whose filter matches descendant elements whose qualified name is qualifiedName.
128 return HTMLCollection::create(*this, [qualified_name](Element const& element) {
129 return element.qualified_name() == qualified_name;
130 }).release_value_but_fixme_should_propagate_errors();
131}
132
133// https://dom.spec.whatwg.org/#concept-getelementsbytagnamens
134// NOTE: This method is only exposed on Document and Element, but is in ParentNode to prevent code duplication.
135JS::NonnullGCPtr<HTMLCollection> ParentNode::get_elements_by_tag_name_ns(DeprecatedFlyString const& nullable_namespace, DeprecatedFlyString const& local_name)
136{
137 // 1. If namespace is the empty string, set it to null.
138 DeprecatedString namespace_ = nullable_namespace;
139 if (namespace_.is_empty())
140 namespace_ = {};
141
142 // 2. If both namespace and localName are "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches descendant elements.
143 if (namespace_ == "*" && local_name == "*") {
144 return HTMLCollection::create(*this, [](Element const&) {
145 return true;
146 }).release_value_but_fixme_should_propagate_errors();
147 }
148
149 // 3. Otherwise, if namespace is "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches descendant elements whose local name is localName.
150 if (namespace_ == "*") {
151 return HTMLCollection::create(*this, [local_name](Element const& element) {
152 return element.local_name() == local_name;
153 }).release_value_but_fixme_should_propagate_errors();
154 }
155
156 // 4. Otherwise, if localName is "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches descendant elements whose namespace is namespace.
157 if (local_name == "*") {
158 return HTMLCollection::create(*this, [namespace_](Element const& element) {
159 return element.namespace_() == namespace_;
160 }).release_value_but_fixme_should_propagate_errors();
161 }
162
163 // 5. Otherwise, return a HTMLCollection rooted at root, whose filter matches descendant elements whose namespace is namespace and local name is localName.
164 return HTMLCollection::create(*this, [namespace_, local_name](Element const& element) {
165 return element.namespace_() == namespace_ && element.local_name() == local_name;
166 }).release_value_but_fixme_should_propagate_errors();
167}
168
169// https://dom.spec.whatwg.org/#dom-parentnode-prepend
170WebIDL::ExceptionOr<void> ParentNode::prepend(Vector<Variant<JS::Handle<Node>, DeprecatedString>> const& nodes)
171{
172 // 1. Let node be the result of converting nodes into a node given nodes and this’s node document.
173 auto node = TRY(convert_nodes_to_single_node(nodes, document()));
174
175 // 2. Pre-insert node into this before this’s first child.
176 (void)TRY(pre_insert(node, first_child()));
177
178 return {};
179}
180
181WebIDL::ExceptionOr<void> ParentNode::append(Vector<Variant<JS::Handle<Node>, DeprecatedString>> const& nodes)
182{
183 // 1. Let node be the result of converting nodes into a node given nodes and this’s node document.
184 auto node = TRY(convert_nodes_to_single_node(nodes, document()));
185
186 // 2. Append node to this.
187 (void)TRY(append_child(node));
188
189 return {};
190}
191
192WebIDL::ExceptionOr<void> ParentNode::replace_children(Vector<Variant<JS::Handle<Node>, DeprecatedString>> const& nodes)
193{
194 // 1. Let node be the result of converting nodes into a node given nodes and this’s node document.
195 auto node = TRY(convert_nodes_to_single_node(nodes, document()));
196
197 // 2. Ensure pre-insertion validity of node into this before null.
198 TRY(ensure_pre_insertion_validity(node, nullptr));
199
200 // 3. Replace all with node within this.
201 replace_all(*node);
202 return {};
203}
204
205}