HTMLCollection.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/FlyString.h>
  8. #include <AK/Function.h>
  9. #include <AK/Noncopyable.h>
  10. #include <LibWeb/Bindings/Wrappable.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. // FIXME: HTMLCollection currently does no caching. It will re-filter on every access!
  19. // We should teach it how to cache results. The main challenge is invalidating
  20. // these caches, since this needs to happen on various kinds of DOM mutation.
  21. class HTMLCollection
  22. : public RefCounted<HTMLCollection>
  23. , public Bindings::Wrappable {
  24. AK_MAKE_NONCOPYABLE(HTMLCollection);
  25. AK_MAKE_NONMOVABLE(HTMLCollection);
  26. public:
  27. using WrapperType = Bindings::HTMLCollectionWrapper;
  28. static NonnullRefPtr<HTMLCollection> create(ParentNode& root, Function<bool(Element const&)> filter)
  29. {
  30. return adopt_ref(*new HTMLCollection(root, move(filter)));
  31. }
  32. ~HTMLCollection();
  33. size_t length();
  34. Element* item(size_t index) const;
  35. Element* named_item(FlyString const& name) const;
  36. Vector<NonnullRefPtr<Element>> collect_matching_elements() const;
  37. Vector<String> supported_property_names() const;
  38. bool is_supported_property_index(u32) const;
  39. protected:
  40. HTMLCollection(ParentNode& root, Function<bool(Element const&)> filter);
  41. NonnullRefPtr<ParentNode> root() { return m_root; }
  42. private:
  43. NonnullRefPtr<ParentNode> m_root;
  44. Function<bool(Element const&)> m_filter;
  45. };
  46. }
  47. namespace Web::Bindings {
  48. HTMLCollectionWrapper* wrap(JS::GlobalObject&, DOM::HTMLCollection&);
  49. }