/* * Copyright (c) 2022, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include namespace Web::DOM { // https://dom.spec.whatwg.org/#treewalker class TreeWalker final : public Bindings::PlatformObject { WEB_PLATFORM_OBJECT(TreeWalker, Bindings::PlatformObject); public: static WebIDL::ExceptionOr> create(Node& root, unsigned what_to_show, JS::GCPtr); virtual ~TreeWalker() override; JS::NonnullGCPtr 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(); JS::NonnullGCPtr root() { return m_root; } NodeFilter* filter() { return m_filter.ptr(); } unsigned what_to_show() const { return m_what_to_show; } private: explicit TreeWalker(Node& root); virtual JS::ThrowCompletionOr initialize(JS::Realm&) override; virtual void visit_edges(Cell::Visitor&) override; 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 JS::NonnullGCPtr m_root; // https://dom.spec.whatwg.org/#treewalker-current JS::NonnullGCPtr 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::GCPtr m_filter; // https://dom.spec.whatwg.org/#concept-traversal-active bool m_active { false }; }; }