Skip to content

Truly independent web browser

LibWeb: Resolve container units past a size container without a box

sideshowbarker850ae2708ead
diff --git a/Libraries/LibWeb/CSS/Length.cpp b/Libraries/LibWeb/CSS/Length.cpp
index 80729983a1a..8c87a5e6a14 100644
--- a/Libraries/LibWeb/CSS/Length.cpp
+++ b/Libraries/LibWeb/CSS/Length.cpp
@@ -155,32 +155,38 @@ double Length::container_relative_length_to_px_without_rounding(ResolutionContex
auto& subject = *context.subject_element;
const_cast<DOM::Element&>(subject).set_style_depends_on_size_container_query();
- auto query_container = get_or_compute_query_container_for_axis(context, physical_axis);
- // A container unit asks about the container's box exactly as a size query does, so the
- // container has to know that a resize of it is a change for something under it.
- if (query_container)
- const_cast<DOM::Element&>(*query_container).set_is_size_query_container();
- if (!query_container) {
- context.record_viewport_relative_length_resolution();
- auto viewport_length = physical_axis == ContainerRelativeAxis::Width ? context.viewport_rect.width() : context.viewport_rect.height();
- return viewport_length.to_double();
- }
-
// The container's box is read from the committed layout, without making its layout node.
- auto const* arena = query_container->document().layout_node_arena_if_created();
- Layout::ForcedReadScope read { query_container->document() };
- CSSPixelSize container_size;
- if (!arena || !Layout::RustFFI::layout_row_bound_committed_content_size(arena->host(), read, query_container->style_node_id().value(), &container_size)) {
- // A running partial relayout pass reports layout as up to date, but a container
- // with no paintable yet still needs the post-layout evaluation, which routes the
- // follow-up pass to the full layout path that resolves the container's size.
- if (!subject.document().layout_is_up_to_date() || subject.document().is_running_update_layout())
- const_cast<DOM::Document&>(subject.document()).set_needs_container_query_evaluation_after_layout(*query_container);
- return 0.0;
+ auto const* arena = subject.document().layout_node_arena_if_created();
+ GC::Ptr<DOM::Element const> boxless_container;
+ Optional<double> container_length;
+ for (auto query_container = get_or_compute_query_container_for_axis(context, physical_axis); query_container;
+ query_container = nearest_query_container_for_axis(*query_container, physical_axis, false)) {
+ // A container unit asks about the container's box exactly as a size query does, so the
+ // container has to know that a resize of it is a change for something under it.
+ const_cast<DOM::Element&>(*query_container).set_is_size_query_container();
+ Layout::ForcedReadScope read { subject.document() };
+ CSSPixelSize container_size;
+ if (arena && Layout::RustFFI::layout_row_bound_committed_content_size(arena->host(), read, query_container->style_node_id().value(), &container_size)) {
+ container_length = (physical_axis == ContainerRelativeAxis::Width ? container_size.width() : container_size.height()).to_double();
+ break;
+ }
+ // A container without a box, as one under an element that is not rendered has, is not eligible: Its size
+ // queries are unknown, so the unit resolves against the next eligible container out.
+ boxless_container = query_container;
}
- auto container_length = physical_axis == ContainerRelativeAxis::Width ? container_size.width() : container_size.height();
- return container_length.to_double();
+ // A running partial relayout pass reports layout as up to date, but a container with no
+ // paintable yet still needs the post-layout evaluation, which routes the follow-up pass to
+ // the full layout path that resolves the container's size. A container below the outermost
+ // one without a box can only get one after it does.
+ if (boxless_container && (!subject.document().layout_is_up_to_date() || subject.document().is_running_update_layout()))
+ const_cast<DOM::Document&>(subject.document()).set_needs_container_query_evaluation_after_layout(*boxless_container);
+ if (container_length.has_value())
+ return *container_length;
+
+ context.record_viewport_relative_length_resolution();
+ auto viewport_length = physical_axis == ContainerRelativeAxis::Width ? context.viewport_rect.width() : context.viewport_rect.height();
+ return viewport_length.to_double();
};
auto resolve_physical_axis_length = [&](ContainerRelativeAxis physical_axis) {
diff --git a/Libraries/LibWeb/Rust/src/css/style/container_queries.rs b/Libraries/LibWeb/Rust/src/css/style/container_queries.rs
index a18928593dd..7e65f486d76 100644
--- a/Libraries/LibWeb/Rust/src/css/style/container_queries.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/container_queries.rs
@@ -53,14 +53,15 @@ impl ContainerVerdict {
/// One physical axis' basis for a subject's container-relative lengths, as
/// `Length::container_relative_length_to_px_without_rounding` finds it: the content size of the
-/// nearest flat-tree ancestor that accepts size queries on that axis as of the last layout commit,
-/// zero while that container has no box, and the viewport's size when there is no such container.
+/// nearest flat-tree ancestor that accepts size queries on that axis and has a box as of the last
+/// layout commit, and the viewport's size when there is no such container.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct ContainerUnitBasis {
basis: f64,
/// The container the basis is read from, or none for the viewport.
container: Option<StyleNodeID>,
- container_has_no_box: bool,
+ /// The outermost container below that one that accepts size queries on the axis but had no box.
+ boxless_container: Option<StyleNodeID>,
}
impl ContainerUnitBasis {
@@ -69,10 +70,10 @@ impl ContainerUnitBasis {
self.container
}
- /// Whether the container has no box as of the last layout commit, so its queries are evaluated again after the
- /// next layout gives it one.
- pub(crate) fn container_has_no_box(&self) -> bool {
- self.container_has_no_box
+ /// The outermost container the basis skipped for having no box as of the last layout commit, which the units are
+ /// evaluated against again once a layout gives it one. A container below it can only get a box after it does.
+ pub(crate) fn boxless_container(&self) -> Option<StyleNodeID> {
+ self.boxless_container
}
}
@@ -283,6 +284,7 @@ impl RetainedState {
} else {
self.projection.flat_tree_parent(subject)
};
+ let mut boxless_container = None;
while let Some(node) = ancestor {
ancestor = self.projection.flat_tree_parent(node);
let Some(inputs) = records.query_row(node) else {
@@ -301,19 +303,21 @@ impl RetainedState {
continue;
}
let snapshot = self.layout_style_snapshot(node).unwrap_or_default();
+ // A container without a box, as one under an element that is not rendered has, is not eligible: Its size
+ // queries are unknown, so the units resolve against the next eligible container out.
+ if !snapshot.has_committed_box {
+ boxless_container = Some(node);
+ continue;
+ }
let raw = if axis_is_horizontal {
snapshot.content_width_raw
} else {
snapshot.content_height_raw
};
return ContainerUnitBasis {
- basis: if snapshot.has_committed_box {
- crate::css::css_pixels::CssPixels::from_raw(raw).to_double()
- } else {
- 0.0
- },
+ basis: crate::css::css_pixels::CssPixels::from_raw(raw).to_double(),
container: Some(node),
- container_has_no_box: !snapshot.has_committed_box,
+ boxless_container,
};
}
let document = &self.document_style_computation_inputs;
@@ -324,7 +328,7 @@ impl RetainedState {
document.viewport_height
},
container: None,
- container_has_no_box: false,
+ boxless_container,
}
}
@@ -372,20 +376,22 @@ impl RetainedState {
}
let basis = self.container_unit_basis(self, node, axis_is_horizontal, subject_is_pseudo_element);
match basis.container {
- Some(container) => {
- verdict
- .effects
- .push((container, FfiContainerEffectKind::SizeContainerUsage));
- if basis.container_has_no_box {
- verdict
- .effects
- .push((container, FfiContainerEffectKind::UnitsNeedEvaluationAfterLayout));
- }
- }
+ Some(container) => verdict
+ .effects
+ .push((container, FfiContainerEffectKind::SizeContainerUsage)),
None => verdict
.effects
.push((node, FfiContainerEffectKind::SubjectViewportDependency)),
}
+ if let Some(boxless_container) = basis.boxless_container {
+ verdict
+ .effects
+ .push((boxless_container, FfiContainerEffectKind::SizeContainerUsage));
+ verdict.effects.push((
+ boxless_container,
+ FfiContainerEffectKind::UnitsNeedEvaluationAfterLayout,
+ ));
+ }
}
self.note_container_effects_for_host(node, &std::sync::Arc::new(verdict));
}
@@ -607,18 +613,21 @@ impl RetainedState {
.push((candidate, FfiContainerEffectKind::SizeContainerUsage));
}
for basis in bases.0 {
- let Some(container) = basis.container else {
- continue;
- };
- verdict
- .effects
- .push((container, FfiContainerEffectKind::SizeContainerUsage));
- // A container-relative length against a container without a box resolved to
- // zero, as it does for the host, which evaluates it again after layout.
- if basis.container_has_no_box {
+ if let Some(container) = basis.container {
+ verdict
+ .effects
+ .push((container, FfiContainerEffectKind::SizeContainerUsage));
+ }
+ // A container-relative length skipped a container without a box, as it does for the host, which
+ // evaluates it again after layout.
+ if let Some(boxless_container) = basis.boxless_container {
verdict
.effects
- .push((container, FfiContainerEffectKind::UnitsNeedEvaluationAfterLayout));
+ .push((boxless_container, FfiContainerEffectKind::SizeContainerUsage));
+ verdict.effects.push((
+ boxless_container,
+ FfiContainerEffectKind::UnitsNeedEvaluationAfterLayout,
+ ));
}
}
if requirements & CONTAINER_QUERY_REQUIRES_STYLE != 0 {
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 aaa39427220..c73db8ae699 100644
--- a/Libraries/LibWeb/Rust/src/css/style/size_container_invalidation.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/size_container_invalidation.rs
@@ -189,6 +189,16 @@ impl StyleEngine {
}
}
+ /// Take the box of an element whose new style makes it a size container, which no commit has gathered a row for.
+ /// The container-relative units below it that were computed while it had no box resolved past it, to a container
+ /// further out or the viewport, so they are computed again against its box before any layout runs again. Their
+ /// records can come out the same as before the element became a container, so nothing else would ask for that.
+ pub(crate) fn apply_new_size_container_geometry(&mut self, row: crate::layout::style_snapshot::CommittedGeometry) {
+ let container = row.node;
+ self.apply_layout_style_snapshot_commit(&[row]);
+ self.record_size_container_query_dependents_after_input_change(container, true);
+ }
+
/// The elements the dependent walks have visited, optionally starting the count again.
#[cfg(test)]
pub fn size_query_container_scan_visits(&mut self, reset: bool) -> u64 {
diff --git a/Libraries/LibWeb/Rust/src/layout/run_state.rs b/Libraries/LibWeb/Rust/src/layout/run_state.rs
index 9e57cb2300a..b9ac7e3f9bc 100644
--- a/Libraries/LibWeb/Rust/src/layout/run_state.rs
+++ b/Libraries/LibWeb/Rust/src/layout/run_state.rs
@@ -102,16 +102,19 @@ impl HostFindings {
FfiCommitMessageKind::StyleDependsOnSizeContainerQuery,
));
for basis in bases.width_and_height() {
- let Some(container) = basis.container() else {
- continue;
- };
- self.note_message(FfiCommitMessage::new(
- container.raw(),
- FfiCommitMessageKind::SizeQueryContainerResolvedAgainst,
- ));
- if basis.container_has_no_box() {
+ if let Some(container) = basis.container() {
self.note_message(FfiCommitMessage::new(
container.raw(),
+ FfiCommitMessageKind::SizeQueryContainerResolvedAgainst,
+ ));
+ }
+ if let Some(boxless_container) = basis.boxless_container() {
+ self.note_message(FfiCommitMessage::new(
+ boxless_container.raw(),
+ FfiCommitMessageKind::SizeQueryContainerResolvedAgainst,
+ ));
+ self.note_message(FfiCommitMessage::new(
+ boxless_container.raw(),
FfiCommitMessageKind::ContainerQueryEvaluationNeededAfterLayout,
));
}
diff --git a/Libraries/LibWeb/Rust/src/layout/style_snapshot.rs b/Libraries/LibWeb/Rust/src/layout/style_snapshot.rs
index de347920380..c0489325921 100644
--- a/Libraries/LibWeb/Rust/src/layout/style_snapshot.rs
+++ b/Libraries/LibWeb/Rust/src/layout/style_snapshot.rs
@@ -128,7 +128,7 @@ impl LayoutNodeArena {
}
let row = self.committed_geometry(node, style_node);
if row.has_committed_box {
- self.with_style_engine(|engine| engine.apply_layout_style_snapshot_commit(&[row]));
+ self.with_style_engine(|engine| engine.apply_new_size_container_geometry(row));
}
}
diff --git a/Tests/LibWeb/Text/expected/css/size-container-without-a-box.txt b/Tests/LibWeb/Text/expected/css/size-container-without-a-box.txt
new file mode 100644
index 00000000000..e59861a0cc2
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/size-container-without-a-box.txt
@@ -0,0 +1,14 @@
+Ancestor hidden from the start: pad=80px svg=40px q=rgb(255, 0, 0)
+Shown: pad=40px svg=20px q=rgb(0, 128, 0)
+Hidden: pad=80px svg=40px q=rgb(255, 0, 0)
+Shown again: pad=40px svg=20px q=rgb(0, 128, 0)
+Container itself hidden: pad=80px svg=40px q=rgb(255, 0, 0)
+Ancestor shown: pad=40px svg=20px q=rgb(0, 128, 0)
+Container shown: pad=40px svg=20px q=rgb(0, 128, 0)
+Hidden across frames: pad=80px svg=40px q=rgb(255, 0, 0)
+Shown across frames: pad=40px svg=20px q=rgb(0, 128, 0)
+Hidden between outer and inner container: pad=60px q=rgb(255, 0, 0)
+Inner container itself hidden: pad=60px q=rgb(255, 0, 0)
+Hidden above both containers: pad=80px q=rgb(255, 0, 0)
+Hidden below the only container: pad=60px q=rgb(0, 0, 0)
+Hidden between, shown: pad=40px q=rgb(0, 128, 0)
diff --git a/Tests/LibWeb/Text/input/css/size-container-without-a-box.html b/Tests/LibWeb/Text/input/css/size-container-without-a-box.html
new file mode 100644
index 00000000000..963632f003f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/size-container-without-a-box.html
@@ -0,0 +1,64 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ .c { container-type: inline-size; width: 400px; }
+ .pad { padding-top: 10cqw; }
+ svg { height: 5cqw; }
+ .q { color: red; }
+ @container (min-width: 300px) { .q { color: green; } }
+ .hidden { display: none; }
+ .outer { container-type: inline-size; width: 600px; }
+</style>
+<div id="a" class="hidden"><div class="c"><div class="pad"><svg></svg><span class="q">q</span></div></div></div>
+<div id="b"><div class="c"><div class="pad"><svg></svg><span class="q">q</span></div></div></div>
+<div id="c" class="c hidden"><div class="pad"><svg></svg><span class="q">q</span></div></div>
+<div class="outer"><div class="hidden"><div class="c"><div id="d" class="pad q"></div></div></div></div>
+<div class="outer"><div class="c hidden"><div id="e" class="pad q"></div></div></div>
+<div class="hidden"><div class="outer"><div class="c"><div id="f" class="pad q"></div></div></div></div>
+<div class="outer"><div class="hidden"><div id="g" class="pad"></div></div></div>
+<script>
+ function read(id) {
+ const root = document.getElementById(id);
+ const pad = root.querySelector(".pad");
+ const svg = root.querySelector("svg");
+ const q = root.querySelector(".q");
+ return `pad=${getComputedStyle(pad).paddingTop} svg=${getComputedStyle(svg).height} q=${getComputedStyle(q).color}`;
+ }
+
+ function readNested(id) {
+ const style = getComputedStyle(document.getElementById(id));
+ return `pad=${style.paddingTop} q=${style.color}`;
+ }
+
+ function frames() {
+ return new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
+ }
+
+ asyncTest(async done => {
+ await frames();
+ println(`Ancestor hidden from the start: ${read("a")}`);
+ println(`Shown: ${read("b")}`);
+ b.classList.add("hidden");
+ println(`Hidden: ${read("b")}`);
+ b.classList.remove("hidden");
+ println(`Shown again: ${read("b")}`);
+ println(`Container itself hidden: ${read("c")}`);
+ a.classList.remove("hidden");
+ println(`Ancestor shown: ${read("a")}`);
+ c.classList.remove("hidden");
+ println(`Container shown: ${read("c")}`);
+ b.classList.add("hidden");
+ await frames();
+ println(`Hidden across frames: ${read("b")}`);
+ b.classList.remove("hidden");
+ await frames();
+ println(`Shown across frames: ${read("b")}`);
+ println(`Hidden between outer and inner container: ${readNested("d")}`);
+ println(`Inner container itself hidden: ${readNested("e")}`);
+ println(`Hidden above both containers: ${readNested("f")}`);
+ println(`Hidden below the only container: ${readNested("g")}`);
+ document.querySelector("#d").parentElement.parentElement.classList.remove("hidden");
+ println(`Hidden between, shown: ${readNested("d")}`);
+ done();
+ });
+</script>