1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 12:48:10 +00:00

LibWeb: Make HTMLCollection and subclasses GC-allocated

This commit is contained in:
Andreas Kling 2022-09-01 20:50:16 +02:00
parent 4c887bf6c3
commit 2bba97964b
22 changed files with 115 additions and 85 deletions

View file

@ -1,25 +1,39 @@
/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Bindings/HTMLCollectionPrototype.h>
#include <LibWeb/DOM/Element.h>
#include <LibWeb/DOM/HTMLCollection.h>
#include <LibWeb/DOM/ParentNode.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/Namespace.h>
namespace Web::DOM {
JS::NonnullGCPtr<HTMLCollection> HTMLCollection::create(ParentNode& root, Function<bool(Element const&)> filter)
{
return *root.heap().allocate<HTMLCollection>(root.realm(), root, move(filter));
}
HTMLCollection::HTMLCollection(ParentNode& root, Function<bool(Element const&)> filter)
: m_root(JS::make_handle(root))
: LegacyPlatformObject(root.window().ensure_web_prototype<Bindings::HTMLCollectionPrototype>("HTMLCollection"))
, m_root(root)
, m_filter(move(filter))
{
}
HTMLCollection::~HTMLCollection() = default;
void HTMLCollection::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_root.ptr());
}
JS::MarkedVector<Element*> HTMLCollection::collect_matching_elements() const
{
JS::MarkedVector<Element*> elements(m_root->heap());
@ -109,4 +123,19 @@ bool HTMLCollection::is_supported_property_index(u32 index) const
return index < elements.size();
}
JS::Value HTMLCollection::item_value(size_t index) const
{
auto* element = item(index);
if (!element)
return JS::js_undefined();
return const_cast<Element*>(element);
}
JS::Value HTMLCollection::named_item_value(FlyString const& index) const
{
auto* element = named_item(index);
if (!element)
return JS::js_undefined();
return const_cast<Element*>(element);
}
}