Truly independent web browser
LibWeb: Defer microtasks until a layout update’s re-snap is done
sideshowbarker6413a84a4982
diff --git a/Libraries/LibWeb/Layout/UpdateLayout.cpp b/Libraries/LibWeb/Layout/UpdateLayout.cppindex b7cc275404e..fb50ad7dcd7 100644--- a/Libraries/LibWeb/Layout/UpdateLayout.cpp+++ b/Libraries/LibWeb/Layout/UpdateLayout.cpp@@ -240,6 +240,14 @@ void Document::update_style_and_layout_once(Layout::BegunRead const& read, Updat&& animation_sampling_scope == ThrottledAnimationSamplingScope::Document)flush_throttled_animation_style_update();+ // The stabilization epoch defers microtask checkpoints. It ends after the cleanup below, so that a checkpoint that a+ // re-snap's scroll reaches doesn't run script while the update is still going, which could leave its caller reading+ // layout that the script made stale.+ begin_style_stabilization_epoch();+ ScopeGuard end_stabilization_epoch = [&] {+ end_style_stabilization_epoch();+ };+auto& arena = layout_node_arena();Layout::RustFFI::document_host_begin_update_layout(arena.host());ScopeGuard guard = [&] {@@ -253,11 +261,6 @@ void Document::update_style_and_layout_once(Layout::BegunRead const& read, Updatpage().client().flush_pending_dom_mutations();};- begin_style_stabilization_epoch();- ScopeGuard end_stabilization_epoch = [&] {- end_style_stabilization_epoch();- };-// Keep shared style records alive across both style and layout, so temporary views// during layout tree construction and layout do not need individual record pins.style_computer().begin_style_record_view_epoch();diff --git a/Tests/LibWeb/Crash/UIEvents/focus-change-during-click-resnaps-and-runs-microtasks.html b/Tests/LibWeb/Crash/UIEvents/focus-change-during-click-resnaps-and-runs-microtasks.htmlnew file mode 100644index 00000000000..2f92a99ea18--- /dev/null+++ b/Tests/LibWeb/Crash/UIEvents/focus-change-during-click-resnaps-and-runs-microtasks.html@@ -0,0 +1,62 @@+<!DOCTYPE html>+<html class="test-wait">+<style>+ body {+ margin: 0;+ }+ #field {+ display: block;+ width: 200px;+ height: 30px;+ }+ #scroller {+ overflow-y: scroll;+ height: 100px;+ width: 200px;+ scroll-snap-type: y mandatory;+ }+ #scroller > div {+ height: 100px;+ scroll-snap-align: start;+ }+ #scroller > #spacer {+ height: 50px;+ scroll-snap-align: none;+ }+ body:has(#field:focus) #scroller > #spacer {+ height: 80px;+ }+ @keyframes fade {+ from { opacity: 0.5; }+ to { opacity: 1; }+ }+ #animated {+ animation: fade 10s linear;+ }+ body:has(#field:focus) #animated {+ animation: none;+ }+</style>+<input id="field">+<div id="scroller"><div id="spacer"></div><div id="target">target</div><div>after</div></div>+<p id="animated">animated</p>+<p id="text">text</p>+<script>+ // Focusing the field on mousedown cancels the animation, which queues the rejection of its finished promise, and+ // moves the snapped target, so the layout update after the focus re-snaps the scroll container. The reaction must+ // not run inside that layout update, where its change to the DOM would leave the mousedown's default actions+ // reading stale layout.+ scroller.scrollTop = 50;+ requestAnimationFrame(() => {+ requestAnimationFrame(async () => {+ for (const animation of animated.getAnimations())+ animation.finished.catch(() => {+ text.firstChild.data = "changed";+ document.body.prepend(document.createElement("div"));+ });+ await internals.clickThroughUIProcess(20, 15);+ document.documentElement.classList.remove("test-wait");+ });+ });+</script>+</html>