LiveNodeList.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  3. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Heap/Heap.h>
  8. #include <LibJS/Runtime/Error.h>
  9. #include <LibWeb/DOM/LiveNodeList.h>
  10. #include <LibWeb/DOM/Node.h>
  11. namespace Web::DOM {
  12. JS::NonnullGCPtr<NodeList> LiveNodeList::create(JS::Realm& realm, Node& root, Scope scope, Function<bool(Node const&)> filter)
  13. {
  14. return realm.heap().allocate<LiveNodeList>(realm, realm, root, scope, move(filter));
  15. }
  16. LiveNodeList::LiveNodeList(JS::Realm& realm, Node& root, Scope scope, Function<bool(Node const&)> filter)
  17. : NodeList(realm)
  18. , m_root(root)
  19. , m_filter(move(filter))
  20. , m_scope(scope)
  21. {
  22. }
  23. LiveNodeList::~LiveNodeList() = default;
  24. void LiveNodeList::visit_edges(Cell::Visitor& visitor)
  25. {
  26. Base::visit_edges(visitor);
  27. visitor.visit(m_root.ptr());
  28. }
  29. JS::MarkedVector<Node*> LiveNodeList::collection() const
  30. {
  31. JS::MarkedVector<Node*> nodes(heap());
  32. if (m_scope == Scope::Descendants) {
  33. m_root->for_each_in_subtree([&](auto& node) {
  34. if (m_filter(node))
  35. nodes.append(const_cast<Node*>(&node));
  36. return IterationDecision::Continue;
  37. });
  38. } else {
  39. m_root->for_each_child([&](auto& node) {
  40. if (m_filter(node))
  41. nodes.append(const_cast<Node*>(&node));
  42. return IterationDecision::Continue;
  43. });
  44. }
  45. return nodes;
  46. }
  47. Node* LiveNodeList::first_matching(Function<bool(Node const&)> const& filter) const
  48. {
  49. Node* matched_node = nullptr;
  50. if (m_scope == Scope::Descendants) {
  51. m_root->for_each_in_subtree([&](auto& node) {
  52. if (m_filter(node) && filter(node)) {
  53. matched_node = const_cast<Node*>(&node);
  54. return IterationDecision::Break;
  55. }
  56. return IterationDecision::Continue;
  57. });
  58. } else {
  59. m_root->for_each_child([&](auto& node) {
  60. if (m_filter(node) && filter(node)) {
  61. matched_node = const_cast<Node*>(&node);
  62. return IterationDecision::Break;
  63. }
  64. return IterationDecision::Continue;
  65. });
  66. }
  67. return matched_node;
  68. }
  69. // https://dom.spec.whatwg.org/#dom-nodelist-length
  70. u32 LiveNodeList::length() const
  71. {
  72. return collection().size();
  73. }
  74. // https://dom.spec.whatwg.org/#dom-nodelist-item
  75. Node const* LiveNodeList::item(u32 index) const
  76. {
  77. // 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.
  78. auto nodes = collection();
  79. if (index >= nodes.size())
  80. return nullptr;
  81. return nodes[index];
  82. }
  83. // https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-indices
  84. bool LiveNodeList::is_supported_property_index(u32 index) const
  85. {
  86. // 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.
  87. // If there are no such elements, then there are no supported property indices.
  88. return index < length();
  89. }
  90. }