Skip to content

Truly independent web browser

LibWeb: Sample hidden animations for a read below display:none

Andreas Kling71ecd56b8f21
diff --git a/Libraries/LibWeb/CSS/StyleComputer.cpp b/Libraries/LibWeb/CSS/StyleComputer.cpp
index 9961f4281d9..9330b459931 100644
--- a/Libraries/LibWeb/CSS/StyleComputer.cpp
+++ b/Libraries/LibWeb/CSS/StyleComputer.cpp
@@ -884,6 +884,12 @@ void StyleComputer::publish_animated_custom_properties(ComputedStyleWorkingSet&
void StyleComputer::invalidate_animated_custom_property_readers(DOM::AbstractElement abstract_element, bool recompute_style) const
{
auto& element = abstract_element.element();
+ // NB: A read below an element that is not displayed samples the animations on its way down. No pass styles what is
+ // below that element, and what the read goes on to demand computes from the environment the sample published.
+ if (m_inheritance_chain_read_depth > 0) {
+ if (auto style = element.computed_style(); style && (style->in_display_none_subtree() || style->display().is_none()))
+ return;
+ }
auto& style_engine = element.document().style_computer().style_engine();
if (recompute_style) {
style_engine.record_derived_element_style_input_change(element.style_node_id(), StyleEngine::PublishedStyle | StyleEngine::RecomputeStyle);
diff --git a/Libraries/LibWeb/CSS/StyleComputer.h b/Libraries/LibWeb/CSS/StyleComputer.h
index e91191df616..e5fa42cf587 100644
--- a/Libraries/LibWeb/CSS/StyleComputer.h
+++ b/Libraries/LibWeb/CSS/StyleComputer.h
@@ -94,6 +94,8 @@ public:
void begin_style_update() const;
void end_style_update() const;
+ void begin_inheritance_chain_read() const { ++m_inheritance_chain_read_depth; }
+ void end_inheritance_chain_read() const { --m_inheritance_chain_read_depth; }
struct ComputedStyleInvalidation {
RequiredInvalidationAfterStyleChange invalidation;
@@ -286,6 +288,7 @@ private:
mutable double m_root_element_effective_zoom { 1 };
mutable u64 m_style_update_depth { 0 };
+ mutable u64 m_inheritance_chain_read_depth { 0 };
mutable Optional<MediaEnvironmentSnapshot> m_style_update_media_environment;
mutable Optional<Parser::ValueParserFFI::FfiMediaEnvironment> m_style_update_ffi_media_environment;
u64 m_viewport_environment_version { 0 };
diff --git a/Libraries/LibWeb/CSS/UpdateStyle.cpp b/Libraries/LibWeb/CSS/UpdateStyle.cpp
index a1ec52588e5..6c9329cf46d 100644
--- a/Libraries/LibWeb/CSS/UpdateStyle.cpp
+++ b/Libraries/LibWeb/CSS/UpdateStyle.cpp
@@ -253,6 +253,23 @@ static void sample_animations_for_installed_record(DOM::AbstractElement abstract
context.publish();
}
+// No style update samples the animations of an element that is not displayed, nor those of its pseudo-elements. A read
+// below it samples them for itself, over the styles they hold.
+void StyleEngineFFI::web_css_sample_undisplayed_animations_for_read(StyleReactionApplication* application, DOM::Element* element)
+{
+ auto& style_computer = application->document->style_computer();
+ auto sample = [&](DOM::AbstractElement abstract_element) {
+ auto const& style = abstract_element.installed_style();
+ if (!style || (!style_computer.has_animations_to_compose(abstract_element) && !style.view().animated_overlay && !abstract_element.custom_property_data_is_animation_overlay()))
+ return;
+ if (element->has_inclusive_ancestor_with_display_none_ignoring_animations())
+ sample_animations_for_installed_record(abstract_element);
+ };
+ sample(DOM::AbstractElement { *element });
+ for (auto pseudo_element : DOM::Element::computed_pseudo_elements)
+ sample(DOM::AbstractElement { *element, pseudo_element });
+}
+
// What the style engine reads of an element it applies a reaction to.
static StyleEngineFFI::FfiReactionElement describe_reaction_element(DOM::Document const& document, DOM::Element* element)
{
@@ -844,6 +861,8 @@ static bool update_style_for_element(Layout::BegunRead const& read, DOM::Documen
}
StyleReactionApplication application { read, document };
+ document.style_computer().begin_inheritance_chain_read();
+ ScopeGuard end_inheritance_chain_read = [&] { document.style_computer().end_inheritance_chain_read(); };
auto answer = StyleEngineFFI::style_engine_update_style_for_inheritance_chain(document.style_computer().style_engine().host(), &read, &application, first_element_of_inheritance_chain(abstract_element), static_cast<StyleEngineFFI::FfiStyleUpdateMode>(mode), embedding_document_layout_was_stale);
apply_document_style_invalidation_after_style_change(document, application.invalidation);
return answer_style_read(abstract_element, answer);
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/demand.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/demand.rs
index f5c2acddff3..7383a9043b6 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/demand.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/demand.rs
@@ -101,7 +101,10 @@ impl Live {
/// The elements `node` inherits through, the topmost first, and the index of the first of them
/// whose record is not current. A pass keeps current the record of every element whose
- /// ancestors are displayed, and computes none for the descendants of one that is not.
+ /// ancestors are displayed, and computes none for the descendants of one that is not. No pass
+ /// samples the animations of an element that is not displayed, so the record of one whose
+ /// animations a read sampled is not current either, as it could read the custom properties they
+ /// animate.
pub(super) fn demand_chain(
&self,
state: &RetainedState,
@@ -113,7 +116,16 @@ impl Live {
let mut parent_kept_current = true;
for (index, &element) in chain.iter().enumerate() {
match self.records.get(element) {
- Some(record) if parent_kept_current => parent_kept_current = record.is_displayed(),
+ Some(record)
+ if parent_kept_current
+ && (record.is_displayed()
+ || state
+ .element_custom_property_data
+ .get(&element)
+ .is_none_or(|held| held.sampled_over.is_none())) =>
+ {
+ parent_kept_current = record.is_displayed();
+ }
_ if self.computed_on_demand(element) => parent_kept_current = false,
_ => return (chain, Some(index)),
}
diff --git a/Libraries/LibWeb/Rust/src/css/style/reaction_application.rs b/Libraries/LibWeb/Rust/src/css/style/reaction_application.rs
index b476f34a760..bd9f6f4a4a1 100644
--- a/Libraries/LibWeb/Rust/src/css/style/reaction_application.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/reaction_application.rs
@@ -110,6 +110,10 @@ unsafe extern "C" {
fn web_css_note_style_update_has_reactions(application: *mut HostStyleReactionApplication);
fn web_css_sample_animations_for_style_update(application: *mut HostStyleReactionApplication);
fn web_css_begin_style_update_pass(application: *mut HostStyleReactionApplication, first: bool);
+ fn web_css_sample_undisplayed_animations_for_read(
+ application: *mut HostStyleReactionApplication,
+ element: *mut HostElement,
+ );
fn web_css_style_reaction_element(application: *mut HostStyleReactionApplication, node: u32) -> FfiReactionElement;
fn web_css_install_styles(
application: *mut HostStyleReactionApplication,
@@ -147,12 +151,6 @@ impl Application<'_> {
for &record in &installation.pinned_records {
engine.unpin_layout_style_record(record);
}
- // NB: Installing the record replays the element's animations over it, which ask to restyle what reads the
- // custom properties they sample. The next demand of the walk reads those from here, and what the walk
- // does not reach computes when something reads it.
- // FIXME: A read below an element that is not displayed stays at the first sampled value of a custom property
- // animation that moves later.
- engine.consume_element_style_input(node);
});
true
}
@@ -380,6 +378,16 @@ impl Application<'_> {
{
return FfiTargetedStyleAnswer::HeldStyle;
}
+ // NB: No style update samples the animations of an element that is not displayed. What a read below one sees of
+ // them is sampled here, from the top down, for the demands below to compute from.
+ if mode == FfiStyleUpdateMode::Normal && scan.nearest_display_none.is_some() {
+ let elements: smallvec::SmallVec<[_; 32]> =
+ self.inheritance_chain(first).map(|element| element.element).collect();
+ for element in elements.into_iter().rev() {
+ // SAFETY: The host lends its application for the call, and the element is live while it is read.
+ unsafe { web_css_sample_undisplayed_animations_for_read(self.host_application, element.0) };
+ }
+ }
// Normal mode also re-cascades the target path under its display:none ancestors.
let mut topmost_element_to_recompute = scan.topmost_requiring_style;
if mode == FfiStyleUpdateMode::Normal && topmost_element_to_recompute.is_none() {
diff --git a/Libraries/LibWeb/Rust/src/css/style_compute/ffi_test_stubs.rs b/Libraries/LibWeb/Rust/src/css/style_compute/ffi_test_stubs.rs
index 7d84d96a36b..31b2fdca5b0 100644
--- a/Libraries/LibWeb/Rust/src/css/style_compute/ffi_test_stubs.rs
+++ b/Libraries/LibWeb/Rust/src/css/style_compute/ffi_test_stubs.rs
@@ -70,6 +70,13 @@ extern "C" fn web_css_sample_animations_for_style_update(_application: *mut Host
unreachable!("no unit test applies style reactions");
}
#[unsafe(no_mangle)]
+extern "C" fn web_css_sample_undisplayed_animations_for_read(
+ _application: *mut HostStyleReactionApplication,
+ _element: *mut HostElement,
+) {
+ unreachable!("no unit test applies style reactions");
+}
+#[unsafe(no_mangle)]
extern "C" fn web_css_begin_style_update_pass(_application: *mut HostStyleReactionApplication, _first: bool) {
unreachable!("no unit test applies style reactions");
}
diff --git a/Tests/LibWeb/Text/expected/css/style-engine/animation-sample-read-below-display-none.txt b/Tests/LibWeb/Text/expected/css/style-engine/animation-sample-read-below-display-none.txt
index a2fd61ea091..535c60a6c94 100644
--- a/Tests/LibWeb/Text/expected/css/style-engine/animation-sample-read-below-display-none.txt
+++ b/Tests/LibWeb/Text/expected/css/style-engine/animation-sample-read-below-display-none.txt
@@ -1,35 +1,75 @@
An element hidden once it has been shown:
shown at 10%: 36.000001deg
hidden at 10%: 36.000001deg
-hidden at 20%: 36.000001deg
-hidden at 20%, sibling: 36.000001deg
-hidden at 30%, sibling: 36.000001deg
-hidden at 30%: 36.000001deg
+hidden at 20%: 72.000001deg
+hidden at 20%, sibling: 72.000001deg
+hidden at 30%, sibling: 108.000004deg
+hidden at 30%: 108.000004deg
shown again at 30%: 108.000004deg
shown at 40%: 144.000002deg
An element never shown:
at 10%: 36.000001deg
at 10%, own property: 36.000001deg
-at 20%: 36.000001deg
-at 20%, middle: 36.000001deg
-at 20%, sibling: 36.000001deg
-at 20%, own property: 36.000001deg
-cancelled: 36.000001deg
-cancelled, own property: 36.000001deg
+at 20%: 72.000001deg
+at 20%, middle: 72.000001deg
+at 20%, sibling: 72.000001deg
+at 20%, own property: 72.000001deg
+cancelled: 0deg
+cancelled, own property: 0deg
shown: 0deg
An animated inherited color:
at 10%: rgb(10, 10, 10)
at 20%: rgb(20, 20, 20)
cancelled: rgb(0, 0, 0)
An animated element that is itself not displayed:
-at 10%: 0deg
-at 20%: 0deg
+at 10%: 36.000001deg
+at 20%: 72.000001deg
cancelled: 0deg
An animated element above another that is not displayed:
at 10%: 36.000001deg
-at 20%: 36.000001deg
-cancelled: 36.000001deg
+at 20%: 72.000001deg
+cancelled: 0deg
A style query on a custom property that does not inherit:
at 10%: rgb(0, 0, 0)
-at 60%: rgb(0, 0, 0)
+at 60%: rgb(0, 128, 0)
at 10% again: rgb(0, 0, 0)
+A color reading an animation between two elements not displayed:
+at 10%: rgb(10, 10, 10)
+at 10%, again: rgb(10, 10, 10)
+at 20%: rgb(20, 20, 20)
+at 20%, again: rgb(20, 20, 20)
+at 30%: rgb(30, 30, 30)
+at 30%, again: rgb(30, 30, 30)
+cancelled: rgb(0, 0, 0)
+A color reading an animation of a hidden element above another:
+at 10%: rgb(10, 10, 10)
+at 10%, again: rgb(10, 10, 10)
+at 20%: rgb(20, 20, 20)
+at 20%, again: rgb(20, 20, 20)
+at 30%: rgb(30, 30, 30)
+at 30%, again: rgb(30, 30, 30)
+cancelled: rgb(0, 0, 0)
+A color below a hidden animated element, read further down:
+at 10%: rgb(10, 10, 10)
+at 10%, again: rgb(10, 10, 10)
+at 20%: rgb(20, 20, 20)
+at 20%, again: rgb(20, 20, 20)
+at 30%: rgb(30, 30, 30)
+at 30%, again: rgb(30, 30, 30)
+cancelled: rgb(0, 0, 0)
+An animated color between two elements not displayed:
+at 10%: rgb(10, 10, 10)
+at 10%, again: rgb(10, 10, 10)
+at 20%: rgb(20, 20, 20)
+at 20%, again: rgb(20, 20, 20)
+at 30%: rgb(30, 30, 30)
+at 30%, again: rgb(30, 30, 30)
+cancelled: rgb(0, 0, 0)
+An animated color of a hidden element above another:
+at 10%: rgb(10, 10, 10)
+at 10%, again: rgb(10, 10, 10)
+at 20%: rgb(20, 20, 20)
+at 20%, again: rgb(20, 20, 20)
+at 30%: rgb(30, 30, 30)
+at 30%, again: rgb(30, 30, 30)
+cancelled: rgb(0, 0, 0)
diff --git a/Tests/LibWeb/Text/input/css/style-engine/animation-sample-read-below-display-none.html b/Tests/LibWeb/Text/input/css/style-engine/animation-sample-read-below-display-none.html
index df6557129d1..967dca1c48c 100644
--- a/Tests/LibWeb/Text/input/css/style-engine/animation-sample-read-below-display-none.html
+++ b/Tests/LibWeb/Text/input/css/style-engine/animation-sample-read-below-display-none.html
@@ -10,9 +10,17 @@
inherits: false;
initial-value: before;
}
+ @property --tone {
+ syntax: "<color>";
+ inherits: true;
+ initial-value: rgb(0, 0, 0);
+ }
.none {
display: none;
}
+ .toned {
+ color: var(--tone);
+ }
.rotates {
rotate: var(--spin);
}
@@ -42,6 +50,21 @@
<div class="none">
<div id="animated-6"><div id="query-reader-6" class="query-reader"><div id="below-6"></div></div></div>
</div>
+<div class="none">
+ <div id="animated-7" class="toned"><div class="none"><div id="reader-7"></div></div></div>
+</div>
+<div id="animated-8" class="none toned">
+ <div class="none"><div id="reader-8"></div></div>
+</div>
+<div id="animated-9" class="none">
+ <div class="toned"><div class="none"><div><div id="reader-9"></div></div></div></div>
+</div>
+<div class="none">
+ <div id="animated-10"><div class="none"><div id="reader-10"></div></div></div>
+</div>
+<div id="animated-11" class="none">
+ <div class="none"><div id="reader-11"></div></div>
+</div>
<script src="../../include.js"></script>
<script>
function spinOf(element) {
@@ -123,5 +146,22 @@
println(`at 60%: ${getComputedStyle(element("below-6")).color}`);
sixth.currentTime = 10000;
println(`at 10% again: ${getComputedStyle(element("below-6")).color}`);
+
+ function readRepeatedly(label, animatedId, readerId, property) {
+ println(label);
+ const animation = animate(element(animatedId), property, "rgb(0, 0, 0)", "rgb(100, 100, 100)");
+ for (const time of [10000, 20000, 30000]) {
+ animation.currentTime = time;
+ println(`at ${time / 1000}%: ${getComputedStyle(element(readerId)).color}`);
+ println(`at ${time / 1000}%, again: ${getComputedStyle(element(readerId)).color}`);
+ }
+ animation.cancel();
+ println(`cancelled: ${getComputedStyle(element(readerId)).color}`);
+ }
+ readRepeatedly("A color reading an animation between two elements not displayed:", "animated-7", "reader-7", "--tone");
+ readRepeatedly("A color reading an animation of a hidden element above another:", "animated-8", "reader-8", "--tone");
+ readRepeatedly("A color below a hidden animated element, read further down:", "animated-9", "reader-9", "--tone");
+ readRepeatedly("An animated color between two elements not displayed:", "animated-10", "reader-10", "color");
+ readRepeatedly("An animated color of a hidden element above another:", "animated-11", "reader-11", "color");
});
</script>