StaticNodeList.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Heap/Heap.h>
  7. #include <LibJS/Runtime/Error.h>
  8. #include <LibWeb/DOM/StaticNodeList.h>
  9. namespace Web::DOM {
  10. WebIDL::ExceptionOr<JS::NonnullGCPtr<NodeList>> StaticNodeList::create(JS::Realm& realm, Vector<JS::Handle<Node>> static_nodes)
  11. {
  12. return MUST_OR_THROW_OOM(realm.heap().allocate<StaticNodeList>(realm, realm, move(static_nodes)));
  13. }
  14. StaticNodeList::StaticNodeList(JS::Realm& realm, Vector<JS::Handle<Node>> static_nodes)
  15. : NodeList(realm)
  16. {
  17. for (auto& node : static_nodes)
  18. m_static_nodes.append(*node);
  19. }
  20. StaticNodeList::~StaticNodeList() = default;
  21. void StaticNodeList::visit_edges(Cell::Visitor& visitor)
  22. {
  23. Base::visit_edges(visitor);
  24. for (auto& node : m_static_nodes)
  25. visitor.visit(node);
  26. }
  27. // https://dom.spec.whatwg.org/#dom-nodelist-length
  28. u32 StaticNodeList::length() const
  29. {
  30. return m_static_nodes.size();
  31. }
  32. // https://dom.spec.whatwg.org/#dom-nodelist-item
  33. Node const* StaticNodeList::item(u32 index) const
  34. {
  35. // 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.
  36. if (index >= m_static_nodes.size())
  37. return nullptr;
  38. return m_static_nodes[index];
  39. }
  40. // https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-indices
  41. bool StaticNodeList::is_supported_property_index(u32 index) const
  42. {
  43. // 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.
  44. // If there are no such elements, then there are no supported property indices.
  45. return index < m_static_nodes.size();
  46. }
  47. }