Truly independent web browser
LibWeb: Name a box's position option style in the view of the pass
Aliaksandr Kalenikb0a3512ab26b
diff --git a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rsindex 093bb9beef9..9bacdbcacdb 100644--- a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs+++ b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs@@ -3325,28 +3325,25 @@ impl<'pass> AbsposEngine<'pass> {) -> CssPixels {let child_box = child.child_box;let position_option = self.determine_position_fallback_styles(&child);- let cells = used_values::cells_before_sizing(&self.callbacks, child_box, ContainingBlockConstraints::default());- let (mut inputs, default_scroll_shift) = self.pending_child_layout_inputs(&mut child);- inputs.position_option = position_option.choice();- let border_box_block_end = self.layout_element(run, child_box, cells, inputs, Some(default_scroll_shift));- drop(position_option);- border_box_block_end+ self.with_position_option_style(child_box, position_option.style, |engine| {+ let run = run.with_callbacks(engine.callbacks);+ let cells =+ used_values::cells_before_sizing(&engine.callbacks, child_box, ContainingBlockConstraints::default());+ let (mut inputs, default_scroll_shift) = engine.pending_child_layout_inputs(&mut child);+ inputs.position_option = position_option.choice;+ engine.layout_element(&run, child_box, cells, inputs, Some(default_scroll_shift))+ })}/// https://drafts.csswg.org/css-anchor-position-1/#determine-position-fallback-styles- /// Chooses the position option the pending child `child` is laid out with, and makes its style reads see the- /// styles of that option until the answer is dropped.- fn determine_position_fallback_styles(- &self,- child: &abspos_inputs::PendingAbsposChild,- ) -> PositionOptionStyles<'pass> {+ /// Chooses the position option the pending child `child` is laid out with, whose styles live until the answer is+ /// dropped.+ fn determine_position_fallback_styles(&self, child: &abspos_inputs::PendingAbsposChild) -> PositionOptionStyles {let node = child.child_box;let mut styles = PositionOptionStyles {- callbacks: self.callbacks,- node,choice: None,+ style: None,option_styles: Vec::new(),- installed: false,};let style = StyleValues::for_node(&self.callbacks, node);let anchor = style.anchor();@@ -3433,11 +3430,11 @@ impl<'pass> AbsposEngine<'pass> {.iter().filter_map(|&option| {let style = option_style(option)?;- let size = self.with_position_option_style(node, style, || {+ let size = self.with_position_option_style(node, style, |engine| {let mut child = *child;- let (inputs, _) = self.pending_child_layout_inputs(&mut child);- let size = self.inset_modified_containing_block_size(node, &inputs);- let horizontal_is_frame_inline = !self.callbacks.frame.is_transposed();+ let (inputs, _) = engine.pending_child_layout_inputs(&mut child);+ let size = engine.inset_modified_containing_block_size(node, &inputs);+ let horizontal_is_frame_inline = !engine.callbacks.frame.is_transposed();let inline_axis_is_horizontal = writing_mode == writing_mode::HORIZONTAL_TB;let (horizontal, vertical) = if horizontal_is_frame_inline {(size.inline_size, size.block_size)@@ -3489,7 +3486,9 @@ impl<'pass> AbsposEngine<'pass> {let Some(style) = option_style(option) else {return false;};- self.with_position_option_style(node, style, || self.position_option_fits(child, choice_of(option)))+ self.with_position_option_style(node, style, |engine| {+ engine.position_option_fits(child, choice_of(option))+ })};// NB: The box determines its position fallback styles only where it overflows with its current styles.// INTEROP: Other browsers take the first option of a list that position-try-order sorted that fits, even@@ -3510,30 +3509,30 @@ impl<'pass> AbsposEngine<'pass> {.unwrap_or(current)};let option = options[chosen];- let style = option_style(option).flatten();styles.choice = Some(choice_of(option));+ styles.style = option_style(option).flatten();styles.option_styles = option_styles.into_inner();- if let Some(style) = style {- self.callbacks.scratch().push_position_option_style(node, style);- styles.installed = true;- }styles}- /// Runs `callback` with the style reads of `node` seeing `style`, or the style of its row for none.+ /// Runs `callback` with this engine as it lays out `node` with `style`, which its style reads see, or with the+ /// style of its row for none.fn with_position_option_style<T>(&self,node: Node,style: Option<node_data::StylePayloadsRef>,- callback: impl FnOnce() -> T,+ callback: impl for<'option> FnOnce(&AbsposEngine<'option>) -> T,) -> T {let Some(style) = style else {- return callback();+ return callback(self);};- self.callbacks.scratch().push_position_option_style(node, style);- let result = callback();- self.callbacks.scratch().pop_position_option_style(node);- result+ self.callbacks.with_position_option_style(node, style, |callbacks| {+ callback(&AbsposEngine {+ state: self.state,+ callbacks,+ fragments: self.fragments.clone(),+ })+ })}/// The size of the inset-modified containing block that `inputs` give the box `node`, treating auto inset values@@ -3970,30 +3969,14 @@ pub(crate) fn compute_inset_native()}-/// The position option a box is laid out with, whose styles its style reads see while this lives, and the styles of its-/// position options, which the style reads of the box address until then.-struct PositionOptionStyles<'pass> {- callbacks: LayoutPass<'pass>,- node: Node,+/// The position option a box is laid out with and the styles it makes, if any, and the styles of the box's position+/// options, which the style reads of the box address while this lives.+struct PositionOptionStyles {choice: Option<abspos_inputs::PositionOptionChoice>,+ style: Option<node_data::StylePayloadsRef>,/// Boxed so that the payloads the style reads address stay in place as the list grows.#[expect(clippy::vec_box)]option_styles: Vec<Box<LayoutStyle>>,- installed: bool,-}--impl PositionOptionStyles<'_> {- fn choice(&self) -> Option<abspos_inputs::PositionOptionChoice> {- self.choice- }-}--impl Drop for PositionOptionStyles<'_> {- fn drop(&mut self) {- if self.installed {- self.callbacks.scratch().pop_position_option_style(self.node);- }- }}#[cfg(test)]diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rsindex 5fc84a765d9..d445de4f124 100644--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs@@ -1314,10 +1314,18 @@ pub(crate) struct FormattingContextRun<'pass> {impl<'pass> FormattingContextRun<'pass> {/// This run, laying out in `frame`.pub(crate) fn in_frame_of(&self, frame: geometry::Frame) -> Self {- Self {+ self.with_callbacks(self.callbacks.in_frame_of(frame.writing_mode()))+ }++ /// This run, laying out with the pass view `callbacks`.+ pub(crate) fn with_callbacks<'view>(&self, callbacks: LayoutPass<'view>) -> FormattingContextRun<'view>+ where+ 'pass: 'view,+ {+ FormattingContextRun {state: self.state,box_: self.box_,- callbacks: self.callbacks.in_frame_of(frame.writing_mode()),+ callbacks,should_collect_devtools_layout_data: self.should_collect_devtools_layout_data,treat_block_axis_percentage_insets_as_auto_beyond_root: self.treat_block_axis_percentage_insets_as_auto_beyond_root,@@ -3632,6 +3640,7 @@ fn compute_root_layout(input: LayoutStageInput<'_>, should_collect_devtools_layoentry_run_state,&entry_fragments,&callbacks,+ scratch,should_collect_devtools_layout_data,)})@@ -3653,6 +3662,7 @@ fn finish_entry_pass(entry_run_state: &RunState,entry_fragments: &std::rc::Rc<fragment_tree::RunFragmentBuilder>,callbacks: &LayoutPass<'_>,+ scratch: &super::run_state::LayoutScratch,should_collect_devtools_layout_data: bool,) -> LayoutStageOutput {abspos_engine::drain_abspos_with_placed_containing_blocks(@@ -3666,7 +3676,7 @@ fn finish_entry_pass(!pass_fragments.roots.is_empty(),"an entry pass always produces the entry root's fragment");- let mut host_findings = callbacks.scratch().take_host_findings_outside_runs();+ let mut host_findings = scratch.take_host_findings_outside_runs();if let Some(found_by_entry_run) = entry_run_state.take_host_findings() {host_findings.receive(*found_by_entry_run);}@@ -3731,7 +3741,7 @@ fn compute_subtree_layout_fragments(input: LayoutStageInput<'_>, containing_bloc} else {layout_subtree_with_frozen_root_geometry(&entry_run);}- finish_entry_pass(entry_run_state, &entry_fragments, &callbacks, false)+ finish_entry_pass(entry_run_state, &entry_fragments, &callbacks, scratch, false)},)}diff --git a/Libraries/LibWeb/Rust/src/layout/layout_pass.rs b/Libraries/LibWeb/Rust/src/layout/layout_pass.rsindex 5953d91154b..01437616252 100644--- a/Libraries/LibWeb/Rust/src/layout/layout_pass.rs+++ b/Libraries/LibWeb/Rust/src/layout/layout_pass.rs@@ -17,6 +17,9 @@ use super::*;////// A pass holds no part of the host. Every host call takes the main thread token, which a pass is/// not handed, so a pass has no way to call into the document.+///+/// Each run is handed a copy of its own, through which the code inside the run reaches what the run+/// keeps for it, so no state that the runs share tells which run is in progress.#[derive(Clone, Copy)]pub(crate) struct LayoutPass<'arena> {arena: &'arena LayoutNodeArena,@@ -37,6 +40,17 @@ pub(crate) struct LayoutPass<'arena> {/// Whether this pass view is inside a run whose result is discarded, which reads no memoized layout from before it/// and keeps none: the run cache, the in-flow block cache, inline item stashes and intrinsic sizes.reads_no_memoized_layout: bool,+ /// The innermost of the position options the boxes this pass view lays out are laid out with.+ position_option_styles: Option<&'arena PositionOptionStyle<'arena>>,+}++/// The style a box is laid out with in place of the style of its row: that of one of its position options. It links to+/// the position option the box is laid out inside of, if any.+/// https://drafts.csswg.org/css-anchor-position-1/#apply-a-position-option+struct PositionOptionStyle<'pass> {+ node: Node,+ style: super::node_data::StylePayloadsRef,+ enclosing: Option<&'pass PositionOptionStyle<'pass>>,}impl<'arena> LayoutPass<'arena> {@@ -56,6 +70,7 @@ impl<'arena> LayoutPass<'arena> {host_findings: &scratch.host_findings_outside_runs,intrinsic_size_caches: &scratch.intrinsic_size_caches,reads_no_memoized_layout: false,+ position_option_styles: None,}}@@ -104,11 +119,6 @@ impl<'arena> LayoutPass<'arena> {self.arena}- /// The layout stage's own scratch, which the runs of the pass share.- pub(crate) fn scratch(&self) -> &'arena super::run_state::LayoutScratch {- self.scratch- }-/// Lays out `run`, whose result is discarded, with this pass as that run sees it: it reads no memoized layout from/// before it and keeps none, so that it computes everything afresh and leaves nothing for the runs after it. It/// measures intrinsic sizes into caches of its own, which start empty and are dropped when it returns.@@ -122,6 +132,40 @@ impl<'arena> LayoutPass<'arena> {})}+ /// Runs `run` with this pass as it lays out `node` with `style`, the style of one of its position options, which+ /// the style reads of `node` see in place of the style of its row. The payloads `style` names must stay alive+ /// until `run` returns.+ pub(crate) fn with_position_option_style<R>(+ self,+ node: Node,+ style: super::node_data::StylePayloadsRef,+ run: impl for<'option> FnOnce(LayoutPass<'option>) -> R,+ ) -> R {+ let position_option_style = PositionOptionStyle {+ node,+ style,+ enclosing: self.position_option_styles,+ };+ let pass: LayoutPass<'_> = self;+ run(LayoutPass {+ position_option_styles: Some(&position_option_style),+ ..pass+ })+ }++ /// The style the style reads of `node` see in place of the style of its row, if any.+ #[inline]+ fn position_option_style(&self, node: Node) -> Option<super::node_data::StylePayloadsRef> {+ let mut position_option_style = self.position_option_styles;+ while let Some(option) = position_option_style {+ if option.node == node {+ return Some(option.style);+ }+ position_option_style = option.enclosing;+ }+ None+ }+/// Whether this pass view is inside a run whose result is discarded, which reads no memoized layout from before it/// and keeps none.pub(crate) fn reads_no_memoized_layout(&self) -> bool {@@ -196,8 +240,8 @@ impl<'arena> LayoutPass<'arena> {/// The style of `node`: that of its row, or the position option it is being laid out with.#[inline]pub(crate) fn style_payloads_if_styled(&self, node: Node) -> Option<&'arena FfiStylePayloads> {- if let Some(style) = self.scratch.position_option_style(node) {- // SAFETY: A position option's style record stays pinned while a box is laid out with it.+ if let Some(style) = self.position_option_style(node) {+ // SAFETY: A position option's style stays alive while a box is laid out with it.return unsafe { super::node_data::style_payloads(style) };}self.arena.style_payloads(node)diff --git a/Libraries/LibWeb/Rust/src/layout/run_state.rs b/Libraries/LibWeb/Rust/src/layout/run_state.rsindex 6f2e94ada28..fd8cdfaf11c 100644--- a/Libraries/LibWeb/Rust/src/layout/run_state.rs+++ b/Libraries/LibWeb/Rust/src/layout/run_state.rs@@ -11,15 +11,10 @@ use super::*;/// keeps for later passes.pub(crate) struct LayoutScratch {/// The intrinsic sizes passes measured, kept for the passes after them.- pub(crate) intrinsic_size_caches: super::layout_node_arena::IntrinsicSizeCaches,+ pub(super) intrinsic_size_caches: super::layout_node_arena::IntrinsicSizeCaches,/// The inline items a block container generated, kept for its later runs in the same pass. The/// items borrow fonts for the current pass, so the stashes are cleared when the pass ends.inline_item_stashes: RefCell<HashMap<NodeSlotId, super::inline_level_iterator::StashedInlineItems>>,- /// The styles of the boxes being laid out with one of their position options, which the style reads of each box- /// see in place of the style of its row.- /// https://drafts.csswg.org/css-anchor-position-1/#apply-a-position-option- position_option_styles: RefCell<Vec<(NodeSlotId, super::node_data::StylePayloadsRef)>>,- has_position_option_styles: Cell<bool>,/// What the pass found out for the host outside any run, which its driver reports itself.pub(super) host_findings_outside_runs: RefCell<HostFindings>,}@@ -92,41 +87,12 @@ impl Default for LayoutScratch {Self {intrinsic_size_caches: Default::default(),inline_item_stashes: RefCell::new(HashMap::default()),- position_option_styles: RefCell::new(Vec::new()),- has_position_option_styles: Cell::new(false),host_findings_outside_runs: RefCell::new(HostFindings::default()),}}}impl LayoutScratch {- /// Makes the style reads of `node` see `style` until [`Self::pop_position_option_style`].- pub(crate) fn push_position_option_style(&self, node: NodeSlotId, style: super::node_data::StylePayloadsRef) {- self.position_option_styles.borrow_mut().push((node, style));- self.has_position_option_styles.set(true);- }-- pub(crate) fn pop_position_option_style(&self, node: NodeSlotId) {- let mut styles = self.position_option_styles.borrow_mut();- let popped = styles.pop();- debug_assert!(popped.is_some_and(|(popped, _)| popped == node));- self.has_position_option_styles.set(!styles.is_empty());- }-- /// The style the style reads of `node` see in place of the style of its row, if any.- #[inline]- pub(crate) fn position_option_style(&self, node: NodeSlotId) -> Option<super::node_data::StylePayloadsRef> {- if !self.has_position_option_styles.get() {- return None;- }- self.position_option_styles- .borrow()- .iter()- .rev()- .find(|(styled, _)| *styled == node)- .map(|(_, style)| *style)- }-pub(super) fn store_inline_item_stash(&self,block_container: NodeSlotId,