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
parent 7165d69868
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

@ -260,4 +260,27 @@ GC::Ref<HTMLCollection> ParentNode::get_elements_by_class_name(StringView class_
});
}
GC::Ptr<Element> ParentNode::get_element_by_id(FlyString const& id) const
{
// For document and shadow root we have a cache that allows fast lookup.
if (is_document()) {
auto const& document = static_cast<Document const&>(*this);
return document.element_by_id().get(id);
}
if (is_shadow_root()) {
auto const& shadow_root = static_cast<ShadowRoot const&>(*this);
return shadow_root.element_by_id().get(id);
}
GC::Ptr<Element> found_element;
const_cast<ParentNode&>(*this).for_each_in_inclusive_subtree_of_type<Element>([&](Element& element) {
if (element.id() == id) {
found_element = &element;
return TraversalDecision::Break;
}
return TraversalDecision::Continue;
});
return found_element;
}
}