LibWeb: Avoid full tree traversal for non-subject :has() invalidation

Instead of checking all elements in a document for containment in
`:has()` invalidation set, we could narrow this down to ancestors and
ancestor siblings, like we already do for subject `:has()` invalidation.

This change brings great improvement on GitHub that has selectors with
non-subject `:has()` and sibling combinators (e.g., `.a:has(.b) ~ .c`)
which prior to this change meant style invalidation for whole document.
This commit is contained in:
Aliaksandr Kalenik 2025-02-12 16:33:24 +01:00 committed by Andreas Kling
commit 327dc8e82a
Notes: github-actions[bot] 2025-02-13 15:25:48 +00:00
6 changed files with 42 additions and 54 deletions

View file

@ -498,6 +498,8 @@ CSS::RequiredInvalidationAfterStyleChange Element::recompute_style()
VERIFY(parent());
m_affected_by_has_pseudo_class_in_subject_position = false;
m_affected_by_has_pseudo_class_in_non_subject_position = false;
m_affected_by_has_pseudo_class_with_relative_selector_that_has_sibling_combinator = false;
m_affected_by_sibling_combinator = false;
m_affected_by_first_or_last_child_pseudo_class = false;
m_affected_by_nth_child_pseudo_class = false;
@ -1312,6 +1314,16 @@ bool Element::includes_properties_from_invalidation_set(CSS::InvalidationSet con
return includes_any;
}
void Element::invalidate_style_if_affected_by_has()
{
if (affected_by_has_pseudo_class_in_subject_position()) {
set_needs_style_update(true);
}
if (affected_by_has_pseudo_class_in_non_subject_position()) {
invalidate_style(StyleInvalidationReason::Other, { { CSS::InvalidationSet::Property::Type::PseudoClass, CSS::PseudoClass::Has } }, {});
}
}
bool Element::has_pseudo_elements() const
{
if (m_pseudo_element_data) {