Skip to content

Truly independent web browser

LibWeb: Republish what held environments hand down on @property changes

sideshowbarkeracc15d026bca
diff --git a/Libraries/LibWeb/CSS/StyleEngineBridge.cpp b/Libraries/LibWeb/CSS/StyleEngineBridge.cpp
index a36c6ba9d35..9246afebc98 100644
--- a/Libraries/LibWeb/CSS/StyleEngineBridge.cpp
+++ b/Libraries/LibWeb/CSS/StyleEngineBridge.cpp
@@ -825,27 +825,54 @@ bool StyleEngine::counter(Layout::BegunRead const& read, size_t index, StringVie
return true;
}
-void StyleEngine::set_element_custom_property_data(Layout::BegunRead const& read, DOM::Element const& element, CustomPropertyData const* data)
+// Resolves the children's environments over what the element hands down, and substitutes the element's own values under
+// what its animations sampled, laid over what its style resolves to: name the environment the element holds, and the
+// one its animation overlay lies over, with what each hands down under the registrations as they are now.
+void StyleEngine::note_element_custom_property_environments(Layout::BegunRead const& read, DOM::Element const& element, CustomPropertyData const& data)
{
- // A move of the environment the element inherits reads whether this is its animation overlay, and
- // whether what the element's style resolves to declares custom properties of its own.
- bool const is_animation_overlay = data && data->is_animation_overlay_for({ element });
- auto const* base = is_animation_overlay ? data->parent().ptr() : data;
- // The engine resolves the children's environments over what the element hands down, and substitutes the
- // element's own values under what its animations sampled, laid over what its style resolves to.
auto note = [&](CustomPropertyData const& environment) {
auto inheritable = environment.inheritable(read, element.document());
StyleEngineFFI::style_engine_note_custom_property_environment(host(), environment.identity(), environment.rust_store(),
inheritable ? inheritable->identity() : 0, inheritable ? inheritable->rust_store() : nullptr);
};
- if (base)
- note(*base);
- if (is_animation_overlay)
- note(*data);
+ if (data.is_animation_overlay_for({ element })) {
+ if (auto base = data.parent())
+ note(*base);
+ }
+ note(data);
+}
+
+void StyleEngine::set_element_custom_property_data(Layout::BegunRead const& read, DOM::Element const& element, CustomPropertyData const* data)
+{
+ // A move of the environment the element inherits reads whether this is its animation overlay, and
+ // whether what the element's style resolves to declares custom properties of its own.
+ bool const is_animation_overlay = data && data->is_animation_overlay_for({ element });
+ auto const* base = is_animation_overlay ? data->parent().ptr() : data;
+ if (data)
+ note_element_custom_property_environments(read, element, *data);
StyleEngineFFI::style_engine_set_element_custom_property_data(m_render_document->host(), element.style_node_id().value(), data, data ? data->identity() : 0, is_animation_overlay, base && base->declared_count() > 0,
base ? base->identity() : 0);
}
+// The registrations decide what an environment hands down, and the engine resolves a child's environment over what
+// its parent's hands down as the host last named it. Once the registrations change, name it again for every
+// environment an element holds, so a child the engine styles from here on gets an environment the host can install.
+void StyleEngine::republish_element_custom_property_environments(Layout::BegunRead const& read, DOM::Document& document)
+{
+ struct Context {
+ StyleEngine& style_engine;
+ Layout::BegunRead const& read;
+ GC::Ref<DOM::Document> document;
+ } context { *this, read, document };
+ StyleEngineFFI::style_engine_for_each_element_custom_property_environment(host(), &read, &context, [](void* context_pointer, u32 style_node, void const* data) {
+ auto& context = *static_cast<Context*>(context_pointer);
+ auto element = context.document->style_computer().element_for_style_node(StyleNodeID { style_node });
+ if (!element)
+ return;
+ context.style_engine.note_element_custom_property_environments(context.read, *element, *static_cast<CustomPropertyData const*>(data));
+ });
+}
+
static_assert(to_underlying(PseudoElement::KnownPseudoElementCount) <= 64);
// The engine resolves fonts against the @font-face table and cascade memo it was given, at the generation of the
diff --git a/Libraries/LibWeb/CSS/StyleEngineBridge.h b/Libraries/LibWeb/CSS/StyleEngineBridge.h
index d51802a5638..95733a6c1e0 100644
--- a/Libraries/LibWeb/CSS/StyleEngineBridge.h
+++ b/Libraries/LibWeb/CSS/StyleEngineBridge.h
@@ -440,6 +440,8 @@ public:
// The custom-property environment each element holds is kept here; the element keeps none of its own.
void set_element_custom_property_data(Layout::BegunRead const& read, DOM::Element const&, CustomPropertyData const*);
+ void note_element_custom_property_environments(Layout::BegunRead const& read, DOM::Element const&, CustomPropertyData const&);
+ void republish_element_custom_property_environments(Layout::BegunRead const& read, DOM::Document&);
// Enumerates the engine's counters. Returns false once index is past the last counter.
bool counter(Layout::BegunRead const& read, size_t index, StringView& out_name, u64& out_value) const;
diff --git a/Libraries/LibWeb/CSS/UpdateStyle.cpp b/Libraries/LibWeb/CSS/UpdateStyle.cpp
index a9417cbb703..996208ed2be 100644
--- a/Libraries/LibWeb/CSS/UpdateStyle.cpp
+++ b/Libraries/LibWeb/CSS/UpdateStyle.cpp
@@ -207,10 +207,12 @@ static void note_style_reaction_applied(Layout::BegunRead const& read, DOM::Elem
// One element's computed style answers for another only while the inputs it was keyed on still mean what they meant. A
// transaction boundary is exactly where they stop doing so: a declaration keyed on by identity may have been edited,
// and a sheet may have come or gone. The engine resolves custom properties against the registrations in force, and an
-// @property rule registers through this cache.
-static void prepare_for_style_engine_transaction(DOM::Document& document)
+// @property rule registers through this cache. A registration change moves what every environment an element holds
+// hands down, which the engine is told again before the transaction derives records under them.
+static void prepare_for_style_engine_transaction(Layout::BegunRead const& read, DOM::Document& document)
{
document.build_registered_properties_cache_for_style_update();
+ document.republish_custom_property_environments_for_style_update(read);
document.style_computer().prepare_for_style_engine_transaction();
}
@@ -232,7 +234,7 @@ StyleEngineFFI::FfiTakenStyleTransaction StyleEngineFFI::web_css_take_style_tran
} else {
auto transaction_setup_started_at = MonotonicTime::now();
++counters.style_engine_transaction_setups;
- prepare_for_style_engine_transaction(document);
+ prepare_for_style_engine_transaction(read, document);
auto setup_microseconds = microseconds_since(transaction_setup_started_at);
counters.style_engine_transaction_setup_microseconds += setup_microseconds;
counters.style_update_submission_microseconds += setup_microseconds;
@@ -791,7 +793,7 @@ static Optional<StyleUpdateInputs> let_style_update_fly(Layout::BegunRead const&
document.sample_animation_effects_needing_style_update();
if (!style_computer.style_engine().has_pending_transaction(read))
return {};
- prepare_for_style_engine_transaction(document);
+ prepare_for_style_engine_transaction(read, document);
if (!style_computer.style_engine().let_style_transaction_fly(read, *inputs, root->style_node_id(), blocker))
return {};
return inputs;
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index 6aa8e528d13..e00017e6e6a 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -11731,6 +11731,11 @@ void Document::did_change_custom_property_registrations(Optional<Utf16FlyString>
{
++m_custom_property_registration_generation;
sync_custom_property_registrations_to_rust();
+ // What every environment an element holds hands down follows the registrations. The engine has to be told
+ // again, or a child it styles under one registration resolves over an environment the host can't install.
+ // That waits for the next style update, which reaches the engine under a read of its own. A change beside
+ // a frame in flight leaves the flight alone.
+ m_custom_property_environments_need_republishing = true;
// An @property rule's program input already reaches the elements declaring or referencing its
// name. CSS.registerProperty() has no rule, so publish the equivalent named input explicitly.
@@ -11740,6 +11745,14 @@ void Document::did_change_custom_property_registrations(Optional<Utf16FlyString>
}
}
+void Document::republish_custom_property_environments_for_style_update(Layout::BegunRead const& read)
+{
+ if (!m_custom_property_environments_need_republishing)
+ return;
+ m_custom_property_environments_need_republishing = false;
+ style_computer().style_engine().republish_element_custom_property_environments(read, *this);
+}
+
void Document::sync_custom_property_registrations_to_rust()
{
m_rust_custom_property_registry_synced = true;
diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h
index e524b6b26e5..abe99455c06 100644
--- a/Libraries/LibWeb/DOM/Document.h
+++ b/Libraries/LibWeb/DOM/Document.h
@@ -974,6 +974,9 @@ public:
void mark_style_attribute_dirty(Element&);
void synchronize_dirty_style_attributes();
void flush_deferred_style_change_event();
+ // Once the registrations changed, name again to the style engine what every environment an
+ // element holds hands down, before the transaction that derives records under them.
+ void republish_custom_property_environments_for_style_update(Layout::BegunRead const&);
// The style engine resolves substitutions against the Rust registry, whose parse context is
// set up by the first sync; a document without registrations syncs once for it.
void build_registered_properties_cache_for_style_update()
@@ -2369,6 +2372,7 @@ private:
size_t m_custom_property_registration_generation { 0 };
void const* m_rust_custom_property_registry { nullptr };
bool m_rust_custom_property_registry_synced { false };
+ bool m_custom_property_environments_need_republishing { false };
CSS::StyleScope m_style_scope;
diff --git a/Libraries/LibWeb/Rust/src/css/style/custom_property_environments.rs b/Libraries/LibWeb/Rust/src/css/style/custom_property_environments.rs
index 1c31446d993..c6d2db9a645 100644
--- a/Libraries/LibWeb/Rust/src/css/style/custom_property_environments.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/custom_property_environments.rs
@@ -396,12 +396,11 @@ impl CustomPropertyEnvironments {
}
/// The registrations changed. What a written value substitutes to under an environment depends
- /// on them, and so does what an environment C++ published hands down, which C++ names again
- /// as it hands the environment over: forget both.
+ /// on them: forget those. So does what an environment C++ published hands down, which C++
+ /// names again for every environment an element holds as the registrations change, and as it
+ /// hands any environment over.
pub(super) fn registrations_changed(&mut self) {
self.substitutions.clear();
- self.inheritable
- .retain(|&identity, _| identity & ENGINE_ENVIRONMENT_IDENTITY_BIT != 0);
}
pub(super) fn remember_substitution(
diff --git a/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs b/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs
index 9902ca3aa15..fb93bbce9b0 100644
--- a/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs
@@ -1188,6 +1188,31 @@ pub extern "C" fn style_engine_transition_baseline(host: &DocumentHost, node: u3
/// Whether the engine has a style transaction pending, which the host knows without asking where it wrote nothing
/// since its last job.
+/// Hands the host every custom-property environment the engine holds for an element, with the element's style node,
+/// for the host to name again what each hands down. The callbacks run outside the engine, so each may queue a write
+/// to it or ask it.
+#[unsafe(no_mangle)]
+pub extern "C" fn style_engine_for_each_element_custom_property_environment(
+ host: &DocumentHost,
+ read: &BegunRead,
+ context: *mut c_void,
+ callback: extern "C" fn(*mut c_void, u32, *const c_void),
+) {
+ // The engine keeps each environment alive while an element holds it, and nothing moves one on before the
+ // callbacks have run.
+ let held: Vec<(u32, *const c_void)> = with_engine(read, host, |engine| {
+ engine
+ .retained
+ .element_custom_property_data
+ .iter()
+ .map(|(node, held)| (node.raw(), held.data.data()))
+ .collect()
+ });
+ for (node, data) in held {
+ callback(context, node, data);
+ }
+}
+
#[unsafe(no_mangle)]
pub extern "C" fn style_engine_has_pending_transaction(host: &DocumentHost, read: &BegunRead) -> bool {
match host.known_engine_facts() {
diff --git a/Tests/LibWeb/Text/expected/css/custom-property-registration-change-under-animated-custom-property.txt b/Tests/LibWeb/Text/expected/css/custom-property-registration-change-under-animated-custom-property.txt
new file mode 100644
index 00000000000..d0a4284738d
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/custom-property-registration-change-under-animated-custom-property.txt
@@ -0,0 +1,3 @@
+rgb(1, 2, 3)
+50
+0
diff --git a/Tests/LibWeb/Text/input/css/custom-property-registration-change-under-animated-custom-property.html b/Tests/LibWeb/Text/input/css/custom-property-registration-change-under-animated-custom-property.html
new file mode 100644
index 00000000000..59ab08af9db
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/custom-property-registration-change-under-animated-custom-property.html
@@ -0,0 +1,31 @@
+<!DOCTYPE html>
+<style>
+ @property --fade { syntax: "<number>"; inherits: false; initial-value: 0; }
+ @property --t { syntax: "<number>"; inherits: true; initial-value: 0; }
+ @keyframes count { from { --t: 0; } to { --t: 100; } }
+ .grid { display: grid; --fade: 1; --t: 0; animation: count 10s linear infinite paused; animation-delay: -5s; }
+ .child { color: rgb(1, 2, 3); }
+</style>
+<div class="grid" id="grid"><div>existing</div></div>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ // Start the animation, which samples --t into the environment the grid hands its children: paused
+ // halfway, so the sample is 50.
+ document.body.offsetHeight;
+ // Change the registrations after that environment was published.
+ const style = document.createElement("style");
+ style.textContent = "@property --late { syntax: '<length>'; inherits: true; initial-value: 0px; }";
+ document.head.appendChild(style);
+ // A child inserted under the animating grid gets its first style under the new registrations.
+ const child = document.createElement("div");
+ child.className = "child";
+ child.textContent = "new";
+ grid.appendChild(child);
+ document.body.offsetHeight;
+ const computed = getComputedStyle(child);
+ println(computed.color);
+ println(computed.getPropertyValue("--t"));
+ println(computed.getPropertyValue("--fade"));
+ });
+</script>