HTMLCollection.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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);
  35. Element* named_item(FlyString const& name);
  36. Vector<NonnullRefPtr<Element>> collect_matching_elements();
  37. protected:
  38. HTMLCollection(ParentNode& root, Function<bool(Element const&)> filter);
  39. private:
  40. NonnullRefPtr<ParentNode> m_root;
  41. Function<bool(Element const&)> m_filter;
  42. };
  43. }
  44. namespace Web::Bindings {
  45. HTMLCollectionWrapper* wrap(JS::GlobalObject&, DOM::HTMLCollection&);
  46. }