Skip to content

Truly independent web browser

LibWeb: Wait for a size container’s box before restyling under it

sideshowbarker623f89b834c3
diff --git a/Libraries/LibWeb/Rust/src/css/style/size_container_invalidation.rs b/Libraries/LibWeb/Rust/src/css/style/size_container_invalidation.rs
index 426883161b7..aaa39427220 100644
--- a/Libraries/LibWeb/Rust/src/css/style/size_container_invalidation.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/size_container_invalidation.rs
@@ -27,6 +27,9 @@ pub(super) struct SizeContainerQueryFacts {
/// Containers a style computation asked about before they had a committed box. The layout
/// that gives them one is where their dependents move.
needing_evaluation_after_layout: HashSet<StyleNodeID>,
+ /// Containers asked about without a box that a full layout left without one, as one under an element that is not
+ /// rendered stays. Their dependents read nothing new until a commit gives them a box.
+ awaiting_box: HashSet<StyleNodeID>,
/// Queried containers whose font moved since their dependents were last recorded, each with the record it held
/// before the move. An animated container passes through its base record within every update, on its way to the
/// record with its animated values, so only a font that differs from the one before it counts.
@@ -82,6 +85,7 @@ impl SizeContainerQueryFacts {
self.queried_containers.remove(&node);
self.dependents.remove(&node);
self.needing_evaluation_after_layout.remove(&node);
+ self.awaiting_box.remove(&node);
self.font_moves.remove(&node);
self.availability_changed.remove(&node);
self.available.remove(&node);
@@ -169,7 +173,18 @@ impl StyleEngine {
&mut self,
rows: &[crate::layout::style_snapshot::CommittedGeometry],
) {
- for container in self.retained.take_layout_style_snapshot_rows(rows) {
+ let mut resized = self.retained.take_layout_style_snapshot_rows(rows);
+ // A container its dependents read without a box answers them once a commit gives it one.
+ let awaiting_box = &mut self.retained.size_container_queries.awaiting_box;
+ if !awaiting_box.is_empty() {
+ let mut recorded: HashSet<StyleNodeID> = resized.iter().copied().collect();
+ for geometry in rows {
+ if geometry.has_committed_box && awaiting_box.remove(&geometry.node) && recorded.insert(geometry.node) {
+ resized.push(geometry.node);
+ }
+ }
+ }
+ for container in resized {
self.record_size_container_query_dependents(container);
}
}
@@ -182,9 +197,21 @@ impl StyleEngine {
}
/// A layout gave the containers that had no box when they were asked about one; their
- /// dependents are computed again against it.
+ /// dependents are computed again against it. A container the layout left without a box has
+ /// nothing new to answer, so its dependents wait for the commit that gives it one; computing
+ /// them again against no box would only ask for this evaluation again, after every layout.
pub fn evaluate_size_containers_needing_evaluation_after_layout(&mut self) {
let mut containers = std::mem::take(&mut self.retained.size_container_queries.needing_evaluation_after_layout);
+ containers.retain(|&container| {
+ let has_box = self
+ .retained
+ .layout_style_snapshot(container)
+ .is_some_and(|row| row.has_committed_box);
+ if !has_box {
+ self.retained.size_container_queries.awaiting_box.insert(container);
+ }
+ has_box
+ });
let availability_changed = std::mem::take(&mut self.retained.size_container_queries.availability_changed);
containers.extend(availability_changed.iter().copied());
containers.extend(std::mem::take(&mut self.retained.size_container_queries.font_moves).into_keys());
diff --git a/Tests/LibWeb/Crash/CSS/size-container-with-animation-hidden-by-media-query.html b/Tests/LibWeb/Crash/CSS/size-container-with-animation-hidden-by-media-query.html
new file mode 100644
index 00000000000..bb1b69043e7
--- /dev/null
+++ b/Tests/LibWeb/Crash/CSS/size-container-with-animation-hidden-by-media-query.html
@@ -0,0 +1,30 @@
+<!DOCTYPE html>
+<html class="test-wait">
+<iframe id="frame" style="width: 1200px; height: 300px" srcdoc='<!DOCTYPE html>
+<style>
+ @font-face { font-family: Test; font-display: swap; src: url("does-not-exist.woff2") format("woff2"); }
+ @property --w { syntax: "*"; inherits: false; }
+ :root { --x: 1; }
+ html { font-family: Test, sans-serif; }
+ .panel { display: none; }
+ @media (min-width: 1024px) { .panel { display: block; } }
+ .container { container-type: inline-size; }
+ .pad { padding-block: 3.5cqw; }
+ .caret { animation: blink 2s infinite; }
+ .label { --w: 500; }
+ svg { height: 2.2cqw; }
+</style>
+<div class="panel"><div class="container"><div class="pad">
+ <p>Text<span class="caret"></span></p>
+ <div><span class="label"><svg></svg>Label</span></div>
+</div></div></div>'></iframe>
+<script>
+ frame.onload = () => setTimeout(() => {
+ frame.style.width = "390px";
+ setTimeout(() => {
+ getComputedStyle(frame.contentDocument.querySelector(".panel")).display;
+ document.documentElement.classList.remove("test-wait");
+ }, 500);
+ }, 500);
+</script>
+</html>
diff --git a/Tests/LibWeb/Ref/expected/size-container-revealed-after-hidden-ref.html b/Tests/LibWeb/Ref/expected/size-container-revealed-after-hidden-ref.html
new file mode 100644
index 00000000000..7101eaad6ce
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/size-container-revealed-after-hidden-ref.html
@@ -0,0 +1,7 @@
+<!DOCTYPE html>
+<style>
+ body { margin: 0; }
+ div { width: 400px; height: 40px; background: green; }
+</style>
+<div></div>
+<div></div>
diff --git a/Tests/LibWeb/Ref/input/size-container-revealed-after-hidden.html b/Tests/LibWeb/Ref/input/size-container-revealed-after-hidden.html
new file mode 100644
index 00000000000..7536dde0b35
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/size-container-revealed-after-hidden.html
@@ -0,0 +1,31 @@
+<!DOCTYPE html>
+<html class="reftest-wait">
+<link rel="match" href="../expected/size-container-revealed-after-hidden-ref.html">
+<style>
+ body { margin: 0; }
+ .c { container-type: inline-size; width: 400px; }
+ .h { height: 10cqw; background: green; }
+ .hidden { display: none; }
+ #skipped { content-visibility: hidden; }
+ #skipped.shown { content-visibility: visible; }
+</style>
+<div id="skipped"><div class="c"><div id="reader" class="h"></div></div></div>
+<div id="hidden" class="hidden"><div class="c"><div class="h"></div></div></div>
+<script>
+ function afterFrames(callback) {
+ requestAnimationFrame(() => requestAnimationFrame(callback));
+ }
+
+ // The reader under the skipped contents computes against a container without a box, and showing the contents
+ // gives the container one without restyling the reader. Nothing reads the style or the layout after that before
+ // the rendering update paints it.
+ afterFrames(() => {
+ getComputedStyle(reader).height;
+ afterFrames(() => {
+ skipped.classList.add("shown");
+ hidden.classList.remove("hidden");
+ afterFrames(() => document.documentElement.classList.remove("reftest-wait"));
+ });
+ });
+</script>
+</html>