diff --git a/Libraries/LibWeb/Editing/Internal/Algorithms.cpp b/Libraries/LibWeb/Editing/Internal/Algorithms.cpp index b7473b78b6e..754f03ecd3f 100644 --- a/Libraries/LibWeb/Editing/Internal/Algorithms.cpp +++ b/Libraries/LibWeb/Editing/Internal/Algorithms.cpp @@ -873,20 +873,20 @@ bool is_collapsed_whitespace_node(GC::Ref node) ancestor = ancestor->parent(); // 7. Let reference be node. - auto reference = node; + GC::Ptr reference = node; // 8. While reference is a descendant of ancestor: while (reference->is_descendant_of(*ancestor)) { // 1. Let reference be the node before it in tree order. - reference = *reference->previous_in_pre_order(); + reference = reference->previous_in_pre_order(); // 2. If reference is a block node or a br, return true. - if (is_block_node(reference) || is(*reference)) + if (is_block_node(*reference) || is(*reference)) return true; // 3. If reference is a Text node that is not a whitespace node, or is an img, break from // this loop. - if ((is(*reference) && !is_whitespace_node(reference)) || is(*reference)) + if ((is(*reference) && !is_whitespace_node(*reference)) || is(*reference)) break; } @@ -896,15 +896,19 @@ bool is_collapsed_whitespace_node(GC::Ref node) // 10. While reference is a descendant of ancestor: while (reference->is_descendant_of(*ancestor)) { // 1. Let reference be the node after it in tree order, or null if there is no such node. - reference = *reference->next_in_pre_order(); + reference = reference->next_in_pre_order(); + + // NOTE: Both steps below and the loop condition require a reference, so break if it's null. + if (!reference) + break; // 2. If reference is a block node or a br, return true. - if (is_block_node(reference) || is(*reference)) + if (is_block_node(*reference) || is(*reference)) return true; // 3. If reference is a Text node that is not a whitespace node, or is an img, break from // this loop. - if ((is(*reference) && !is_whitespace_node(reference)) || is(*reference)) + if ((is(*reference) && !is_whitespace_node(*reference)) || is(*reference)) break; }