Truly independent web browser
LibWeb: Invalidate counter styles only for sheets that can define them
Tim Ledbetter81f2c3a8306f
diff --git a/Libraries/LibWeb/CSS/Invalidation/MediaQueryInvalidator.cpp b/Libraries/LibWeb/CSS/Invalidation/MediaQueryInvalidator.cppindex 64a1fd90c9d..3127a0b7dec 100644--- a/Libraries/LibWeb/CSS/Invalidation/MediaQueryInvalidator.cpp+++ b/Libraries/LibWeb/CSS/Invalidation/MediaQueryInvalidator.cpp@@ -8,6 +8,7 @@#include <LibWeb/CSS/StyleComputer.h>#include <LibWeb/CSS/StyleEngineInput.h>#include <LibWeb/CSS/StyleScope.h>+#include <LibWeb/CSS/StyleSheetInvalidation.h>#include <LibWeb/CSS/StyleSheetState.h>#include <LibWeb/DOM/Document.h>#include <LibWeb/DOM/ShadowRoot.h>@@ -35,7 +36,7 @@ void evaluate_media_rules_and_publish_conditions(DOM::Document& document)style_sheet.record_conditions_for_owners();style_sheet.for_each_owning_style_scope([&](StyleScope& style_scope) {if (result.style_cache_inputs_changed)- style_scope.invalidate_style_cache();+ invalidate_rule_cache_after_style_sheet_change(style_scope, style_sheet);if (result.layer_order_inputs_changed)scopes_with_changed_layer_order.set(&style_scope);});@@ -58,6 +59,7 @@ void evaluate_media_rules_and_publish_conditions(DOM::Document& document)if (!changed)return;document.style_scope().invalidate_style_cache();+ document.style_scope().invalidate_counter_style_cache();style_sheet.reload_fonts_after_media_query_change();});}diff --git a/Libraries/LibWeb/CSS/RustRule.h b/Libraries/LibWeb/CSS/RustRule.hindex 46629245523..c1c0ed4cbd7 100644--- a/Libraries/LibWeb/CSS/RustRule.h+++ b/Libraries/LibWeb/CSS/RustRule.h@@ -182,6 +182,7 @@ public:size_t size() const { return Parser::ValueParserFFI::rust_rule_list_count(m_list); }bool has_implicit_scope() const { return Parser::ValueParserFFI::rust_rule_list_has_implicit_scope(m_list); }bool has_anonymous_layer() const { return Parser::ValueParserFFI::rust_rule_list_has_anonymous_layer(m_list); }+ bool has_counter_style_rules() const { return Parser::ValueParserFFI::rust_rule_list_has_counter_style_rules(m_list); }FlatPtr shared_contents_identity() const { return Parser::ValueParserFFI::rust_rule_list_shared_contents_identity(m_list); }RustRuleList clone_shared_contents() const{diff --git a/Libraries/LibWeb/CSS/StyleScope.cpp b/Libraries/LibWeb/CSS/StyleScope.cppindex 5825ac66ec2..a4efe032a05 100644--- a/Libraries/LibWeb/CSS/StyleScope.cpp+++ b/Libraries/LibWeb/CSS/StyleScope.cpp@@ -262,7 +262,6 @@ void StyleScope::build_rule_cache()void StyleScope::invalidate_style_cache(){document().note_style_sheet_set_change();- invalidate_counter_style_cache();m_rule_cache.clear();m_has_published_rule_cache = false;// The registered custom properties cache is built from the document's active stylesheets, so it only needs a@@ -275,6 +274,7 @@ void StyleScope::invalidate_user_style_sheet(){m_user_style_sheet = nullptr;invalidate_style_cache();+ invalidate_counter_style_cache();}void StyleScope::build_user_style_sheet_if_needed()@@ -679,6 +679,7 @@ void StyleScope::build_counter_style_cache(Layout::BegunRead const& read)// and so is what a document that defines no counter style of its own registers. Every new iframe// and SVG image registers them in its first style update, so they are made once.static Parser::ValueParserFFI::RegisteredCounterStyles const* user_agent_counter_styles = nullptr;+ m_sheets_define_counter_styles = !rules.is_empty();bool const registers_only_user_agent_counter_styles = is_document && rules.is_empty();Parser::ValueParserFFI::RegisteredCounterStyles const* counter_styles = nullptr;if (registers_only_user_agent_counter_styles && user_agent_counter_styles) {diff --git a/Libraries/LibWeb/CSS/StyleScope.h b/Libraries/LibWeb/CSS/StyleScope.hindex 1a262ab93c2..a46f62c9ca3 100644--- a/Libraries/LibWeb/CSS/StyleScope.h+++ b/Libraries/LibWeb/CSS/StyleScope.h@@ -130,6 +130,7 @@ public:void for_each_active_css_style_sheet(Function<void(CSS::StyleSheetState&)> const& callback) const;void invalidate_counter_style_cache();+ [[nodiscard]] bool sheets_define_counter_styles() const { return m_sheets_define_counter_styles; }void build_counter_style_cache(Layout::BegunRead const& read);u64 counter_style_environment_identity(Layout::BegunRead const& read) const;void publish_counter_style_lookup_chain(Layout::BegunRead const& read) const;@@ -150,6 +151,7 @@ public:bool m_needs_counter_style_cache_update : 1 { true };bool m_is_doing_counter_style_cache_update : 1 { false };+ bool m_sheets_define_counter_styles : 1 { false };bool m_has_published_named_layer_order : 1 { false };// Whether the style engine holds this scope's layer order and keyframes as the rule cache resolved them.bool m_has_published_rule_cache : 1 { false };diff --git a/Libraries/LibWeb/CSS/StyleSheetInvalidation.cpp b/Libraries/LibWeb/CSS/StyleSheetInvalidation.cppindex 95a04f7f05b..c349be2db04 100644--- a/Libraries/LibWeb/CSS/StyleSheetInvalidation.cpp+++ b/Libraries/LibWeb/CSS/StyleSheetInvalidation.cpp@@ -18,15 +18,22 @@ void invalidate_rule_cache_after_style_sheet_change(DOM::Node& document_or_shadoreturn;if (auto* shadow_root = as_if<DOM::ShadowRoot>(document_or_shadow_root))- shadow_root->style_scope().invalidate_style_cache();+ invalidate_rule_cache_after_style_sheet_change(shadow_root->style_scope(), sheet);else- document_or_shadow_root.document().style_scope().invalidate_style_cache();+ invalidate_rule_cache_after_style_sheet_change(document_or_shadow_root.document().style_scope(), sheet);+}++void invalidate_rule_cache_after_style_sheet_change(StyleScope& style_scope, StyleSheetState const& sheet)+{+ style_scope.invalidate_style_cache();+ if (sheet.has_counter_style_rules() || style_scope.sheets_define_counter_styles())+ style_scope.invalidate_counter_style_cache();}void invalidate_rule_cache_for_style_sheet_owners(StyleSheetState const& style_sheet){- style_sheet.for_each_owning_style_scope([](StyleScope& style_scope) {- style_scope.invalidate_style_cache();+ style_sheet.for_each_owning_style_scope([&](StyleScope& style_scope) {+ invalidate_rule_cache_after_style_sheet_change(style_scope, style_sheet);});}diff --git a/Libraries/LibWeb/CSS/StyleSheetInvalidation.h b/Libraries/LibWeb/CSS/StyleSheetInvalidation.hindex 187bfc70221..da2469306a3 100644--- a/Libraries/LibWeb/CSS/StyleSheetInvalidation.h+++ b/Libraries/LibWeb/CSS/StyleSheetInvalidation.h@@ -15,6 +15,10 @@ namespace Web::CSS {// list-membership before invoking this.void invalidate_rule_cache_after_style_sheet_change(DOM::Node& document_or_shadow_root, StyleSheetState const& sheet);+// Drop the rule cache of a scope one of its sheets changed in, and the scope's counter styles when the sheet can+// change them.+void invalidate_rule_cache_after_style_sheet_change(StyleScope&, StyleSheetState const& sheet);+// Drop the rule cache of every scope that owns `style_sheet`, because the rules it contributes to them moved.void invalidate_rule_cache_for_style_sheet_owners(StyleSheetState const& style_sheet);diff --git a/Libraries/LibWeb/CSS/StyleSheetState.cpp b/Libraries/LibWeb/CSS/StyleSheetState.cppindex c4cc5a76c7c..728c377919f 100644--- a/Libraries/LibWeb/CSS/StyleSheetState.cpp+++ b/Libraries/LibWeb/CSS/StyleSheetState.cpp@@ -603,6 +603,18 @@ bool StyleSheetState::has_document_owner() constreturn false;}+bool StyleSheetState::has_counter_style_rules() const+{+ if (native_rules().has_counter_style_rules())+ return true;+ for (auto const& import_rule : m_import_rules) {+ auto const* imported_sheet = import_rule->loaded_style_sheet();+ if (imported_sheet && imported_sheet->has_counter_style_rules())+ return true;+ }+ return false;+}+GC::Ptr<DOM::Document> StyleSheetState::owning_document() const{if (!m_owning_documents_or_shadow_roots.is_empty())diff --git a/Libraries/LibWeb/CSS/StyleSheetState.h b/Libraries/LibWeb/CSS/StyleSheetState.hindex 4a58564b73f..f7a76111bdb 100644--- a/Libraries/LibWeb/CSS/StyleSheetState.h+++ b/Libraries/LibWeb/CSS/StyleSheetState.h@@ -128,6 +128,7 @@ public:void disconnect_font_faces_in_rules(Parser::ValueParserFFI::NativeRuleList const*);Vector<NonnullRefPtr<StyleSheetImport>> const& import_rules() const { return m_import_rules; }+ bool has_counter_style_rules() const;StyleSheetImport* import_for_rule(u64 identity) const;Optional<::URL::URL> base_url() const { return m_base_url; }diff --git a/Libraries/LibWeb/Rust/src/css/rule/read.rs b/Libraries/LibWeb/Rust/src/css/rule/read.rsindex 2afd99e488f..0941f1b15d3 100644--- a/Libraries/LibWeb/Rust/src/css/rule/read.rs+++ b/Libraries/LibWeb/Rust/src/css/rule/read.rs@@ -64,6 +64,18 @@ pub extern "C" fn rust_rule_list_has_implicit_scope(list: &NativeRuleList) -> bo.is_break()}+#[unsafe(no_mangle)]+pub extern "C" fn rust_rule_list_has_counter_style_rules(list: &NativeRuleList) -> bool {+ list.visit_descendant_rules(&mut |rule| {+ if rule.rule_type() == NativeRuleType::CounterStyle {+ ControlFlow::Break(())+ } else {+ ControlFlow::Continue(())+ }+ })+ .is_break()+}+#[unsafe(no_mangle)]pub extern "C" fn rust_rule_list_has_anonymous_layer(list: &NativeRuleList) -> bool {list.visit_descendant_rules(&mut |rule| {diff --git a/Tests/LibWeb/Text/expected/css/style-engine/counter-style-cache-after-sheet-changes.txt b/Tests/LibWeb/Text/expected/css/style-engine/counter-style-cache-after-sheet-changes.txtnew file mode 100644index 00000000000..f37de4c54d1--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/style-engine/counter-style-cache-after-sheet-changes.txt@@ -0,0 +1,2 @@+shadow sheet defining the counter style changes the marker: true+sheet reordering layers changes the winning definition: truediff --git a/Tests/LibWeb/Text/input/css/style-engine/counter-style-cache-after-sheet-changes.html b/Tests/LibWeb/Text/input/css/style-engine/counter-style-cache-after-sheet-changes.htmlnew file mode 100644index 00000000000..137c0c49efd--- /dev/null+++ b/Tests/LibWeb/Text/input/css/style-engine/counter-style-cache-after-sheet-changes.html@@ -0,0 +1,40 @@+<!DOCTYPE html>+<script src="../../include.js"></script>+<style>+ #host, #layered { font-family: monospace; }+</style>+<style id="layered-definitions">+ @layer base, override;+ #layered { display: inline-block; counter-reset: item 1; }+ #layered::before { content: counter(item, layered-marker); }+ @layer override { @counter-style layered-marker { system: cyclic; symbols: "BBBBBBBB"; } }+ @layer base { @counter-style layered-marker { system: cyclic; symbols: "A"; } }+</style>+<div id="host"></div>+<div id="layered"></div>+<script>+ test(() => {+ const shadowRoot = host.attachShadow({ mode: "open" });+ shadowRoot.innerHTML = `+ <style>+ #target { display: inline-block; counter-reset: item 1; }+ #target::before { content: counter(item, marker); }+ </style>+ <div id="target"></div>+ `;+ const target = shadowRoot.getElementById("target");+ const width = element => element.getBoundingClientRect().width;+ const initialWidth = width(target);++ const definition = document.createElement("style");+ definition.textContent = '@counter-style marker { system: cyclic; symbols: "BBBBBBBB"; }';+ shadowRoot.append(definition);+ println(`shadow sheet defining the counter style changes the marker: ${width(target) > initialWidth}`);++ const layeredWidth = width(layered);+ const reorder = document.createElement("style");+ reorder.textContent = "@layer override, base;";+ document.getElementById("layered-definitions").before(reorder);+ println(`sheet reordering layers changes the winning definition: ${width(layered) < layeredWidth}`);+ });+</script>