HTMLCollection.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. static WebIDL::ExceptionOr<JS::NonnullGCPtr<HTMLCollection>> create(ParentNode& root, Function<bool(Element const&)> filter);
  26. virtual ~HTMLCollection() override;
  27. size_t length();
  28. Element* item(size_t index) const;
  29. Element* named_item(DeprecatedFlyString const& name) const;
  30. JS::MarkedVector<Element*> collect_matching_elements() const;
  31. virtual WebIDL::ExceptionOr<JS::Value> item_value(size_t index) const override;
  32. virtual WebIDL::ExceptionOr<JS::Value> named_item_value(DeprecatedFlyString const& name) const override;
  33. virtual Vector<DeprecatedString> supported_property_names() const override;
  34. virtual bool is_supported_property_index(u32) const override;
  35. protected:
  36. HTMLCollection(ParentNode& root, Function<bool(Element const&)> filter);
  37. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  38. JS::NonnullGCPtr<ParentNode> root() { return *m_root; }
  39. private:
  40. virtual void visit_edges(Cell::Visitor&) override;
  41. // ^Bindings::LegacyPlatformObject
  42. virtual bool supports_indexed_properties() const override { return true; }
  43. virtual bool supports_named_properties() const override { return true; }
  44. virtual bool has_indexed_property_setter() const override { return false; }
  45. virtual bool has_named_property_setter() const override { return false; }
  46. virtual bool has_named_property_deleter() const override { return false; }
  47. virtual bool has_legacy_override_built_ins_interface_extended_attribute() const override { return false; }
  48. virtual bool has_legacy_unenumerable_named_properties_interface_extended_attribute() const override { return true; }
  49. virtual bool has_global_interface_extended_attribute() const override { return false; }
  50. virtual bool indexed_property_setter_has_identifier() const override { return false; }
  51. virtual bool named_property_setter_has_identifier() const override { return false; }
  52. virtual bool named_property_deleter_has_identifier() const override { return false; }
  53. JS::NonnullGCPtr<ParentNode> m_root;
  54. Function<bool(Element const&)> m_filter;
  55. };
  56. }