Skip to content

Truly independent web browser

LibWeb: Cancel the animations of a pseudo-element that is display: none

Andreas Kling62c1e0e3646a
diff --git a/Libraries/LibWeb/CSS/UpdateStyle.cpp b/Libraries/LibWeb/CSS/UpdateStyle.cpp
index fb9a17edad8..afe7d6cb6f4 100644
--- a/Libraries/LibWeb/CSS/UpdateStyle.cpp
+++ b/Libraries/LibWeb/CSS/UpdateStyle.cpp
@@ -355,6 +355,16 @@ void StyleEngineFFI::web_css_install_styles(StyleReactionApplication* applicatio
pseudo_element_before_change[i] = element->installed_style(pseudo_element);
}
}
+ // The pseudo-elements that are displayed before their swaps, ignoring animations: one that no longer is after
+ // them ends the animations it owns.
+ u16 pseudo_elements_displayed_before = 0;
+ if (element && element_effects.pseudo_elements_composed != 0 && element->has_css_animations_or_transitions()) {
+ for (auto pseudo_element : DOM::Element::computed_pseudo_elements) {
+ auto const& style = element->installed_style(pseudo_element);
+ if (style && !style.display_is_none_ignoring_animations())
+ pseudo_elements_displayed_before |= static_cast<u16>(1 << to_underlying(pseudo_element));
+ }
+ }
// The effects compare the record the element moves to with the one it moves from, which the installation pins
// while the host applies it.
InstalledStyle const old_style { element_effects.old_record };
@@ -396,7 +406,7 @@ void StyleEngineFFI::web_css_install_styles(StyleReactionApplication* applicatio
// The host composes the records swapped in for the element's pseudo-elements that owe the plan of their
// animations or the transition step, or whose element has animations, as it composes those it settles.
if (element_effects.pseudo_elements_composed != 0) {
- application->invalidation |= element->compose_style_pseudo_element_records(read, element_effects.pseudo_elements_composed, element_effects.pseudo_element_transition_steps, pseudo_element_before_change);
+ application->invalidation |= element->compose_style_pseudo_element_records(read, element_effects.pseudo_elements_composed, element_effects.pseudo_element_transition_steps, pseudo_element_before_change, pseudo_elements_displayed_before);
}
if (display_none_before.has_value()) {
element->apply_display_none_change(*display_none_before);
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp
index 4842dae997b..b8b18ae19a3 100644
--- a/Libraries/LibWeb/DOM/Element.cpp
+++ b/Libraries/LibWeb/DOM/Element.cpp
@@ -1862,7 +1862,7 @@ RefPtr<CSS::CustomPropertyData const> Element::style_custom_property_environment
return declarations;
}
-CSS::RequiredInvalidationAfterStyleChange Element::compose_style_pseudo_element_records(Layout::BegunRead const& read, u16 composed, u16 transition_steps, Array<CSS::InstalledStyle, computed_pseudo_elements.size()> const& before_change_styles)
+CSS::RequiredInvalidationAfterStyleChange Element::compose_style_pseudo_element_records(Layout::BegunRead const& read, u16 composed, u16 transition_steps, Array<CSS::InstalledStyle, computed_pseudo_elements.size()> const& before_change_styles, u16 displayed_before)
{
auto& style_computer = document().style_computer();
CSS::RequiredInvalidationAfterStyleChange invalidation;
@@ -1877,6 +1877,18 @@ CSS::RequiredInvalidationAfterStyleChange Element::compose_style_pseudo_element_
style_computer.compose_installed_engine_record(read, { *this, pseudo_element }, before_change);
if ((transition_steps & bit) && before_change)
invalidation |= style_computer.run_transition_step_for_installed_record(read, { *this, pseudo_element }, before_change);
+ // https://drafts.csswg.org/css-animations-2/#owning-element-section
+ // If the display property of an element is set to none and its display value would compute to none when
+ // ignoring the Transitions and Animations cascade origins, then terminate running animations with this owning
+ // element.
+ // NB: As for an element, this applies once the record holds the pseudo-element's animations and its transition
+ // step has run, and only as its display turns to none: a transition of display that starts from there keeps
+ // the pseudo-element displayed until it ends.
+ if ((displayed_before & bit) && installed_style(pseudo_element).display_is_none_ignoring_animations() && (has_css_defined_animations(pseudo_element) || has_existing_transitions(pseudo_element))) {
+ cancel_css_animations_and_transitions_of(pseudo_element);
+ // NB: The animations and transitions this terminated compose no more over the record.
+ style_computer.compose_installed_engine_record(read, { *this, pseudo_element }, {});
+ }
}
apply_computed_pseudo_element_styles_to_layout_nodes_if_needed(read, invalidation);
return invalidation;
diff --git a/Libraries/LibWeb/DOM/Element.h b/Libraries/LibWeb/DOM/Element.h
index bdc4a953dbb..08f51ea86cd 100644
--- a/Libraries/LibWeb/DOM/Element.h
+++ b/Libraries/LibWeb/DOM/Element.h
@@ -435,8 +435,10 @@ public:
// Composes the style records swapped in for those of the element's pseudo-elements the `composed` mask names, a
// bit at each kind: applies the plan each record decides, samples the animations over it, and runs the transition
// step for those the `transition_steps` mask names, against the record each held before, which
- // `before_change_style_records` names in the order of `computed_pseudo_elements`.
- CSS::RequiredInvalidationAfterStyleChange compose_style_pseudo_element_records(Layout::BegunRead const& read, u16 composed, u16 transition_steps, Array<CSS::InstalledStyle, computed_pseudo_elements.size()> const& before_change_styles);
+ // `before_change_style_records` names in the order of `computed_pseudo_elements`. A pseudo-element the
+ // `displayed_before` mask names, which was displayed before its record was swapped in, ends the CSS animations and
+ // transitions it owns where the record is `display: none`.
+ CSS::RequiredInvalidationAfterStyleChange compose_style_pseudo_element_records(Layout::BegunRead const& read, u16 composed, u16 transition_steps, Array<CSS::InstalledStyle, computed_pseudo_elements.size()> const& before_change_styles, u16 displayed_before);
void set_needs_layout_tree_rebuild(Layout::BegunRead const& read, SetNeedsLayoutTreeUpdateReason, CSS::LayoutTreeRebuildRoot);
diff --git a/Tests/LibWeb/Text/expected/css/pseudo-element-animations-canceled-by-display-none.txt b/Tests/LibWeb/Text/expected/css/pseudo-element-animations-canceled-by-display-none.txt
new file mode 100644
index 00000000000..b62ef1d106d
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/pseudo-element-animations-canceled-by-display-none.txt
@@ -0,0 +1,10 @@
+Animated ::before, displayed: inline, rgb(255, 0, 0), animations: [color]
+Animated ::before, display: none: none, rgb(0, 128, 0), animations: []
+Animated ::before, displayed again: inline, rgb(255, 0, 0), animations: [color]
+Transitioned ::before, displayed: inline, rgb(0, 128, 0), animations: []
+Transitioned ::before, transition started: inline, rgb(0, 128, 0), animations: [color]
+Transitioned ::before, display: none: none, rgb(0, 0, 255), animations: []
+Transitioned ::before, displayed again: inline, rgb(0, 0, 255), animations: []
+Exiting ::before, displayed: inline, rgb(255, 0, 0), animations: [color]
+Exiting ::before, display: none: inline, rgb(0, 128, 0), animations: [display]
+Exiting ::before, restyled during its display transition: inline, rgb(0, 128, 0), animations: [display]
diff --git a/Tests/LibWeb/Text/input/css/pseudo-element-animations-canceled-by-display-none.html b/Tests/LibWeb/Text/input/css/pseudo-element-animations-canceled-by-display-none.html
new file mode 100644
index 00000000000..146b37b4034
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/pseudo-element-animations-canceled-by-display-none.html
@@ -0,0 +1,58 @@
+<!DOCTYPE html>
+<style>
+ @keyframes color {
+ from { color: rgb(255, 0, 0); }
+ to { color: rgb(255, 0, 0); }
+ }
+ .target::before {
+ content: "x";
+ color: rgb(0, 128, 0);
+ }
+ .target.hidden::before { display: none; }
+ .target.restyled::before { text-decoration: underline; }
+ #animated::before { animation: color 100s; }
+ #transitioned::before { transition: color 100s steps(1, end); }
+ #transitioned.moved::before { color: rgb(0, 0, 255); }
+ #exiting::before {
+ animation: color 100s;
+ transition: display 100s allow-discrete;
+ }
+</style>
+<div id="animated" class="target"></div>
+<div id="transitioned" class="target"></div>
+<div id="exiting" class="target"></div>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ const dump = (label, element) => {
+ const animations = element
+ .getAnimations({ subtree: true })
+ .map(animation => animation.transitionProperty ?? animation.animationName);
+ const style = getComputedStyle(element, "::before");
+ println(`${label}: ${style.display}, ${style.color}, animations: [${animations}]`);
+ };
+
+ const animated = document.getElementById("animated");
+ dump("Animated ::before, displayed", animated);
+ animated.classList.add("hidden");
+ dump("Animated ::before, display: none", animated);
+ animated.classList.remove("hidden");
+ dump("Animated ::before, displayed again", animated);
+
+ const transitioned = document.getElementById("transitioned");
+ dump("Transitioned ::before, displayed", transitioned);
+ transitioned.classList.add("moved");
+ dump("Transitioned ::before, transition started", transitioned);
+ transitioned.classList.add("hidden");
+ dump("Transitioned ::before, display: none", transitioned);
+ transitioned.classList.remove("hidden");
+ dump("Transitioned ::before, displayed again", transitioned);
+
+ const exiting = document.getElementById("exiting");
+ dump("Exiting ::before, displayed", exiting);
+ exiting.classList.add("hidden");
+ dump("Exiting ::before, display: none", exiting);
+ exiting.classList.add("restyled");
+ dump("Exiting ::before, restyled during its display transition", exiting);
+ });
+</script>