Serenity Operating System
1/*
2 * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <LibWeb/Bindings/Intrinsics.h>
8#include <LibWeb/DOM/Node.h>
9#include <LibWeb/DOM/NodeFilter.h>
10#include <LibWeb/DOM/TreeWalker.h>
11#include <LibWeb/WebIDL/AbstractOperations.h>
12#include <LibWeb/WebIDL/DOMException.h>
13
14namespace Web::DOM {
15
16TreeWalker::TreeWalker(Node& root)
17 : PlatformObject(root.realm())
18 , m_root(root)
19 , m_current(root)
20{
21}
22
23TreeWalker::~TreeWalker() = default;
24
25JS::ThrowCompletionOr<void> TreeWalker::initialize(JS::Realm& realm)
26{
27 MUST_OR_THROW_OOM(Base::initialize(realm));
28 set_prototype(&Bindings::ensure_web_prototype<Bindings::TreeWalkerPrototype>(realm, "TreeWalker"));
29
30 return {};
31}
32
33void TreeWalker::visit_edges(Cell::Visitor& visitor)
34{
35 Base::visit_edges(visitor);
36 visitor.visit(m_filter.ptr());
37 visitor.visit(m_root.ptr());
38 visitor.visit(m_current.ptr());
39}
40
41// https://dom.spec.whatwg.org/#dom-document-createtreewalker
42WebIDL::ExceptionOr<JS::NonnullGCPtr<TreeWalker>> TreeWalker::create(Node& root, unsigned what_to_show, JS::GCPtr<NodeFilter> filter)
43{
44 // 1. Let walker be a new TreeWalker object.
45 // 2. Set walker’s root and walker’s current to root.
46 auto& realm = root.realm();
47 auto walker = MUST_OR_THROW_OOM(realm.heap().allocate<TreeWalker>(realm, root));
48
49 // 3. Set walker’s whatToShow to whatToShow.
50 walker->m_what_to_show = what_to_show;
51
52 // 4. Set walker’s filter to filter.
53 walker->m_filter = filter;
54
55 // 5. Return walker.
56 return walker;
57}
58
59// https://dom.spec.whatwg.org/#dom-treewalker-currentnode
60JS::NonnullGCPtr<Node> TreeWalker::current_node() const
61{
62 return *m_current;
63}
64
65// https://dom.spec.whatwg.org/#dom-treewalker-currentnode
66void TreeWalker::set_current_node(Node& node)
67{
68 m_current = node;
69}
70
71// https://dom.spec.whatwg.org/#dom-treewalker-parentnode
72JS::ThrowCompletionOr<JS::GCPtr<Node>> TreeWalker::parent_node()
73{
74 // 1. Let node be this’s current.
75 JS::GCPtr<Node> node = m_current;
76
77 // 2. While node is non-null and is not this’s root:
78 while (node && node != m_root) {
79 // 1. Set node to node’s parent.
80 node = node->parent();
81
82 // 2. If node is non-null and filtering node within this returns FILTER_ACCEPT,
83 // then set this’s current to node and return node.
84 if (node) {
85 auto result = TRY(filter(*node));
86 if (result == NodeFilter::Result::FILTER_ACCEPT) {
87 m_current = *node;
88 return node;
89 }
90 }
91 }
92
93 return nullptr;
94}
95
96// https://dom.spec.whatwg.org/#dom-treewalker-firstchild
97JS::ThrowCompletionOr<JS::GCPtr<Node>> TreeWalker::first_child()
98{
99 return traverse_children(ChildTraversalType::First);
100}
101
102// https://dom.spec.whatwg.org/#dom-treewalker-lastchild
103JS::ThrowCompletionOr<JS::GCPtr<Node>> TreeWalker::last_child()
104{
105 return traverse_children(ChildTraversalType::Last);
106}
107
108// https://dom.spec.whatwg.org/#dom-treewalker-previoussibling
109JS::ThrowCompletionOr<JS::GCPtr<Node>> TreeWalker::previous_sibling()
110{
111 return traverse_siblings(SiblingTraversalType::Previous);
112}
113
114// https://dom.spec.whatwg.org/#dom-treewalker-nextsibling
115JS::ThrowCompletionOr<JS::GCPtr<Node>> TreeWalker::next_sibling()
116{
117 return traverse_siblings(SiblingTraversalType::Next);
118}
119
120// https://dom.spec.whatwg.org/#dom-treewalker-previousnode
121JS::ThrowCompletionOr<JS::GCPtr<Node>> TreeWalker::previous_node()
122{
123 // 1. Let node be this’s current.
124 JS::NonnullGCPtr<Node> node = m_current;
125
126 // 2. While node is not this’s root:
127 while (node != m_root) {
128 // 1. Let sibling be node’s previous sibling.
129 JS::GCPtr<Node> sibling = node->previous_sibling();
130
131 // 2. While sibling is non-null:
132 while (sibling) {
133 // 1. Set node to sibling.
134 node = *sibling;
135
136 // 2. Let result be the result of filtering node within this.
137 auto result = TRY(filter(*node));
138
139 // 3. While result is not FILTER_REJECT and node has a child:
140 while (result != NodeFilter::Result::FILTER_REJECT && node->has_children()) {
141 // 1. Set node to node’s last child.
142 node = *node->last_child();
143
144 // 2. Set result to the result of filtering node within this.
145 result = TRY(filter(*node));
146 }
147
148 // 4. If result is FILTER_ACCEPT, then set this’s current to node and return node.
149 if (result == NodeFilter::Result::FILTER_ACCEPT) {
150 m_current = node;
151 return node;
152 }
153
154 // 5. Set sibling to node’s previous sibling.
155 sibling = node->previous_sibling();
156 }
157
158 // 3. If node is this’s root or node’s parent is null, then return null.
159 if (node == m_root || !node->parent())
160 return nullptr;
161
162 // 4. Set node to node’s parent.
163 node = *node->parent();
164
165 // 5. If the return value of filtering node within this is FILTER_ACCEPT, then set this’s current to node and return node.
166 if (TRY(filter(*node)) == NodeFilter::Result::FILTER_ACCEPT) {
167 m_current = node;
168 return node;
169 }
170 }
171 // 3. Return null.
172 return nullptr;
173}
174
175// https://dom.spec.whatwg.org/#dom-treewalker-nextnode
176JS::ThrowCompletionOr<JS::GCPtr<Node>> TreeWalker::next_node()
177{
178 // 1. Let node be this’s current.
179 JS::NonnullGCPtr<Node> node = m_current;
180
181 // 2. Let result be FILTER_ACCEPT.
182 auto result = NodeFilter::Result::FILTER_ACCEPT;
183
184 // 3. While true:
185 while (true) {
186 // 1. While result is not FILTER_REJECT and node has a child:
187 while (result != NodeFilter::Result::FILTER_REJECT && node->has_children()) {
188 // 1. Set node to its first child.
189 node = *node->first_child();
190
191 // 2. Set result to the result of filtering node within this.
192 result = TRY(filter(*node));
193
194 // 3. If result is FILTER_ACCEPT, then set this’s current to node and return node.
195 if (result == NodeFilter::Result::FILTER_ACCEPT) {
196 m_current = *node;
197 return node;
198 }
199 }
200
201 // 2. Let sibling be null.
202 JS::GCPtr<Node> sibling = nullptr;
203
204 // 3. Let temporary be node.
205 JS::GCPtr<Node> temporary = node;
206
207 // 4. While temporary is non-null:
208 while (temporary) {
209 // 1. If temporary is this’s root, then return null.
210 if (temporary == m_root)
211 return nullptr;
212
213 // 2. Set sibling to temporary’s next sibling.
214 sibling = temporary->next_sibling();
215
216 // 3. If sibling is non-null, then set node to sibling and break.
217 if (sibling) {
218 node = *sibling;
219 break;
220 }
221
222 // 4. Set temporary to temporary’s parent.
223 temporary = temporary->parent();
224 }
225
226 // 5. Set result to the result of filtering node within this.
227 result = TRY(filter(*node));
228
229 // 6. If result is FILTER_ACCEPT, then set this’s current to node and return node.
230 if (result == NodeFilter::Result::FILTER_ACCEPT) {
231 m_current = *node;
232 return node;
233 }
234 }
235}
236
237// https://dom.spec.whatwg.org/#concept-node-filter
238JS::ThrowCompletionOr<NodeFilter::Result> TreeWalker::filter(Node& node)
239{
240 // 1. If traverser’s active flag is set, then throw an "InvalidStateError" DOMException.
241 if (m_active)
242 return throw_completion(WebIDL::InvalidStateError::create(realm(), "NodeIterator is already active"));
243
244 // 2. Let n be node’s nodeType attribute value − 1.
245 auto n = node.node_type() - 1;
246
247 // 3. If the nth bit (where 0 is the least significant bit) of traverser’s whatToShow is not set, then return FILTER_SKIP.
248 if (!(m_what_to_show & (1u << n)))
249 return NodeFilter::Result::FILTER_SKIP;
250
251 // 4. If traverser’s filter is null, then return FILTER_ACCEPT.
252 if (!m_filter)
253 return NodeFilter::Result::FILTER_ACCEPT;
254
255 // 5. Set traverser’s active flag.
256 m_active = true;
257
258 // 6. Let result be the return value of call a user object’s operation with traverser’s filter, "acceptNode", and « node ».
259 // If this throws an exception, then unset traverser’s active flag and rethrow the exception.
260 auto result = WebIDL::call_user_object_operation(m_filter->callback(), "acceptNode", {}, &node);
261 if (result.is_abrupt()) {
262 m_active = false;
263 return result;
264 }
265
266 // 7. Unset traverser’s active flag.
267 m_active = false;
268
269 // 8. Return result.
270 auto result_value = TRY(result.value()->to_i32(vm()));
271 return static_cast<NodeFilter::Result>(result_value);
272}
273
274// https://dom.spec.whatwg.org/#concept-traverse-children
275JS::ThrowCompletionOr<JS::GCPtr<Node>> TreeWalker::traverse_children(ChildTraversalType type)
276{
277 // 1. Let node be walker’s current.
278 JS::GCPtr<Node> node = m_current;
279
280 // 2. Set node to node’s first child if type is first, and node’s last child if type is last.
281 node = type == ChildTraversalType::First ? node->first_child() : node->last_child();
282
283 // 3. While node is non-null:
284 while (node) {
285 // 1. Let result be the result of filtering node within walker.
286 auto result = TRY(filter(*node));
287
288 // 2. If result is FILTER_ACCEPT, then set walker’s current to node and return node.
289 if (result == NodeFilter::Result::FILTER_ACCEPT) {
290 m_current = *node;
291 return node;
292 }
293
294 // 3. If result is FILTER_SKIP, then:
295 if (result == NodeFilter::Result::FILTER_SKIP) {
296 // 1. Let child be node’s first child if type is first, and node’s last child if type is last.
297 JS::GCPtr<Node> child = type == ChildTraversalType::First ? node->first_child() : node->last_child();
298
299 // 2. If child is non-null, then set node to child and continue.
300 if (child) {
301 node = child;
302 continue;
303 }
304 }
305
306 // 4. While node is non-null:
307 while (node) {
308 // 1. Let sibling be node’s next sibling if type is first, and node’s previous sibling if type is last.
309 JS::GCPtr<Node> sibling = type == ChildTraversalType::First ? node->next_sibling() : node->previous_sibling();
310
311 // 2. If sibling is non-null, then set node to sibling and break.
312 if (sibling) {
313 node = sibling;
314 break;
315 }
316
317 // 3. Let parent be node’s parent.
318 JS::GCPtr<Node> parent = node->parent();
319
320 // 4. If parent is null, walker’s root, or walker’s current, then return null.
321 if (!parent || parent == m_root || parent == m_current)
322 return nullptr;
323
324 // 5. Set node to parent.
325 node = parent;
326 }
327 }
328
329 // 4. Return null.
330 return nullptr;
331}
332
333// https://dom.spec.whatwg.org/#concept-traverse-siblings
334JS::ThrowCompletionOr<JS::GCPtr<Node>> TreeWalker::traverse_siblings(SiblingTraversalType type)
335{
336 // 1. Let node be walker’s current.
337 JS::GCPtr<Node> node = m_current;
338
339 // 2. If node is root, then return null.
340 if (node == m_root)
341 return nullptr;
342
343 // 3. While true:
344 while (true) {
345 // 1. Let sibling be node’s next sibling if type is next, and node’s previous sibling if type is previous.
346 JS::GCPtr<Node> sibling = type == SiblingTraversalType::Next ? node->next_sibling() : node->previous_sibling();
347
348 // 2. While sibling is non-null:
349 while (sibling) {
350 // 1. Set node to sibling.
351 node = sibling;
352
353 // 2. Let result be the result of filtering node within walker.
354 auto result = TRY(filter(*node));
355
356 // 3. If result is FILTER_ACCEPT, then set walker’s current to node and return node.
357 if (result == NodeFilter::Result::FILTER_ACCEPT) {
358 m_current = *node;
359 return node;
360 }
361
362 // 4. Set sibling to node’s first child if type is next, and node’s last child if type is previous.
363 sibling = type == SiblingTraversalType::Next ? node->first_child() : node->last_child();
364
365 // 5. If result is FILTER_REJECT or sibling is null, then set sibling to node’s next sibling if type is next, and node’s previous sibling if type is previous.
366 if (result == NodeFilter::Result::FILTER_REJECT || !sibling)
367 sibling = type == SiblingTraversalType::Next ? node->next_sibling() : node->previous_sibling();
368 }
369
370 // 3. Set node to node’s parent.
371 node = node->parent();
372
373 // 4. If node is null or walker’s root, then return null.
374 if (!node || node == m_root)
375 return nullptr;
376
377 // 5. If the return value of filtering node within walker is FILTER_ACCEPT, then return null.
378 if (TRY(filter(*node)) == NodeFilter::Result::FILTER_ACCEPT)
379 return nullptr;
380 }
381}
382
383}