StaticNodeList.cpp 1.7 KB

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