LibWeb: Don't create unwanted layout nodes when recomputing style

When recomputing the style for an element that previously didn't have
a corresponding layout node, it may become necessary to create a layout
node for it.

However, we should not do this if it's within a subtree that can't have
layout children. Nor should we do it for elements who have an ancestor
with display:none.
This commit is contained in:
Andreas Kling 2022-03-04 23:36:36 +01:00
parent a6a8ba80fc
commit 7d052250f2
Notes: sideshowbarker 2024-07-17 17:55:44 +09:00

View file

@ -304,9 +304,19 @@ void Element::recompute_style()
auto new_specified_css_values = document().style_computer().compute_style(*this);
m_specified_css_values = new_specified_css_values;
if (!layout_node()) {
// This element doesn't have a corresponding layout node.
// If the new style is display:none, bail.
if (new_specified_css_values->display().is_none())
return;
// We need a new layout tree here!
// If we're inside a display:none ancestor or an ancestor that can't have children, bail.
for (auto* ancestor = parent_element(); ancestor; ancestor = ancestor->parent_element()) {
if (!ancestor->layout_node() || !ancestor->layout_node()->can_have_children())
return;
}
// Okay, we need a new layout subtree here.
Layout::TreeBuilder tree_builder;
(void)tree_builder.build(*this);
return;