mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-24 09:52:31 +00:00
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:
parent
7165d69868
commit
8cae20af1b
Notes:
github-actions[bot]
2025-03-26 08:37:18 +00:00
Author: https://github.com/kalenikaliaksandr
Commit: 8cae20af1b
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4086
15 changed files with 157 additions and 51 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue