HTMLAllCollection.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Function.h>
  8. #include <LibJS/Forward.h>
  9. #include <LibJS/Heap/GCPtr.h>
  10. #include <LibWeb/Bindings/PlatformObject.h>
  11. #include <LibWeb/DOM/HTMLCollection.h>
  12. #include <LibWeb/Forward.h>
  13. namespace Web::HTML {
  14. // FIXME: Should be part of HTML namespace!
  15. class HTMLAllCollection : public Bindings::PlatformObject {
  16. WEB_PLATFORM_OBJECT(HTMLAllCollection, Bindings::PlatformObject);
  17. JS_DECLARE_ALLOCATOR(HTMLAllCollection);
  18. public:
  19. enum class Scope {
  20. Children,
  21. Descendants,
  22. };
  23. [[nodiscard]] static JS::NonnullGCPtr<HTMLAllCollection> create(DOM::ParentNode& root, Scope, Function<bool(DOM::Element const&)> filter);
  24. virtual ~HTMLAllCollection() override;
  25. size_t length() const;
  26. Variant<JS::NonnullGCPtr<DOM::HTMLCollection>, JS::NonnullGCPtr<DOM::Element>, Empty> item(Optional<FlyString> const& name_or_index) const;
  27. Variant<JS::NonnullGCPtr<DOM::HTMLCollection>, JS::NonnullGCPtr<DOM::Element>, Empty> named_item(FlyString const& name) const;
  28. JS::MarkedVector<JS::NonnullGCPtr<DOM::Element>> collect_matching_elements() const;
  29. virtual WebIDL::ExceptionOr<JS::Value> item_value(size_t index) const override;
  30. virtual WebIDL::ExceptionOr<JS::Value> named_item_value(FlyString const& name) const override;
  31. virtual Vector<FlyString> supported_property_names() const override;
  32. virtual bool is_supported_property_index(u32) const override;
  33. protected:
  34. HTMLAllCollection(DOM::ParentNode& root, Scope, Function<bool(DOM::Element const&)> filter);
  35. virtual void initialize(JS::Realm&) override;
  36. virtual bool is_htmldda() const override { return true; }
  37. private:
  38. Variant<JS::NonnullGCPtr<DOM::HTMLCollection>, JS::NonnullGCPtr<DOM::Element>, Empty> get_the_all_named_elements(FlyString const& name) const;
  39. JS::GCPtr<DOM::Element> get_the_all_indexed_element(u32 index) const;
  40. Variant<JS::NonnullGCPtr<DOM::HTMLCollection>, JS::NonnullGCPtr<DOM::Element>, Empty> get_the_all_indexed_or_named_elements(JS::PropertyKey const& name_or_index) const;
  41. virtual void visit_edges(Cell::Visitor&) override;
  42. JS::NonnullGCPtr<DOM::ParentNode> m_root;
  43. Function<bool(DOM::Element const&)> m_filter;
  44. Scope m_scope { Scope::Descendants };
  45. };
  46. }