diff --git a/Libraries/LibWeb/CSS/StyleComputer.cpp b/Libraries/LibWeb/CSS/StyleComputer.cpp index 7e678e0f7de..5ebe8982025 100644 --- a/Libraries/LibWeb/CSS/StyleComputer.cpp +++ b/Libraries/LibWeb/CSS/StyleComputer.cpp @@ -2520,6 +2520,9 @@ void StyleComputer::collect_selector_insights(Selector const& selector, Selector if (simple_selector.pseudo_class().type == PseudoClass::Has) { insights.has_has_selectors = true; } + if (simple_selector.pseudo_class().type == PseudoClass::Defined) { + insights.has_defined_selectors = true; + } for (auto const& argument_selector : simple_selector.pseudo_class().argument_selector_list) { collect_selector_insights(*argument_selector, insights); } @@ -3012,6 +3015,12 @@ bool StyleComputer::has_has_selectors() const return m_selector_insights->has_has_selectors; } +bool StyleComputer::has_defined_selectors() const +{ + build_rule_cache_if_needed(); + return m_selector_insights->has_defined_selectors; +} + bool StyleComputer::has_attribute_selector(FlyString const& attribute_name) const { build_rule_cache_if_needed(); diff --git a/Libraries/LibWeb/CSS/StyleComputer.h b/Libraries/LibWeb/CSS/StyleComputer.h index 8ceef16a23c..66c789ed46e 100644 --- a/Libraries/LibWeb/CSS/StyleComputer.h +++ b/Libraries/LibWeb/CSS/StyleComputer.h @@ -170,6 +170,7 @@ public: void collect_animation_into(DOM::Element&, Optional, GC::Ref animation, ComputedProperties&, AnimationRefresh = AnimationRefresh::No) const; [[nodiscard]] bool has_has_selectors() const; + [[nodiscard]] bool has_defined_selectors() const; [[nodiscard]] bool has_attribute_selector(FlyString const& attribute_name) const; size_t number_of_css_font_faces_with_loading_in_progress() const; @@ -250,6 +251,7 @@ private: struct SelectorInsights { bool has_has_selectors { false }; + bool has_defined_selectors { false }; HashTable all_names_used_in_attribute_selectors; }; diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp index d836bfb1040..64b817b6d72 100644 --- a/Libraries/LibWeb/DOM/Element.cpp +++ b/Libraries/LibWeb/DOM/Element.cpp @@ -2266,7 +2266,9 @@ void Element::set_custom_element_state(CustomElementState state) if (m_custom_element_state == state) return; m_custom_element_state = state; - invalidate_style(StyleInvalidationReason::CustomElementStateChange); + + if (document().style_computer().has_defined_selectors()) + invalidate_style(StyleInvalidationReason::CustomElementStateChange); } // https://html.spec.whatwg.org/multipage/dom.html#html-element-constructors