From 41f56b0df991e35943977116cbe4a2902285111c Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 24 Dec 2023 20:59:00 +0100 Subject: [PATCH] LibWeb: Let supported_property_names() return Vector Ultimately, this API should probably be replaced with something that updates a cache on relevant DOM mutations instead of regenerating the list of property names again and again. --- .../LibWeb/Bindings/LegacyPlatformObject.cpp | 10 +++++----- .../LibWeb/Bindings/LegacyPlatformObject.h | 2 +- Userland/Libraries/LibWeb/DOM/HTMLCollection.cpp | 6 +++--- Userland/Libraries/LibWeb/DOM/HTMLCollection.h | 2 +- Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp | 4 ++-- Userland/Libraries/LibWeb/DOM/NamedNodeMap.h | 2 +- Userland/Libraries/LibWeb/HTML/DOMStringMap.cpp | 6 +++--- Userland/Libraries/LibWeb/HTML/DOMStringMap.h | 2 +- Userland/Libraries/LibWeb/HTML/MimeTypeArray.cpp | 8 ++++---- Userland/Libraries/LibWeb/HTML/MimeTypeArray.h | 2 +- Userland/Libraries/LibWeb/HTML/Plugin.cpp | 8 ++++---- Userland/Libraries/LibWeb/HTML/Plugin.h | 2 +- Userland/Libraries/LibWeb/HTML/PluginArray.cpp | 14 +++++++------- Userland/Libraries/LibWeb/HTML/PluginArray.h | 2 +- Userland/Libraries/LibWeb/HTML/Storage.cpp | 4 ++-- Userland/Libraries/LibWeb/HTML/Storage.h | 2 +- Userland/Libraries/LibWeb/HTML/Window.cpp | 10 ++-------- Userland/Libraries/LibWeb/HTML/Window.h | 2 +- .../Libraries/LibWeb/WebIDL/AbstractOperations.cpp | 2 +- 19 files changed, 42 insertions(+), 48 deletions(-) diff --git a/Userland/Libraries/LibWeb/Bindings/LegacyPlatformObject.cpp b/Userland/Libraries/LibWeb/Bindings/LegacyPlatformObject.cpp index fdaf1363af1..64fe6b3b67b 100644 --- a/Userland/Libraries/LibWeb/Bindings/LegacyPlatformObject.cpp +++ b/Userland/Libraries/LibWeb/Bindings/LegacyPlatformObject.cpp @@ -126,7 +126,7 @@ WebIDL::ExceptionOr LegacyPlatformObject::invoke_indexed_property_setter(J WebIDL::ExceptionOr LegacyPlatformObject::invoke_named_property_setter(String const& property_name, JS::Value value) { // 1. Let creating be true if P is not a supported property name, and false otherwise. - Vector supported_property_names = this->supported_property_names(); + auto supported_property_names = this->supported_property_names(); bool creating = !supported_property_names.contains_slow(property_name); // FIXME: We do not have this information at this point, so converting the value is left as an exercise to the inheritor of LegacyPlatformObject. @@ -219,8 +219,8 @@ JS::ThrowCompletionOr LegacyPlatformObject::internal_define_own_property(J // 1. Let creating be true if P is not a supported property name, and false otherwise. // NOTE: This is in it's own variable to enforce the type. - Vector supported_property_names = this->supported_property_names(); - bool creating = !supported_property_names.contains_slow(MUST(String::from_byte_string(property_name_as_string))); + auto supported_property_names = this->supported_property_names(); + bool creating = !supported_property_names.contains_slow(MUST(FlyString::from_deprecated_fly_string(property_name_as_string))); // 2. If O implements an interface with the [LegacyOverrideBuiltIns] extended attribute or O does not have an own property named P, then: // NOTE: Own property lookup has to be done manually instead of using Object::has_own_property, as that would use the overridden internal_get_own_property. @@ -348,7 +348,7 @@ JS::ThrowCompletionOr> LegacyPlatformObject::interna // 3. If O supports named properties, then for each P of O’s supported property names that is visible according to the named property visibility algorithm, append P to keys. if (supports_named_properties()) { for (auto& named_property : supported_property_names()) { - if (TRY(WebIDL::is_named_property_exposed_on_object({ this }, named_property.to_byte_string()))) + if (TRY(WebIDL::is_named_property_exposed_on_object({ this }, named_property.to_deprecated_fly_string()))) keys.append(JS::PrimitiveString::create(vm, named_property)); } } @@ -381,7 +381,7 @@ WebIDL::ExceptionOr LegacyPlatformObject::named_item_value(FlyString return JS::js_undefined(); } -Vector LegacyPlatformObject::supported_property_names() const +Vector LegacyPlatformObject::supported_property_names() const { return {}; } diff --git a/Userland/Libraries/LibWeb/Bindings/LegacyPlatformObject.h b/Userland/Libraries/LibWeb/Bindings/LegacyPlatformObject.h index 7e43d6b74b8..2f147004acf 100644 --- a/Userland/Libraries/LibWeb/Bindings/LegacyPlatformObject.h +++ b/Userland/Libraries/LibWeb/Bindings/LegacyPlatformObject.h @@ -37,7 +37,7 @@ public: virtual WebIDL::ExceptionOr item_value(size_t index) const; virtual WebIDL::ExceptionOr named_item_value(FlyString const& name) const; - virtual Vector supported_property_names() const; + virtual Vector supported_property_names() const; virtual bool is_supported_property_index(u32) const; // NOTE: These will crash if you make has_named_property_setter return true but do not override these methods. diff --git a/Userland/Libraries/LibWeb/DOM/HTMLCollection.cpp b/Userland/Libraries/LibWeb/DOM/HTMLCollection.cpp index 8e4a2901c48..ed18a597e29 100644 --- a/Userland/Libraries/LibWeb/DOM/HTMLCollection.cpp +++ b/Userland/Libraries/LibWeb/DOM/HTMLCollection.cpp @@ -99,10 +99,10 @@ Element* HTMLCollection::named_item(FlyString const& name_) const } // https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-names -Vector HTMLCollection::supported_property_names() const +Vector HTMLCollection::supported_property_names() const { // 1. Let result be an empty list. - Vector result; + Vector result; // 2. For each element represented by the collection, in tree order: auto elements = collect_matching_elements(); @@ -119,7 +119,7 @@ Vector HTMLCollection::supported_property_names() const if (auto maybe_name = element->attribute(HTML::AttributeNames::name); maybe_name.has_value()) { auto name = maybe_name.release_value(); if (!name.is_empty() && !result.contains_slow(name)) - result.append(move(name)); + result.append(name); } } } diff --git a/Userland/Libraries/LibWeb/DOM/HTMLCollection.h b/Userland/Libraries/LibWeb/DOM/HTMLCollection.h index c18dd7bc97d..508a02e8f71 100644 --- a/Userland/Libraries/LibWeb/DOM/HTMLCollection.h +++ b/Userland/Libraries/LibWeb/DOM/HTMLCollection.h @@ -46,7 +46,7 @@ public: virtual WebIDL::ExceptionOr item_value(size_t index) const override; virtual WebIDL::ExceptionOr named_item_value(FlyString const& name) const override; - virtual Vector supported_property_names() const override; + virtual Vector supported_property_names() const override; virtual bool is_supported_property_index(u32) const override; protected: diff --git a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp index 10f19c0b139..d1b74e68fdd 100644 --- a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp +++ b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp @@ -49,10 +49,10 @@ bool NamedNodeMap::is_supported_property_index(u32 index) const } // https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-names%E2%91%A0 -Vector NamedNodeMap::supported_property_names() const +Vector NamedNodeMap::supported_property_names() const { // 1. Let names be the qualified names of the attributes in this NamedNodeMap object’s attribute list, with duplicates omitted, in order. - Vector names; + Vector names; names.ensure_capacity(m_attributes.size()); for (auto const& attribute : m_attributes) { diff --git a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.h b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.h index 375ada0bac8..a8137a6d126 100644 --- a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.h +++ b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.h @@ -26,7 +26,7 @@ public: ~NamedNodeMap() = default; virtual bool is_supported_property_index(u32 index) const override; - virtual Vector supported_property_names() const override; + virtual Vector supported_property_names() const override; virtual WebIDL::ExceptionOr item_value(size_t index) const override; virtual WebIDL::ExceptionOr named_item_value(FlyString const& name) const override; diff --git a/Userland/Libraries/LibWeb/HTML/DOMStringMap.cpp b/Userland/Libraries/LibWeb/HTML/DOMStringMap.cpp index 01e158fd6f4..ae06483c9fa 100644 --- a/Userland/Libraries/LibWeb/HTML/DOMStringMap.cpp +++ b/Userland/Libraries/LibWeb/HTML/DOMStringMap.cpp @@ -91,13 +91,13 @@ Vector DOMStringMap::get_name_value_pairs() const // https://html.spec.whatwg.org/multipage/dom.html#concept-domstringmap-pairs // NOTE: There isn't a direct link to this, so the link is to one of the algorithms above it. -Vector DOMStringMap::supported_property_names() const +Vector DOMStringMap::supported_property_names() const { // The supported property names on a DOMStringMap object at any instant are the names of each pair returned from getting the DOMStringMap's name-value pairs at that instant, in the order returned. - Vector names; + Vector names; auto name_value_pairs = get_name_value_pairs(); for (auto& name_value_pair : name_value_pairs) { - names.append(name_value_pair.name.to_string()); + names.append(name_value_pair.name); } return names; } diff --git a/Userland/Libraries/LibWeb/HTML/DOMStringMap.h b/Userland/Libraries/LibWeb/HTML/DOMStringMap.h index 13648b3f0b8..8ca05b243f1 100644 --- a/Userland/Libraries/LibWeb/HTML/DOMStringMap.h +++ b/Userland/Libraries/LibWeb/HTML/DOMStringMap.h @@ -37,7 +37,7 @@ private: // ^LegacyPlatformObject virtual WebIDL::ExceptionOr named_item_value(FlyString const&) const override; - virtual Vector supported_property_names() const override; + virtual Vector supported_property_names() const override; virtual bool supports_indexed_properties() const override { return false; } virtual bool supports_named_properties() const override { return true; } diff --git a/Userland/Libraries/LibWeb/HTML/MimeTypeArray.cpp b/Userland/Libraries/LibWeb/HTML/MimeTypeArray.cpp index c76a1b8f18f..508e272914b 100644 --- a/Userland/Libraries/LibWeb/HTML/MimeTypeArray.cpp +++ b/Userland/Libraries/LibWeb/HTML/MimeTypeArray.cpp @@ -28,7 +28,7 @@ void MimeTypeArray::initialize(JS::Realm& realm) } // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewing-support:support-named-properties-2 -Vector MimeTypeArray::supported_property_names() const +Vector MimeTypeArray::supported_property_names() const { // The MimeTypeArray interface supports named properties. If the user agent's PDF viewer supported is true, then they are the PDF viewer mime types. Otherwise, they are the empty list. auto const& window = verify_cast(HTML::relevant_global_object(*this)); @@ -36,9 +36,9 @@ Vector MimeTypeArray::supported_property_names() const return {}; // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewer-mime-types - static Vector const mime_types = { - "application/pdf"_string, - "text/pdf"_string, + static Vector const mime_types = { + "application/pdf"_fly_string, + "text/pdf"_fly_string, }; return mime_types; diff --git a/Userland/Libraries/LibWeb/HTML/MimeTypeArray.h b/Userland/Libraries/LibWeb/HTML/MimeTypeArray.h index f0c267304d2..cd734c2f0da 100644 --- a/Userland/Libraries/LibWeb/HTML/MimeTypeArray.h +++ b/Userland/Libraries/LibWeb/HTML/MimeTypeArray.h @@ -28,7 +28,7 @@ private: virtual void initialize(JS::Realm&) override; // ^Bindings::LegacyPlatformObject - virtual Vector supported_property_names() const override; + virtual Vector supported_property_names() const override; virtual WebIDL::ExceptionOr item_value(size_t index) const override; virtual WebIDL::ExceptionOr named_item_value(FlyString const& name) const override; virtual bool is_supported_property_index(u32) const override; diff --git a/Userland/Libraries/LibWeb/HTML/Plugin.cpp b/Userland/Libraries/LibWeb/HTML/Plugin.cpp index 5d459965e09..99f482d54bf 100644 --- a/Userland/Libraries/LibWeb/HTML/Plugin.cpp +++ b/Userland/Libraries/LibWeb/HTML/Plugin.cpp @@ -52,7 +52,7 @@ String Plugin::filename() const } // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewing-support:support-named-properties-3 -Vector Plugin::supported_property_names() const +Vector Plugin::supported_property_names() const { // The Plugin interface supports named properties. If the user agent's PDF viewer supported is true, then they are the PDF viewer mime types. Otherwise, they are the empty list. auto const& window = verify_cast(HTML::relevant_global_object(*this)); @@ -60,9 +60,9 @@ Vector Plugin::supported_property_names() const return {}; // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewer-mime-types - static Vector const mime_types = { - "application/pdf"_string, - "text/pdf"_string, + static Vector const mime_types = { + "application/pdf"_fly_string, + "text/pdf"_fly_string, }; return mime_types; diff --git a/Userland/Libraries/LibWeb/HTML/Plugin.h b/Userland/Libraries/LibWeb/HTML/Plugin.h index 5963e633ff4..72feb5f04e7 100644 --- a/Userland/Libraries/LibWeb/HTML/Plugin.h +++ b/Userland/Libraries/LibWeb/HTML/Plugin.h @@ -34,7 +34,7 @@ private: virtual void initialize(JS::Realm&) override; // ^Bindings::LegacyPlatformObject - virtual Vector supported_property_names() const override; + virtual Vector supported_property_names() const override; virtual WebIDL::ExceptionOr item_value(size_t index) const override; virtual WebIDL::ExceptionOr named_item_value(FlyString const& name) const override; virtual bool is_supported_property_index(u32) const override; diff --git a/Userland/Libraries/LibWeb/HTML/PluginArray.cpp b/Userland/Libraries/LibWeb/HTML/PluginArray.cpp index fd086a7bc11..ed47c474390 100644 --- a/Userland/Libraries/LibWeb/HTML/PluginArray.cpp +++ b/Userland/Libraries/LibWeb/HTML/PluginArray.cpp @@ -34,7 +34,7 @@ void PluginArray::refresh() const } // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewing-support:support-named-properties -Vector PluginArray::supported_property_names() const +Vector PluginArray::supported_property_names() const { // The PluginArray interface supports named properties. If the user agent's PDF viewer supported is true, then they are the PDF viewer plugin names. Otherwise, they are the empty list. auto const& window = verify_cast(HTML::relevant_global_object(*this)); @@ -42,12 +42,12 @@ Vector PluginArray::supported_property_names() const return {}; // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewer-plugin-names - static Vector const plugin_names = { - "PDF Viewer"_string, - "Chrome PDF Viewer"_string, - "Chromium PDF Viewer"_string, - "Microsoft Edge PDF Viewer"_string, - "WebKit built-in PDF"_string, + static Vector const plugin_names = { + "PDF Viewer"_fly_string, + "Chrome PDF Viewer"_fly_string, + "Chromium PDF Viewer"_fly_string, + "Microsoft Edge PDF Viewer"_fly_string, + "WebKit built-in PDF"_fly_string, }; return plugin_names; diff --git a/Userland/Libraries/LibWeb/HTML/PluginArray.h b/Userland/Libraries/LibWeb/HTML/PluginArray.h index ed263470112..d4e74ac0b78 100644 --- a/Userland/Libraries/LibWeb/HTML/PluginArray.h +++ b/Userland/Libraries/LibWeb/HTML/PluginArray.h @@ -29,7 +29,7 @@ private: virtual void initialize(JS::Realm&) override; // ^Bindings::LegacyPlatformObject - virtual Vector supported_property_names() const override; + virtual Vector supported_property_names() const override; virtual WebIDL::ExceptionOr item_value(size_t index) const override; virtual WebIDL::ExceptionOr named_item_value(FlyString const& name) const override; virtual bool is_supported_property_index(u32) const override; diff --git a/Userland/Libraries/LibWeb/HTML/Storage.cpp b/Userland/Libraries/LibWeb/HTML/Storage.cpp index 20ececb9800..b2caff3950c 100644 --- a/Userland/Libraries/LibWeb/HTML/Storage.cpp +++ b/Userland/Libraries/LibWeb/HTML/Storage.cpp @@ -149,10 +149,10 @@ void Storage::broadcast(StringView key, StringView old_value, StringView new_val // FIXME: Implement. } -Vector Storage::supported_property_names() const +Vector Storage::supported_property_names() const { // The supported property names on a Storage object storage are the result of running get the keys on storage's map. - Vector names; + Vector names; names.ensure_capacity(m_map.size()); for (auto const& key : m_map.keys()) names.unchecked_append(key); diff --git a/Userland/Libraries/LibWeb/HTML/Storage.h b/Userland/Libraries/LibWeb/HTML/Storage.h index b13f632a5a1..97e4baec1df 100644 --- a/Userland/Libraries/LibWeb/HTML/Storage.h +++ b/Userland/Libraries/LibWeb/HTML/Storage.h @@ -40,7 +40,7 @@ private: // ^LegacyPlatformObject virtual WebIDL::ExceptionOr named_item_value(FlyString const&) const override; virtual WebIDL::ExceptionOr delete_value(String const&) override; - virtual Vector supported_property_names() const override; + virtual Vector supported_property_names() const override; virtual WebIDL::ExceptionOr set_value_of_named_property(String const& key, JS::Value value) override; virtual bool supports_indexed_properties() const override { return false; } diff --git a/Userland/Libraries/LibWeb/HTML/Window.cpp b/Userland/Libraries/LibWeb/HTML/Window.cpp index 52f6a70512f..b3285e36794 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.cpp +++ b/Userland/Libraries/LibWeb/HTML/Window.cpp @@ -1548,7 +1548,7 @@ OrderedHashMap> Window::document_tree_child_ } // https://html.spec.whatwg.org/#named-access-on-the-window-object -Vector Window::supported_property_names() +Vector Window::supported_property_names() { // The Window object supports named properties. // The supported property names of a Window object window at any moment consist of the following, @@ -1575,13 +1575,7 @@ Vector Window::supported_property_names() return IterationDecision::Continue; }); - Vector names; - names.ensure_capacity(property_names.size()); - for (auto const& name : property_names) { - names.append(name.to_string()); - } - - return names; + return property_names.values(); } // https://html.spec.whatwg.org/#named-access-on-the-window-object diff --git a/Userland/Libraries/LibWeb/HTML/Window.h b/Userland/Libraries/LibWeb/HTML/Window.h index 2358c2bf30c..575e07ef26e 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.h +++ b/Userland/Libraries/LibWeb/HTML/Window.h @@ -203,7 +203,7 @@ public: [[nodiscard]] OrderedHashMap> document_tree_child_navigable_target_name_property_set(); - [[nodiscard]] Vector supported_property_names(); + [[nodiscard]] Vector supported_property_names(); [[nodiscard]] WebIDL::ExceptionOr named_item_value(FlyString const&); private: diff --git a/Userland/Libraries/LibWeb/WebIDL/AbstractOperations.cpp b/Userland/Libraries/LibWeb/WebIDL/AbstractOperations.cpp index 3aa6393b547..c4625fc07e4 100644 --- a/Userland/Libraries/LibWeb/WebIDL/AbstractOperations.cpp +++ b/Userland/Libraries/LibWeb/WebIDL/AbstractOperations.cpp @@ -317,7 +317,7 @@ JS::ThrowCompletionOr is_named_property_exposed_on_object(Variant supported_property_names = variant.visit([](auto* o) { return o->supported_property_names(); }); + auto supported_property_names = variant.visit([](auto* o) { return o->supported_property_names(); }); auto property_key_string = MUST(String::from_byte_string(property_key.to_string())); if (!supported_property_names.contains_slow(property_key_string)) return false;