Truly independent web browser
LibWeb: Don't animate a child no slot takes
Andreas Klingb777162e6c93
diff --git a/Libraries/LibWeb/CSS/UpdateStyle.cpp b/Libraries/LibWeb/CSS/UpdateStyle.cppindex cef67df808f..a1ec52588e5 100644--- a/Libraries/LibWeb/CSS/UpdateStyle.cpp+++ b/Libraries/LibWeb/CSS/UpdateStyle.cpp@@ -574,7 +574,12 @@ 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();+ constexpr auto assigned_slot_changes = drains_flown_transaction ? DOM::Document::AssignedSlotChanges::FlewPast : DOM::Document::AssignedSlotChanges::Recorded;+ document.settle_elements_whose_assigned_slot_changed(assigned_slot_changes, read, [](DOM::Element& element) {+ // The animations a slot change terminated compose no more over the record.+ if (element.installed_style().view().animated_overlay || element.custom_property_data_is_animation_overlay({}))+ sample_animations_for_installed_record(DOM::AbstractElement { element });+ });if (!applied.had_reactions)return;@@ -623,6 +628,7 @@ static Optional<StyleUpdateInputs> let_style_update_fly(Layout::BegunRead const&prepare_for_style_engine_transaction(read, document);if (!style_computer.style_engine().let_style_update_fly(*inputs, root->style_node_id(), blocker))return {};+ document.note_style_update_flew_past_assigned_slot_changes();return inputs;}diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cppindex 5401d1f2060..d10c6a3cc91 100644--- a/Libraries/LibWeb/DOM/Document.cpp+++ b/Libraries/LibWeb/DOM/Document.cpp@@ -1102,6 +1102,7 @@ void Document::visit_edges(Cell::Visitor& visitor)visitor.visit(m_top_layer_elements);visitor.visit(m_elements_whose_assigned_slot_changed);+ visitor.visit(m_elements_whose_assigned_slot_change_flew_past);visitor.visit(m_picture_in_picture_element);visitor.visit(m_top_layer_pending_removals);visitor.visit(m_elements_with_pending_top_layer_membership_change);@@ -10439,14 +10440,27 @@ void Document::note_element_assigned_slot_changed(Element& 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; });+ auto is_element = [&](auto const& noted) { return noted.ptr() == &element; };+ m_elements_whose_assigned_slot_changed.remove_all_matching(is_element);+ m_elements_whose_assigned_slot_change_flew_past.remove_all_matching(is_element);}-void Document::settle_elements_whose_assigned_slot_changed()+void Document::note_style_update_flew_past_assigned_slot_changes(){- 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();+ m_elements_whose_assigned_slot_change_flew_past.extend(move(m_elements_whose_assigned_slot_changed));+ m_elements_whose_assigned_slot_changed.clear();+}++void Document::settle_elements_whose_assigned_slot_changed(AssignedSlotChanges changes, Layout::BegunRead const& read, Function<void(Element&)> const& compose_element)+{+ auto& noted = changes == AssignedSlotChanges::FlewPast ? m_elements_whose_assigned_slot_change_flew_past : m_elements_whose_assigned_slot_changed;+ GC::RootVector<GC::Ref<Element>> elements { move(noted) };+ noted.clear();+ for (auto& element : elements) {+ // NB: Leaving took the animations of what left along.+ if (element->is_connected())+ element->settle_animations_of_flat_tree_subtree(read, compose_element);+ }}void Document::unregister_shadow_root(Badge<DOM::ShadowRoot>, DOM::ShadowRoot& shadow_root)diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.hindex 99092df0d72..f457d9c3ce2 100644--- a/Libraries/LibWeb/DOM/Document.h+++ b/Libraries/LibWeb/DOM/Document.h@@ -1071,7 +1071,13 @@ public: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();+ // What a style update that flies knows of is settled as it lands. What comes beside it is the next update's.+ void note_style_update_flew_past_assigned_slot_changes();+ enum class AssignedSlotChanges : u8 {+ Recorded,+ FlewPast,+ };+ void settle_elements_whose_assigned_slot_changed(AssignedSlotChanges, Layout::BegunRead const&, Function<void(Element&)> const& compose_element);void set_has_completed_style_update() { m_has_completed_style_update = true; }void mark_style_attribute_dirty(Element&);void synchronize_dirty_style_attributes();@@ -2392,6 +2398,7 @@ private:// 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;+ Vector<GC::Ref<Element>> m_elements_whose_assigned_slot_change_flew_past;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 f8cd2a8d7eb..2ca6f34ff76 100644--- a/Libraries/LibWeb/DOM/Element.cpp+++ b/Libraries/LibWeb/DOM/Element.cpp@@ -2151,6 +2151,20 @@ void Element::play_or_cancel_animations_of_flat_tree_subtree()});}+void Element::settle_animations_of_flat_tree_subtree(Layout::BegunRead const& read, Function<void(Element&)> const& compose_element)+{+ auto const& style_computer = document().style_computer();+ for_each_inclusive_descendant_element_in_flat_tree(*this, [&](Element& element) {+ element.play_or_cancel_animations_after_display_property_change();+ if (element.has_style()) {+ AbstractElement abstract_element { element };+ style_computer.apply_settled_animation_plan(read, abstract_element);+ compose_element(element);+ }+ return TraversalDecision::Continue;+ });+}+void Element::clear_computed_styles_from_display_none_descendants(Layout::BegunRead const& read){for_each_inclusive_descendant_element_in_flat_tree(*this, [this, &read](Element& descendant) {@@ -2319,6 +2333,12 @@ WebIDL::ExceptionOr<void> Element::attach_a_shadow_root(ShadowRootMode mode, boo// 13. Set element’s shadow root to shadow.set_shadow_root(shadow);++ // NB: This takes the children out of the flat tree until a slot takes them.+ for_each_child_of_type<Element>([&](Element& child) {+ document().note_element_assigned_slot_changed(child);+ return IterationDecision::Continue;+ });return {};}diff --git a/Libraries/LibWeb/DOM/Element.h b/Libraries/LibWeb/DOM/Element.hindex 337367e1469..f264e5d8438 100644--- a/Libraries/LibWeb/DOM/Element.h+++ b/Libraries/LibWeb/DOM/Element.h@@ -386,6 +386,9 @@ public: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();+ // As above, for a subtree that may have entered the flat tree, whose animations a record that did not move has yet+ // to plan.+ void settle_animations_of_flat_tree_subtree(Layout::BegunRead const&, Function<void(Element&)> const& compose_element);// 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 690ec9f72ab..da6649db462 100644--- a/Libraries/LibWeb/DOM/Node.cpp+++ b/Libraries/LibWeb/DOM/Node.cpp@@ -4976,6 +4976,10 @@ bool Node::has_inclusive_ancestor_with_display_none_ignoring_animations() const}if (ancestor_element->installed_style().display_is_none_ignoring_animations())return true;+ // NB: A child of a shadow host that no slot takes is not in the flat tree, so it generates no box, as if it+ // were below a display:none element.+ if (auto parent = ancestor_element->parent_element(); parent && parent->shadow_root() && !ancestor_element->assigned_slot_internal())+ return true;ancestor = ancestor_element->element_to_inherit_style_from({}).ptr();}return false;diff --git a/Libraries/LibWeb/Rust/src/css/style/inputs.rs b/Libraries/LibWeb/Rust/src/css/style/inputs.rsindex 1e576527538..787df231acc 100644--- a/Libraries/LibWeb/Rust/src/css/style/inputs.rs+++ b/Libraries/LibWeb/Rust/src/css/style/inputs.rs@@ -646,18 +646,31 @@ impl RetainedState {.or_else(|| self.projection.parent_or_shadow_host(current))}).any(|ancestor| {- self.computed_group_sets- .assigned_style_record(ancestor)- .and_then(|record| self.computed_group_sets.style_record_view(record.raw()))- .is_some_and(|view| {- crate::css::computed_value_views::ComputedValuesView::of(view.base_payloads)- .display()- .is_none()- && !view.longhands().display_none_is_animated()- })+ self.is_child_of_shadow_host_no_slot_takes(ancestor)+ || self+ .computed_group_sets+ .assigned_style_record(ancestor)+ .and_then(|record| self.computed_group_sets.style_record_view(record.raw()))+ .is_some_and(|view| {+ crate::css::computed_value_views::ComputedValuesView::of(view.base_payloads)+ .display()+ .is_none()+ && !view.longhands().display_none_is_animated()+ })})}+ /// Whether `node` is a child of a shadow host that no slot takes. Such an element is not in the flat tree, so it+ /// generates no box, as if it were below a `display: none` element.+ pub(crate) fn is_child_of_shadow_host_no_slot_takes(&self, node: StyleNodeID) -> bool {+ if self.projection.assigned_slot_of(node).is_some() {+ return false;+ }+ self.projection+ .parent(node)+ .is_some_and(|parent| self.projection.shadow_root_of(parent).is_some())+ }+/// Whether the element is a `<slot>`, whose children the flat tree takes elsewhere.#[must_use]pub fn element_is_slot(&self, node: StyleNodeID) -> bool {diff --git a/Libraries/LibWeb/Rust/src/css/style_compute.rs b/Libraries/LibWeb/Rust/src/css/style_compute.rsindex 521f7c2b93d..2b963aec97c 100644--- a/Libraries/LibWeb/Rust/src/css/style_compute.rs+++ b/Libraries/LibWeb/Rust/src/css/style_compute.rs@@ -6801,6 +6801,7 @@ fn in_display_none_subtree_for_animations(// 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 if style_engine.is_child_of_shadow_host_no_slot_takes(node) => return true,true => style_engine.assigned_slot_of(node).or_else(|| style_engine.parent_or_shadow_host(node)),diff --git a/Tests/LibWeb/Text/expected/HTML/frame-in-flight/slot-change-beside-a-style-update-that-flies.txt b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/slot-change-beside-a-style-update-that-flies.txtnew file mode 100644index 00000000000..6e569db97dd--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/slot-change-beside-a-style-update-that-flies.txt@@ -0,0 +1,3 @@+Slotted: 1 animations, opacity 0.5+The change ran beside: in-flight+Moved to a hidden slot: 0 animations, opacity 1diff --git a/Tests/LibWeb/Text/expected/css/css-animation-of-a-child-no-slot-takes.txt b/Tests/LibWeb/Text/expected/css/css-animation-of-a-child-no-slot-takes.txtnew file mode 100644index 00000000000..23e4186e39d--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/css-animation-of-a-child-no-slot-takes.txt@@ -0,0 +1,13 @@+no slot in the shadow tree: 0 animations, opacity 1+its child: 0 animations, opacity 1+a slot takes it: 1 animations, opacity 0.5+its child: 1 animations, opacity 0.5+the slot leaves: 0 animations, opacity 1+its child: 0 animations, opacity 1+no slot of its name: 0 animations, opacity 1+renamed for the slot: 1 animations, opacity 0.5+renamed away: 0 animations, opacity 1+a child of a plain element: 1 animations, opacity 0.5+the parent becomes a host: 0 animations, opacity 1+a slot takes it: 1 animations, opacity 0.5+an element of the shadow tree: 1 animations, opacity 0.5diff --git a/Tests/LibWeb/Text/input/HTML/frame-in-flight/slot-change-beside-a-style-update-that-flies.html b/Tests/LibWeb/Text/input/HTML/frame-in-flight/slot-change-beside-a-style-update-that-flies.htmlnew file mode 100644index 00000000000..67016bde924--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/frame-in-flight/slot-change-beside-a-style-update-that-flies.html@@ -0,0 +1,50 @@+<!DOCTYPE html>+<script src="../../include.js"></script>+<script src="frame-in-flight.js"></script>+<style>+ @keyframes half {+ from,+ to {+ opacity: 0.5;+ }+ }+ .animated {+ animation: half 1000s;+ }+</style>+<div id="host"><div id="child" class="animated"></div></div>+<div id="flip"></div>+<script>+ // A slot change beside a style update that flew is the next style update's, which knows the style of the new slot.+ asyncTest(async done => {+ const host = document.getElementById("host");+ const child = document.getElementById("child");+ const flip = document.getElementById("flip");+ const shadow = host.attachShadow({ mode: "open" });+ shadow.innerHTML = `<slot></slot>`;+ function report(label) {+ println(`${label}: ${child.getAnimations().length} animations, opacity ${getComputedStyle(child).opacity}`);+ }+ report("Slotted");++ const state = await whileRecordingInFlight(+ () => {+ flip.className = "flipped";+ },+ state => {+ // The new slot has no style yet as the update that flew lands, and the one after computes it.+ const hidden = document.createElement("slot");+ hidden.name = "hidden";+ hidden.style.display = "none";+ shadow.append(hidden);+ child.slot = "hidden";+ return state;+ },+ "style"+ );+ println(`The change ran beside: ${state}`);+ await twoFramesLanded();+ report("Moved to a hidden slot");+ done();+ });+</script>diff --git a/Tests/LibWeb/Text/input/css/css-animation-of-a-child-no-slot-takes.html b/Tests/LibWeb/Text/input/css/css-animation-of-a-child-no-slot-takes.htmlnew file mode 100644index 00000000000..c671f3543eb--- /dev/null+++ b/Tests/LibWeb/Text/input/css/css-animation-of-a-child-no-slot-takes.html@@ -0,0 +1,66 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<style>+ @keyframes half {+ from, to { opacity: 0.5; }+ }+ .animated {+ animation: half 1000s;+ }+</style>+<body>+<script>+ function report(label, target) {+ document.body.offsetWidth;+ println(`${label}: ${target.getAnimations().length} animations, opacity ${getComputedStyle(target).opacity}`);+ }++ function build(shadowHTML, slotName) {+ const host = document.createElement("div");+ const root = host.attachShadow({ mode: "open" });+ root.innerHTML = shadowHTML;+ const target = document.createElement("div");+ target.className = "animated";+ if (slotName)+ target.slot = slotName;+ const child = document.createElement("div");+ child.className = "animated";+ target.append(child);+ host.append(target);+ document.body.append(host);+ return { host, root, target, child };+ }++ test(() => {+ let { host, root, target, child } = build("");+ report("no slot in the shadow tree", target);+ report("its child", child);+ root.innerHTML = "<slot></slot>";+ report("a slot takes it", target);+ report("its child", child);+ root.innerHTML = "";+ report("the slot leaves", target);+ report("its child", child);++ ({ target, child } = build("<slot></slot>", "other"));+ report("no slot of its name", target);+ target.slot = "";+ report("renamed for the slot", target);+ target.slot = "other";+ report("renamed away", target);++ host = document.createElement("div");+ target = document.createElement("div");+ target.className = "animated";+ host.append(target);+ document.body.append(host);+ report("a child of a plain element", target);+ const newRoot = host.attachShadow({ mode: "open" });+ report("the parent becomes a host", target);+ newRoot.innerHTML = "<slot></slot>";+ report("a slot takes it", target);++ ({ root, target } = build("<style>@keyframes half { from, to { opacity: 0.5; } } .animated { animation: half 1000s; }</style><div class=animated id=inside></div>", "other"));+ report("an element of the shadow tree", root.getElementById("inside"));+ });+</script>