LibWeb: Optimize style invalidation caused by DOM structural changes

With this change, siblings of an inserted node are no longer invalidated
unless the insertion could potentially affect their style. By
"potentially affected," we mean elements that are evaluated against the
following selectors during matching:
- Sibling combinators (+ or ~)
- Pseudo-classes :first-child and :last-child
- Pseudo-classes :nth-child, :nth-last-child, :nth-of-type, and
  :nth-last-of-type
This commit is contained in:
Aliaksandr Kalenik 2025-02-05 21:29:34 +01:00 committed by Andreas Kling
commit 61c952fb43
Notes: github-actions[bot] 2025-02-06 19:08:36 +00:00
4 changed files with 42 additions and 4 deletions

View file

@ -439,14 +439,14 @@ void Node::invalidate_style(StyleInvalidationReason reason)
if (reason == StyleInvalidationReason::NodeInsertBefore || reason == StyleInvalidationReason::NodeRemove) {
for (auto* sibling = previous_sibling(); sibling; sibling = sibling->previous_sibling()) {
if (sibling->is_element())
sibling->set_entire_subtree_needs_style_update(true);
if (auto* element = as_if<Element>(sibling); element && element->style_affected_by_structural_changes())
element->set_entire_subtree_needs_style_update(true);
}
}
for (auto* sibling = next_sibling(); sibling; sibling = sibling->next_sibling()) {
if (sibling->is_element())
sibling->set_entire_subtree_needs_style_update(true);
if (auto* element = as_if<Element>(sibling); element && element->style_affected_by_structural_changes())
element->set_entire_subtree_needs_style_update(true);
}
for (auto* ancestor = parent_or_shadow_host(); ancestor; ancestor = ancestor->parent_or_shadow_host())