LibWeb: Be more defensive while traversing ancestors in editing API

In all these cases there should be an ancestor available, but it
definitely cannot hurt to be a bit more defensive about this and prevent
nullptr dereferences.
This commit is contained in:
Jelle Raaijmakers 2024-12-03 23:46:56 +01:00 committed by Jelle Raaijmakers
parent b6939c8035
commit e308a3fd3f
Notes: github-actions[bot] 2024-12-04 05:52:55 +00:00
2 changed files with 26 additions and 26 deletions

View file

@ -151,12 +151,12 @@ bool command_delete_action(DOM::Document& document, String const&)
// 1. Let items be a list of all lis that are ancestors of node.
auto items = Vector<GC::Ref<DOM::Element>>();
GC::Ptr<DOM::Node> ancestor = node->parent();
do {
while (ancestor) {
auto& ancestor_element = static_cast<DOM::Element&>(*ancestor);
if (ancestor_element.local_name() == HTML::TagNames::li)
items.append(ancestor_element);
ancestor = ancestor->parent();
} while (ancestor);
}
// 2. Normalize sublists of each item in items.
for (auto item : items)
@ -178,13 +178,13 @@ bool command_delete_action(DOM::Document& document, String const&)
if (node_element.local_name().is_one_of(HTML::TagNames::dd, HTML::TagNames::dt)) {
ancestor = node->parent();
bool allowed_child_of_any_ancestor = false;
do {
while (ancestor) {
if (is_in_same_editing_host(*node, *ancestor) && is_allowed_child_of_node(GC::Ref { *node }, GC::Ref { *ancestor })) {
allowed_child_of_any_ancestor = true;
break;
}
ancestor = ancestor->parent();
} while (ancestor);
}
if (!allowed_child_of_any_ancestor)
node = set_the_tag_name(node_element, document.default_single_line_container_name());
}
@ -548,14 +548,14 @@ bool command_insert_paragraph_action(DOM::Document& document, String const&)
if (static_cast<DOM::Element&>(*container).local_name().is_one_of(HTML::TagNames::dd, HTML::TagNames::dt)) {
bool allowed_child_of_any_ancestor = false;
GC::Ptr<DOM::Node> ancestor = container->parent();
do {
while (ancestor) {
if (is_allowed_child_of_node(GC::Ref { *container }, GC::Ref { *ancestor })
&& is_in_same_editing_host(*container, *ancestor)) {
allowed_child_of_any_ancestor = true;
break;
}
ancestor = ancestor->parent();
} while (ancestor);
}
if (!allowed_child_of_any_ancestor)
container = set_the_tag_name(static_cast<DOM::Element&>(*container), document.default_single_line_container_name());
}