Skip to content

Truly independent web browser

LibWeb: Keep ancestor intrinsic sizes when an abspos box changes

Aliaksandr Kalenik4b67eca777a9
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
index b80b121f3c9..a5618fca1aa 100644
--- a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
+++ b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
@@ -5674,12 +5674,16 @@ impl LayoutNodeArena {
intrinsic_sizes: AncestorIntrinsicSizes,
) {
self.invalidate_one(node, invalidation);
+ let mut ancestor_intrinsic_sizes = intrinsic_sizes;
if intrinsic_sizes == AncestorIntrinsicSizes::Reset
&& super::node_facts::kind_is_box(self.data(node).kind.get())
{
self.reset_cached_intrinsic_sizes(node);
+ if self.box_contributes_to_no_ancestor_intrinsic_size(node) {
+ ancestor_intrinsic_sizes = AncestorIntrinsicSizes::Kept;
+ }
}
- self.invalidate_ancestors_for_change_below(node, invalidation, intrinsic_sizes);
+ self.invalidate_ancestors_for_change_below(node, invalidation, ancestor_intrinsic_sizes);
}
pub(super) fn invalidate_ancestors_for_change_below(
@@ -5716,14 +5720,16 @@ impl LayoutNodeArena {
}
/// Whether a change inside the box leaves the intrinsic sizes of its ancestors as they are.
- /// Absolutely positioned elements don't contribute to ancestor intrinsic sizes. SVG root
- /// elements have intrinsic sizes determined solely by their own attributes (width, height,
- /// viewBox), not by their children.
+ /// SVG root elements have intrinsic sizes determined solely by their own attributes (width,
+ /// height, viewBox), not by their children.
fn content_does_not_size_box(&self, node: NodeSlotId) -> bool {
- self.data(node).kind.get() == NodeKind::SVGSVGBox
- || self
- .node_style_if_live(node)
- .is_some_and(|style| style.is_absolutely_positioned())
+ self.data(node).kind.get() == NodeKind::SVGSVGBox || self.box_contributes_to_no_ancestor_intrinsic_size(node)
+ }
+
+ /// Absolutely positioned elements don't contribute to ancestor intrinsic sizes.
+ pub(super) fn box_contributes_to_no_ancestor_intrinsic_size(&self, node: NodeSlotId) -> bool {
+ self.node_style_if_live(node)
+ .is_some_and(|style| style.is_absolutely_positioned())
}
pub(super) fn invalidate_one(&mut self, node: NodeSlotId, invalidation: AncestorInvalidation) {
diff --git a/Libraries/LibWeb/Rust/src/layout/partial_relayout.rs b/Libraries/LibWeb/Rust/src/layout/partial_relayout.rs
index fe05a5befa2..d69b6015130 100644
--- a/Libraries/LibWeb/Rust/src/layout/partial_relayout.rs
+++ b/Libraries/LibWeb/Rust/src/layout/partial_relayout.rs
@@ -706,6 +706,12 @@ impl LayoutNodeArena {
data.parent.get(),
)
};
+ let ancestor_intrinsic_sizes_after_change =
+ if node_is_box && self.box_contributes_to_no_ancestor_intrinsic_size(node) {
+ AncestorIntrinsicSizes::Kept
+ } else {
+ AncestorIntrinsicSizes::Reset
+ };
// A dirty node normally implies dirty ancestors, but the walk that marked a partial
// relayout boundary stopped there and left its ancestors clean, so a through-ancestors
@@ -723,7 +729,7 @@ impl LayoutNodeArena {
self.invalidate_ancestors_for_change_below(
node,
AncestorInvalidation::ContentChange,
- AncestorIntrinsicSizes::Reset,
+ ancestor_intrinsic_sizes_after_change,
);
return;
}
@@ -756,7 +762,7 @@ impl LayoutNodeArena {
let mut marks_ancestors_for_layout_update = propagate_through_ancestors;
let mut ancestor_intrinsic_sizes = if propagate_through_ancestors {
- AncestorIntrinsicSizes::Reset
+ ancestor_intrinsic_sizes_after_change
} else {
AncestorIntrinsicSizes::Kept
};
diff --git a/Tests/LibWeb/Text/expected/abspos-own-change-keeps-ancestor-intrinsic-sizes.txt b/Tests/LibWeb/Text/expected/abspos-own-change-keeps-ancestor-intrinsic-sizes.txt
new file mode 100644
index 00000000000..94b40bedcb6
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/abspos-own-change-keeps-ancestor-intrinsic-sizes.txt
@@ -0,0 +1,8 @@
+confined: ancestor measurements missed: 0
+confined: geometry matches a fresh layout: true
+escaping: ancestor measurements missed: 0
+escaping: geometry matches a fresh layout: true
+switching: leaf static, container width 230
+switching: geometry matches a fresh layout: true
+switching: leaf absolute, container width 30
+switching: geometry matches a fresh layout: true
diff --git a/Tests/LibWeb/Text/input/abspos-own-change-keeps-ancestor-intrinsic-sizes.html b/Tests/LibWeb/Text/input/abspos-own-change-keeps-ancestor-intrinsic-sizes.html
new file mode 100644
index 00000000000..6d9b528bebc
--- /dev/null
+++ b/Tests/LibWeb/Text/input/abspos-own-change-keeps-ancestor-intrinsic-sizes.html
@@ -0,0 +1,54 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<style>
+ body { margin: 0; }
+ .shrink { position: absolute; display: flex; }
+ .item { position: relative; display: flex; width: 50px; height: 20px; }
+ .leaf { position: absolute; bottom: 0; width: 20px; height: 10px; }
+ .sib { width: 30px; height: 10px; }
+ .auto-width { width: auto; }
+ .wide { width: 200px; }
+</style>
+<div class="shrink" id="confined"><div class="item" id="confined-item"><div class="leaf" id="confined-leaf"></div></div><div class="sib"></div></div>
+<div class="shrink" id="escaping" style="top: 100px"><div class="item" id="escaping-item"><div class="leaf" id="escaping-leaf"><div style="position: fixed"></div></div></div><div class="sib"></div></div>
+<div class="shrink" id="switching" style="top: 200px"><div class="item auto-width"><div class="leaf wide" id="switching-leaf"></div></div><div class="sib"></div></div>
+<script>
+ // An absolutely positioned box contributes nothing to the intrinsic sizes of its ancestors, so a change of its own
+ // style measures none of them again.
+ test(() => {
+ const geometry = root => {
+ const rootRect = root.getBoundingClientRect();
+ return [root, ...root.querySelectorAll("div")].map(element => {
+ const rect = element.getBoundingClientRect();
+ return `${rect.left - rootRect.left},${rect.top - rootRect.top},${rect.width},${rect.height}`;
+ }).join(" ");
+ };
+ const matchesFreshLayout = container => {
+ const fresh = container.cloneNode(true);
+ fresh.id = "";
+ document.body.appendChild(fresh);
+ const matches = geometry(container) === geometry(fresh);
+ fresh.remove();
+ return matches;
+ };
+
+ for (const name of ["confined", "escaping"]) {
+ document.body.offsetHeight;
+ internals.beginLayoutTrace();
+ document.getElementById(`${name}-leaf`).style.width = "40px";
+ document.body.offsetHeight;
+ const pattern = new RegExp(`#${name}(-item)?\\.\\S* MEASURE .*\\(cache=miss\\)`);
+ const misses = internals.takeLayoutTrace().split("\n").filter(line => pattern.test(line));
+ println(`${name}: ancestor measurements missed: ${misses.length}`);
+ println(`${name}: geometry matches a fresh layout: ${matchesFreshLayout(document.getElementById(name))}`);
+ }
+
+ const switching = document.getElementById("switching");
+ const switchingLeaf = document.getElementById("switching-leaf");
+ for (const position of ["static", "absolute"]) {
+ switchingLeaf.style.position = position;
+ println(`switching: leaf ${position}, container width ${switching.getBoundingClientRect().width}`);
+ println(`switching: geometry matches a fresh layout: ${matchesFreshLayout(switching)}`);
+ }
+ });
+</script>