Truly independent web browser
LibWeb: Lay out SVG container lengths again when their basis moves
Aliaksandr Kalenikd75c64403433
diff --git a/Libraries/LibWeb/Rust/src/css/style/container_queries.rs b/Libraries/LibWeb/Rust/src/css/style/container_queries.rsindex c7ba137ce05..f851b853a36 100644--- a/Libraries/LibWeb/Rust/src/css/style/container_queries.rs+++ b/Libraries/LibWeb/Rust/src/css/style/container_queries.rs@@ -55,7 +55,7 @@ impl ContainerVerdict {/// `Length::container_relative_length_to_px_without_rounding` finds it: the content size of the/// nearest flat-tree ancestor that accepts size queries on that axis as of the last layout commit,/// zero while that container has no box, and the viewport's size when there is no such container.-#[derive(Clone, Copy)]+#[derive(Clone, Copy, Debug, PartialEq)]pub(crate) struct ContainerUnitBasis {basis: f64,/// The container the basis is read from, or none for the viewport.@@ -77,7 +77,7 @@ impl ContainerUnitBasis {}/// The bases of both physical axes, width and height.-#[derive(Clone, Copy)]+#[derive(Clone, Copy, Debug, PartialEq)]pub(crate) struct ContainerUnitBases([ContainerUnitBasis; 2]);impl ContainerUnitBases {diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rsindex 2bc9cbbd38e..20fcd601a00 100644--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs@@ -477,6 +477,7 @@ enum InFlowReuse {validity: fc_run_cache::FcRunCacheValidity,flow_position: CssPixels,abspos_containing_block_info_contributions_received: u64,+ host_findings_noted_in_pass: u64,},}@@ -3563,6 +3564,7 @@ impl<'pass> BlockFormattingContext<'pass> {flow_position: self.flow_position(),abspos_containing_block_info_contributions_received: self.abspos_containing_block_info_contributions_received(),+ host_findings_noted_in_pass: callbacks.host_findings_noted_in_pass(),}}@@ -3636,6 +3638,7 @@ impl<'pass> BlockFormattingContext<'pass> {validity,flow_position,abspos_containing_block_info_contributions_received,+ host_findings_noted_in_pass,} = attemptelse {return;@@ -3651,6 +3654,7 @@ impl<'pass> BlockFormattingContext<'pass> {&& self.floats.borrow().is_empty()&& self.abspos_containing_block_info_contributions_received()== abspos_containing_block_info_contributions_received+ && self.run.callbacks.host_findings_noted_in_pass() == host_findings_noted_in_pass&& used.border_box_block_size(false) != CssPixels::default();if !reusable {return;diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rsindex b3c011ee410..afc37366a3b 100644--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs@@ -3624,18 +3624,23 @@ impl LayoutStageJob {let super::run_state::HostFindings {messages,document_svg_root_natural_size,+ container_length_reads,} = output.host_findings;if let Some((root_box, natural_size)) = document_svg_root_natural_size {state.arena_mut().note_document_svg_root_natural_size(root_box, natural_size);}+ state+ .arena_mut()+ .note_container_bases_read_by_committed_layout(container_length_reads);notifications.prepend_layout_messages(messages);let (arena, scratch) = state.arena_mut_and_layout_scratch();scratch.layout_trace.hand_back_to(&mut arena.layout_trace);scratch.end_layout_pass(arena);arena.reset_layout_update_flags_in_subtree(root);arena.leave_active_layout_pass();+ arena.mark_container_length_readers_whose_bases_moved_for_layout();match kind {// NB: The full pass covered every subtree the last build rebuilt.LayoutStageKind::Root { .. } => arena.clear_pending_rebuilt_subtree_roots(),@@ -3772,6 +3777,7 @@ fn prepare_layout_stage(state: &mut super::ArenaHandle, kind: LayoutStageKind) {if matches!(kind, LayoutStageKind::Root { .. }) {arena.clear_partial_relayout_escape();}+ arena.mark_container_length_readers_whose_bases_moved_for_layout();scratch.apply_memo_invalidations(arena);scratch.layout_trace.take_over_from(&mut arena.layout_trace);arena.begin_active_layout_pass();diff --git a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rsindex e7c5f457911..dfa6fdd84d1 100644--- a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs+++ b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs@@ -1221,6 +1221,8 @@ pub(crate) struct LayoutNodeArena {/// The natural size layout last negotiated for the box of an `<svg>` document element, and that/// box. An `<object>` showing the document is sized from it.document_svg_root_natural_size: Option<(NodeSlotId, crate::css::style::NaturalSize)>,+ container_bases_read_by_committed_layout:+ HashMap<NodeSlotId, Vec<(StyleNodeID, crate::css::style::ContainerUnitBases)>>,/// The document element's natural size as it was last handed to the `<object>` showing the/// document, if it has been.handed_over_document_svg_root_natural_size: Option<crate::css::style::NaturalSize>,@@ -1326,6 +1328,7 @@ impl LayoutNodeArena {nodes_enrolled_for_replaced_content_facts_sync: Vec::new(),owned_image_natural_sizes: HashMap::default(),document_svg_root_natural_size: None,+ container_bases_read_by_committed_layout: HashMap::default(),handed_over_document_svg_root_natural_size: (None),layout_style_snapshot_commit: Vec::new(),layout_tree_update_marks: Default::default(),@@ -3061,6 +3064,49 @@ impl LayoutNodeArena {self.document_svg_root_natural_size = Some((root_box, natural_size));}+ pub(crate) fn note_container_bases_read_by_committed_layout(+ &mut self,+ reads: Vec<super::run_state::ContainerLengthRead>,+ ) {+ let mut readers_noted = HashSet::default();+ for read in reads {+ let bases_read = self+ .container_bases_read_by_committed_layout+ .entry(read.reader)+ .or_default();+ if readers_noted.insert(read.reader) {+ bases_read.clear();+ }+ bases_read.push((read.element, read.bases));+ }+ }++ pub(crate) fn mark_container_length_readers_whose_bases_moved_for_layout(&mut self) {+ if self.container_bases_read_by_committed_layout.is_empty() {+ return;+ }+ let mut readers_whose_bases_moved = Vec::new();+ for (&reader, bases_read) in &self.container_bases_read_by_committed_layout {+ if !self.slot_is_live(reader) {+ readers_whose_bases_moved.push(reader);+ continue;+ }+ let bases_moved = bases_read.iter().any(|&(element, bases)| {+ self.dom_node_style_node(reader) != Some(element)+ || self.with_style_store(|engine| engine.container_unit_bases(element, false)) != bases+ });+ if bases_moved {+ readers_whose_bases_moved.push(reader);+ }+ }+ for reader in readers_whose_bases_moved {+ self.container_bases_read_by_committed_layout.remove(&reader);+ if self.slot_is_live(reader) {+ self.set_needs_layout_update(reader, true);+ }+ }+ }+/// The natural size of the document's `<svg>` document element as its last layout negotiated/// it, unless it is what the last call handed over. The size is all absent if the document/// element is no `<svg>` with a box.diff --git a/Libraries/LibWeb/Rust/src/layout/layout_pass.rs b/Libraries/LibWeb/Rust/src/layout/layout_pass.rsindex d6dd9f853d5..5451f21a7de 100644--- a/Libraries/LibWeb/Rust/src/layout/layout_pass.rs+++ b/Libraries/LibWeb/Rust/src/layout/layout_pass.rs@@ -209,9 +209,14 @@ impl<'arena> LayoutPass<'arena> {/// Notes what the run this pass view was handed to found out for the host, or the pass outside any run.pub(crate) fn note_host_findings(&self, note: impl FnOnce(&mut super::run_state::HostFindings)) {+ self.scratch.note_host_findings_noted_in_pass();note(&mut self.host_findings.borrow_mut());}+ pub(crate) fn host_findings_noted_in_pass(&self) -> u64 {+ self.scratch.host_findings_noted_in_pass()+ }+/// Hands what a finished run found out to the run this pass view was handed to, which used its result.pub(crate) fn receive_host_findings(&self, findings: Option<Box<super::run_state::HostFindings>>) {if let Some(findings) = findings {diff --git a/Libraries/LibWeb/Rust/src/layout/run_repeatability.rs b/Libraries/LibWeb/Rust/src/layout/run_repeatability.rsindex 149fa17413a..c058715b0e1 100644--- a/Libraries/LibWeb/Rust/src/layout/run_repeatability.rs+++ b/Libraries/LibWeb/Rust/src/layout/run_repeatability.rs@@ -258,6 +258,7 @@ fn host_findings_match(left: Option<&run_state::HostFindings>, right: Option<&rulet left = left.unwrap_or(&none);let right = right.unwrap_or(&none);same_elements_in_any_order(&left.messages, &right.messages)+ && same_elements_in_any_order(&left.container_length_reads, &right.container_length_reads)&& left.document_svg_root_natural_size == right.document_svg_root_natural_size}diff --git a/Libraries/LibWeb/Rust/src/layout/run_state.rs b/Libraries/LibWeb/Rust/src/layout/run_state.rsindex 693efcbfa2e..c3a429cba3f 100644--- a/Libraries/LibWeb/Rust/src/layout/run_state.rs+++ b/Libraries/LibWeb/Rust/src/layout/run_state.rs@@ -13,6 +13,7 @@ use super::*;pub(crate) struct LayoutScratch {/// The intrinsic sizes passes measured, kept for the passes after them.pub(super) intrinsic_size_caches: super::layout_node_arena::IntrinsicSizeCaches,+ host_findings_noted_in_pass: Cell<u64>,/// The results of formatting context runs, kept for the runs after them in this pass and the passes after it.fc_run_cache_store: super::fc_run_cache::FcRunCacheStore,/// The layouts of in-flow blocks, kept for the passes after the one that laid them out.@@ -34,19 +35,35 @@ pub(crate) struct LayoutScratch {pub(crate) struct HostFindings {pub(crate) messages: Vec<super::commit::FfiCommitMessage>,pub(crate) document_svg_root_natural_size: Option<(NodeSlotId, crate::css::style::NaturalSize)>,+ pub(crate) container_length_reads: Vec<ContainerLengthRead>,+}++#[derive(Clone, Copy, Debug, PartialEq)]+pub(crate) struct ContainerLengthRead {+ pub(crate) reader: NodeSlotId,+ pub(crate) element: crate::css::style::tree::StyleNodeID,+ pub(crate) bases: crate::css::style::ContainerUnitBases,}impl HostFindings {pub(crate) fn is_empty(&self) -> bool {- self.messages.is_empty() && self.document_svg_root_natural_size.is_none()+ self.messages.is_empty()+ && self.document_svg_root_natural_size.is_none()+ && self.container_length_reads.is_empty()}pub(crate) fn note_container_length_dependencies(&mut self,+ reader: NodeSlotId,element: crate::css::style::tree::StyleNodeID,bases: &crate::css::style::ContainerUnitBases,) {use super::commit::{FfiCommitMessage, FfiCommitMessageKind};+ self.note_container_length_read(ContainerLengthRead {+ reader,+ element,+ bases: *bases,+ });self.note_message(FfiCommitMessage::new(element.raw(),FfiCommitMessageKind::StyleDependsOnSizeContainerQuery,@@ -74,10 +91,19 @@ impl HostFindings {}}+ fn note_container_length_read(&mut self, read: ContainerLengthRead) {+ if !self.container_length_reads.contains(&read) {+ self.container_length_reads.push(read);+ }+ }+pub(crate) fn receive(&mut self, other: HostFindings) {for message in other.messages {self.note_message(message);}+ for read in other.container_length_reads {+ self.note_container_length_read(read);+ }if other.document_svg_root_natural_size.is_some() {self.document_svg_root_natural_size = other.document_svg_root_natural_size;}@@ -140,6 +166,15 @@ impl LayoutScratch {);}+ pub(super) fn note_host_findings_noted_in_pass(&self) {+ self.host_findings_noted_in_pass+ .set(self.host_findings_noted_in_pass.get() + 1);+ }++ pub(super) fn host_findings_noted_in_pass(&self) -> u64 {+ self.host_findings_noted_in_pass.get()+ }+/// Brings the run memos up to date with what the commit into `arena` did to their boxes, and sweeps the run cache.pub(crate) fn end_layout_pass(&mut self, arena: &mut LayoutNodeArena) {self.apply_memo_invalidations(arena);diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rsindex 5082622439a..f2b4821a4af 100644--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs@@ -2528,7 +2528,7 @@ fn length_resolution_context(let bases = callbacks.arena().with_style_store(|engine| engine.container_unit_bases(element, false));- callbacks.note_host_findings(|findings| findings.note_container_length_dependencies(element, &bases));+ callbacks.note_host_findings(|findings| findings.note_container_length_dependencies(node, element, &bases));bases});let mut context = crate::css::style_compute::FfiLengthResolutionContext {diff --git a/Tests/LibWeb/Text/expected/svg-container-relative-lengths-follow-their-container.txt b/Tests/LibWeb/Text/expected/svg-container-relative-lengths-follow-their-container.txtnew file mode 100644index 00000000000..1c1e0db5c9a--- /dev/null+++ b/Tests/LibWeb/Text/expected/svg-container-relative-lengths-follow-their-container.txt@@ -0,0 +1,2 @@+After load, in a 300px container: x 150+After the container grew to 400px: x 200diff --git a/Tests/LibWeb/Text/input/svg-container-relative-lengths-follow-their-container.html b/Tests/LibWeb/Text/input/svg-container-relative-lengths-follow-their-container.htmlnew file mode 100644index 00000000000..d4d1f233427--- /dev/null+++ b/Tests/LibWeb/Text/input/svg-container-relative-lengths-follow-their-container.html@@ -0,0 +1,23 @@+<!DOCTYPE html>+<script src="include.js"></script>+<div id="container" style="container-type: size; width: 300px; height: 100px">+ <div id="wrapper" style="width: 100px">+ <svg id="svg" width="100%" height="100"><text id="label" x="50cqw" y="50">Label</text></svg>+ </div>+</div>+<script>+ function labelOffset() {+ return Math.round(label.getBoundingClientRect().left - svg.getBoundingClientRect().left);+ }++ asyncTest(async done => {+ await animationFrame();+ await animationFrame();+ println(`After load, in a 300px container: x ${labelOffset()}`);+ container.style.width = "400px";+ await animationFrame();+ await animationFrame();+ println(`After the container grew to 400px: x ${labelOffset()}`);+ done();+ });+</script>