LiveNodeList.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 <LibWeb/DOM/LiveNodeList.h>
  8. #include <LibWeb/DOM/Node.h>
  9. namespace Web::DOM {
  10. WebIDL::ExceptionOr<JS::NonnullGCPtr<NodeList>> LiveNodeList::create(JS::Realm& realm, Node& root, Scope scope, Function<bool(Node const&)> filter)
  11. {
  12. return MUST_OR_THROW_OOM(realm.heap().allocate<LiveNodeList>(realm, realm, root, scope, move(filter)));
  13. }
  14. LiveNodeList::LiveNodeList(JS::Realm& realm, Node& root, Scope scope, Function<bool(Node const&)> filter)
  15. : NodeList(realm)
  16. , m_root(root)
  17. , m_filter(move(filter))
  18. , m_scope(scope)
  19. {
  20. }
  21. LiveNodeList::~LiveNodeList() = default;
  22. void LiveNodeList::visit_edges(Cell::Visitor& visitor)
  23. {
  24. Base::visit_edges(visitor);
  25. visitor.visit(m_root.ptr());
  26. }
  27. JS::MarkedVector<Node*> LiveNodeList::collection() const
  28. {
  29. JS::MarkedVector<Node*> nodes(heap());
  30. if (m_scope == Scope::Descendants) {
  31. m_root->for_each_in_subtree([&](auto& node) {
  32. if (m_filter(node))
  33. nodes.append(const_cast<Node*>(&node));
  34. return IterationDecision::Continue;
  35. });
  36. } else {
  37. m_root->for_each_child([&](auto& node) {
  38. if (m_filter(node))
  39. nodes.append(const_cast<Node*>(&node));
  40. return IterationDecision::Continue;
  41. });
  42. }
  43. return nodes;
  44. }
  45. // https://dom.spec.whatwg.org/#dom-nodelist-length
  46. u32 LiveNodeList::length() const
  47. {
  48. return collection().size();
  49. }
  50. // https://dom.spec.whatwg.org/#dom-nodelist-item
  51. Node const* LiveNodeList::item(u32 index) const
  52. {
  53. // 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.
  54. auto nodes = collection();
  55. if (index >= nodes.size())
  56. return nullptr;
  57. return nodes[index];
  58. }
  59. // https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-indices
  60. bool LiveNodeList::is_supported_property_index(u32 index) const
  61. {
  62. // 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.
  63. // If there are no such elements, then there are no supported property indices.
  64. return index < length();
  65. }
  66. }