LibWeb: Maintain a mapping for fast lookup in getElementById()

With this change we maintain a data structure that maps ids to
corresponding elements. This allows us to avoid tree traversal in
getElementById() in all cases except ones when lookup happens for
unconnected elements.
This commit is contained in:
Aliaksandr Kalenik 2025-03-25 17:30:52 +00:00 committed by Andreas Kling
commit 8cae20af1b
Notes: github-actions[bot] 2025-03-26 08:37:18 +00:00
15 changed files with 157 additions and 51 deletions

View file

@ -25,7 +25,6 @@
#include <LibWeb/CSS/CSSStyleSheet.h>
#include <LibWeb/CSS/StyleSheetList.h>
#include <LibWeb/Cookie/Cookie.h>
#include <LibWeb/DOM/NonElementParentNode.h>
#include <LibWeb/DOM/ParentNode.h>
#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/HTML/CrossOrigin/OpenerPolicy.h>
@ -158,7 +157,6 @@ enum class PolicyControlledFeature : u8 {
class Document
: public ParentNode
, public NonElementParentNode<Document>
, public HTML::GlobalEventHandlers {
WEB_PLATFORM_OBJECT(Document, ParentNode);
GC_DECLARE_ALLOCATOR(Document);
@ -742,7 +740,7 @@ public:
GC::Ptr<HTML::SessionHistoryEntry> latest_entry() const { return m_latest_entry; }
void set_latest_entry(GC::Ptr<HTML::SessionHistoryEntry> e) { m_latest_entry = e; }
void element_id_changed(Badge<DOM::Element>, GC::Ref<DOM::Element> element);
void element_id_changed(Badge<DOM::Element>, GC::Ref<DOM::Element> element, Optional<FlyString> old_id);
void element_with_id_was_added(Badge<DOM::Element>, GC::Ref<DOM::Element> element);
void element_with_id_was_removed(Badge<DOM::Element>, GC::Ref<DOM::Element> element);
void element_name_changed(Badge<DOM::Element>, GC::Ref<DOM::Element> element);
@ -895,6 +893,8 @@ public:
m_pending_nodes_for_style_invalidation_due_to_presence_of_has.set(node.make_weak_ptr<Node>());
}
ElementByIdMap& element_by_id() const;
protected:
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override;
@ -952,6 +952,7 @@ private:
GC::Ptr<Node> m_active_favicon;
WeakPtr<HTML::BrowsingContext> m_browsing_context;
URL::URL m_url;
mutable OwnPtr<ElementByIdMap> m_element_by_id;
GC::Ptr<HTML::Window> m_window;