StaticNodeList.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. for (auto& node : m_static_nodes)
  26. visitor.visit(node);
  27. }
  28. // https://dom.spec.whatwg.org/#dom-nodelist-length
  29. u32 StaticNodeList::length() const
  30. {
  31. return m_static_nodes.size();
  32. }
  33. // https://dom.spec.whatwg.org/#dom-nodelist-item
  34. Node const* StaticNodeList::item(u32 index) const
  35. {
  36. // 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.
  37. if (index >= m_static_nodes.size())
  38. return nullptr;
  39. return m_static_nodes[index];
  40. }
  41. // https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-indices
  42. bool StaticNodeList::is_supported_property_index(u32 index) const
  43. {
  44. // 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.
  45. // If there are no such elements, then there are no supported property indices.
  46. return index < m_static_nodes.size();
  47. }
  48. }