1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 09:57:35 +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

@ -184,7 +184,7 @@ static bool is_form_control(DOM::Element const& element)
}
// https://html.spec.whatwg.org/multipage/forms.html#dom-form-elements
NonnullRefPtr<DOM::HTMLCollection> HTMLFormElement::elements() const
JS::NonnullGCPtr<DOM::HTMLCollection> HTMLFormElement::elements() const
{
// FIXME: This should return the same HTMLFormControlsCollection object every time,
// but that would cause a reference cycle since HTMLCollection refs the root.

View file

@ -28,7 +28,7 @@ public:
void add_associated_element(Badge<FormAssociatedElement>, HTMLElement&);
void remove_associated_element(Badge<FormAssociatedElement>, HTMLElement&);
NonnullRefPtr<DOM::HTMLCollection> elements() const;
JS::NonnullGCPtr<DOM::HTMLCollection> elements() const;
unsigned length() const;
private:

View file

@ -4,19 +4,29 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Bindings/HTMLOptionsCollectionPrototype.h>
#include <LibWeb/DOM/DOMException.h>
#include <LibWeb/HTML/HTMLOptGroupElement.h>
#include <LibWeb/HTML/HTMLOptionElement.h>
#include <LibWeb/HTML/HTMLOptionsCollection.h>
#include <LibWeb/HTML/HTMLSelectElement.h>
#include <LibWeb/HTML/Window.h>
namespace Web::HTML {
JS::NonnullGCPtr<HTMLOptionsCollection> HTMLOptionsCollection::create(DOM::ParentNode& root, Function<bool(DOM::Element const&)> filter)
{
return *root.heap().allocate<HTMLOptionsCollection>(root.realm(), root, move(filter));
}
HTMLOptionsCollection::HTMLOptionsCollection(DOM::ParentNode& root, Function<bool(DOM::Element const&)> filter)
: DOM::HTMLCollection(root, move(filter))
{
set_prototype(&root.window().ensure_web_prototype<Bindings::HTMLOptionsCollectionPrototype>("HTMLOptionsCollection"));
}
HTMLOptionsCollection::~HTMLOptionsCollection() = default;
// https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#dom-htmloptionscollection-add
DOM::ExceptionOr<void> HTMLOptionsCollection::add(HTMLOptionOrOptGroupElement element, Optional<HTMLElementOrElementIndex> before)
{

View file

@ -16,24 +16,18 @@ using HTMLOptionOrOptGroupElement = Variant<JS::Handle<HTMLOptionElement>, JS::H
using HTMLElementOrElementIndex = Variant<JS::Handle<HTMLElement>, i32>;
class HTMLOptionsCollection final : public DOM::HTMLCollection {
public:
using WrapperType = Bindings::HTMLOptionsCollectionWrapper;
WEB_PLATFORM_OBJECT(HTMLOptionsCollection, DOM::HTMLCollection);
static NonnullRefPtr<HTMLOptionsCollection> create(DOM::ParentNode& root, Function<bool(DOM::Element const&)> filter)
{
return adopt_ref(*new HTMLOptionsCollection(root, move(filter)));
}
public:
static JS::NonnullGCPtr<HTMLOptionsCollection> create(DOM::ParentNode& root, Function<bool(DOM::Element const&)> filter);
virtual ~HTMLOptionsCollection() override;
DOM::ExceptionOr<void> add(HTMLOptionOrOptGroupElement element, Optional<HTMLElementOrElementIndex> before = {});
protected:
private:
HTMLOptionsCollection(DOM::ParentNode& root, Function<bool(DOM::Element const&)> filter);
};
}
namespace Web::Bindings {
HTMLOptionsCollectionWrapper* wrap(JS::Realm&, HTML::HTMLOptionsCollection&);
}
WRAPPER_HACK(HTMLOptionsCollection, Web::HTML)

View file

@ -22,8 +22,14 @@ HTMLSelectElement::HTMLSelectElement(DOM::Document& document, DOM::QualifiedName
HTMLSelectElement::~HTMLSelectElement() = default;
void HTMLSelectElement::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_options.ptr());
}
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-options
RefPtr<HTMLOptionsCollection> const& HTMLSelectElement::options()
JS::GCPtr<HTMLOptionsCollection> const& HTMLSelectElement::options()
{
if (!m_options) {
m_options = HTMLOptionsCollection::create(*this, [](DOM::Element const& element) {
@ -41,7 +47,7 @@ RefPtr<HTMLOptionsCollection> const& HTMLSelectElement::options()
DOM::ExceptionOr<void> HTMLSelectElement::add(HTMLOptionOrOptGroupElement element, Optional<HTMLElementOrElementIndex> before)
{
// Similarly, the add(element, before) method must act like its namesake method on that same options collection.
return const_cast<RefPtr<HTMLOptionsCollection>&>(options())->add(move(element), move(before));
return const_cast<HTMLOptionsCollection&>(*options()).add(move(element), move(before));
}
// https://html.spec.whatwg.org/multipage/form-elements.html#concept-select-option-list

View file

@ -23,7 +23,7 @@ class HTMLSelectElement final
public:
virtual ~HTMLSelectElement() override;
RefPtr<HTMLOptionsCollection> const& options();
JS::GCPtr<HTMLOptionsCollection> const& options();
DOM::ExceptionOr<void> add(HTMLOptionOrOptGroupElement element, Optional<HTMLElementOrElementIndex> before = {});
@ -56,7 +56,9 @@ public:
private:
HTMLSelectElement(DOM::Document&, DOM::QualifiedName);
RefPtr<HTMLOptionsCollection> m_options;
virtual void visit_edges(Cell::Visitor&) override;
JS::GCPtr<HTMLOptionsCollection> m_options;
};
}

View file

@ -222,7 +222,7 @@ void HTMLTableElement::delete_t_foot()
}
}
NonnullRefPtr<DOM::HTMLCollection> HTMLTableElement::t_bodies()
JS::NonnullGCPtr<DOM::HTMLCollection> HTMLTableElement::t_bodies()
{
return DOM::HTMLCollection::create(*this, [](DOM::Element const& element) {
return element.local_name() == TagNames::tbody;
@ -253,7 +253,7 @@ JS::NonnullGCPtr<HTMLTableSectionElement> HTMLTableElement::create_t_body()
return static_cast<HTMLTableSectionElement&>(*tbody);
}
NonnullRefPtr<DOM::HTMLCollection> HTMLTableElement::rows()
JS::NonnullGCPtr<DOM::HTMLCollection> HTMLTableElement::rows()
{
HTMLTableElement* table_node = this;
// FIXME: The elements in the collection must be ordered such that those elements whose parent is a thead are

View file

@ -35,10 +35,10 @@ public:
JS::NonnullGCPtr<HTMLTableSectionElement> create_t_foot();
void delete_t_foot();
NonnullRefPtr<DOM::HTMLCollection> t_bodies();
JS::NonnullGCPtr<DOM::HTMLCollection> t_bodies();
JS::NonnullGCPtr<HTMLTableSectionElement> create_t_body();
NonnullRefPtr<DOM::HTMLCollection> rows();
JS::NonnullGCPtr<DOM::HTMLCollection> rows();
DOM::ExceptionOr<JS::NonnullGCPtr<HTMLTableRowElement>> insert_row(long index);
DOM::ExceptionOr<void> delete_row(long index);

View file

@ -23,7 +23,7 @@ HTMLTableRowElement::HTMLTableRowElement(DOM::Document& document, DOM::Qualified
HTMLTableRowElement::~HTMLTableRowElement() = default;
// https://html.spec.whatwg.org/multipage/tables.html#dom-tr-cells
NonnullRefPtr<DOM::HTMLCollection> HTMLTableRowElement::cells() const
JS::NonnullGCPtr<DOM::HTMLCollection> HTMLTableRowElement::cells() const
{
// The cells attribute must return an HTMLCollection rooted at this tr element,
// whose filter matches only td and th elements that are children of the tr element.
@ -42,7 +42,7 @@ int HTMLTableRowElement::row_index() const
// or a parent tbody, thead, or tfoot element and a grandparent table element,
// return the index of this tr element in that table element's rows collection.
// If there is no such table element, then the attribute must return 1.
auto rows_collection = [&]() -> RefPtr<DOM::HTMLCollection> {
auto rows_collection = [&]() -> JS::GCPtr<DOM::HTMLCollection> {
if (!parent())
return nullptr;
if (is<HTMLTableElement>(*parent()))
@ -67,7 +67,7 @@ int HTMLTableRowElement::section_row_index() const
// return the index of the tr element in the parent element's rows collection
// (for tables, that's HTMLTableElement's rows collection; for table sections, that's HTMLTableSectionElement's rows collection).
// If there is no such parent element, then the attribute must return 1.
auto rows_collection = [&]() -> RefPtr<DOM::HTMLCollection> {
auto rows_collection = [&]() -> JS::GCPtr<DOM::HTMLCollection> {
if (!parent())
return nullptr;
if (is<HTMLTableElement>(*parent()))

View file

@ -16,7 +16,7 @@ class HTMLTableRowElement final : public HTMLElement {
public:
virtual ~HTMLTableRowElement() override;
NonnullRefPtr<DOM::HTMLCollection> cells() const;
JS::NonnullGCPtr<DOM::HTMLCollection> cells() const;
int row_index() const;
int section_row_index() const;

View file

@ -24,7 +24,7 @@ HTMLTableSectionElement::HTMLTableSectionElement(DOM::Document& document, DOM::Q
HTMLTableSectionElement::~HTMLTableSectionElement() = default;
// https://html.spec.whatwg.org/multipage/tables.html#dom-tbody-rows
NonnullRefPtr<DOM::HTMLCollection> HTMLTableSectionElement::rows() const
JS::NonnullGCPtr<DOM::HTMLCollection> HTMLTableSectionElement::rows() const
{
// The rows attribute must return an HTMLCollection rooted at this element,
// whose filter matches only tr elements that are children of this element.

View file

@ -17,7 +17,7 @@ class HTMLTableSectionElement final : public HTMLElement {
public:
virtual ~HTMLTableSectionElement() override;
NonnullRefPtr<DOM::HTMLCollection> rows() const;
JS::NonnullGCPtr<DOM::HTMLCollection> rows() const;
DOM::ExceptionOr<JS::NonnullGCPtr<HTMLTableRowElement>> insert_row(long index);
DOM::ExceptionOr<void> delete_row(long index);