Skip to content

Truly independent web browser

LibWeb: Return the same collection from repeated getElementsByTagName()

Andreas Klingfc53f1a2f6e2
diff --git a/Libraries/LibWeb/DOM/Node.h b/Libraries/LibWeb/DOM/Node.h
index 647c74b1bbc..fabfcb1d18a 100644
--- a/Libraries/LibWeb/DOM/Node.h
+++ b/Libraries/LibWeb/DOM/Node.h
@@ -9,6 +9,7 @@
#include <AK/DistinctNumeric.h>
#include <AK/Function.h>
#include <AK/GenericShorthands.h>
+#include <AK/HashMap.h>
#include <AK/RefPtr.h>
#include <AK/TypeCasts.h>
#include <AK/Utf16FlyString.h>
@@ -16,6 +17,7 @@
#include <AK/Utf16StringBuilder.h>
#include <AK/Utf16View.h>
#include <AK/Vector.h>
+#include <LibGC/Weak.h>
#include <LibWeb/Bindings/Node.h>
#include <LibWeb/DOM/EventTarget.h>
#include <LibWeb/DOM/FragmentSerializationMode.h>
@@ -667,6 +669,16 @@ protected:
GC::Ptr<NodeList> child_nodes;
GC::Ptr<HTMLCollection> children;
OwnPtr<HTMLCollectionCacheRegistration::List> html_collections_with_valid_caches;
+
+ // The collections getElementsByTagName() returned, which it may return again for the same argument while the
+ // type of the root's node document stays the same, so that type is kept with each.
+ struct CollectionByTagName {
+ GC::Weak<HTMLCollection> collection;
+ bool for_html_document { false };
+ };
+ OwnPtr<HashMap<Utf16FlyString, CollectionByTagName>> collections_by_tag_name;
+ // The size the map grows to before the entries of collected collections are swept from it.
+ size_t collections_by_tag_name_sweep_size { 0 };
};
void register_html_collection_with_valid_cache(HTMLCollection&);
diff --git a/Libraries/LibWeb/DOM/ParentNode.cpp b/Libraries/LibWeb/DOM/ParentNode.cpp
index dd44c8bde19..893f59f0943 100644
--- a/Libraries/LibWeb/DOM/ParentNode.cpp
+++ b/Libraries/LibWeb/DOM/ParentNode.cpp
@@ -90,9 +90,36 @@ GC::Ref<HTMLCollection> ParentNode::children()
return *children;
}
+// https://dom.spec.whatwg.org/#dom-document-getelementsbytagname
+// https://dom.spec.whatwg.org/#dom-element-getelementsbytagname
+GC::Ref<HTMLCollection> ParentNode::get_elements_by_tag_name(Utf16FlyString const& qualified_name)
+{
+ // https://dom.spec.whatwg.org/#concept-getelementsbytagname
+ // When invoked with the same argument, and as long as root’s node document’s type has not changed, the same
+ // HTMLCollection object may be returned as returned by an earlier call.
+ auto for_html_document = document().document_type() == Document::Type::HTML;
+ auto& rare_data = ensure_rare_data();
+ auto& collections = rare_data.collections_by_tag_name;
+ if (!collections) {
+ collections = make<HashMap<Utf16FlyString, RareData::CollectionByTagName>>();
+ } else if (auto it = collections->find(qualified_name); it != collections->end() && it->value.for_html_document == for_html_document) {
+ if (auto collection = it->value.collection.ptr())
+ return *collection;
+ }
+ // Forget the collections that were collected, so that calls with ever new arguments do not grow the map. The sweep
+ // waits for the map to double in size, so that it costs each call a constant amount of work.
+ if (collections->size() >= rare_data.collections_by_tag_name_sweep_size) {
+ collections->remove_all_matching([](auto const&, auto const& cached) { return !cached.collection; });
+ rare_data.collections_by_tag_name_sweep_size = max<size_t>(8, collections->size() * 2);
+ }
+ auto collection = create_elements_by_tag_name_collection(qualified_name);
+ collections->set(qualified_name, { collection, for_html_document });
+ return collection;
+}
+
// https://dom.spec.whatwg.org/#concept-getelementsbytagname
// NOTE: This method is only exposed on Document and Element, but is in ParentNode to prevent code duplication.
-GC::Ref<HTMLCollection> ParentNode::get_elements_by_tag_name(Utf16FlyString const& qualified_name)
+GC::Ref<HTMLCollection> ParentNode::create_elements_by_tag_name_collection(Utf16FlyString const& qualified_name)
{
// 1. If qualifiedName is "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches only descendant elements.
if (qualified_name == u"*"sv) {
diff --git a/Libraries/LibWeb/DOM/ParentNode.h b/Libraries/LibWeb/DOM/ParentNode.h
index b9f8b483f9c..d6de0db4acb 100644
--- a/Libraries/LibWeb/DOM/ParentNode.h
+++ b/Libraries/LibWeb/DOM/ParentNode.h
@@ -51,6 +51,8 @@ protected:
private:
friend class Node;
+ GC::Ref<HTMLCollection> create_elements_by_tag_name_collection(Utf16FlyString const&);
+
// The counters behind Node::dom_tree_version() and Node::character_data_version(), for the tree rooted here.
u64 m_dom_tree_version { 0 };
u64 m_character_data_version { 0 };
diff --git a/Tests/LibWeb/Text/expected/wpt-import/webidl/current-realm.txt b/Tests/LibWeb/Text/expected/wpt-import/webidl/current-realm.txt
index 94de0b02bd3..26ac921f568 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/webidl/current-realm.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/webidl/current-realm.txt
@@ -2,8 +2,8 @@ Harness status: OK
Found 33 tests
-30 Pass
-3 Fail
+31 Pass
+2 Fail
Pass querySelectorAll
Pass createElement
Pass createElementNS
@@ -19,7 +19,7 @@ Pass createNodeIterator
Pass createTreeWalker
Pass Request
Pass Response
-Fail getElementsByTagName
+Pass getElementsByTagName
Fail getElementsByTagNameNS
Fail getElementsByClassName
Pass createDocumentType