ladybird/Libraries/LibWeb/CSS/Invalidation/AdoptedStyleSheetInvalidator.cpp
Tim Ledbetter 013ede7214 LibWeb: Use targeted invalidation when adopting a stylesheet
Previously, adding or removing a constructed `CSSStyleSheet` via
`document.adoptedStyleSheets` invalidated style for the entire document
or shadow root. We now route this through the same style invalidation
machinery that's used when we add or remove `<style>` elements,
resulting in fewer full invalidations.
2026-05-10 20:19:32 +02:00

35 lines
1.5 KiB
C++

/*
* Copyright (c) 2026-present, the Ladybird developers
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/CSS/CSSStyleSheet.h>
#include <LibWeb/CSS/Invalidation/AdoptedStyleSheetInvalidator.h>
#include <LibWeb/CSS/StyleSheetInvalidation.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Node.h>
#include <LibWeb/DOM/StyleInvalidationReason.h>
namespace Web::CSS::Invalidation {
void invalidate_style_after_adopting_style_sheet(DOM::Node& document_or_shadow_root, CSSStyleSheet& style_sheet)
{
style_sheet.add_owning_document_or_shadow_root(document_or_shadow_root);
style_sheet.load_pending_image_resources(document_or_shadow_root.document());
// Evaluate the sheet's media queries before the next style update so the cascade can see its rules. Otherwise the
// rule cache may be rebuilt (e.g. via a :has() invalidation pass) before Document::evaluate_media_rules has a
// chance to populate MediaList::m_matches.
style_sheet.evaluate_media_queries(document_or_shadow_root.document());
invalidate_style_for_stylesheet_change(document_or_shadow_root, style_sheet, DOM::StyleInvalidationReason::AdoptedStyleSheetsList);
}
void invalidate_style_after_removing_adopted_style_sheet(DOM::Node& document_or_shadow_root, CSSStyleSheet& style_sheet)
{
style_sheet.remove_owning_document_or_shadow_root(document_or_shadow_root);
invalidate_style_for_stylesheet_change(document_or_shadow_root, style_sheet, DOM::StyleInvalidationReason::AdoptedStyleSheetsList);
}
}