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

@ -3520,8 +3520,12 @@ void Element::attribute_changed(FlyString const& local_name, Optional<String> co
else
m_id = value_or_empty;
if (is_connected())
document().element_id_changed({}, *this);
if (is_connected()) {
Optional<FlyString> old_value_fly_string;
if (old_value.has_value())
old_value_fly_string = *old_value;
document().element_id_changed({}, *this, old_value_fly_string);
}
} else if (local_name == HTML::AttributeNames::name) {
if (value_or_empty.is_empty())
m_name = {};
@ -3591,6 +3595,14 @@ CSS::StyleSheetList& Element::document_or_shadow_root_style_sheets()
return document().style_sheets();
}
ElementByIdMap& Element::document_or_shadow_root_element_by_id_map()
{
auto& root_node = root();
if (is<ShadowRoot>(root_node))
return static_cast<ShadowRoot&>(root_node).element_by_id();
return document().element_by_id();
}
// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-element-gethtml
WebIDL::ExceptionOr<String> Element::get_html(GetHTMLOptions const& options) const
{