diff --git a/Libraries/LibWeb/Editing/Internal/Algorithms.cpp b/Libraries/LibWeb/Editing/Internal/Algorithms.cpp index 07d27a1107f..5e624c89513 100644 --- a/Libraries/LibWeb/Editing/Internal/Algorithms.cpp +++ b/Libraries/LibWeb/Editing/Internal/Algorithms.cpp @@ -481,27 +481,34 @@ bool is_allowed_child_of_node(Variant, FlyString> child, Vari if (child.has>()) child_node = child.get>(); - // 1. If parent is "colgroup", "table", "tbody", "tfoot", "thead", "tr", or an HTML element with local name equal to - // one of those, and child is a Text node whose data does not consist solely of space characters, return false. - auto parent_local_name = parent.visit( - [](FlyString local_name) { return local_name; }, - [](DOM::Node const* node) { return static_cast(node)->local_name(); }); - auto parent_is_table_like = parent_local_name.is_one_of(HTML::TagNames::colgroup, HTML::TagNames::table, - HTML::TagNames::tbody, HTML::TagNames::tfoot, HTML::TagNames::thead, HTML::TagNames::tr); - if (parent_is_table_like && is(child_node.ptr())) { - auto child_text_content = child_node->text_content().release_value(); - if (!all_of(child_text_content.bytes_as_string_view(), Infra::is_ascii_whitespace)) + GC::Ptr parent_node; + if (parent.has>()) + parent_node = parent.get>(); + + if (parent.has() || is(parent_node.ptr())) { + auto parent_local_name = parent.visit( + [](FlyString local_name) { return local_name; }, + [](GC::Ref node) { return static_cast(*node).local_name(); }); + + // 1. If parent is "colgroup", "table", "tbody", "tfoot", "thead", "tr", or an HTML element with local name equal to + // one of those, and child is a Text node whose data does not consist solely of space characters, return false. + auto parent_is_table_like = parent_local_name.is_one_of(HTML::TagNames::colgroup, HTML::TagNames::table, + HTML::TagNames::tbody, HTML::TagNames::tfoot, HTML::TagNames::thead, HTML::TagNames::tr); + if (parent_is_table_like && is(child_node.ptr())) { + auto child_text_content = child_node->text_content().release_value(); + if (!all_of(child_text_content.bytes_as_string_view(), Infra::is_ascii_whitespace)) + return false; + } + + // 2. If parent is "script", "style", "plaintext", or "xmp", or an HTML element with local name equal to one of + // those, and child is not a Text node, return false. + if ((child.has() || !is(child_node.ptr())) + && parent_local_name.is_one_of(HTML::TagNames::script, HTML::TagNames::style, HTML::TagNames::plaintext, HTML::TagNames::xmp)) return false; } - // 2. If parent is "script", "style", "plaintext", or "xmp", or an HTML element with local name equal to one of - // those, and child is not a Text node, return false. - if ((child.has() || !is(*child_node)) - && parent_local_name.is_one_of(HTML::TagNames::script, HTML::TagNames::style, HTML::TagNames::plaintext, HTML::TagNames::xmp)) - return false; - // 3. If child is a document, DocumentFragment, or DocumentType, return false. - if (child_node && (is(*child_node) || is(*child_node) || is(*child_node))) + if (is(child_node.ptr()) || is(child_node.ptr()) || is(child_node.ptr())) return false; // 4. If child is an HTML element, set child to the local name of child. @@ -523,7 +530,7 @@ bool is_allowed_child_of_node(Variant, FlyString> child, Vari HTML::TagNames::h5, HTML::TagNames::h6); }; - if (parent.has>() && is(*parent.get>())) { + if (is(parent_node.ptr())) { auto& parent_html_element = static_cast(*parent.get>()); // 1. If child is "a", and parent or some ancestor of parent is an a, return false. @@ -560,19 +567,17 @@ bool is_allowed_child_of_node(Variant, FlyString> child, Vari // 4. Let parent be the local name of parent. parent = parent_html_element.local_name(); + parent_node = {}; } // 7. If parent is an Element or DocumentFragment, return true. - if (parent.has>()) { - auto parent_element = parent.get>(); - if (is(*parent_element) || is(*parent_element)) - return true; - } + if (is(parent_node.ptr()) || is(parent_node.ptr())) + return true; // 8. If parent is not a string, return false. if (!parent.has()) return false; - parent_local_name = parent.get(); + auto parent_local_name = parent.get(); // 9. If parent is on the left-hand side of an entry on the following list, then return true if child is listed on // the right-hand side of that entry, and false otherwise.