LiveNodeList.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOM/LiveNodeList.h>
  7. #include <LibWeb/DOM/Node.h>
  8. namespace Web::DOM {
  9. LiveNodeList::LiveNodeList(Node& root, Function<bool(Node const&)> filter)
  10. : m_root(root)
  11. , m_filter(move(filter))
  12. {
  13. }
  14. NonnullRefPtrVector<Node> LiveNodeList::collection() const
  15. {
  16. NonnullRefPtrVector<Node> nodes;
  17. m_root->for_each_in_inclusive_subtree_of_type<Node>([&](auto& node) {
  18. if (m_filter(node))
  19. nodes.append(node);
  20. return IterationDecision::Continue;
  21. });
  22. return nodes;
  23. }
  24. // https://dom.spec.whatwg.org/#dom-nodelist-length
  25. u32 LiveNodeList::length() const
  26. {
  27. return collection().size();
  28. }
  29. // https://dom.spec.whatwg.org/#dom-nodelist-item
  30. Node const* LiveNodeList::item(u32 index) const
  31. {
  32. // The item(index) method must return the indexth node in the collection. If there is no indexth node in the collection, then the method must return null.
  33. auto nodes = collection();
  34. if (index >= nodes.size())
  35. return nullptr;
  36. return &nodes[index];
  37. }
  38. // https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-indices
  39. bool LiveNodeList::is_supported_property_index(u32 index) const
  40. {
  41. // The object’s supported property indices are the numbers in the range zero to one less than the number of nodes represented by the collection.
  42. // If there are no such elements, then there are no supported property indices.
  43. return index < length();
  44. }
  45. }