1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:17:36 +00:00

LibWeb+LibJS: Make the EventTarget hierarchy (incl. DOM) GC-allocated

This is a monster patch that turns all EventTargets into GC-allocated
PlatformObjects. Their C++ wrapper classes are removed, and the LibJS
garbage collector is now responsible for their lifetimes.

There's a fair amount of hacks and band-aids in this patch, and we'll
have a lot of cleanup to do after this.
This commit is contained in:
Andreas Kling 2022-08-28 13:42:07 +02:00
parent bb547ce1c4
commit 6f433c8656
445 changed files with 4797 additions and 4268 deletions

View file

@ -1,10 +1,11 @@
/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020, Luke Wilde <lukew@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/ElementFactory.h>
#include <LibWeb/HTML/HTMLAnchorElement.h>
#include <LibWeb/HTML/HTMLAreaElement.h>
@ -93,7 +94,7 @@
namespace Web::DOM {
// https://dom.spec.whatwg.org/#concept-create-element
NonnullRefPtr<Element> create_element(Document& document, FlyString local_name, FlyString namespace_, FlyString prefix)
JS::NonnullGCPtr<Element> create_element(Document& document, FlyString local_name, FlyString namespace_, FlyString prefix)
{
// 1. If prefix was not given, let prefix be null.
// NOTE: This is already taken care of by `prefix` having a default value.
@ -113,182 +114,183 @@ NonnullRefPtr<Element> create_element(Document& document, FlyString local_name,
// then set results custom element state to "undefined".
// 8. Return result.
auto& realm = document.realm();
auto lowercase_tag_name = local_name.to_lowercase();
auto qualified_name = QualifiedName { local_name, prefix, namespace_ };
if (lowercase_tag_name == HTML::TagNames::a)
return adopt_ref(*new HTML::HTMLAnchorElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLAnchorElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::area)
return adopt_ref(*new HTML::HTMLAreaElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLAreaElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::audio)
return adopt_ref(*new HTML::HTMLAudioElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLAudioElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::base)
return adopt_ref(*new HTML::HTMLBaseElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLBaseElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::blink)
return adopt_ref(*new HTML::HTMLBlinkElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLBlinkElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::body)
return adopt_ref(*new HTML::HTMLBodyElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLBodyElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::br)
return adopt_ref(*new HTML::HTMLBRElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLBRElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::button)
return adopt_ref(*new HTML::HTMLButtonElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLButtonElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::canvas)
return adopt_ref(*new HTML::HTMLCanvasElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLCanvasElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::data)
return adopt_ref(*new HTML::HTMLDataElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLDataElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::datalist)
return adopt_ref(*new HTML::HTMLDataListElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLDataListElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::details)
return adopt_ref(*new HTML::HTMLDetailsElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLDetailsElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::dialog)
return adopt_ref(*new HTML::HTMLDialogElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLDialogElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::dir)
return adopt_ref(*new HTML::HTMLDirectoryElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLDirectoryElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::div)
return adopt_ref(*new HTML::HTMLDivElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLDivElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::dl)
return adopt_ref(*new HTML::HTMLDListElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLDListElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::embed)
return adopt_ref(*new HTML::HTMLEmbedElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLEmbedElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::fieldset)
return adopt_ref(*new HTML::HTMLFieldSetElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLFieldSetElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::font)
return adopt_ref(*new HTML::HTMLFontElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLFontElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::form)
return adopt_ref(*new HTML::HTMLFormElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLFormElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::frame)
return adopt_ref(*new HTML::HTMLFrameElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLFrameElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::frameset)
return adopt_ref(*new HTML::HTMLFrameSetElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLFrameSetElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::head)
return adopt_ref(*new HTML::HTMLHeadElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLHeadElement>(realm, document, move(qualified_name));
if (lowercase_tag_name.is_one_of(HTML::TagNames::h1, HTML::TagNames::h2, HTML::TagNames::h3, HTML::TagNames::h4, HTML::TagNames::h5, HTML::TagNames::h6))
return adopt_ref(*new HTML::HTMLHeadingElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLHeadingElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::hr)
return adopt_ref(*new HTML::HTMLHRElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLHRElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::html)
return adopt_ref(*new HTML::HTMLHtmlElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLHtmlElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::iframe)
return adopt_ref(*new HTML::HTMLIFrameElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLIFrameElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::img)
return adopt_ref(*new HTML::HTMLImageElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLImageElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::input)
return adopt_ref(*new HTML::HTMLInputElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLInputElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::label)
return adopt_ref(*new HTML::HTMLLabelElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLLabelElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::legend)
return adopt_ref(*new HTML::HTMLLegendElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLLegendElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::li)
return adopt_ref(*new HTML::HTMLLIElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLLIElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::link)
return adopt_ref(*new HTML::HTMLLinkElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLLinkElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::map)
return adopt_ref(*new HTML::HTMLMapElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLMapElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::marquee)
return adopt_ref(*new HTML::HTMLMarqueeElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLMarqueeElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::menu)
return adopt_ref(*new HTML::HTMLMenuElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLMenuElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::meta)
return adopt_ref(*new HTML::HTMLMetaElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLMetaElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::meter)
return adopt_ref(*new HTML::HTMLMeterElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLMeterElement>(realm, document, move(qualified_name));
if (lowercase_tag_name.is_one_of(HTML::TagNames::ins, HTML::TagNames::del))
return adopt_ref(*new HTML::HTMLModElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLModElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::object)
return adopt_ref(*new HTML::HTMLObjectElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLObjectElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::ol)
return adopt_ref(*new HTML::HTMLOListElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLOListElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::optgroup)
return adopt_ref(*new HTML::HTMLOptGroupElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLOptGroupElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::option)
return adopt_ref(*new HTML::HTMLOptionElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLOptionElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::output)
return adopt_ref(*new HTML::HTMLOutputElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLOutputElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::p)
return adopt_ref(*new HTML::HTMLParagraphElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLParagraphElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::param)
return adopt_ref(*new HTML::HTMLParamElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLParamElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::picture)
return adopt_ref(*new HTML::HTMLPictureElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLPictureElement>(realm, document, move(qualified_name));
// NOTE: The obsolete elements "listing" and "xmp" are explicitly mapped to HTMLPreElement in the specification.
if (lowercase_tag_name.is_one_of(HTML::TagNames::pre, HTML::TagNames::listing, HTML::TagNames::xmp))
return adopt_ref(*new HTML::HTMLPreElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLPreElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::progress)
return adopt_ref(*new HTML::HTMLProgressElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLProgressElement>(realm, document, move(qualified_name));
if (lowercase_tag_name.is_one_of(HTML::TagNames::blockquote, HTML::TagNames::q))
return adopt_ref(*new HTML::HTMLQuoteElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLQuoteElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::script)
return adopt_ref(*new HTML::HTMLScriptElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLScriptElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::select)
return adopt_ref(*new HTML::HTMLSelectElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLSelectElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::slot)
return adopt_ref(*new HTML::HTMLSlotElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLSlotElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::source)
return adopt_ref(*new HTML::HTMLSourceElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLSourceElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::span)
return adopt_ref(*new HTML::HTMLSpanElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLSpanElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::style)
return adopt_ref(*new HTML::HTMLStyleElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLStyleElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::caption)
return adopt_ref(*new HTML::HTMLTableCaptionElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLTableCaptionElement>(realm, document, move(qualified_name));
if (lowercase_tag_name.is_one_of(Web::HTML::TagNames::td, Web::HTML::TagNames::th))
return adopt_ref(*new HTML::HTMLTableCellElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLTableCellElement>(realm, document, move(qualified_name));
if (lowercase_tag_name.is_one_of(HTML::TagNames::colgroup, HTML::TagNames::col))
return adopt_ref(*new HTML::HTMLTableColElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLTableColElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::table)
return adopt_ref(*new HTML::HTMLTableElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLTableElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::tr)
return adopt_ref(*new HTML::HTMLTableRowElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLTableRowElement>(realm, document, move(qualified_name));
if (lowercase_tag_name.is_one_of(HTML::TagNames::tbody, HTML::TagNames::thead, HTML::TagNames::tfoot))
return adopt_ref(*new HTML::HTMLTableSectionElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLTableSectionElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::template_)
return adopt_ref(*new HTML::HTMLTemplateElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLTemplateElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::textarea)
return adopt_ref(*new HTML::HTMLTextAreaElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLTextAreaElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::time)
return adopt_ref(*new HTML::HTMLTimeElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLTimeElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::title)
return adopt_ref(*new HTML::HTMLTitleElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLTitleElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::track)
return adopt_ref(*new HTML::HTMLTrackElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLTrackElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::ul)
return adopt_ref(*new HTML::HTMLUListElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLUListElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == HTML::TagNames::video)
return adopt_ref(*new HTML::HTMLVideoElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLVideoElement>(realm, document, move(qualified_name));
if (lowercase_tag_name.is_one_of(
HTML::TagNames::article, HTML::TagNames::section, HTML::TagNames::nav, HTML::TagNames::aside, HTML::TagNames::hgroup, HTML::TagNames::header, HTML::TagNames::footer, HTML::TagNames::address, HTML::TagNames::dt, HTML::TagNames::dd, HTML::TagNames::figure, HTML::TagNames::figcaption, HTML::TagNames::main, HTML::TagNames::em, HTML::TagNames::strong, HTML::TagNames::small, HTML::TagNames::s, HTML::TagNames::cite, HTML::TagNames::dfn, HTML::TagNames::abbr, HTML::TagNames::ruby, HTML::TagNames::rt, HTML::TagNames::rp, HTML::TagNames::code, HTML::TagNames::var, HTML::TagNames::samp, HTML::TagNames::kbd, HTML::TagNames::sub, HTML::TagNames::sup, HTML::TagNames::i, HTML::TagNames::b, HTML::TagNames::u, HTML::TagNames::mark, HTML::TagNames::bdi, HTML::TagNames::bdo, HTML::TagNames::wbr, HTML::TagNames::summary, HTML::TagNames::noscript,
// Obsolete
HTML::TagNames::acronym, HTML::TagNames::basefont, HTML::TagNames::big, HTML::TagNames::center, HTML::TagNames::nobr, HTML::TagNames::noembed, HTML::TagNames::noframes, HTML::TagNames::plaintext, HTML::TagNames::rb, HTML::TagNames::rtc, HTML::TagNames::strike, HTML::TagNames::tt))
return adopt_ref(*new HTML::HTMLElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == SVG::TagNames::svg)
return adopt_ref(*new SVG::SVGSVGElement(document, move(qualified_name)));
return *realm.heap().allocate<SVG::SVGSVGElement>(realm, document, move(qualified_name));
// FIXME: Support SVG's mixedCase tag names properly.
if (lowercase_tag_name.equals_ignoring_case(SVG::TagNames::clipPath))
return adopt_ref(*new SVG::SVGClipPathElement(document, move(qualified_name)));
return *realm.heap().allocate<SVG::SVGClipPathElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == SVG::TagNames::circle)
return adopt_ref(*new SVG::SVGCircleElement(document, move(qualified_name)));
return *realm.heap().allocate<SVG::SVGCircleElement>(realm, document, move(qualified_name));
if (lowercase_tag_name.equals_ignoring_case(SVG::TagNames::defs))
return adopt_ref(*new SVG::SVGDefsElement(document, move(qualified_name)));
return *realm.heap().allocate<SVG::SVGDefsElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == SVG::TagNames::ellipse)
return adopt_ref(*new SVG::SVGEllipseElement(document, move(qualified_name)));
return *realm.heap().allocate<SVG::SVGEllipseElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == SVG::TagNames::line)
return adopt_ref(*new SVG::SVGLineElement(document, move(qualified_name)));
return *realm.heap().allocate<SVG::SVGLineElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == SVG::TagNames::path)
return adopt_ref(*new SVG::SVGPathElement(document, move(qualified_name)));
return *realm.heap().allocate<SVG::SVGPathElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == SVG::TagNames::polygon)
return adopt_ref(*new SVG::SVGPolygonElement(document, move(qualified_name)));
return *realm.heap().allocate<SVG::SVGPolygonElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == SVG::TagNames::polyline)
return adopt_ref(*new SVG::SVGPolylineElement(document, move(qualified_name)));
return *realm.heap().allocate<SVG::SVGPolylineElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == SVG::TagNames::rect)
return adopt_ref(*new SVG::SVGRectElement(document, move(qualified_name)));
return *realm.heap().allocate<SVG::SVGRectElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == SVG::TagNames::g)
return adopt_ref(*new SVG::SVGGElement(document, move(qualified_name)));
return *realm.heap().allocate<SVG::SVGGElement>(realm, document, move(qualified_name));
if (lowercase_tag_name == SVG::TagNames::text)
return adopt_ref(*new SVG::SVGTextContentElement(document, move(qualified_name)));
return *realm.heap().allocate<SVG::SVGTextContentElement>(realm, document, move(qualified_name));
// FIXME: If name is a valid custom element name, then return HTMLElement.
return adopt_ref(*new HTML::HTMLUnknownElement(document, move(qualified_name)));
return *realm.heap().allocate<HTML::HTMLUnknownElement>(realm, document, move(qualified_name));
}
}