From 8cbe27b2f972473efa2673e26c0e76530ccfa710 Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Tue, 15 Jul 2025 18:43:32 +0200 Subject: [PATCH] LibWeb: Move `perform_pending_style_invalidations()` in StyleInvalidator This change introduces StyleInvalidator as a preparation for upcoming change that will make `perform_pending_style_invalidations()` take care of pending invalidation sets. --- Libraries/LibWeb/CMakeLists.txt | 1 + Libraries/LibWeb/DOM/Document.cpp | 33 +++-------------- Libraries/LibWeb/DOM/Document.h | 2 ++ Libraries/LibWeb/DOM/StyleInvalidator.cpp | 44 +++++++++++++++++++++++ Libraries/LibWeb/DOM/StyleInvalidator.h | 22 ++++++++++++ Libraries/LibWeb/Forward.h | 1 + 6 files changed, 74 insertions(+), 29 deletions(-) create mode 100644 Libraries/LibWeb/DOM/StyleInvalidator.cpp create mode 100644 Libraries/LibWeb/DOM/StyleInvalidator.h diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt index a69eb0c928a..8d32480ee99 100644 --- a/Libraries/LibWeb/CMakeLists.txt +++ b/Libraries/LibWeb/CMakeLists.txt @@ -282,6 +282,7 @@ set(SOURCES DOM/StaticNodeList.cpp DOM/StaticRange.cpp DOM/StyleElementUtils.cpp + DOM/StyleInvalidator.cpp DOM/Text.cpp DOM/TreeWalker.cpp DOM/Utils.cpp diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp index 47ff5c7f249..162396ac0bb 100644 --- a/Libraries/LibWeb/DOM/Document.cpp +++ b/Libraries/LibWeb/DOM/Document.cpp @@ -76,6 +76,7 @@ #include #include #include +#include #include #include #include @@ -459,6 +460,7 @@ Document::Document(JS::Realm& realm, const URL::URL& url, TemporaryDocumentForFr , m_url(url) , m_temporary_document_for_fragment_parsing(temporary_document_for_fragment_parsing) , m_editing_host_manager(EditingHostManager::create(realm, *this)) + , m_style_invalidator(realm.heap().allocate()) { m_legacy_platform_object_flags = PlatformObject::LegacyPlatformObjectFlags { .supports_named_properties = true, @@ -616,6 +618,7 @@ void Document::visit_edges(Cell::Visitor& visitor) visitor.visit(m_session_storage_holder); visitor.visit(m_render_blocking_elements); visitor.visit(m_policy_container); + visitor.visit(m_style_invalidator); } // https://w3c.github.io/selection-api/#dom-document-getselection @@ -1492,34 +1495,6 @@ void Document::update_layout(UpdateLayoutReason reason) return invalidation; } -// This function makes a full pass over the entire DOM and converts "entire subtree needs style update" -// into "needs style update" for each inclusive descendant where it's found. -static void perform_pending_style_invalidations(Node& node, bool invalidate_entire_subtree) -{ - invalidate_entire_subtree |= node.entire_subtree_needs_style_update(); - - if (invalidate_entire_subtree) { - node.set_needs_style_update_internal(true); - if (node.has_child_nodes()) - node.set_child_needs_style_update(true); - } - - for (auto* child = node.first_child(); child; child = child->next_sibling()) { - perform_pending_style_invalidations(*child, invalidate_entire_subtree); - } - - if (node.is_element()) { - auto& element = static_cast(node); - if (auto shadow_root = element.shadow_root()) { - perform_pending_style_invalidations(*shadow_root, invalidate_entire_subtree); - if (invalidate_entire_subtree) - node.set_child_needs_style_update(true); - } - } - - node.set_entire_subtree_needs_style_update(false); -} - void Document::update_style() { if (!browsing_context()) @@ -1536,7 +1511,7 @@ void Document::update_style() if (!needs_full_style_update() && !needs_style_update() && !child_needs_style_update()) return; - perform_pending_style_invalidations(*this, false); + m_style_invalidator->perform_pending_style_invalidations(*this, false); // NOTE: If this is a document hosting