Skip to content

Commit d5edd62

Browse files
LibWeb: Limit usage of getElementById() cache to connected roots
Fixes bug when we always return null from getElementById() on unconnected roots because id to element cache is only maintained for connected roots. Fixes broken Perf-Dashboard suite in Speedometer 3.
1 parent 3c2a2bb commit d5edd62

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

Libraries/LibWeb/DOM/ParentNode.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -262,14 +262,16 @@ GC::Ref<HTMLCollection> ParentNode::get_elements_by_class_name(StringView class_
262262

263263
GC::Ptr<Element> ParentNode::get_element_by_id(FlyString const& id) const
264264
{
265-
// For document and shadow root we have a cache that allows fast lookup.
266-
if (is_document()) {
267-
auto const& document = static_cast<Document const&>(*this);
268-
return document.element_by_id().get(id);
269-
}
270-
if (is_shadow_root()) {
271-
auto const& shadow_root = static_cast<ShadowRoot const&>(*this);
272-
return shadow_root.element_by_id().get(id);
265+
if (is_connected()) {
266+
// For connected document and shadow root we have a cache that allows fast lookup.
267+
if (is_document()) {
268+
auto const& document = static_cast<Document const&>(*this);
269+
return document.element_by_id().get(id);
270+
}
271+
if (is_shadow_root()) {
272+
auto const& shadow_root = static_cast<ShadowRoot const&>(*this);
273+
return shadow_root.element_by_id().get(id);
274+
}
273275
}
274276

275277
GC::Ptr<Element> found_element;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
HelloWorld
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<body></body>
3+
<script src="../include.js"></script>
4+
<script>
5+
test(() => {
6+
let host = document.createElement("div");
7+
let shadow = host.attachShadow({ mode: "open" });
8+
shadow.innerHTML = `<p id="foo">Hello</p><span id="bar">World</span>`;
9+
let foo = shadow.getElementById("foo");
10+
let bar = shadow.getElementById("bar");
11+
println(foo.textContent + bar.textContent);
12+
});
13+
</script>

0 commit comments

Comments
 (0)