Coverage Report

Created: 2025-09-05 06:52

/src/serenity/Userland/Libraries/LibWeb/DOM/TreeWalker.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#pragma once
8
9
#include <LibWeb/DOM/NodeFilter.h>
10
11
namespace Web::DOM {
12
13
// https://dom.spec.whatwg.org/#treewalker
14
class TreeWalker final : public Bindings::PlatformObject {
15
    WEB_PLATFORM_OBJECT(TreeWalker, Bindings::PlatformObject);
16
    JS_DECLARE_ALLOCATOR(TreeWalker);
17
18
public:
19
    [[nodiscard]] static JS::NonnullGCPtr<TreeWalker> create(Node& root, unsigned what_to_show, JS::GCPtr<NodeFilter>);
20
21
    virtual ~TreeWalker() override;
22
23
    JS::NonnullGCPtr<Node> current_node() const;
24
    void set_current_node(Node&);
25
26
    JS::ThrowCompletionOr<JS::GCPtr<Node>> parent_node();
27
    JS::ThrowCompletionOr<JS::GCPtr<Node>> first_child();
28
    JS::ThrowCompletionOr<JS::GCPtr<Node>> last_child();
29
    JS::ThrowCompletionOr<JS::GCPtr<Node>> previous_sibling();
30
    JS::ThrowCompletionOr<JS::GCPtr<Node>> next_sibling();
31
    JS::ThrowCompletionOr<JS::GCPtr<Node>> previous_node();
32
    JS::ThrowCompletionOr<JS::GCPtr<Node>> next_node();
33
34
0
    JS::NonnullGCPtr<Node> root() { return m_root; }
35
36
0
    NodeFilter* filter() { return m_filter.ptr(); }
37
38
0
    unsigned what_to_show() const { return m_what_to_show; }
39
40
private:
41
    explicit TreeWalker(Node& root);
42
43
    virtual void initialize(JS::Realm&) override;
44
    virtual void visit_edges(Cell::Visitor&) override;
45
46
    enum class ChildTraversalType {
47
        First,
48
        Last,
49
    };
50
    JS::ThrowCompletionOr<JS::GCPtr<Node>> traverse_children(ChildTraversalType);
51
52
    enum class SiblingTraversalType {
53
        Next,
54
        Previous,
55
    };
56
    JS::ThrowCompletionOr<JS::GCPtr<Node>> traverse_siblings(SiblingTraversalType);
57
58
    JS::ThrowCompletionOr<NodeFilter::Result> filter(Node&);
59
60
    // https://dom.spec.whatwg.org/#concept-traversal-root
61
    JS::NonnullGCPtr<Node> m_root;
62
63
    // https://dom.spec.whatwg.org/#treewalker-current
64
    JS::NonnullGCPtr<Node> m_current;
65
66
    // https://dom.spec.whatwg.org/#concept-traversal-whattoshow
67
    unsigned m_what_to_show { 0 };
68
69
    // https://dom.spec.whatwg.org/#concept-traversal-filter
70
    JS::GCPtr<NodeFilter> m_filter;
71
72
    // https://dom.spec.whatwg.org/#concept-traversal-active
73
    bool m_active { false };
74
};
75
76
}