HTMLCollection.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright (c) 2021-2022, Andreas Kling <andreas@ladybird.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 <LibGC/Ptr.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. GC_DECLARE_ALLOCATOR(HTMLCollection);
  21. public:
  22. enum class Scope {
  23. Children,
  24. Descendants,
  25. };
  26. [[nodiscard]] static GC::Ref<HTMLCollection> create(ParentNode& root, Scope, ESCAPING 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. GC::RootVector<GC::Ref<Element>> collect_matching_elements() const;
  32. virtual Optional<JS::Value> item_value(size_t index) const override;
  33. virtual 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_name(FlyString const&) const override;
  36. protected:
  37. HTMLCollection(ParentNode& root, Scope, ESCAPING Function<bool(Element const&)> filter);
  38. virtual void initialize(JS::Realm&) override;
  39. GC::Ref<ParentNode> root() { return *m_root; }
  40. GC::Ref<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. void update_name_to_element_mappings_if_needed() const;
  45. mutable u64 m_cached_dom_tree_version { 0 };
  46. mutable Vector<GC::Ref<Element>> m_cached_elements;
  47. mutable OwnPtr<OrderedHashMap<FlyString, GC::Ref<Element>>> m_cached_name_to_element_mappings;
  48. GC::Ref<ParentNode> m_root;
  49. Function<bool(Element const&)> m_filter;
  50. Scope m_scope { Scope::Descendants };
  51. };
  52. }