Truly independent web browser
LibWeb: Enumerate an element’s attribute names in linear time
sideshowbarkera77aced1a001
diff --git a/Libraries/LibWeb/Bindings/PlatformObject.cpp b/Libraries/LibWeb/Bindings/PlatformObject.cppindex e6ff73d7398..dbb1c300293 100644--- a/Libraries/LibWeb/Bindings/PlatformObject.cpp+++ b/Libraries/LibWeb/Bindings/PlatformObject.cpp@@ -141,7 +141,7 @@ static bool is_supported_property_index(JS::HostObject const& wrapper, u32 index}// https://webidl.spec.whatwg.org/#dfn-named-property-visibility-JS::ThrowCompletionOr<bool> is_named_property_exposed_on_object(JS::HostObject const& wrapper, JS::PropertyKey const& property_key)+JS::ThrowCompletionOr<bool> is_named_property_exposed_on_object(JS::HostObject const& wrapper, JS::PropertyKey const& property_key, NameIsSupported name_is_supported){// The spec doesn't say anything about the type of the property name here.// Numbers can be converted to a string, which is fine and what other engines do.@@ -166,8 +166,7 @@ JS::ThrowCompletionOr<bool> is_named_property_exposed_on_object(JS::HostObject c}// 1. If P is not a supported property name of O, then return false.- auto property_name = property_key_to_utf16_fly_string(property_key);- if (!is_supported_property_name(wrapper, property_name))+ if (name_is_supported == NameIsSupported::No && !is_supported_property_name(wrapper, property_key_to_utf16_fly_string(property_key)))return false;// 2. If O has an own property named P, then return false.@@ -657,7 +656,7 @@ JS::ThrowCompletionOr<void> legacy_platform_object_internal_own_property_keys(JS// 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 (legacy_info->supports_named_properties) {for (auto& named_property : supported_property_names(wrapper)) {- if (TRY(is_named_property_exposed_on_object(wrapper, named_property)))+ if (TRY(is_named_property_exposed_on_object(wrapper, named_property, NameIsSupported::Yes)))append_to_keys(JS::PrimitiveString::create(vm, named_property));}}diff --git a/Libraries/LibWeb/Bindings/PlatformObject.h b/Libraries/LibWeb/Bindings/PlatformObject.hindex 74f1c71aee5..d30d9f481e6 100644--- a/Libraries/LibWeb/Bindings/PlatformObject.h+++ b/Libraries/LibWeb/Bindings/PlatformObject.h@@ -125,8 +125,13 @@ void finalize_platform_object(JS::HostObject& wrapper);}// https://webidl.spec.whatwg.org/#dfn-named-property-visibility-// The wrapper must be a legacy platform object or the wrapper of a [Global] interface.-WEB_API JS::ThrowCompletionOr<bool> is_named_property_exposed_on_object(JS::HostObject const& wrapper, JS::PropertyKey const&);+// The wrapper must be a legacy platform object or the wrapper of a [Global] interface. A caller that took the name from+// the object's supported property names passes NameIsSupported::Yes, which skips checking that again.+enum class NameIsSupported {+ No,+ Yes,+};+WEB_API JS::ThrowCompletionOr<bool> is_named_property_exposed_on_object(JS::HostObject const& wrapper, JS::PropertyKey const&, NameIsSupported = NameIsSupported::No);// Defines the property via OrdinaryDefineOwnProperty and, if that created a new own property,// preserves the object's wrapper so the expando stays alive as long as the wrappable does.diff --git a/Libraries/LibWeb/DOM/NamedNodeMap.cpp b/Libraries/LibWeb/DOM/NamedNodeMap.cppindex 01fafab1aea..7c930d5df79 100644--- a/Libraries/LibWeb/DOM/NamedNodeMap.cpp+++ b/Libraries/LibWeb/DOM/NamedNodeMap.cpp@@ -6,6 +6,7 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <AK/HashTable.h>#include <LibGC/Heap.h>#include <LibWeb/Bindings/Wrappable.h>#include <LibWeb/DOM/Attr.h>@@ -75,10 +76,11 @@ Vector<Utf16FlyString> 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<Utf16FlyString> names;names.ensure_capacity(length());+ HashTable<Utf16FlyString> seen_names;for (size_t index = 0; index < length(); ++index) {auto name = associated_element().m_attributes->at(index).name.as_string();- if (!names.contains_slow(name))+ if (seen_names.set(name) == HashSetResult::InsertedNewEntry)names.append(move(name));}@@ -93,6 +95,21 @@ Vector<Utf16FlyString> NamedNodeMap::supported_property_names() constreturn names;}+// OPTIMIZATION: A name is one of supported_property_names() if an attribute in the list has it as its qualified name,+// and it has no ASCII uppercase when the element is in the HTML namespace and its node document is an HTML+// document. Checking that directly avoids building the whole list for each name.+bool NamedNodeMap::is_supported_property_name(Utf16FlyString const& name) const+{+ if (associated_element().namespace_uri() == Namespace::HTML && associated_element().document().is_html_document() && contains_ascii_uppercase(name.view()))+ return false;++ for (size_t index = 0; index < length(); ++index) {+ if (associated_element().m_attributes->at(index).name.as_string() == name)+ return true;+ }+ return false;+}+// https://dom.spec.whatwg.org/#dom-namednodemap-itemGC::Ptr<Attr> NamedNodeMap::item(u32 index) const{diff --git a/Libraries/LibWeb/DOM/NamedNodeMap.h b/Libraries/LibWeb/DOM/NamedNodeMap.hindex 3441dab4317..5db433a4775 100644--- a/Libraries/LibWeb/DOM/NamedNodeMap.h+++ b/Libraries/LibWeb/DOM/NamedNodeMap.h@@ -27,6 +27,7 @@ public:~NamedNodeMap() = default;virtual Vector<Utf16FlyString> supported_property_names() const override;+ virtual bool is_supported_property_name(Utf16FlyString const&) const override;size_t length() const;bool is_empty() const { return length() == 0; }diff --git a/Tests/LibWeb/Text/expected/DOM/NamedNodeMap-many-attributes.txt b/Tests/LibWeb/Text/expected/DOM/NamedNodeMap-many-attributes.txtnew file mode 100644index 00000000000..fbc20e43c0a--- /dev/null+++ b/Tests/LibWeb/Text/expected/DOM/NamedNodeMap-many-attributes.txt@@ -0,0 +1,7 @@+Indices: 3003+Named properties: 3001+First: data-attribute-0, last: duplicate+Uppercase exposed: false+Duplicate listed once: true+"data-attribute-1234" in attributes: true+"missing" in attributes: falsediff --git a/Tests/LibWeb/Text/expected/wpt-import/dom/collections/namednodemap-supported-property-names.txt b/Tests/LibWeb/Text/expected/wpt-import/dom/collections/namednodemap-supported-property-names.txtnew file mode 100644index 00000000000..b81c9b6da60--- /dev/null+++ b/Tests/LibWeb/Text/expected/wpt-import/dom/collections/namednodemap-supported-property-names.txt@@ -0,0 +1,8 @@+Harness status: OK++Found 3 tests++3 Pass+Pass Object.getOwnPropertyNames on NamedNodeMap+Pass Object.getOwnPropertyNames on NamedNodeMap of input+Pass Object.getOwnPropertyNames on NamedNodeMap after attribute removal\ No newline at end of filediff --git a/Tests/LibWeb/Text/input/DOM/NamedNodeMap-many-attributes.html b/Tests/LibWeb/Text/input/DOM/NamedNodeMap-many-attributes.htmlnew file mode 100644index 00000000000..c26442f9294--- /dev/null+++ b/Tests/LibWeb/Text/input/DOM/NamedNodeMap-many-attributes.html@@ -0,0 +1,24 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ test(() => {+ // Enumerating an element's attribute names takes time linear in their number.+ const element = document.createElement("div");+ const count = 3000;+ for (let i = 0; i < count; ++i)+ element.setAttribute(`data-attribute-${i}`, "");+ element.setAttributeNS(null, "Uppercase", "");+ element.setAttributeNS("urn:a", "duplicate", "");+ element.setAttributeNS("urn:b", "duplicate", "");++ const names = Object.getOwnPropertyNames(element.attributes);+ const namedProperties = names.filter(name => !/^\d+$/.test(name));+ println(`Indices: ${names.length - namedProperties.length}`);+ println(`Named properties: ${namedProperties.length}`);+ println(`First: ${namedProperties[0]}, last: ${namedProperties[namedProperties.length - 1]}`);+ println(`Uppercase exposed: ${"Uppercase" in element.attributes}`);+ println(`Duplicate listed once: ${namedProperties.filter(name => name === "duplicate").length === 1}`);+ println(`"data-attribute-1234" in attributes: ${"data-attribute-1234" in element.attributes}`);+ println(`"missing" in attributes: ${"missing" in element.attributes}`);+ });+</script>diff --git a/Tests/LibWeb/Text/input/wpt-import/dom/collections/namednodemap-supported-property-names.html b/Tests/LibWeb/Text/input/wpt-import/dom/collections/namednodemap-supported-property-names.htmlnew file mode 100644index 00000000000..61ca30b8e11--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/dom/collections/namednodemap-supported-property-names.html@@ -0,0 +1,30 @@+<!DOCTYPE HTML>+<meta charset=utf-8>+<title>NamedNodeMap Test: Supported property names</title>+<script src="../../resources/testharness.js"></script>+<script src="../../resources/testharnessreport.js"></script>+<div id="log"></div>+<div id="simple" class="fancy">Simple</div>+<input id="result" type="text" value="" width="200px">+<script>++test(function() {+ var elt = document.querySelector('#simple');+ assert_array_equals(Object.getOwnPropertyNames(elt.attributes),+ ['0','1','id','class']);+}, "Object.getOwnPropertyNames on NamedNodeMap");++test(function() {+ var result = document.getElementById("result");+ assert_array_equals(Object.getOwnPropertyNames(result.attributes),+ ['0','1','2','3','id','type','value','width']);+}, "Object.getOwnPropertyNames on NamedNodeMap of input");++test(function() {+ var result = document.getElementById("result");+ result.removeAttribute("width");+ assert_array_equals(Object.getOwnPropertyNames(result.attributes),+ ['0','1','2','id','type','value']);+}, "Object.getOwnPropertyNames on NamedNodeMap after attribute removal");++</script>\ No newline at end of file