ladybird/Userland/Libraries/LibWeb/DOM/TreeWalker.h
Lenny Maiorani c37820b898 Libraries: Use default constructors/destructors in LibWeb
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#cother-other-default-operation-rules

"The compiler is more likely to get the default semantics right and
you cannot implement these functions better than the compiler."
2022-03-17 17:23:49 +00:00

76 lines
2.1 KiB
C++

/*
* 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, RefPtr<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; }
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
RefPtr<DOM::NodeFilter> m_filter;
// https://dom.spec.whatwg.org/#concept-traversal-active
bool m_active { false };
};
}