/* * Copyright (c) 2022, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include namespace Web::DOM { // https://dom.spec.whatwg.org/#treewalker class TreeWalker : public RefCounted , public Bindings::Wrappable { public: using WrapperType = Bindings::TreeWalkerWrapper; static NonnullRefPtr create(Node& root, unsigned what_to_show, RefPtr); virtual ~TreeWalker() override = default; NonnullRefPtr current_node() const; void set_current_node(Node&); JS::ThrowCompletionOr> parent_node(); JS::ThrowCompletionOr> first_child(); JS::ThrowCompletionOr> last_child(); JS::ThrowCompletionOr> previous_sibling(); JS::ThrowCompletionOr> next_sibling(); JS::ThrowCompletionOr> previous_node(); JS::ThrowCompletionOr> next_node(); NonnullRefPtr 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> traverse_children(ChildTraversalType); enum class SiblingTraversalType { Next, Previous, }; JS::ThrowCompletionOr> traverse_siblings(SiblingTraversalType); JS::ThrowCompletionOr filter(Node&); // https://dom.spec.whatwg.org/#concept-traversal-root NonnullRefPtr m_root; // https://dom.spec.whatwg.org/#treewalker-current NonnullRefPtr 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 m_filter; // https://dom.spec.whatwg.org/#concept-traversal-active bool m_active { false }; }; }