LiveNodeList.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  3. * Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Function.h>
  9. #include <LibWeb/DOM/NodeList.h>
  10. namespace Web::DOM {
  11. // FIXME: Just like HTMLCollection, LiveNodeList currently does no caching.
  12. class LiveNodeList : public NodeList {
  13. WEB_PLATFORM_OBJECT(LiveNodeList, NodeList);
  14. GC_DECLARE_ALLOCATOR(LiveNodeList);
  15. public:
  16. enum class Scope {
  17. Children,
  18. Descendants,
  19. };
  20. [[nodiscard]] static GC::Ref<NodeList> create(JS::Realm&, Node const& root, Scope, ESCAPING Function<bool(Node const&)> filter);
  21. virtual ~LiveNodeList() override;
  22. virtual u32 length() const override;
  23. virtual Node const* item(u32 index) const override;
  24. protected:
  25. LiveNodeList(JS::Realm&, Node const& root, Scope, ESCAPING Function<bool(Node const&)> filter);
  26. Node* first_matching(Function<bool(Node const&)> const& filter) const;
  27. private:
  28. virtual void visit_edges(Cell::Visitor&) override;
  29. GC::RootVector<Node*> collection() const;
  30. GC::Ref<Node const> m_root;
  31. Function<bool(Node const&)> m_filter;
  32. Scope m_scope { Scope::Descendants };
  33. };
  34. }