mirror of
https://github.com/SerenityOS/serenity
synced 2026-05-05 06:32:26 +02:00
LibWeb: Make HTMLCollection and subclasses GC-allocated
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user