Skip to content

Truly independent web browser

LibWeb: Stop following a size container that keeps moving

Andreas Klingfb59d9637ba0
diff --git a/Libraries/LibWeb/DOM/CommitMessages.cpp b/Libraries/LibWeb/DOM/CommitMessages.cpp
index f52b851218b..ae38eec524b 100644
--- a/Libraries/LibWeb/DOM/CommitMessages.cpp
+++ b/Libraries/LibWeb/DOM/CommitMessages.cpp
@@ -102,7 +102,8 @@ void CommitMessages::apply(Layout::BegunRead const& read, Message const& message
// Only an element can be a query container; the viewport names the document, which is not one. Layout says
// this of every size container, but `container-type` is set far more widely than it is asked about, and one
// no size query or container-relative unit resolved against has no dependent to record.
- if (auto* element = as_if<Element>(message.identity.resolve(m_document).ptr()); element && element->is_size_query_container())
+ if (auto* element = as_if<Element>(message.identity.resolve(m_document).ptr()); element && element->is_size_query_container()
+ && m_document->note_size_container_moved_in_stabilization_epoch(element->style_node_id()))
m_document->style_computer().style_engine().record_size_container_query_dependents(element->style_node_id());
return;
case Kind::NavigableContainerViewportCommitted:
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index b81abc98e7d..d4058070fea 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -2392,6 +2392,7 @@ void Document::begin_style_stabilization_epoch()
m_style_stabilization_has_style_reactions = false;
m_animated_custom_property_changes_in_stabilization_epoch.clear();
m_animated_custom_property_moves_in_stabilization_epoch = 0;
+ m_size_container_moves_in_stabilization_epoch.clear();
HTML::note_rendering_trace_event("style stabilization epoch"sv);
++m_transition_generation;
}
@@ -2428,6 +2429,23 @@ u64 Document::note_animated_custom_properties_moved_in_stabilization_epoch(CSS::
return ++m_animated_custom_property_changes_in_stabilization_epoch.ensure(style_node);
}
+bool Document::note_size_container_moved_in_stabilization_epoch(CSS::StyleNodeID style_node)
+{
+ if (!is_in_style_stabilization_epoch())
+ return true;
+
+ // Inline-size containment keeps a container's content out of its intrinsic inline size, but the content can still
+ // move its inline size through its block size: a grid item with a preferred aspect ratio stretched in its row has
+ // its column sized from the row again, and the row grows to fit the content. Content sized in container-relative
+ // units then grows with the container, and the container with it, without ever settling. Such a container's
+ // dependents keep the style they computed against its box from the moves before.
+ constexpr u64 MOVES_OF_A_SIZE_CONTAINER_IN_AN_EPOCH = 8;
+ auto moves = ++m_size_container_moves_in_stabilization_epoch.ensure(style_node);
+ if (moves == MOVES_OF_A_SIZE_CONTAINER_IN_AN_EPOCH + 1)
+ HTML::note_rendering_trace_event("size container move guard"sv);
+ return moves <= MOVES_OF_A_SIZE_CONTAINER_IN_AN_EPOCH;
+}
+
bool Document::is_at_style_stabilization_bound(Layout::BegunRead const& read) const
{
// NB: A style update runs at most this many passes.
diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h
index a8171e9239c..348eb3d9326 100644
--- a/Libraries/LibWeb/DOM/Document.h
+++ b/Libraries/LibWeb/DOM/Document.h
@@ -1632,6 +1632,9 @@ public:
[[nodiscard]] bool is_at_style_stabilization_bound(Layout::BegunRead const& read) const;
// How many times an element's animated custom properties moved in this style stabilization epoch, counting this one.
[[nodiscard]] u64 note_animated_custom_properties_moved_in_stabilization_epoch(CSS::StyleNodeID);
+ // Whether the dependents of a size container whose box moved are computed again, as they are for its first few
+ // moves in a style stabilization epoch.
+ [[nodiscard]] bool note_size_container_moved_in_stabilization_epoch(CSS::StyleNodeID);
void end_style_stabilization_epoch();
void note_style_stabilization_has_style_reactions()
{
@@ -2132,6 +2135,7 @@ private:
HashMap<CSS::StyleNodeID, u64> m_animated_custom_property_changes_in_stabilization_epoch;
u64 m_animated_custom_property_moves_in_stabilization_epoch { 0 };
+ HashMap<CSS::StyleNodeID, u64> m_size_container_moves_in_stabilization_epoch;
bool m_created_for_appropriate_template_contents { false };
GC::Ptr<Document> m_associated_inert_template_document;
GC::Ptr<Document> m_appropriate_template_contents_owner_document;
diff --git a/Tests/LibWeb/Text/expected/css/size-container-growing-with-its-units-settles.txt b/Tests/LibWeb/Text/expected/css/size-container-growing-with-its-units-settles.txt
new file mode 100644
index 00000000000..36012f16f0c
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/size-container-growing-with-its-units-settles.txt
@@ -0,0 +1 @@
+item width is finite: true
diff --git a/Tests/LibWeb/Text/input/css/size-container-growing-with-its-units-settles.html b/Tests/LibWeb/Text/input/css/size-container-growing-with-its-units-settles.html
new file mode 100644
index 00000000000..28ed89046a8
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/size-container-growing-with-its-units-settles.html
@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ .grid { display: grid; grid-template-columns: repeat(4, 1fr); width: 800px; }
+ .item { aspect-ratio: 354 / 419; container-type: inline-size; }
+ h2 { font-size: 50cqw; }
+</style>
+<div class="grid"><div class="item"><h2>Join the family</h2></div></div>
+<script>
+ // A stretched grid item with a preferred aspect ratio has its column sized again from its row, which grows to fit
+ // content sized in units of the item. The item then grows with each style and layout pass, and the update must
+ // stop growing it rather than run out of passes.
+ test(() => {
+ const width = document.querySelector(".item").getBoundingClientRect().width;
+ println(`item width is finite: ${Number.isFinite(width) && width < 1e7}`);
+ });
+</script>