Przeglądaj źródła

LibWeb: Don't invalidate style for entire document on attribute change

We now only invalidate the style of the context element and all of its
descendants. It's still very aggressive, but much less than before.

Note that this will need to become a lot smarter once we implement the
CSS :has() selector.
Andreas Kling 2 lat temu
rodzic
commit
ccd72a2add

+ 13 - 8
Userland/Libraries/LibWeb/DOM/Element.cpp

@@ -123,8 +123,7 @@ WebIDL::ExceptionOr<void> Element::set_attribute(FlyString const& name, String c
 
     parse_attribute(attribute->local_name(), value);
 
-    // FIXME: Invalidate less.
-    document().invalidate_style();
+    invalidate_style_after_attribute_change(name);
 
     return {};
 }
@@ -191,8 +190,7 @@ void Element::remove_attribute(FlyString const& name)
 
     did_remove_attribute(name);
 
-    // FIXME: Invalidate less.
-    document().invalidate_style();
+    invalidate_style_after_attribute_change(name);
 }
 
 // https://dom.spec.whatwg.org/#dom-element-hasattribute
@@ -225,8 +223,7 @@ WebIDL::ExceptionOr<bool> Element::toggle_attribute(FlyString const& name, Optio
 
             parse_attribute(new_attribute->local_name(), "");
 
-            // FIXME: Invalidate less.
-            document().invalidate_style();
+            invalidate_style_after_attribute_change(name);
 
             return true;
         }
@@ -241,8 +238,7 @@ WebIDL::ExceptionOr<bool> Element::toggle_attribute(FlyString const& name, Optio
 
         did_remove_attribute(name);
 
-        // FIXME: Invalidate less.
-        document().invalidate_style();
+        invalidate_style_after_attribute_change(name);
     }
 
     // 6. Return true.
@@ -980,4 +976,13 @@ void Element::scroll_into_view(Optional<Variant<bool, ScrollIntoViewOptions>> ar
     // FIXME: 8. Optionally perform some other action that brings the element to the user’s attention.
 }
 
+void Element::invalidate_style_after_attribute_change(FlyString const& attribute_name)
+{
+    // FIXME: Only invalidate if the attribute can actually affect style.
+    (void)attribute_name;
+
+    // FIXME: This will need to become smarter when we implement the :has() selector.
+    invalidate_style();
+}
+
 }

+ 2 - 0
Userland/Libraries/LibWeb/DOM/Element.h

@@ -173,6 +173,8 @@ protected:
 private:
     void make_html_uppercased_qualified_name();
 
+    void invalidate_style_after_attribute_change(FlyString const& attribute_name);
+
     WebIDL::ExceptionOr<JS::GCPtr<Node>> insert_adjacent(String const& where, JS::NonnullGCPtr<Node> node);
 
     QualifiedName m_qualified_name;