From ccd72a2adda7cf86bcaa6b24755b874042bcabb6 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 29 Oct 2022 13:04:18 +0200 Subject: [PATCH] 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. --- Userland/Libraries/LibWeb/DOM/Element.cpp | 21 +++++++++++++-------- Userland/Libraries/LibWeb/DOM/Element.h | 2 ++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index ac4e6c7f71..dcdd9a2ed5 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -123,8 +123,7 @@ WebIDL::ExceptionOr 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 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 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> 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(); +} + } diff --git a/Userland/Libraries/LibWeb/DOM/Element.h b/Userland/Libraries/LibWeb/DOM/Element.h index 6265b1b80c..2af1e0e094 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.h +++ b/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> insert_adjacent(String const& where, JS::NonnullGCPtr node); QualifiedName m_qualified_name;