summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/DOM/TreeWalker.h
blob: 7c4c322dce4dc6e36bad481f46ef2e2e803954ec (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
 * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/RefCounted.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/DOM/NodeFilter.h>

namespace Web::DOM {

// https://dom.spec.whatwg.org/#treewalker
class TreeWalker
    : public RefCounted<TreeWalker>
    , public Bindings::Wrappable {
public:
    using WrapperType = Bindings::TreeWalkerWrapper;

    static NonnullRefPtr<TreeWalker> create(Node& root, unsigned what_to_show, NodeFilter*);
    virtual ~TreeWalker() override = default;

    NonnullRefPtr<Node> current_node() const;
    void set_current_node(Node&);

    JS::ThrowCompletionOr<RefPtr<Node>> parent_node();
    JS::ThrowCompletionOr<RefPtr<Node>> first_child();
    JS::ThrowCompletionOr<RefPtr<Node>> last_child();
    JS::ThrowCompletionOr<RefPtr<Node>> previous_sibling();
    JS::ThrowCompletionOr<RefPtr<Node>> next_sibling();
    JS::ThrowCompletionOr<RefPtr<Node>> previous_node();
    JS::ThrowCompletionOr<RefPtr<Node>> next_node();

    NonnullRefPtr<Node> root() { return m_root; }

    NodeFilter* filter() { return m_filter.cell(); }

    unsigned what_to_show() const { return m_what_to_show; }

private:
    TreeWalker(Node& root);

    enum class ChildTraversalType {
        First,
        Last,
    };
    JS::ThrowCompletionOr<RefPtr<Node>> traverse_children(ChildTraversalType);

    enum class SiblingTraversalType {
        Next,
        Previous,
    };
    JS::ThrowCompletionOr<RefPtr<Node>> traverse_siblings(SiblingTraversalType);

    JS::ThrowCompletionOr<NodeFilter::Result> filter(Node&);

    // https://dom.spec.whatwg.org/#concept-traversal-root
    NonnullRefPtr<DOM::Node> m_root;

    // https://dom.spec.whatwg.org/#treewalker-current
    NonnullRefPtr<DOM::Node> m_current;

    // https://dom.spec.whatwg.org/#concept-traversal-whattoshow
    unsigned m_what_to_show { 0 };

    // https://dom.spec.whatwg.org/#concept-traversal-filter
    JS::Handle<DOM::NodeFilter> m_filter;

    // https://dom.spec.whatwg.org/#concept-traversal-active
    bool m_active { false };
};

}