1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 00:37:35 +00:00

LibWeb: Delete LegacyPlatformObject and move behavior to PlatformObject

We have two known PlatformObjects that need to implement some of the
behavior of LegacyPlatformObjects to date: Window, and HTMLFormElement.

To make this not require double (or virtual) inheritance of
PlatformObject, move the behavior of LegacyPlatformObject into
PlatformObject. The selection of LegacyPlatformObject behavior is done
with a new bitfield of feature flags instead of a dozen virtual
functions that return bool. This change simplifies every class involved
in the diff with the notable exception of Window, which now needs some
ugly const casts to implement named property access.
This commit is contained in:
Andrew Kaster 2024-01-09 16:05:03 -07:00 committed by Andreas Kling
parent 6047f1adcb
commit 521ed0e911
41 changed files with 684 additions and 801 deletions

View file

@ -21,9 +21,15 @@ JS::NonnullGCPtr<DOMStringMap> DOMStringMap::create(DOM::Element& element)
}
DOMStringMap::DOMStringMap(DOM::Element& element)
: LegacyPlatformObject(element.realm())
: PlatformObject(element.realm())
, m_associated_element(element)
{
m_legacy_platform_object_flags = LegacyPlatformObjectFlags {
.supports_named_properties = true,
.has_named_property_setter = true,
.has_named_property_deleter = true,
.has_legacy_override_built_ins_interface_extended_attribute = true,
};
}
DOMStringMap::~DOMStringMap() = default;
@ -121,7 +127,7 @@ String DOMStringMap::determine_value_of_named_property(FlyString const& name) co
// https://html.spec.whatwg.org/multipage/dom.html#dom-domstringmap-setitem
WebIDL::ExceptionOr<void> DOMStringMap::set_value_of_new_named_property(String const& name, JS::Value unconverted_value)
{
// NOTE: Since LegacyPlatformObject does not know the type of value, we must convert it ourselves.
// NOTE: Since PlatformObject does not know the type of value, we must convert it ourselves.
// The type of `value` is `DOMString`.
auto value = TRY(unconverted_value.to_string(vm()));
@ -170,7 +176,7 @@ WebIDL::ExceptionOr<void> DOMStringMap::set_value_of_existing_named_property(Str
}
// https://html.spec.whatwg.org/multipage/dom.html#dom-domstringmap-removeitem
WebIDL::ExceptionOr<Bindings::LegacyPlatformObject::DidDeletionFail> DOMStringMap::delete_value(String const& name)
WebIDL::ExceptionOr<Bindings::PlatformObject::DidDeletionFail> DOMStringMap::delete_value(String const& name)
{
StringBuilder builder;

View file

@ -7,14 +7,14 @@
#pragma once
#include <LibWeb/Bindings/LegacyPlatformObject.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/Forward.h>
namespace Web::HTML {
// https://html.spec.whatwg.org/multipage/dom.html#domstringmap
class DOMStringMap final : public Bindings::LegacyPlatformObject {
WEB_PLATFORM_OBJECT(DOMStringMap, Bindings::LegacyPlatformObject);
class DOMStringMap final : public Bindings::PlatformObject {
WEB_PLATFORM_OBJECT(DOMStringMap, Bindings::PlatformObject);
JS_DECLARE_ALLOCATOR(DOMStringMap);
public:
@ -35,22 +35,10 @@ private:
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override;
// ^LegacyPlatformObject
// ^PlatformObject
virtual WebIDL::ExceptionOr<JS::Value> named_item_value(FlyString const&) const override;
virtual Vector<FlyString> supported_property_names() const override;
virtual bool supports_indexed_properties() const override { return false; }
virtual bool supports_named_properties() const override { return true; }
virtual bool has_indexed_property_setter() const override { return false; }
virtual bool has_named_property_setter() const override { return true; }
virtual bool has_named_property_deleter() const override { return true; }
virtual bool has_legacy_override_built_ins_interface_extended_attribute() const override { return true; }
virtual bool has_legacy_unenumerable_named_properties_interface_extended_attribute() const override { return false; }
virtual bool has_global_interface_extended_attribute() const override { return false; }
virtual bool indexed_property_setter_has_identifier() const override { return false; }
virtual bool named_property_setter_has_identifier() const override { return false; }
virtual bool named_property_deleter_has_identifier() const override { return false; }
struct NameValuePair {
FlyString name;
String value;

View file

@ -6,7 +6,7 @@
#pragma once
#include <LibWeb/Bindings/LegacyPlatformObject.h>
#include <LibWeb/Bindings/PlatformObject.h>
namespace Web::HTML {

View file

@ -15,8 +15,13 @@ namespace Web::HTML {
JS_DEFINE_ALLOCATOR(MimeTypeArray);
MimeTypeArray::MimeTypeArray(JS::Realm& realm)
: Bindings::LegacyPlatformObject(realm)
: Bindings::PlatformObject(realm)
{
m_legacy_platform_object_flags = LegacyPlatformObjectFlags {
.supports_indexed_properties = true,
.supports_named_properties = true,
.has_legacy_unenumerable_named_properties_interface_extended_attribute = true,
};
}
MimeTypeArray::~MimeTypeArray() = default;

View file

@ -6,13 +6,13 @@
#pragma once
#include <LibWeb/Bindings/LegacyPlatformObject.h>
#include <LibWeb/Bindings/PlatformObject.h>
namespace Web::HTML {
// https://html.spec.whatwg.org/multipage/system-state.html#mimetypearray
class MimeTypeArray : public Bindings::LegacyPlatformObject {
WEB_PLATFORM_OBJECT(MimeTypeArray, Bindings::LegacyPlatformObject);
class MimeTypeArray : public Bindings::PlatformObject {
WEB_PLATFORM_OBJECT(MimeTypeArray, Bindings::PlatformObject);
JS_DECLARE_ALLOCATOR(MimeTypeArray);
public:
@ -27,23 +27,11 @@ private:
virtual void initialize(JS::Realm&) override;
// ^Bindings::LegacyPlatformObject
// ^Bindings::PlatformObject
virtual Vector<FlyString> supported_property_names() const override;
virtual WebIDL::ExceptionOr<JS::Value> item_value(size_t index) const override;
virtual WebIDL::ExceptionOr<JS::Value> named_item_value(FlyString const& name) const override;
virtual bool is_supported_property_index(u32) const override;
virtual bool supports_indexed_properties() const override { return true; }
virtual bool supports_named_properties() const override { return true; }
virtual bool has_indexed_property_setter() const override { return false; }
virtual bool has_named_property_setter() const override { return false; }
virtual bool has_named_property_deleter() const override { return false; }
virtual bool has_legacy_override_built_ins_interface_extended_attribute() const override { return false; }
virtual bool has_legacy_unenumerable_named_properties_interface_extended_attribute() const override { return true; }
virtual bool has_global_interface_extended_attribute() const override { return false; }
virtual bool indexed_property_setter_has_identifier() const override { return false; }
virtual bool named_property_setter_has_identifier() const override { return false; }
virtual bool named_property_deleter_has_identifier() const override { return false; }
};
}

View file

@ -15,9 +15,14 @@ namespace Web::HTML {
JS_DEFINE_ALLOCATOR(Plugin);
Plugin::Plugin(JS::Realm& realm, String name)
: Bindings::LegacyPlatformObject(realm)
: Bindings::PlatformObject(realm)
, m_name(move(name))
{
m_legacy_platform_object_flags = LegacyPlatformObjectFlags {
.supports_indexed_properties = true,
.supports_named_properties = true,
.has_legacy_unenumerable_named_properties_interface_extended_attribute = true,
};
}
Plugin::~Plugin() = default;

View file

@ -6,13 +6,13 @@
#pragma once
#include <LibWeb/Bindings/LegacyPlatformObject.h>
#include <LibWeb/Bindings/PlatformObject.h>
namespace Web::HTML {
// https://html.spec.whatwg.org/multipage/system-state.html#dom-plugin
class Plugin : public Bindings::LegacyPlatformObject {
WEB_PLATFORM_OBJECT(Plugin, Bindings::LegacyPlatformObject);
class Plugin : public Bindings::PlatformObject {
WEB_PLATFORM_OBJECT(Plugin, Bindings::PlatformObject);
JS_DECLARE_ALLOCATOR(Plugin);
public:
@ -33,23 +33,11 @@ private:
virtual void initialize(JS::Realm&) override;
// ^Bindings::LegacyPlatformObject
// ^Bindings::PlatformObject
virtual Vector<FlyString> supported_property_names() const override;
virtual WebIDL::ExceptionOr<JS::Value> item_value(size_t index) const override;
virtual WebIDL::ExceptionOr<JS::Value> named_item_value(FlyString const& name) const override;
virtual bool is_supported_property_index(u32) const override;
virtual bool supports_indexed_properties() const override { return true; }
virtual bool supports_named_properties() const override { return true; }
virtual bool has_indexed_property_setter() const override { return false; }
virtual bool has_named_property_setter() const override { return false; }
virtual bool has_named_property_deleter() const override { return false; }
virtual bool has_legacy_override_built_ins_interface_extended_attribute() const override { return false; }
virtual bool has_legacy_unenumerable_named_properties_interface_extended_attribute() const override { return true; }
virtual bool has_global_interface_extended_attribute() const override { return false; }
virtual bool indexed_property_setter_has_identifier() const override { return false; }
virtual bool named_property_setter_has_identifier() const override { return false; }
virtual bool named_property_deleter_has_identifier() const override { return false; }
};
}

View file

@ -15,8 +15,13 @@ namespace Web::HTML {
JS_DEFINE_ALLOCATOR(PluginArray);
PluginArray::PluginArray(JS::Realm& realm)
: Bindings::LegacyPlatformObject(realm)
: Bindings::PlatformObject(realm)
{
m_legacy_platform_object_flags = LegacyPlatformObjectFlags {
.supports_indexed_properties = true,
.supports_named_properties = true,
.has_legacy_unenumerable_named_properties_interface_extended_attribute = true,
};
}
PluginArray::~PluginArray() = default;

View file

@ -6,13 +6,13 @@
#pragma once
#include <LibWeb/Bindings/LegacyPlatformObject.h>
#include <LibWeb/Bindings/PlatformObject.h>
namespace Web::HTML {
// https://html.spec.whatwg.org/multipage/system-state.html#pluginarray
class PluginArray : public Bindings::LegacyPlatformObject {
WEB_PLATFORM_OBJECT(PluginArray, Bindings::LegacyPlatformObject);
class PluginArray : public Bindings::PlatformObject {
WEB_PLATFORM_OBJECT(PluginArray, Bindings::PlatformObject);
JS_DECLARE_ALLOCATOR(PluginArray);
public:
@ -28,23 +28,11 @@ private:
virtual void initialize(JS::Realm&) override;
// ^Bindings::LegacyPlatformObject
// ^Bindings::PlatformObject
virtual Vector<FlyString> supported_property_names() const override;
virtual WebIDL::ExceptionOr<JS::Value> item_value(size_t index) const override;
virtual WebIDL::ExceptionOr<JS::Value> named_item_value(FlyString const& name) const override;
virtual bool is_supported_property_index(u32) const override;
virtual bool supports_indexed_properties() const override { return true; }
virtual bool supports_named_properties() const override { return true; }
virtual bool has_indexed_property_setter() const override { return false; }
virtual bool has_named_property_setter() const override { return false; }
virtual bool has_named_property_deleter() const override { return false; }
virtual bool has_legacy_override_built_ins_interface_extended_attribute() const override { return false; }
virtual bool has_legacy_unenumerable_named_properties_interface_extended_attribute() const override { return true; }
virtual bool has_global_interface_extended_attribute() const override { return false; }
virtual bool indexed_property_setter_has_identifier() const override { return false; }
virtual bool named_property_setter_has_identifier() const override { return false; }
virtual bool named_property_deleter_has_identifier() const override { return false; }
};
}

View file

@ -19,8 +19,16 @@ JS::NonnullGCPtr<Storage> Storage::create(JS::Realm& realm)
}
Storage::Storage(JS::Realm& realm)
: Bindings::LegacyPlatformObject(realm)
: Bindings::PlatformObject(realm)
{
m_legacy_platform_object_flags = LegacyPlatformObjectFlags {
.supports_named_properties = true,
.has_named_property_setter = true,
.has_named_property_deleter = true,
.has_legacy_override_built_ins_interface_extended_attribute = true,
.named_property_setter_has_identifier = true,
.named_property_deleter_has_identifier = true,
};
}
Storage::~Storage() = default;
@ -167,7 +175,7 @@ WebIDL::ExceptionOr<JS::Value> Storage::named_item_value(FlyString const& name)
return JS::PrimitiveString::create(vm(), value.release_value());
}
WebIDL::ExceptionOr<Bindings::LegacyPlatformObject::DidDeletionFail> Storage::delete_value(String const& name)
WebIDL::ExceptionOr<Bindings::PlatformObject::DidDeletionFail> Storage::delete_value(String const& name)
{
remove_item(name);
return DidDeletionFail::NotRelevant;
@ -175,7 +183,7 @@ WebIDL::ExceptionOr<Bindings::LegacyPlatformObject::DidDeletionFail> Storage::de
WebIDL::ExceptionOr<void> Storage::set_value_of_named_property(String const& key, JS::Value unconverted_value)
{
// NOTE: Since LegacyPlatformObject does not know the type of value, we must convert it ourselves.
// NOTE: Since PlatformObject does not know the type of value, we must convert it ourselves.
// The type of `value` is `DOMString`.
auto value = TRY(unconverted_value.to_string(vm()));
return set_item(key, value);

View file

@ -8,13 +8,13 @@
#pragma once
#include <AK/HashMap.h>
#include <LibWeb/Bindings/LegacyPlatformObject.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/WebIDL/ExceptionOr.h>
namespace Web::HTML {
class Storage : public Bindings::LegacyPlatformObject {
WEB_PLATFORM_OBJECT(Storage, Bindings::LegacyPlatformObject);
class Storage : public Bindings::PlatformObject {
WEB_PLATFORM_OBJECT(Storage, Bindings::PlatformObject);
JS_DECLARE_ALLOCATOR(Storage);
public:
@ -37,24 +37,12 @@ private:
virtual void initialize(JS::Realm&) override;
// ^LegacyPlatformObject
// ^PlatformObject
virtual WebIDL::ExceptionOr<JS::Value> named_item_value(FlyString const&) const override;
virtual WebIDL::ExceptionOr<DidDeletionFail> delete_value(String const&) override;
virtual Vector<FlyString> supported_property_names() const override;
virtual WebIDL::ExceptionOr<void> set_value_of_named_property(String const& key, JS::Value value) override;
virtual bool supports_indexed_properties() const override { return false; }
virtual bool supports_named_properties() const override { return true; }
virtual bool has_indexed_property_setter() const override { return false; }
virtual bool has_named_property_setter() const override { return true; }
virtual bool has_named_property_deleter() const override { return true; }
virtual bool has_legacy_override_built_ins_interface_extended_attribute() const override { return true; }
virtual bool has_legacy_unenumerable_named_properties_interface_extended_attribute() const override { return false; }
virtual bool has_global_interface_extended_attribute() const override { return false; }
virtual bool indexed_property_setter_has_identifier() const override { return false; }
virtual bool named_property_setter_has_identifier() const override { return true; }
virtual bool named_property_deleter_has_identifier() const override { return true; }
void reorder();
void broadcast(StringView key, StringView old_value, StringView new_value);

View file

@ -106,6 +106,11 @@ JS::NonnullGCPtr<Window> Window::create(JS::Realm& realm)
Window::Window(JS::Realm& realm)
: DOM::EventTarget(realm)
{
m_legacy_platform_object_flags = LegacyPlatformObjectFlags {
.supports_named_properties = true,
.has_legacy_unenumerable_named_properties_interface_extended_attribute = true,
.has_global_interface_extended_attribute = true,
};
}
void Window::visit_edges(JS::Cell::Visitor& visitor)
@ -1560,8 +1565,11 @@ OrderedHashMap<FlyString, JS::NonnullGCPtr<Navigable>> Window::document_tree_chi
}
// https://html.spec.whatwg.org/#named-access-on-the-window-object
Vector<FlyString> Window::supported_property_names()
Vector<FlyString> Window::supported_property_names() const
{
// FIXME: Make the const-correctness of the methods this method calls less cowboy.
auto& mutable_this = const_cast<Window&>(*this);
// The Window object supports named properties.
// The supported property names of a Window object window at any moment consist of the following,
// in tree order according to the element that contributed them, ignoring later duplicates:
@ -1569,7 +1577,7 @@ Vector<FlyString> Window::supported_property_names()
HashTable<FlyString> property_names;
// - window's document-tree child navigable target name property set;
auto child_navigable_property_set = document_tree_child_navigable_target_name_property_set();
auto child_navigable_property_set = mutable_this.document_tree_child_navigable_target_name_property_set();
for (auto& entry : child_navigable_property_set)
property_names.set(entry.key, AK::HashSetExistingEntryBehavior::Keep);
@ -1591,19 +1599,22 @@ Vector<FlyString> Window::supported_property_names()
}
// https://html.spec.whatwg.org/#named-access-on-the-window-object
WebIDL::ExceptionOr<JS::Value> Window::named_item_value(FlyString const& name)
WebIDL::ExceptionOr<JS::Value> Window::named_item_value(FlyString const& name) const
{
// FIXME: Make the const-correctness of the methods this method calls less cowboy.
auto& mutable_this = const_cast<Window&>(*this);
// To determine the value of a named property name in a Window object window, the user agent must return the value obtained using the following steps:
// 1. Let objects be the list of named objects of window with the name name.
// NOTE: There will be at least one such object, since the algorithm would otherwise not have been invoked by Web IDL.
auto objects = named_objects(name);
auto objects = mutable_this.named_objects(name);
// 2. If objects contains a navigable, then:
if (!objects.navigables.is_empty()) {
// 1. Let container be the first navigable container in window's associated Document's descendants whose content navigable is in objects.
JS::GCPtr<NavigableContainer> container = nullptr;
associated_document().for_each_in_subtree_of_type<HTML::NavigableContainer>([&](HTML::NavigableContainer& navigable_container) {
mutable_this.associated_document().for_each_in_subtree_of_type<HTML::NavigableContainer>([&](HTML::NavigableContainer& navigable_container) {
if (!navigable_container.content_navigable())
return IterationDecision::Continue;
if (objects.navigables.contains_slow(JS::NonnullGCPtr { *navigable_container.content_navigable() })) {
@ -1623,7 +1634,7 @@ WebIDL::ExceptionOr<JS::Value> Window::named_item_value(FlyString const& name)
// 4. Otherwise return an HTMLCollection rooted at window's associated Document,
// whose filter matches only named objects of window with the name name. (By definition, these will all be elements.)
return DOM::HTMLCollection::create(associated_document(), DOM::HTMLCollection::Scope::Descendants, [name](auto& element) -> bool {
return DOM::HTMLCollection::create(mutable_this.associated_document(), DOM::HTMLCollection::Scope::Descendants, [name](auto& element) -> bool {
if ((is<HTMLEmbedElement>(element) || is<HTMLFormElement>(element) || is<HTMLImageElement>(element) || is<HTMLObjectElement>(element))
&& (element.attribute(AttributeNames::name) == name))
return true;

View file

@ -203,8 +203,8 @@ public:
[[nodiscard]] OrderedHashMap<FlyString, JS::NonnullGCPtr<Navigable>> document_tree_child_navigable_target_name_property_set();
[[nodiscard]] Vector<FlyString> supported_property_names();
[[nodiscard]] WebIDL::ExceptionOr<JS::Value> named_item_value(FlyString const&);
[[nodiscard]] Vector<FlyString> supported_property_names() const override;
[[nodiscard]] WebIDL::ExceptionOr<JS::Value> named_item_value(FlyString const&) const override;
private:
explicit Window(JS::Realm&);