TreeWalker.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibWeb/DOM/NodeFilter.h>
  8. namespace Web::DOM {
  9. // https://dom.spec.whatwg.org/#treewalker
  10. class TreeWalker final : public Bindings::PlatformObject {
  11. WEB_PLATFORM_OBJECT(TreeWalker, Bindings::PlatformObject);
  12. public:
  13. static JS::NonnullGCPtr<TreeWalker> create(Node& root, unsigned what_to_show, JS::GCPtr<NodeFilter>);
  14. virtual ~TreeWalker() override;
  15. JS::NonnullGCPtr<Node> current_node() const;
  16. void set_current_node(Node&);
  17. JS::ThrowCompletionOr<JS::GCPtr<Node>> parent_node();
  18. JS::ThrowCompletionOr<JS::GCPtr<Node>> first_child();
  19. JS::ThrowCompletionOr<JS::GCPtr<Node>> last_child();
  20. JS::ThrowCompletionOr<JS::GCPtr<Node>> previous_sibling();
  21. JS::ThrowCompletionOr<JS::GCPtr<Node>> next_sibling();
  22. JS::ThrowCompletionOr<JS::GCPtr<Node>> previous_node();
  23. JS::ThrowCompletionOr<JS::GCPtr<Node>> next_node();
  24. JS::NonnullGCPtr<Node> root() { return m_root; }
  25. NodeFilter* filter() { return m_filter.ptr(); }
  26. unsigned what_to_show() const { return m_what_to_show; }
  27. private:
  28. explicit TreeWalker(Node& root);
  29. virtual void visit_edges(Cell::Visitor&) override;
  30. enum class ChildTraversalType {
  31. First,
  32. Last,
  33. };
  34. JS::ThrowCompletionOr<JS::GCPtr<Node>> traverse_children(ChildTraversalType);
  35. enum class SiblingTraversalType {
  36. Next,
  37. Previous,
  38. };
  39. JS::ThrowCompletionOr<JS::GCPtr<Node>> traverse_siblings(SiblingTraversalType);
  40. JS::ThrowCompletionOr<NodeFilter::Result> filter(Node&);
  41. // https://dom.spec.whatwg.org/#concept-traversal-root
  42. JS::NonnullGCPtr<Node> m_root;
  43. // https://dom.spec.whatwg.org/#treewalker-current
  44. JS::NonnullGCPtr<Node> m_current;
  45. // https://dom.spec.whatwg.org/#concept-traversal-whattoshow
  46. unsigned m_what_to_show { 0 };
  47. // https://dom.spec.whatwg.org/#concept-traversal-filter
  48. JS::GCPtr<NodeFilter> m_filter;
  49. // https://dom.spec.whatwg.org/#concept-traversal-active
  50. bool m_active { false };
  51. };
  52. }