mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-05-11 01:22:43 +02:00
CSSStyleSheet::evaluate_media_queries previously flagged "no recorded result yet" as a match-state change, so every freshly-loaded sheet fired MediaQueryChangedMatchState on the first pass through Document::evaluate_media_rules. For sheets added through adoptedStyleSheets that piled an extra full-document style invalidation on top of the AdoptedStyleSheetsList one, recomputing every element a second time for nothing. Drop the !has_value() leg so the very first evaluation establishes the baseline silently. The sheet's rules already entered the cascade through StyleSheetListAddSheet, AdoptedStyleSheetsList, or invalidate_owners, each of which performs its own targeted invalidation. Two callers relied on the implicit "first eval forces a refresh" behavior to handle freshly-mutated state: - invalidate_owners resets m_did_match, then leans on the next eval to repopulate it. With the new semantics it must also re-evaluate the sheet eagerly so MediaList::matches() and inner @media state are fresh before the next rule cache build reads them. - The adoptedStyleSheets on_set callback didn't evaluate at all, relying on Document::evaluate_media_rules to populate MediaList::m_matches. That worked accidentally because the false flip retriggered invalidate_rule_cache after the matches had been populated. Mirror StyleSheetList::add_sheet by evaluating the sheet at adopt time so the rule cache build sees the correct match state even if it runs first (e.g. via a :has() invalidation pass).
58 lines
3.2 KiB
C++
58 lines
3.2 KiB
C++
/*
|
||
* Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
|
||
*
|
||
* SPDX-License-Identifier: BSD-2-Clause
|
||
*/
|
||
|
||
#include <LibWeb/CSS/CSSStyleSheet.h>
|
||
#include <LibWeb/CSS/StyleComputer.h>
|
||
#include <LibWeb/DOM/AdoptedStyleSheets.h>
|
||
#include <LibWeb/DOM/Document.h>
|
||
|
||
namespace Web::DOM {
|
||
|
||
GC::Ref<WebIDL::ObservableArray> create_adopted_style_sheets_list(Node& document_or_shadow_root)
|
||
{
|
||
auto adopted_style_sheets = WebIDL::ObservableArray::create(document_or_shadow_root.realm());
|
||
adopted_style_sheets->set_on_set_an_indexed_value_callback([&document_or_shadow_root](JS::Value& value) -> WebIDL::ExceptionOr<void> {
|
||
auto& vm = document_or_shadow_root.vm();
|
||
auto style_sheet = value.as_if<CSS::CSSStyleSheet>();
|
||
if (!style_sheet)
|
||
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "CSSStyleSheet");
|
||
|
||
// The set an indexed value algorithm for adoptedStyleSheets, given value and index, is the following:
|
||
// 1. If value’s constructed flag is not set, or its constructor document is not equal to this
|
||
// DocumentOrShadowRoot's node document, throw a "NotAllowedError" DOMException.
|
||
if (!style_sheet->constructed())
|
||
return WebIDL::NotAllowedError::create(document_or_shadow_root.realm(), "StyleSheet's constructed flag is not set."_utf16);
|
||
if (!style_sheet->constructed() || style_sheet->constructor_document().ptr() != &document_or_shadow_root.document())
|
||
return WebIDL::NotAllowedError::create(document_or_shadow_root.realm(), "Sharing a StyleSheet between documents is not allowed."_utf16);
|
||
|
||
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());
|
||
|
||
auto& style_scope = document_or_shadow_root.is_shadow_root() ? as<DOM::ShadowRoot>(document_or_shadow_root).style_scope() : document_or_shadow_root.document().style_scope();
|
||
style_scope.invalidate_rule_cache();
|
||
document_or_shadow_root.invalidate_style(DOM::StyleInvalidationReason::AdoptedStyleSheetsList);
|
||
return {};
|
||
});
|
||
adopted_style_sheets->set_on_delete_an_indexed_value_callback([&document_or_shadow_root](JS::Value value) -> WebIDL::ExceptionOr<void> {
|
||
auto& style_sheet = value.as<CSS::CSSStyleSheet>();
|
||
auto& style_scope = document_or_shadow_root.is_shadow_root() ? as<DOM::ShadowRoot>(document_or_shadow_root).style_scope() : document_or_shadow_root.document().style_scope();
|
||
style_sheet.remove_owning_document_or_shadow_root(document_or_shadow_root);
|
||
style_scope.invalidate_rule_cache();
|
||
document_or_shadow_root.invalidate_style(DOM::StyleInvalidationReason::AdoptedStyleSheetsList);
|
||
return {};
|
||
});
|
||
|
||
return adopted_style_sheets;
|
||
}
|
||
|
||
}
|