Skip to content

Truly independent web browser

LibWeb: Move a layout node with its row's style as a frame lands

Andreas Klinga72643bfb825
diff --git a/Libraries/LibWeb/Rust/src/layout/tree_mutation.rs b/Libraries/LibWeb/Rust/src/layout/tree_mutation.rs
index 891ca73dfda..f6f077ccc57 100644
--- a/Libraries/LibWeb/Rust/src/layout/tree_mutation.rs
+++ b/Libraries/LibWeb/Rust/src/layout/tree_mutation.rs
@@ -137,6 +137,46 @@ impl HostWorkDue {
self.box_presence.is_empty() && self.calls.is_empty()
}
+ /// Takes out of the work what it tells layout nodes of their rows' styles, which a frame's layout round pays as the
+ /// frame lands, ahead of what is left, which waits for the host's layout update.
+ pub(crate) fn take_shell_style_changes(&mut self) -> Self {
+ let (changes, rest) = std::mem::take(&mut self.calls)
+ .into_iter()
+ .partition(|call| matches!(call, DueHostCall::ShellStyleChanged { .. }));
+ self.calls = rest;
+ Self {
+ box_presence: Default::default(),
+ calls: changes,
+ }
+ }
+
+ /// Has what the work tells layout nodes of their rows' styles read as `arena` does as the job or frame that owes it
+ /// ends, which is when the host pays it. A row may have moved on since the work came to owe it and let go of the
+ /// record it held then, while the record it holds now stays alive until a later job writes the row, which the host
+ /// pays for in turn. A row that has gone is owed nothing.
+ pub(crate) fn follow_rows(&mut self, arena: &LayoutNodeArena) {
+ self.calls.retain_mut(|call| {
+ let DueHostCall::ShellStyleChanged {
+ facts,
+ record,
+ payloads,
+ derived,
+ ..
+ } = call
+ else {
+ return true;
+ };
+ let Some(kind) = arena.node_kind_if_live(facts.id) else {
+ return false;
+ };
+ facts.kind = kind;
+ *record = arena.node_style_record(facts.id);
+ *payloads = arena.data(facts.id).style.get();
+ *derived = arena.node_style_record_is_derived(facts.id);
+ true
+ });
+ }
+
/// Makes the host calls owed.
pub(crate) fn pay(self, main_thread: &MainThread) {
self.box_presence.tell(main_thread);
@@ -347,7 +387,7 @@ mod ffi_test_stubs {
#[cfg(test)]
mod tests {
- use super::UnplacedLayoutNode;
+ use super::{DueHostCall, HostCalls, HostWorkDue, MainThread, OwedHostWork, UnplacedLayoutNode};
use crate::layout::LayoutNodeArena;
use crate::layout::layout_node_arena::NodeAllocation;
use crate::layout::node_data::NodeSlotId;
@@ -530,4 +570,67 @@ mod tests {
let node = arena.allocate_for_test();
let _ = UnplacedLayoutNode::new(node.slot);
}
+
+ /// The rows and records `due` tells layout nodes of.
+ fn shell_styles(due: &HostWorkDue) -> Vec<(NodeSlotId, u64)> {
+ due.calls
+ .iter()
+ .filter_map(|call| match call {
+ DueHostCall::ShellStyleChanged { facts, record, .. } => Some((facts.id, *record)),
+ _ => None,
+ })
+ .collect()
+ }
+
+ #[test]
+ fn a_layout_node_hears_of_the_style_its_row_holds_as_the_job_ends() {
+ let main_thread = MainThread::for_test();
+ let mut arena = LayoutNodeArena::new();
+ let moved = arena.allocate_for_test().slot;
+ let freed = arena.allocate_for_test().slot;
+ arena.set_node_style(moved, 7, None);
+ arena.set_node_style(freed, 7, None);
+
+ let work = OwedHostWork::default();
+ arena.queue_box_presence();
+ HostCalls(&work).shell_style_changed(moved, false);
+ HostCalls(&work).shell_style_changed(freed, false);
+ let mut due = work.resolve(&mut arena);
+ assert_eq!(shell_styles(&due), [(moved, 7), (freed, 7)]);
+
+ // A later write of the same job moves one row on, and frees the other.
+ arena.set_node_style(moved, 9, None);
+ arena
+ .free_subtree(freed)
+ .destroy_shells_and_invoke_callbacks(&main_thread);
+ due.follow_rows(&arena);
+ assert_eq!(shell_styles(&due), [(moved, 9)]);
+
+ arena
+ .free_subtree(moved)
+ .destroy_shells_and_invoke_callbacks(&main_thread);
+ }
+
+ #[test]
+ fn a_rounds_shell_style_changes_leave_the_rest_of_what_it_owes() {
+ let main_thread = MainThread::for_test();
+ let mut arena = LayoutNodeArena::new();
+ let row = arena.allocate_for_test().slot;
+ arena.set_node_style(row, 7, None);
+
+ let work = OwedHostWork::default();
+ arena.queue_box_presence();
+ HostCalls(&work).shell_style_changed(row, false);
+ HostCalls(&work).shell_style_damaged(row, 1);
+ let mut due = work.resolve(&mut arena);
+ let changes = due.take_shell_style_changes();
+ assert_eq!(shell_styles(&changes), [(row, 7)]);
+ assert_eq!(changes.calls.len(), 1);
+ assert!(shell_styles(&due).is_empty());
+ assert_eq!(due.calls.len(), 1);
+
+ arena
+ .free_subtree(row)
+ .destroy_shells_and_invoke_callbacks(&main_thread);
+ }
}
diff --git a/Libraries/LibWeb/Rust/src/layout/update_layout.rs b/Libraries/LibWeb/Rust/src/layout/update_layout.rs
index db62dda35fb..2652a6997e3 100644
--- a/Libraries/LibWeb/Rust/src/layout/update_layout.rs
+++ b/Libraries/LibWeb/Rust/src/layout/update_layout.rs
@@ -619,6 +619,12 @@ pub(crate) struct FlownRound {
}
impl FlownRound {
+ /// Takes what the round owes layout nodes of their rows' styles, which the host pays as the frame lands: a layout
+ /// node hears of its row's style with the row, not once the host comes to take the round in.
+ pub(crate) fn take_shell_style_changes(&mut self) -> HostWorkDue {
+ self.answer.work.take_shell_style_changes()
+ }
+
/// Pays what the round owes `host` in `read`, where no layout update goes on from it.
pub(crate) fn pay(mut self, main_thread: &MainThread, host: &DocumentHost, read: &BegunRead) {
self.answer.pay(main_thread, host, read);
diff --git a/Libraries/LibWeb/Rust/src/render_state.rs b/Libraries/LibWeb/Rust/src/render_state.rs
index 83694c9bc74..b36f19f46b3 100644
--- a/Libraries/LibWeb/Rust/src/render_state.rs
+++ b/Libraries/LibWeb/Rust/src/render_state.rs
@@ -168,6 +168,9 @@ impl RenderState {
/// host learns of the engine's deferred element style inputs, where they moved, and the facts of the state as the
/// job leaves it.
fn take_owed(&mut self) -> Owed {
+ for work in &mut self.owed {
+ work.follow_rows(self.arena.arena());
+ }
self.engine_mut().resolve_relational_arrival_keys();
let engine = self.engine_ref();
let arena = self.arena.arena();
@@ -269,9 +272,13 @@ impl RenderState {
&mut self,
round: crate::layout::SealedRound,
) -> Option<(crate::layout::FlownRound, crate::layout::row_reads::RowSnapshot)> {
- round
- .run(&mut self.arena)
- .map(|round| (round, self.arena.arena_mut().publish_row_snapshot(false)))
+ let mut round = round.run(&mut self.arena)?;
+ let shell_style_changes = round.take_shell_style_changes();
+ if !shell_style_changes.is_empty() {
+ self.owed.push(shell_style_changes);
+ }
+ let rows = self.arena.arena_mut().publish_row_snapshot(false);
+ Some((round, rows))
}
}
diff --git a/Libraries/LibWeb/Rust/src/render_state/document_host.rs b/Libraries/LibWeb/Rust/src/render_state/document_host.rs
index 560a83a978b..ea91dff4609 100644
--- a/Libraries/LibWeb/Rust/src/render_state/document_host.rs
+++ b/Libraries/LibWeb/Rust/src/render_state/document_host.rs
@@ -643,14 +643,6 @@ impl DocumentHost {
) {
self.changes.give_back(changes);
self.take_marks_back(marks);
- self.pay(owed);
- if let Some(style) = style {
- let previous = self
- .flown_style
- .borrow_mut()
- .replace(FlownStyle::Landed(Box::new(style)));
- debug_assert!(previous.is_none(), "one style update of a document flies at a time");
- }
if let Some((round, rows)) = round {
// The layout round that flew with the frame wrote the render state the paint properties are prepared from.
self.note_render_state_write();
@@ -662,6 +654,14 @@ impl DocumentHost {
// The frame's job ended with the rows, so they read as the arena unless the host wrote it since.
*self.rows.borrow_mut() = Some(Rc::new(rows));
}
+ self.pay(owed);
+ if let Some(style) = style {
+ let previous = self
+ .flown_style
+ .borrow_mut()
+ .replace(FlownStyle::Landed(Box::new(style)));
+ debug_assert!(previous.is_none(), "one style update of a document flies at a time");
+ }
self.note_frame_wait();
}
diff --git a/Tests/LibWeb/Crash/CSS/table-box-style-moves-after-its-layout-node-holds-it.html b/Tests/LibWeb/Crash/CSS/table-box-style-moves-after-its-layout-node-holds-it.html
new file mode 100644
index 00000000000..da159802f0a
--- /dev/null
+++ b/Tests/LibWeb/Crash/CSS/table-box-style-moves-after-its-layout-node-holds-it.html
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<html class="test-wait">
+<body></body>
+<script>
+ // A frame's layout round derives a style for the table's box, and a style read moves the box on from it before the
+ // host takes the round in. The table's layout node must not hear of the style the box has let go of.
+ window.addEventListener("load", () => setTimeout(async () => {
+ const frame = () => new Promise(resolve => requestAnimationFrame(resolve));
+ const make = (tag, parent) => parent.appendChild(document.createElement(tag));
+ make("select", document.body);
+ const summary = make("summary", make("table", document.body));
+ const cell = make("td", summary);
+ cell.hidden = true;
+ for (const text of [
+ "@layer reset { * { --read: 0; --moved: 0; } }",
+ ".never { --read: 1; border: 1px solid; }",
+ "",
+ "@media (prefers-color-scheme: light) { :nth-child(2n+1) + * ~ * { --moved: 1; margin-right: 7px; } }",
+ ])
+ make("style", document.head).textContent = text;
+ const read = () => {
+ for (const element of document.querySelectorAll("*"))
+ getComputedStyle(element).getPropertyValue("--read");
+ };
+ internals.setPreferredColorScheme("light");
+ await frame();
+ read();
+ await frame();
+ internals.setPreferredColorScheme("dark");
+ read();
+ await frame();
+ cell.append("text");
+ await frame();
+ await frame();
+ internals.setPreferredColorScheme("auto");
+ document.documentElement.classList.remove("test-wait");
+ }, 50));
+</script>
+</html>