Skip to content

Truly independent web browser

LibWeb: Settle style updates before a tree build demands a record

Andreas Kling55ec4e4f97e6
diff --git a/Libraries/LibWeb/CSS/UpdateStyle.cpp b/Libraries/LibWeb/CSS/UpdateStyle.cpp
index a8104e6fde6..a87ee14e269 100644
--- a/Libraries/LibWeb/CSS/UpdateStyle.cpp
+++ b/Libraries/LibWeb/CSS/UpdateStyle.cpp
@@ -857,7 +857,23 @@ void Document::update_style()
{
Layout::ForcedReadScope read { style_computer().style_engine().render_document() };
drain_flown_style_update(read);
+ auto const stopped_passes = [&] { return style_invalidation_counters().style_update_pass_guard_hits; };
+ auto stopped_before = stopped_passes();
CSS::update_style(read, *this);
+ if (stopped_passes() == stopped_before)
+ return;
+
+ // NB: A style update stops after a number of passes with what they fed back still pending, as a chain of custom
+ // property animations does that moves one level per pass. What reads the style next wants them all, such as a
+ // tree build demanding a record, which reads current input only.
+ begin_style_stabilization_epoch();
+ ScopeGuard end_stabilization_epoch = [&] { end_style_stabilization_epoch(); };
+ stopped_before = stopped_passes();
+ while (style_computer().style_engine().has_pending_transaction(read) && !is_at_style_stabilization_bound(read)) {
+ CSS::update_style(read, *this);
+ if (stopped_passes() == exchange(stopped_before, stopped_passes()))
+ break;
+ }
}
bool Document::let_style_update_fly(Layout::RustFFI::FfiFlightBlocker blocker)
diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h
index f24718fe7fc..81d5fd0fb21 100644
--- a/Libraries/LibWeb/DOM/Document.h
+++ b/Libraries/LibWeb/DOM/Document.h
@@ -1536,7 +1536,6 @@ 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);
- [[nodiscard]] u64 animated_custom_property_moves_in_stabilization_epoch() const { return m_animated_custom_property_moves_in_stabilization_epoch; }
void end_style_stabilization_epoch();
void note_style_stabilization_has_style_reactions()
{
diff --git a/Libraries/LibWeb/Layout/UpdateLayout.cpp b/Libraries/LibWeb/Layout/UpdateLayout.cpp
index 5ac76d7a5c5..e2bfb3a62c8 100644
--- a/Libraries/LibWeb/Layout/UpdateLayout.cpp
+++ b/Libraries/LibWeb/Layout/UpdateLayout.cpp
@@ -35,7 +35,6 @@ Layout::RustFFI::FfiLayoutUpdateHostCallbacks Document::layout_update_host_callb
return {
.context = this,
.connected_element_count = [](void* context, Layout::BegunRead const* read) -> u32 { return CSS::StyleEngineFFI::style_engine_connected_element_count(static_cast<Document*>(context)->style_computer().style_engine().host(), read); },
- .animated_custom_property_moves = [](void* context, Layout::BegunRead const*) -> u64 { return static_cast<Document*>(context)->animated_custom_property_moves_in_stabilization_epoch(); },
.update_style = [](void* context, Layout::BegunRead const*) { static_cast<Document*>(context)->update_style(); },
.process_pending_list_item_renumbers = [](void* context, Layout::BegunRead const*) { static_cast<Document*>(context)->process_pending_list_item_renumbers(); },
.process_pending_top_layer_layout_changes = [](void* context, Layout::BegunRead const* read) { static_cast<Document*>(context)->process_pending_top_layer_layout_changes(*read); },
diff --git a/Libraries/LibWeb/Rust/src/layout/update_layout.rs b/Libraries/LibWeb/Rust/src/layout/update_layout.rs
index 2d2382e85ab..db62dda35fb 100644
--- a/Libraries/LibWeb/Rust/src/layout/update_layout.rs
+++ b/Libraries/LibWeb/Rust/src/layout/update_layout.rs
@@ -68,7 +68,6 @@ fn read_arena<A, R>(host: &DocumentHost, read: &BegunRead, args: A, answer: fn(&
pub struct FfiLayoutUpdateHostCallbacks {
context: *mut c_void,
connected_element_count: extern "C" fn(*mut c_void, &BegunRead) -> u32,
- animated_custom_property_moves: extern "C" fn(*mut c_void, &BegunRead) -> u64,
update_style: extern "C" fn(*mut c_void, &BegunRead),
process_pending_list_item_renumbers: extern "C" fn(*mut c_void, &BegunRead),
process_pending_top_layer_layout_changes: extern "C" fn(*mut c_void, &BegunRead),
@@ -177,10 +176,6 @@ impl FfiLayoutUpdateHostCallbacks {
(self.connected_element_count)(self.context, read)
}
- fn animated_custom_property_moves(&self, _: &MainThread, read: &BegunRead) -> u64 {
- (self.animated_custom_property_moves)(self.context, read)
- }
-
fn update_style(&self, _: &MainThread, read: &BegunRead) {
(self.update_style)(self.context, read);
}
@@ -1097,8 +1092,7 @@ fn update_layout(
// Size-query dependencies point from a descendant to an ancestor query container. They are
// therefore acyclic, and a coherent style/layout pass can settle at least one more level of
- // a nested dependency chain. One pass per connected element is a conservative exact bound, with
- // one more per move of an animated custom property, as a chain of them moves one value per style pass.
+ // a nested dependency chain. One pass per connected element is a conservative exact bound.
// Recompute it after each pass because an initial style update can enroll the elements of a
// freshly parsed document after the layout update has already started. The bound is at least
// the ordinary limit, so the passes within it do not ask for the count.
@@ -1112,10 +1106,7 @@ fn update_layout(
let mut trace = StabilizationTrace::default();
while layout_pass <= ORDINARY_STABILIZATION_ROUND_LIMIT
|| layout_pass
- < ORDINARY_STABILIZATION_ROUND_LIMIT
- + u64::from(host.connected_element_count(main_thread, read))
- + host.animated_custom_property_moves(main_thread, read)
- + 1
+ < ORDINARY_STABILIZATION_ROUND_LIMIT + u64::from(host.connected_element_count(main_thread, read)) + 1
{
layout_pass += 1;
diff --git a/Tests/LibWeb/Text/expected/css/custom-property-animations-chain-above-a-tree-build.txt b/Tests/LibWeb/Text/expected/css/custom-property-animations-chain-above-a-tree-build.txt
new file mode 100644
index 00000000000..aca7686e80e
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/custom-property-animations-chain-above-a-tree-build.txt
@@ -0,0 +1,2 @@
+has a box: true
+the element: 24px
diff --git a/Tests/LibWeb/Text/input/css/custom-property-animations-chain-above-a-tree-build.html b/Tests/LibWeb/Text/input/css/custom-property-animations-chain-above-a-tree-build.html
new file mode 100644
index 00000000000..9f752d7de79
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/custom-property-animations-chain-above-a-tree-build.html
@@ -0,0 +1,31 @@
+<!DOCTYPE html>
+<body>
+<script src="../include.js"></script>
+<style>
+ #item::before {
+ content: "before";
+ display: list-item;
+ list-style-position: inside;
+ }
+</style>
+<div id="chain"><div id="item">item</div></div>
+<script>
+ test(() => {
+ // A style update stops after a number of passes, and the chain moves one value per pass. The tree build demands
+ // the style of a list marker below it, which it may only do once the chain has settled.
+ const depth = 24;
+ const style = document.createElement("style");
+ const animations = [];
+ for (let level = 1; level <= depth; ++level) {
+ CSS.registerProperty({ name: `--level${level}`, syntax: "<length>", inherits: true, initialValue: "0px" });
+ const previous = level === 1 ? "0px" : `var(--level${level - 1})`;
+ const value = `--level${level}: calc(${previous} + 1px)`;
+ style.textContent += `@keyframes level${level} { from { ${value} } to { ${value} } }\n`;
+ animations.push(`level${level} 1000s linear both`);
+ }
+ document.head.append(style);
+ chain.style.animation = animations.join(", ");
+ println(`has a box: ${item.getBoundingClientRect().height > 0}`);
+ println(`the element: ${getComputedStyle(item).getPropertyValue(`--level${depth}`)}`);
+ });
+</script>