StaticNodeList.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. JS_DEFINE_ALLOCATOR(StaticNodeList);
  11. JS::NonnullGCPtr<NodeList> StaticNodeList::create(JS::Realm& realm, Vector<JS::Handle<Node>> static_nodes)
  12. {
  13. return realm.heap().allocate<StaticNodeList>(realm, realm, move(static_nodes));
  14. }
  15. StaticNodeList::StaticNodeList(JS::Realm& realm, Vector<JS::Handle<Node>> static_nodes)
  16. : NodeList(realm)
  17. {
  18. for (auto& node : static_nodes)
  19. m_static_nodes.append(*node);
  20. }
  21. StaticNodeList::~StaticNodeList() = default;
  22. void StaticNodeList::visit_edges(Cell::Visitor& visitor)
  23. {
  24. Base::visit_edges(visitor);
  25. visitor.visit(m_static_nodes);
  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. }