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