HTMLCollection.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Function.h>
  9. #include <LibJS/Heap/GCPtr.h>
  10. #include <LibWeb/Bindings/PlatformObject.h>
  11. #include <LibWeb/Forward.h>
  12. namespace Web::DOM {
  13. // NOTE: HTMLCollection is in the DOM namespace because it's part of the DOM specification.
  14. // This class implements a live, filtered view of a DOM subtree.
  15. // When constructing an HTMLCollection, you provide a root node + a filter.
  16. // The filter is a simple Function object that answers the question
  17. // "is this Element part of the collection?"
  18. class HTMLCollection : public Bindings::PlatformObject {
  19. WEB_PLATFORM_OBJECT(HTMLCollection, Bindings::PlatformObject);
  20. JS_DECLARE_ALLOCATOR(HTMLCollection);
  21. public:
  22. enum class Scope {
  23. Children,
  24. Descendants,
  25. };
  26. [[nodiscard]] static JS::NonnullGCPtr<HTMLCollection> create(ParentNode& root, Scope, Function<bool(Element const&)> filter);
  27. virtual ~HTMLCollection() override;
  28. size_t length() const;
  29. Element* item(size_t index) const;
  30. Element* named_item(FlyString const& key) const;
  31. JS::MarkedVector<JS::NonnullGCPtr<Element>> collect_matching_elements() const;
  32. virtual WebIDL::ExceptionOr<JS::Value> item_value(size_t index) const override;
  33. virtual WebIDL::ExceptionOr<JS::Value> named_item_value(FlyString const& name) const override;
  34. virtual Vector<FlyString> supported_property_names() const override;
  35. virtual bool is_supported_property_index(u32) const override;
  36. protected:
  37. HTMLCollection(ParentNode& root, Scope, Function<bool(Element const&)> filter);
  38. virtual void initialize(JS::Realm&) override;
  39. JS::NonnullGCPtr<ParentNode> root() { return *m_root; }
  40. JS::NonnullGCPtr<ParentNode const> root() const { return *m_root; }
  41. private:
  42. virtual void visit_edges(Cell::Visitor&) override;
  43. void update_cache_if_needed() const;
  44. mutable u64 m_cached_dom_tree_version { 0 };
  45. mutable Vector<JS::NonnullGCPtr<Element>> m_cached_elements;
  46. JS::NonnullGCPtr<ParentNode> m_root;
  47. Function<bool(Element const&)> m_filter;
  48. Scope m_scope { Scope::Descendants };
  49. };
  50. }