diff --git a/Libraries/LibWeb/Layout/TreeBuilder.cpp b/Libraries/LibWeb/Layout/TreeBuilder.cpp index 3b48c0695d3..81d5ed9386d 100644 --- a/Libraries/LibWeb/Layout/TreeBuilder.cpp +++ b/Libraries/LibWeb/Layout/TreeBuilder.cpp @@ -305,13 +305,11 @@ void TreeBuilder::restructure_block_node_in_inline_parent(NodeWithStyleAndBoxMod }(); nearest_block_ancestor.set_children_are_inline(false); - // Unwind the ancestor stack to find the topmost inline ancestor. + // Find the topmost inline ancestor. GC::Ptr topmost_inline_ancestor; for (auto* ancestor = &parent; ancestor; ancestor = ancestor->parent()) { if (ancestor == &nearest_block_ancestor) break; - if (ancestor == m_ancestor_stack.last()) - m_ancestor_stack.take_last(); if (ancestor->is_inline()) topmost_inline_ancestor = static_cast(ancestor); } @@ -320,7 +318,7 @@ void TreeBuilder::restructure_block_node_in_inline_parent(NodeWithStyleAndBoxMod // We need to host the topmost inline ancestor and its previous siblings in an anonymous "before" wrapper. If an // inline wrapper does not already exist, we create a new one and add it to the nearest block ancestor. GC::Ptr before_wrapper; - if (auto last_child = nearest_block_ancestor.last_child(); last_child->is_anonymous() && last_child->children_are_inline()) { + if (auto* last_child = nearest_block_ancestor.last_child(); last_child->is_anonymous() && last_child->children_are_inline()) { before_wrapper = last_child; } else { before_wrapper = nearest_block_ancestor.create_anonymous_wrapper(); @@ -388,7 +386,12 @@ void TreeBuilder::restructure_block_node_in_inline_parent(NodeWithStyleAndBoxMod current_parent->append_child(new_inline_node); current_parent = new_inline_node; - // Stop recreating nodes when we've reached node's parent + // Replace the node in the ancestor stack with the new node. + auto& node_with_style = static_cast(*inline_node); + if (auto stack_index = m_ancestor_stack.find_first_index(node_with_style); stack_index.has_value()) + m_ancestor_stack[stack_index.release_value()] = new_inline_node; + + // Stop recreating nodes when we've reached node's parent. if (inline_node == &parent) break; } @@ -396,13 +399,6 @@ void TreeBuilder::restructure_block_node_in_inline_parent(NodeWithStyleAndBoxMod after_wrapper->set_children_are_inline(true); nearest_block_ancestor.append_child(after_wrapper); } - - // Rewind the ancestor stack - for (GC::Ptr inline_node = topmost_inline_ancestor; inline_node; inline_node = inline_node->last_child()) { - if (!is(*inline_node)) - break; - m_ancestor_stack.append(static_cast(*inline_node)); - } } static bool is_ignorable_whitespace(Layout::Node const& node) diff --git a/Tests/LibWeb/Ref/expected/block-element-inside-inline-element-ref.html b/Tests/LibWeb/Ref/expected/block-element-inside-inline-element-ref.html index 21f3101cd99..82dc82a5084 100644 --- a/Tests/LibWeb/Ref/expected/block-element-inside-inline-element-ref.html +++ b/Tests/LibWeb/Ref/expected/block-element-inside-inline-element-ref.html @@ -18,5 +18,7 @@ foo
bar
baz
foo
bar
+
+ foo
bar
baz diff --git a/Tests/LibWeb/Ref/input/block-element-inside-inline-element.html b/Tests/LibWeb/Ref/input/block-element-inside-inline-element.html index 694ab595d9e..c78423e40a0 100644 --- a/Tests/LibWeb/Ref/input/block-element-inside-inline-element.html +++ b/Tests/LibWeb/Ref/input/block-element-inside-inline-element.html @@ -40,5 +40,8 @@ target2.setAttribute('style', null); }); + +
+ foo
bar
baz