Explorar el Código

LibWeb: Do lighter style invalidation for `style` attribute changes

When the `style` attribute changes, we only need to update style on the
element itself (unless there are [style] attribute selectors somewhere).

Descendants of the element don't need a full style update, a simple
inheritance propagation is enough.
Andreas Kling hace 7 meses
padre
commit
d5bbf8dcf8
Se han modificado 1 ficheros con 15 adiciones y 1 borrados
  1. 15 1
      Libraries/LibWeb/DOM/Element.cpp

+ 15 - 1
Libraries/LibWeb/DOM/Element.cpp

@@ -1902,7 +1902,21 @@ ErrorOr<void> Element::scroll_into_view(Optional<Variant<bool, ScrollIntoViewOpt
 void Element::invalidate_style_after_attribute_change(FlyString const& attribute_name)
 {
     // FIXME: Only invalidate if the attribute can actually affect style.
-    (void)attribute_name;
+
+    // OPTIMIZATION: For the `style` attribute, unless it's referenced by an attribute selector,
+    //               only invalidate the element itself, then let inheritance propagate to descendants.
+    if (attribute_name == HTML::AttributeNames::style
+        && !document().style_computer().has_attribute_selector(HTML::AttributeNames::style)) {
+        set_needs_style_update(true);
+        for_each_shadow_including_descendant([](Node& node) {
+            if (!node.is_element())
+                return TraversalDecision::Continue;
+            auto& element = static_cast<Element&>(node);
+            element.set_needs_inherited_style_update(true);
+            return TraversalDecision::Continue;
+        });
+        return;
+    }
 
     invalidate_style(StyleInvalidationReason::ElementAttributeChange);
 }