HTMLCollection.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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/DeprecatedFlyString.h>
  9. #include <AK/Function.h>
  10. #include <LibJS/Heap/GCPtr.h>
  11. #include <LibWeb/Bindings/LegacyPlatformObject.h>
  12. #include <LibWeb/Forward.h>
  13. namespace Web::DOM {
  14. // NOTE: HTMLCollection is in the DOM namespace because it's part of the DOM specification.
  15. // This class implements a live, filtered view of a DOM subtree.
  16. // When constructing an HTMLCollection, you provide a root node + a filter.
  17. // The filter is a simple Function object that answers the question
  18. // "is this Element part of the collection?"
  19. // FIXME: HTMLCollection currently does no caching. It will re-filter on every access!
  20. // We should teach it how to cache results. The main challenge is invalidating
  21. // these caches, since this needs to happen on various kinds of DOM mutation.
  22. class HTMLCollection : public Bindings::LegacyPlatformObject {
  23. WEB_PLATFORM_OBJECT(HTMLCollection, Bindings::LegacyPlatformObject);
  24. public:
  25. enum class Scope {
  26. Children,
  27. Descendants,
  28. };
  29. static WebIDL::ExceptionOr<JS::NonnullGCPtr<HTMLCollection>> create(ParentNode& root, Scope, Function<bool(Element const&)> filter);
  30. virtual ~HTMLCollection() override;
  31. size_t length();
  32. Element* item(size_t index) const;
  33. Element* named_item(DeprecatedFlyString const& name) const;
  34. JS::MarkedVector<Element*> collect_matching_elements() const;
  35. virtual WebIDL::ExceptionOr<JS::Value> item_value(size_t index) const override;
  36. virtual WebIDL::ExceptionOr<JS::Value> named_item_value(DeprecatedFlyString const& name) const override;
  37. virtual Vector<DeprecatedString> supported_property_names() const override;
  38. virtual bool is_supported_property_index(u32) const override;
  39. protected:
  40. HTMLCollection(ParentNode& root, Scope, Function<bool(Element const&)> filter);
  41. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  42. JS::NonnullGCPtr<ParentNode> root() { return *m_root; }
  43. private:
  44. virtual void visit_edges(Cell::Visitor&) override;
  45. // ^Bindings::LegacyPlatformObject
  46. virtual bool supports_indexed_properties() const override { return true; }
  47. virtual bool supports_named_properties() const override { return true; }
  48. virtual bool has_indexed_property_setter() const override { return false; }
  49. virtual bool has_named_property_setter() const override { return false; }
  50. virtual bool has_named_property_deleter() const override { return false; }
  51. virtual bool has_legacy_override_built_ins_interface_extended_attribute() const override { return false; }
  52. virtual bool has_legacy_unenumerable_named_properties_interface_extended_attribute() const override { return true; }
  53. virtual bool has_global_interface_extended_attribute() const override { return false; }
  54. virtual bool indexed_property_setter_has_identifier() const override { return false; }
  55. virtual bool named_property_setter_has_identifier() const override { return false; }
  56. virtual bool named_property_deleter_has_identifier() const override { return false; }
  57. JS::NonnullGCPtr<ParentNode> m_root;
  58. Function<bool(Element const&)> m_filter;
  59. Scope m_scope { Scope::Descendants };
  60. };
  61. }