Truly independent web browser
LibWeb: Stop CSS animations below a hidden slot
Andreas Kling94d8184fe986
diff --git a/Libraries/LibWeb/CSS/UpdateStyle.cpp b/Libraries/LibWeb/CSS/UpdateStyle.cppindex 44f7c537c98..cef67df808f 100644--- a/Libraries/LibWeb/CSS/UpdateStyle.cpp+++ b/Libraries/LibWeb/CSS/UpdateStyle.cpp@@ -574,6 +574,7 @@ static void update_style(Layout::BegunRead const& read, DOM::Document& document,timing_counters.style_engine_published_reactions += applied.published_reactions;timing_counters.style_update_pass_guard_hits += applied.pass_guard_hits;timing_counters.style_update_apply_microseconds += applied.apply_microseconds;+ document.settle_elements_whose_assigned_slot_changed();if (!applied.had_reactions)return;@@ -668,6 +669,7 @@ static bool document_has_style_work_a_read_waits_for(Layout::BegunRead const& re|| style_engine.has_recorded_input()|| style_engine.has_flown_style_update()|| subtree_arrivals_may_be_due(read, document)+ || document.has_assigned_slot_change_notes()|| document.has_dirty_style_attributes()|| document.has_unreported_style_attributes_to_report()|| document.needs_highlight_style_update()diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cppindex 1b46585b81f..5401d1f2060 100644--- a/Libraries/LibWeb/DOM/Document.cpp+++ b/Libraries/LibWeb/DOM/Document.cpp@@ -1101,6 +1101,7 @@ void Document::visit_edges(Cell::Visitor& visitor)visitor.visit(view_transition);visitor.visit(m_top_layer_elements);+ visitor.visit(m_elements_whose_assigned_slot_changed);visitor.visit(m_picture_in_picture_element);visitor.visit(m_top_layer_pending_removals);visitor.visit(m_elements_with_pending_top_layer_membership_change);@@ -10429,6 +10430,25 @@ void Document::settle_counter_style_scopes(Layout::BegunRead const& read)m_counter_style_scopes_need_update = false;}+void Document::note_element_assigned_slot_changed(Element& element)+{+ // NB: What is not connected has no animations to settle: it starts them as it connects, with its slot as it is then.+ if (element.is_connected())+ m_elements_whose_assigned_slot_changed.append(element);+}++void Document::forget_assigned_slot_change_note(Element& element)+{+ m_elements_whose_assigned_slot_changed.remove_all_matching([&](auto const& noted) { return noted.ptr() == &element; });+}++void Document::settle_elements_whose_assigned_slot_changed()+{+ GC::RootVector<GC::Ref<Element>> elements { move(m_elements_whose_assigned_slot_changed) };+ for (auto& element : elements)+ element->play_or_cancel_animations_of_flat_tree_subtree();+}+void Document::unregister_shadow_root(Badge<DOM::ShadowRoot>, DOM::ShadowRoot& shadow_root){m_shadow_roots.remove(shadow_root);diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.hindex cf4f7bc06f9..99092df0d72 100644--- a/Libraries/LibWeb/DOM/Document.h+++ b/Libraries/LibWeb/DOM/Document.h@@ -1065,6 +1065,13 @@ public:void set_counter_style_scopes_need_update(bool value) { m_counter_style_scopes_need_update = value; }void note_counter_style_scope_to_settle(ShadowRoot&);void settle_counter_style_scopes(Layout::BegunRead const&);++ // Where a slottable's slot leaves or takes it out of a `display: none` subtree, its animations terminate or resume.+ // That is decided at the next style update, as an assignment can go through no slot on its way to another.+ void note_element_assigned_slot_changed(Element&);+ void forget_assigned_slot_change_note(Element&);+ bool has_assigned_slot_change_notes() const { return !m_elements_whose_assigned_slot_changed.is_empty(); }+ void settle_elements_whose_assigned_slot_changed();void set_has_completed_style_update() { m_has_completed_style_update = true; }void mark_style_attribute_dirty(Element&);void synchronize_dirty_style_attributes();@@ -2384,6 +2391,7 @@ private:ShadowRoot::DocumentShadowRootList m_shadow_roots;// The shadow roots whose counter style registry may resolve or publish differently since the last style update.ShadowRoot::DocumentCounterStyleSettleList m_shadow_roots_with_counter_styles_to_settle;+ Vector<GC::Ref<Element>> m_elements_whose_assigned_slot_changed;u64 m_style_sheet_set_generation { 0 };// The `@keyframes` rows the style engine still holds for shadow roots that left this document.Vector<CSS::StyleScope::DepartedAnimationKeyframes> m_departed_animation_keyframes;diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cppindex 239a6252efc..f8cd2a8d7eb 100644--- a/Libraries/LibWeb/DOM/Element.cpp+++ b/Libraries/LibWeb/DOM/Element.cpp@@ -2111,27 +2111,52 @@ Optional<Element::DisplayNoneState> Element::display_none_state() constreturn DisplayNoneState::of(*computed_values, installed_style());}+// Visits `root` and the elements below it in the flat tree, where `display: none` reaches: a host's slottables are below+// the slots they are assigned to, and not reached from the host.+template<typename Callback>+static void for_each_inclusive_descendant_element_in_flat_tree(Element& root, Callback callback)+{+ root.for_each_shadow_including_inclusive_descendant([&](Node& node) {+ auto* element = as_if<Element>(node);+ if (!element)+ return TraversalDecision::Continue;+ if (element != &root && element->assigned_slot_internal())+ return TraversalDecision::SkipChildrenAndContinue;+ auto decision = callback(*element);+ if (decision != TraversalDecision::Continue)+ return decision;+ if (auto* slot = as_if<HTML::HTMLSlotElement>(*element)) {+ for (auto const& assigned_node : slot->assigned_nodes_internal()) {+ if (auto const* assigned_element = assigned_node.get_pointer<GC::Ref<Element>>())+ for_each_inclusive_descendant_element_in_flat_tree(**assigned_element, callback);+ }+ }+ return TraversalDecision::Continue;+ });+}+void Element::apply_display_none_change(DisplayNoneState before){auto after = display_none_state();VERIFY(after.has_value());- if (before.display_ignoring_animations_is_none != after->display_ignoring_animations_is_none) {- for_each_shadow_including_inclusive_descendant([&](auto& node) {- if (!node.is_element())- return TraversalDecision::Continue;- auto& element = static_cast<Element&>(node);- element.play_or_cancel_animations_after_display_property_change();- return TraversalDecision::Continue;- });- }+ if (before.display_ignoring_animations_is_none != after->display_ignoring_animations_is_none)+ play_or_cancel_animations_of_flat_tree_subtree();+}++void Element::play_or_cancel_animations_of_flat_tree_subtree()+{+ for_each_inclusive_descendant_element_in_flat_tree(*this, [](Element& element) {+ element.play_or_cancel_animations_after_display_property_change();+ return TraversalDecision::Continue;+ });}void Element::clear_computed_styles_from_display_none_descendants(Layout::BegunRead const& read){- for_each_shadow_including_descendant([&read](auto& node) {- auto* element = as_if<Element>(node);- if (!element)+ for_each_inclusive_descendant_element_in_flat_tree(*this, [this, &read](Element& descendant) {+ if (&descendant == this)return TraversalDecision::Continue;+ auto* element = &descendant;if (!element->has_style())return TraversalDecision::SkipChildrenAndContinue;diff --git a/Libraries/LibWeb/DOM/Element.h b/Libraries/LibWeb/DOM/Element.hindex ca27a22f259..337367e1469 100644--- a/Libraries/LibWeb/DOM/Element.h+++ b/Libraries/LibWeb/DOM/Element.h@@ -384,6 +384,8 @@ public:// Terminates or resumes the animations of a subtree whose display, ignoring animations, left or entered none since// `before`, and clears the styles of a subtree that became display:none.void apply_display_none_change(DisplayNoneState before);+ // Terminates or resumes the animations of the element and of what is below it in the flat tree.+ void play_or_cancel_animations_of_flat_tree_subtree();// Clears the styles of the descendants of a display:none element, which have no boxes: a CSSOM read or the subtree's// reveal materializes them again. The styles of the SVG elements visible content can reference are computed again.void clear_computed_styles_from_display_none_descendants(Layout::BegunRead const& read);diff --git a/Libraries/LibWeb/DOM/Node.cpp b/Libraries/LibWeb/DOM/Node.cppindex e2e94e4e52f..690ec9f72ab 100644--- a/Libraries/LibWeb/DOM/Node.cpp+++ b/Libraries/LibWeb/DOM/Node.cpp@@ -2378,6 +2378,8 @@ void Node::set_document(Document& document)m_document = &document;if (auto* element = as_if<Element>(*this)) {+ // NB: The note is settled against the style engine of the document the element is in, and it has left that one.+ old_document.forget_assigned_slot_change_note(*element);if (element->style_uses_if_css_function() || element->style_uses_custom_function() || element->style_depends_on_viewport_metrics())document.add_element_with_viewport_dependent_style(*element);element->on_document_changed(old_document, document);diff --git a/Libraries/LibWeb/DOM/Slottable.cpp b/Libraries/LibWeb/DOM/Slottable.cppindex adf789c3907..6847a837cce 100644--- a/Libraries/LibWeb/DOM/Slottable.cpp+++ b/Libraries/LibWeb/DOM/Slottable.cpp@@ -67,6 +67,8 @@ void SlottableMixin::set_assigned_slot(GC::Ptr<HTML::HTMLSlotElement> assigned_s}if (node.document().may_have_blocking_wheel_event_listener())node.update_inside_blocking_wheel_event_handler_state_for_subtree();+ if (auto* element = as_if<Element>(node))+ node.document().note_element_assigned_slot_changed(*element);}GC::Ptr<HTML::HTMLSlotElement> SlottableMixin::manual_slot_assignment()diff --git a/Libraries/LibWeb/Rust/src/css/style_compute.rs b/Libraries/LibWeb/Rust/src/css/style_compute.rsindex c1bdc3dc6f1..521f7c2b93d 100644--- a/Libraries/LibWeb/Rust/src/css/style_compute.rs+++ b/Libraries/LibWeb/Rust/src/css/style_compute.rs@@ -6798,10 +6798,12 @@ fn in_display_none_subtree_for_animations(if longhands.effective_display(None).is_none() && !longhands.display_none_is_animated() {return true;}- // The element's own display is answered above, so an element's walk starts at its parent; a- // pseudo-element's starts at its originating element.+ // The element's own display is answered above, so an element's walk starts at the slot it is+ // assigned to or its parent; a pseudo-element's starts at its originating element.let start = match pseudo_kind == crate::css::cascaded_properties::NO_PSEUDO_ELEMENT {- true => style_engine.parent_or_shadow_host(node),+ true => style_engine+ .assigned_slot_of(node)+ .or_else(|| style_engine.parent_or_shadow_host(node)),false => Some(node),};start.is_some_and(|start| style_engine.has_inclusive_ancestor_with_display_none_ignoring_animations(start))diff --git a/Tests/LibWeb/Text/expected/css/css-animation-below-a-hidden-slot.txt b/Tests/LibWeb/Text/expected/css/css-animation-below-a-hidden-slot.txtnew file mode 100644index 00000000000..1f9b3b23e8e--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/css-animation-below-a-hidden-slot.txt@@ -0,0 +1,23 @@+plain parent, hidden, animation set: 0 animations, opacity 1+plain parent, then shown: 1 animations, opacity 0.5+plain parent, then hidden again: 0 animations, opacity 1+plain parent, then shown again: 1 animations, opacity 0.5+plain parent, hidden, animation there from the start: 0 animations, opacity 1+plain parent, shown, animation set: 1 animations, opacity 0.5+plain parent, then hidden: 0 animations, opacity 1+slot, hidden, animation set: 0 animations, opacity 1+slot, then shown: 1 animations, opacity 0.5+slot, then hidden again: 0 animations, opacity 1+slot, then shown again: 1 animations, opacity 0.5+slot, hidden, animation there from the start: 0 animations, opacity 1+slot, shown, animation set: 1 animations, opacity 0.5+slot, then hidden: 0 animations, opacity 1+shown ancestor of a slot: 1 animations, opacity 0.5+its slottable's child: 1 animations, opacity 0.5+hidden ancestor of a slot: 0 animations, opacity 1+its slottable's child: 0 animations, opacity 1+shown again: 1 animations, opacity 0.5+its slottable's child: 1 animations, opacity 0.5+in a shown slot: 1 animations, opacity 0.5+moved to a hidden slot: 0 animations, opacity 1+moved back: 1 animations, opacity 0.5diff --git a/Tests/LibWeb/Text/expected/css/style-engine/animation-of-an-element-adopted-as-its-slot-changes.txt b/Tests/LibWeb/Text/expected/css/style-engine/animation-of-an-element-adopted-as-its-slot-changes.txtnew file mode 100644index 00000000000..77261e93864--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/style-engine/animation-of-an-element-adopted-as-its-slot-changes.txt@@ -0,0 +1,3 @@+before: tone+adopted: tone, 1 animation+settled: tone, 1 animationdiff --git a/Tests/LibWeb/Text/input/css/css-animation-below-a-hidden-slot.html b/Tests/LibWeb/Text/input/css/css-animation-below-a-hidden-slot.htmlnew file mode 100644index 00000000000..4c926f92659--- /dev/null+++ b/Tests/LibWeb/Text/input/css/css-animation-below-a-hidden-slot.html@@ -0,0 +1,101 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<style>+ @keyframes half {+ from, to { opacity: 0.5; }+ }+ .animated {+ animation: half 1000s;+ }+ .hidden {+ display: none;+ }+</style>+<body>+<script>+ function report(label, target) {+ document.body.offsetWidth;+ println(`${label}: ${target.getAnimations().length} animations, opacity ${getComputedStyle(target).opacity}`);+ }++ // Builds a host whose shadow tree has one slot, with one slotted element, and gives each kind of hiding the same+ // treatment a plain parent gets.+ function buildSlotted(hidden) {+ const host = document.createElement("div");+ const root = host.attachShadow({ mode: "open" });+ root.innerHTML = `<style>.hidden { display: none; }</style><slot class="${hidden ? "hidden" : ""}"></slot>`;+ const target = document.createElement("div");+ host.appendChild(target);+ document.body.appendChild(host);+ return { hider: root.querySelector("slot"), target };+ }++ function buildPlain(hidden) {+ const parent = document.createElement("div");+ parent.className = hidden ? "hidden" : "";+ const target = document.createElement("div");+ parent.appendChild(target);+ document.body.appendChild(parent);+ return { hider: parent, target };+ }++ test(() => {+ for (const [name, build] of [["plain parent", buildPlain], ["slot", buildSlotted]]) {+ let { hider, target } = build(true);+ document.body.offsetWidth;+ target.className = "animated";+ report(`${name}, hidden, animation set`, target);+ hider.className = "";+ report(`${name}, then shown`, target);+ hider.className = "hidden";+ report(`${name}, then hidden again`, target);+ hider.className = "";+ report(`${name}, then shown again`, target);++ ({ hider, target } = build(true));+ target.className = "animated";+ report(`${name}, hidden, animation there from the start`, target);++ ({ hider, target } = build(false));+ target.className = "animated";+ report(`${name}, shown, animation set`, target);+ hider.className = "hidden";+ getComputedStyle(target).opacity;+ report(`${name}, then hidden`, target);+ }++ // Hiding what holds the slot is hiding the slot.+ let host = document.createElement("div");+ const root = host.attachShadow({ mode: "open" });+ root.innerHTML = "<style>.hidden { display: none; }</style><div><slot></slot></div>";+ let target = document.createElement("div");+ target.className = "animated";+ const child = document.createElement("div");+ child.className = "animated";+ target.append(child);+ host.append(target);+ document.body.append(host);+ report("shown ancestor of a slot", target);+ report("its slottable's child", child);+ root.querySelector("div").className = "hidden";+ report("hidden ancestor of a slot", target);+ report("its slottable's child", child);+ root.querySelector("div").className = "";+ report("shown again", target);+ report("its slottable's child", child);++ // A slottable moves between slots.+ host = document.createElement("div");+ host.attachShadow({ mode: "open" }).innerHTML = "<style>.hidden { display: none; }</style><slot name=shown></slot><slot name=hidden class=hidden></slot>";+ target = document.createElement("div");+ target.className = "animated";+ target.slot = "shown";+ host.append(target);+ document.body.append(host);+ report("in a shown slot", target);+ target.slot = "hidden";+ report("moved to a hidden slot", target);+ target.slot = "shown";+ report("moved back", target);+ });+</script>diff --git a/Tests/LibWeb/Text/input/css/style-engine/animation-of-an-element-adopted-as-its-slot-changes.html b/Tests/LibWeb/Text/input/css/style-engine/animation-of-an-element-adopted-as-its-slot-changes.htmlnew file mode 100644index 00000000000..734d29d340a--- /dev/null+++ b/Tests/LibWeb/Text/input/css/style-engine/animation-of-an-element-adopted-as-its-slot-changes.html@@ -0,0 +1,56 @@+<!DOCTYPE html>+<style>+ @keyframes tone {+ from {+ color: rgb(1, 1, 1);+ }+ to {+ color: rgb(2, 2, 2);+ }+ }+ .animated {+ animation: tone 1000s;+ }+</style>+<iframe id="frame"></iframe>+<script src="../../include.js"></script>+<script>+ asyncTest(async done => {+ const frame = document.getElementById("frame");+ const frameDocument = frame.contentDocument;+ frameDocument.body.innerHTML = `+ <style>+ @keyframes tone {+ from {+ color: rgb(1, 1, 1);+ }+ to {+ color: rgb(2, 2, 2);+ }+ }+ .animated {+ animation: tone 1000s;+ }+ </style>+ <div id="host"><div id="child" class="animated"></div></div>+ <p id="other"></p>+ `;+ const host = frameDocument.getElementById("host");+ const child = frameDocument.getElementById("child");+ const other = frameDocument.getElementById("other");+ println(`before: ${frame.contentWindow.getComputedStyle(child).animationName}`);++ // The child's slot changes, and it leaves before its document settles that.+ host.attachShadow({ mode: "open" });+ const adopted = document.adoptNode(child);+ document.body.append(adopted);+ println(`adopted: ${getComputedStyle(adopted).animationName}, ${adopted.getAnimations().length} animation`);++ // Both documents now run a style update.+ other.className = "changed";+ await new Promise(resolve => frame.contentWindow.requestAnimationFrame(resolve));+ await new Promise(resolve => requestAnimationFrame(resolve));+ println(`settled: ${getComputedStyle(adopted).animationName}, ${adopted.getAnimations().length} animation`);+ done();+ });+</script>