LibWeb: Don't crash when updating a select with detached option elements

`Node::shadow_including_root()` was missing a null check, which caused
a crash when manipulating a select element, whose option elements were
initially detached.
This commit is contained in:
Tim Ledbetter 2024-03-23 15:46:08 +00:00 committed by Andreas Kling
commit 2227674b91
Notes: sideshowbarker 2024-07-17 08:55:54 +09:00
3 changed files with 18 additions and 2 deletions

View file

@ -0,0 +1 @@
PASS (didn't crash)

View file

@ -0,0 +1,13 @@
<!DOCTYPE html>
<script src="../include.js"></script>
<select></select>
<script>
test(() => {
const selectElement = document.querySelector("select");
const optionElement = document.createElement("option");
optionElement.innerHTML = "option text"
selectElement.appendChild(optionElement);
document.body.removeChild(selectElement);
println("PASS (didn't crash)");
});
</script>

View file

@ -320,8 +320,10 @@ Node& Node::shadow_including_root()
// The shadow-including root of an object is its roots hosts shadow-including root,
// if the objects root is a shadow root; otherwise its root.
auto& node_root = root();
if (is<ShadowRoot>(node_root))
return static_cast<ShadowRoot&>(node_root).host()->shadow_including_root();
if (is<ShadowRoot>(node_root)) {
if (auto* host = static_cast<ShadowRoot&>(node_root).host(); host)
return host->shadow_including_root();
}
return node_root;
}