Skip to content

Truly independent web browser

LibWeb: Return formatting context results from an algorithm trait

Aliaksandr Kalenikd71d3b278886
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index a42f3f72f2d..35001b84521 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -1165,6 +1165,42 @@ pub(crate) fn content_alignment_offset(style: ComputedValuesView<'_>, free_space
.offset(free_space, box_alignment::OverflowSafety::Safe)
}
+impl<'pass> formatting_context::FormattingContextAlgorithm<'pass> for BlockFormattingContext<'pass> {
+ fn lay_out(
+ &mut self,
+ run: &FormattingContextRun<'pass>,
+ input: formatting_context::AlgorithmInput<'_>,
+ ) -> formatting_context::AlgorithmOutput {
+ self.run(run, input.body_input);
+ let laid_out_content_block_size = self.block_size_of_contents(false);
+ let mut result = formatting_context::ChildLayoutResult {
+ automatic_content_block_size: self.automatic_content_block_size(),
+ automatic_line_clamp_max_lines: self
+ .automatic_line_clamp_max_lines()
+ .map(|lines| u32::try_from(lines).unwrap_or(u32::MAX)),
+ baselines: self.derived_baselines_of_root_box(),
+ table_wrapper_content_inline_size: self.table_box_in_wrapper_border_box_inline_size.get(),
+ table_box_in_wrapper_border_box_block_size: self.table_box_in_wrapper_border_box_block_size.get(),
+ ..formatting_context::ChildLayoutResult::default()
+ };
+ let mut root_cells = input.root_cells;
+ // https://w3c.github.io/mathml-core/#layout-of-operators
+ // NB: An operator lays out its text in a block formatting context, unless it is drawn with a stretched or large
+ // glyph.
+ if self.facts(run.box_).display().is_math_inside() {
+ math_formatting_context::layout_operator(run, input.root_input, &mut result, &mut root_cells);
+ }
+ formatting_context::AlgorithmOutput {
+ result,
+ root_cells,
+ // NB: The lines are taken after the last read of the automatic content block size, which reads the last of
+ // them.
+ root_lines: std::mem::take(&mut *self.root_lines.borrow_mut()),
+ laid_out_content_block_size: Some(laid_out_content_block_size),
+ }
+ }
+}
+
impl<'pass> BlockFormattingContext<'pass> {
pub(crate) fn new(
run: &FormattingContextRun<'pass>,
@@ -1288,10 +1324,6 @@ impl<'pass> BlockFormattingContext<'pass> {
reached && !self.laying_out_invisible_line_clamp_content.get()
}
- pub(crate) fn table_box_in_wrapper_border_box_block_size(&self) -> Option<CssPixels> {
- self.table_box_in_wrapper_border_box_block_size.get()
- }
-
#[inline]
fn facts(&self, node: Node) -> NodeFacts<'_> {
NodeFacts::new(&self.run.pass, node)
@@ -1520,16 +1552,10 @@ impl<'pass> BlockFormattingContext<'pass> {
self.derived_baselines_of_root_box.set(baselines);
}
- pub(crate) fn derived_baselines_of_root_box(&self) -> DerivedBaselines {
+ fn derived_baselines_of_root_box(&self) -> DerivedBaselines {
self.derived_baselines_of_root_box.get()
}
- /// Takes the lines laid out in the run's root, which the run finishes. Call this after the last read of
- /// `automatic_content_block_size`, which reads the last of these lines.
- pub(crate) fn take_root_lines(&self) -> used_values::LineDataState {
- std::mem::take(&mut *self.root_lines.borrow_mut())
- }
-
/// Resolves the insets of `node`, an in-flow child of `block_container`, keeps them for the box and returns them.
fn compute_inset(
&self,
@@ -6370,7 +6396,7 @@ impl<'pass> BlockFormattingContext<'pass> {
summary
}
- pub(crate) fn run(&self, run: &FormattingContextRun<'pass>, input: LayoutInput) {
+ fn run(&self, run: &FormattingContextRun<'pass>, input: LayoutInput) {
self.lay_out_root_contents(run, input);
let has_line_clamp_point = self.line_clamp_point_found.get();
if has_line_clamp_point || self.clamped_content_is_scrollable_overflow {
@@ -7141,10 +7167,6 @@ impl<'pass> BlockFormattingContext<'pass> {
CssPixels::default()
}
- pub(crate) fn table_wrapper_content_inline_size(&self) -> Option<CssPixels> {
- self.table_box_in_wrapper_border_box_inline_size.get()
- }
-
fn resolve_automatic_line_clamp_block_size(&self, input: LayoutInput) -> Option<CssPixels> {
if self.root_cells.has_definite_block_size {
return Some(self.root_cells.content_block_size);
@@ -7178,7 +7200,7 @@ impl<'pass> BlockFormattingContext<'pass> {
Some(block_size)
}
- pub(crate) fn automatic_line_clamp_max_lines(&self) -> Option<usize> {
+ fn automatic_line_clamp_max_lines(&self) -> Option<usize> {
// https://drafts.csswg.org/css-overflow-4/#line-clamp-containers
// The auto clamp point will be set to the last possible clamp point such that, for it and all previous
// possible clamp points, the line-clamp container's automatic block size is not greater than the block size
@@ -7192,14 +7214,10 @@ impl<'pass> BlockFormattingContext<'pass> {
.or_else(|| (self.line_clamp_line_count.get() == 0).then_some(0))
}
- pub(crate) fn automatic_content_block_size(&self) -> CssPixels {
+ fn automatic_content_block_size(&self) -> CssPixels {
self.block_size_of_contents(self.run.fragmentainer.is_none())
}
- pub(crate) fn laid_out_content_block_size(&self) -> CssPixels {
- self.block_size_of_contents(false)
- }
-
fn block_size_of_contents(&self, honors_size_containment: bool) -> CssPixels {
if let Some(block_size) = self.multicol_automatic_content_block_size.get() {
return block_size;
diff --git a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
index d7fe7a9ae86..2f52ac1e5b5 100644
--- a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
@@ -453,6 +453,35 @@ impl formatting_context::ChildRunShapes for FlexFormattingContext<'_> {
}
}
+impl<'pass> formatting_context::FormattingContextAlgorithm<'pass> for FlexFormattingContext<'pass> {
+ fn lay_out(
+ &mut self,
+ run: &FormattingContextRun<'pass>,
+ input: formatting_context::AlgorithmInput<'_>,
+ ) -> formatting_context::AlgorithmOutput {
+ self.run(run, input.body_input, input.flex_self_block_size_resolution_space);
+ let container_cells = self.container_cells.get();
+ formatting_context::AlgorithmOutput::of_root(
+ formatting_context::ChildLayoutResult {
+ automatic_content_block_size: container_cells.content_block_size,
+ baselines: self.derived_baselines_of_root_box,
+ in_flow_children_margin_box_block_extent: self.in_flow_items_margin_box_block_extent,
+ ..formatting_context::ChildLayoutResult::default()
+ },
+ container_cells,
+ )
+ }
+
+ fn after_root_is_dimensioned(
+ &self,
+ _run: &FormattingContextRun<'pass>,
+ root_cells_in_own_frame: used_values::RootInputCells,
+ ) -> formatting_context::AfterDimensioning {
+ self.parent_did_dimension(root_cells_in_own_frame);
+ formatting_context::AfterDimensioning::LaysOutAbsposContainedByRoot
+ }
+}
+
impl<'pass> FlexFormattingContext<'pass> {
pub(super) fn new(run: &FormattingContextRun<'pass>, container_cells: used_values::RootInputCells) -> Self {
Self {
@@ -4077,7 +4106,7 @@ impl<'pass> FlexFormattingContext<'pass> {
}
}
- pub(super) fn run(
+ fn run(
&mut self,
run: &FormattingContextRun<'pass>,
layout_input: LayoutInput,
@@ -4343,11 +4372,6 @@ impl<'pass> FlexFormattingContext<'pass> {
Some(block_end - block_start)
}
- /// For a container with a preferred aspect ratio, the block size of the margin boxes of its in-flow children.
- pub(super) fn in_flow_items_margin_box_block_extent(&self) -> Option<CssPixels> {
- self.in_flow_items_margin_box_block_extent
- }
-
// https://drafts.csswg.org/css-flexbox-1/#flex-baselines
fn baseline_of_line(&self, line_index: usize, baseline_set: formatting_context::BaselineSet) -> Option<CssPixels> {
self.baseline_of_line_for_purpose(line_index, baseline_set, formatting_context::BaselinePurpose::Container)
@@ -4417,7 +4441,7 @@ impl<'pass> FlexFormattingContext<'pass> {
}
}
- pub(super) fn parent_did_dimension(&self, root_after_epilogue: used_values::RootInputCells) {
+ fn parent_did_dimension(&self, root_after_epilogue: used_values::RootInputCells) {
// NB: The static positions of the abspos children use the container as the run sized it.
self.container_cells.set(root_after_epilogue);
for child in self.run.pass.children(self.run.box_) {
@@ -4434,19 +4458,6 @@ impl<'pass> FlexFormattingContext<'pass> {
}
}
}
-
- pub(super) fn derived_baselines_of_root_box(&self) -> DerivedBaselines {
- self.derived_baselines_of_root_box
- }
-
- /// The cells of the flex container as this context left them.
- pub(super) fn container_cells(&self) -> used_values::RootInputCells {
- self.container_cells.get()
- }
-
- pub(super) fn automatic_content_block_size(&self) -> CssPixels {
- self.container_cells.content_block_size()
- }
}
fn abs(value: CssPixels) -> CssPixels {
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index 9992f7ceef7..7419cb70ed6 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -1486,22 +1486,6 @@ impl<'pass> FormattingContextRun<'pass> {
}
}
-#[allow(
- clippy::large_enum_variant,
- reason = "held on the stack of the run that lays out its root, where boxing would allocate for every run"
-)]
-enum FormattingContextImplementation<'pass> {
- Block(block_formatting_context::BlockFormattingContext<'pass>),
- Flex(flex_formatting_context::FlexFormattingContext<'pass>),
- Grid(grid_formatting_context::GridFormattingContext<'pass>),
- Table(table_formatting_context::TableFormattingContext<'pass>),
- Svg(svg_formatting_context::SvgFormattingContext<'pass>),
- Math(math_formatting_context::MathFormattingContext<'pass>),
- ReplacedWithChildren,
- InternalReplaced,
- FrameSet,
-}
-
pub(crate) fn formatting_context_type_created_by_node_data(
data: &NodeData,
style: Option<ComputedValuesView<'_>>,
@@ -1587,43 +1571,146 @@ pub(crate) fn formatting_context_type_created_by_box(facts: NodeFacts<'_>) -> Op
fc_type
}
-fn create_formatting_context_implementation<'pass>(
- run: &FormattingContextRun<'pass>,
- root_cells_in_own_frame: used_values::RootInputCells,
- subgrid_parent: Option<&grid_formatting_context::SubgridParent<'pass>>,
- fc_type: FormattingContextType,
- previous_line_data: Option<std::sync::Arc<inline_content::InlineContent>>,
-) -> FormattingContextImplementation<'pass> {
- match fc_type {
- FormattingContextType::Block => FormattingContextImplementation::Block(
- block_formatting_context::BlockFormattingContext::new(run, root_cells_in_own_frame, previous_line_data),
- ),
- FormattingContextType::Flex => FormattingContextImplementation::Flex(
- flex_formatting_context::FlexFormattingContext::new(run, root_cells_in_own_frame),
- ),
- FormattingContextType::Grid => FormattingContextImplementation::Grid(
- grid_formatting_context::GridFormattingContext::new(run, root_cells_in_own_frame, subgrid_parent),
- ),
- FormattingContextType::Table => {
- FormattingContextImplementation::Table(table_formatting_context::TableFormattingContext::new(
+/// What a formatting context algorithm lays out the contents of its root from.
+pub(crate) struct AlgorithmInput<'input> {
+ /// The input of the root's contents, in the frame the root establishes.
+ pub(crate) body_input: LayoutInput,
+ /// The input of the run, in the frame of the root's parent.
+ pub(crate) root_input: &'input LayoutInput,
+ /// The root's cells as the run sized them before its contents, in the frame the root establishes.
+ pub(crate) root_cells: used_values::RootInputCells,
+ /// For a flex root that resolves its own automatic block size, the space it resolves it in.
+ pub(crate) flex_self_block_size_resolution_space: Option<AvailableSpace>,
+ /// For a table box, the inline layout the run of its wrapper found for it.
+ pub(crate) table_inline_layout: Option<table_formatting_context::TableInlineLayout>,
+}
+
+/// What a formatting context algorithm hands the run of its root once it laid out the root's contents.
+pub(crate) struct AlgorithmOutput {
+ pub(crate) result: ChildLayoutResult,
+ /// The root's cells as the algorithm left them, in the frame the root establishes.
+ pub(crate) root_cells: used_values::RootInputCells,
+ pub(crate) root_lines: used_values::LineDataState,
+ /// For a block container, the block size of the contents it laid out, which it aligns in its content box.
+ pub(crate) laid_out_content_block_size: Option<CssPixels>,
+}
+
+impl AlgorithmOutput {
+ /// What an algorithm that laid out no lines hands back of a root it left in `root_cells` with `result`.
+ pub(crate) fn of_root(result: ChildLayoutResult, root_cells: used_values::RootInputCells) -> Self {
+ Self {
+ result,
+ root_cells,
+ root_lines: used_values::LineDataState::Empty,
+ laid_out_content_block_size: None,
+ }
+ }
+}
+
+/// Whether the run of a root goes on to lay out the absolutely positioned boxes the root contains once the root is
+/// dimensioned.
+#[derive(Clone, Copy, PartialEq, Eq)]
+pub(crate) enum AfterDimensioning {
+ LaysOutAbsposContainedByRoot,
+ LaysOutNothingMore,
+}
+
+/// The layout a formatting context gives the contents of the root of its run.
+pub(crate) trait FormattingContextAlgorithm<'pass> {
+ /// Lays out the contents of the root of `run`, which lays them out in the frame the root establishes.
+ fn lay_out(&mut self, run: &FormattingContextRun<'pass>, input: AlgorithmInput<'_>) -> AlgorithmOutput;
+
+ /// Takes in the root's final cells `root_cells_in_own_frame`, once the run dimensioned the root.
+ fn after_root_is_dimensioned(
+ &self,
+ _run: &FormattingContextRun<'pass>,
+ _root_cells_in_own_frame: used_values::RootInputCells,
+ ) -> AfterDimensioning {
+ AfterDimensioning::LaysOutAbsposContainedByRoot
+ }
+}
+
+/// A replaced box's internal formatting context, which lays out nothing inside its root.
+#[derive(Default)]
+pub(crate) struct InternalReplacedFormattingContext;
+
+impl<'pass> FormattingContextAlgorithm<'pass> for InternalReplacedFormattingContext {
+ fn lay_out(&mut self, _run: &FormattingContextRun<'pass>, input: AlgorithmInput<'_>) -> AlgorithmOutput {
+ AlgorithmOutput::of_root(ChildLayoutResult::default(), input.root_cells)
+ }
+
+ fn after_root_is_dimensioned(
+ &self,
+ _run: &FormattingContextRun<'pass>,
+ _root_cells_in_own_frame: used_values::RootInputCells,
+ ) -> AfterDimensioning {
+ AfterDimensioning::LaysOutNothingMore
+ }
+}
+
+/// Where the run of a root keeps the formatting context it creates for the root, one of these.
+#[derive(Default)]
+struct FormattingContextAlgorithms<'pass> {
+ block: Option<block_formatting_context::BlockFormattingContext<'pass>>,
+ flex: Option<flex_formatting_context::FlexFormattingContext<'pass>>,
+ grid: Option<grid_formatting_context::GridFormattingContext<'pass>>,
+ table: Option<table_formatting_context::TableFormattingContext<'pass>>,
+ svg: Option<svg_formatting_context::SvgFormattingContext<'pass>>,
+ math: Option<math_formatting_context::MathFormattingContext<'pass>>,
+ replaced_with_children: replaced_with_children_formatting_context::ReplacedWithChildrenFormattingContext,
+ internal_replaced: InternalReplacedFormattingContext,
+ frameset: frameset_formatting_context::FrameSetFormattingContext,
+}
+
+impl<'pass> FormattingContextAlgorithms<'pass> {
+ fn create(
+ &mut self,
+ run: &FormattingContextRun<'pass>,
+ root_cells_in_own_frame: used_values::RootInputCells,
+ subgrid_parent: Option<&grid_formatting_context::SubgridParent<'pass>>,
+ fc_type: FormattingContextType,
+ previous_line_data: Option<std::sync::Arc<inline_content::InlineContent>>,
+ inline_layout_of_child_table_box: Option<table_formatting_context::TableInlineLayout>,
+ ) -> &mut dyn FormattingContextAlgorithm<'pass> {
+ match fc_type {
+ FormattingContextType::Block => {
+ let context = self.block.insert(block_formatting_context::BlockFormattingContext::new(
+ run,
+ root_cells_in_own_frame,
+ previous_line_data,
+ ));
+ if let Some(layout) = inline_layout_of_child_table_box {
+ context.keep_table_inline_layout_of(layout.table_box(), layout);
+ }
+ context
+ }
+ FormattingContextType::Flex => self.flex.insert(flex_formatting_context::FlexFormattingContext::new(
+ run,
+ root_cells_in_own_frame,
+ )),
+ FormattingContextType::Grid => self.grid.insert(grid_formatting_context::GridFormattingContext::new(
+ run,
+ root_cells_in_own_frame,
+ subgrid_parent,
+ )),
+ FormattingContextType::Table => self.table.insert(table_formatting_context::TableFormattingContext::new(
run,
root_cells_in_own_frame,
table_formatting_context::AutoTableInlineSize::FillsAvailableInlineSize,
- ))
- }
- FormattingContextType::Svg => {
- FormattingContextImplementation::Svg(svg_formatting_context::SvgFormattingContext::new(run))
+ )),
+ FormattingContextType::Svg => self.svg.insert(svg_formatting_context::SvgFormattingContext::new(run)),
+ FormattingContextType::Math => self.math.insert(math_formatting_context::MathFormattingContext::new(
+ run,
+ root_cells_in_own_frame,
+ )),
+ FormattingContextType::ReplacedWithChildren => &mut self.replaced_with_children,
+ FormattingContextType::InternalReplaced => &mut self.internal_replaced,
+ FormattingContextType::FrameSet => &mut self.frameset,
}
- FormattingContextType::Math => FormattingContextImplementation::Math(
- math_formatting_context::MathFormattingContext::new(run, root_cells_in_own_frame),
- ),
- FormattingContextType::ReplacedWithChildren => FormattingContextImplementation::ReplacedWithChildren,
- FormattingContextType::InternalReplaced => FormattingContextImplementation::InternalReplaced,
- FormattingContextType::FrameSet => FormattingContextImplementation::FrameSet,
}
}
-fn register_table_abspos_descendants(run: &FormattingContextRun, parent: Node) {
+pub(crate) fn register_table_abspos_descendants(run: &FormattingContextRun, parent: Node) {
for child in run.pass.children(parent) {
let facts = NodeFacts::new(&run.pass, child);
if facts.is_box() {
@@ -2465,7 +2552,7 @@ fn lay_out_run_root_and_contents<'pass>(
run
};
// NB: A table wrapper's run hands the inline layout of its table box to the run of that box.
- let (mut inline_layout_of_root_table_box, inline_layout_of_child_table_box) = match table_inline_layout {
+ let (inline_layout_of_root_table_box, inline_layout_of_child_table_box) = match table_inline_layout {
Some(table_inline_layout) if table_inline_layout.table_box() == box_ => (Some(table_inline_layout), None),
table_inline_layout => (None, table_inline_layout),
};
@@ -2501,118 +2588,40 @@ fn lay_out_run_root_and_contents<'pass>(
flex_self_block_size_resolution_space = None;
}
- let mut implementation = None;
- let mut root_lines = used_values::LineDataState::Empty;
- let mut laid_out_content_block_size = None;
- let (mut result, own_root_after_body) =
- if empty_root_needs_no_formatting_context_algorithm(&pass, box_, fc_type, input, request.mode) {
- // An empty root context has no body output. Root sizing and finalization still
- // run through the shared paths around this branch.
- (
- result_of_empty_root(&pass, box_, fc_type, own_root.get()),
- own_root.get(),
- )
- } else {
- let context_implementation = implementation.insert(create_formatting_context_implementation(
- body_run,
- own_root.get(),
- subgrid_parent,
- fc_type,
- previous_line_data,
- ));
- if let (Some(layout), FormattingContextImplementation::Block(context)) =
- (inline_layout_of_child_table_box, &*context_implementation)
- {
- context.keep_table_inline_layout_of(layout.table_box(), layout);
- }
- match context_implementation {
- FormattingContextImplementation::Block(context) => {
- context.run(body_run, body_input);
- laid_out_content_block_size = Some(context.laid_out_content_block_size());
- let mut result = ChildLayoutResult {
- automatic_content_block_size: context.automatic_content_block_size(),
- automatic_line_clamp_max_lines: context
- .automatic_line_clamp_max_lines()
- .map(|lines| u32::try_from(lines).unwrap_or(u32::MAX)),
- baselines: context.derived_baselines_of_root_box(),
- table_wrapper_content_inline_size: context.table_wrapper_content_inline_size(),
- table_box_in_wrapper_border_box_block_size: context
- .table_box_in_wrapper_border_box_block_size(),
- ..ChildLayoutResult::default()
- };
- let mut root_after_body = own_root.get();
- // https://w3c.github.io/mathml-core/#layout-of-operators
- // NB: An operator lays out its text in a block formatting context, unless it is drawn with a
- // stretched or large glyph.
- if NodeFacts::new(&body_run.pass, box_).display().is_math_inside() {
- math_formatting_context::layout_operator(body_run, input, &mut result, &mut root_after_body);
- }
- root_lines = context.take_root_lines();
- (result, root_after_body)
- }
- FormattingContextImplementation::Flex(context) => {
- context.run(body_run, body_input, flex_self_block_size_resolution_space);
- let baselines = context.derived_baselines_of_root_box();
- let result = ChildLayoutResult {
- automatic_content_block_size: context.automatic_content_block_size(),
- baselines,
- in_flow_children_margin_box_block_extent: context.in_flow_items_margin_box_block_extent(),
- ..ChildLayoutResult::default()
- };
- (result, context.container_cells())
- }
- FormattingContextImplementation::Grid(context) => {
- context.run(body_run, body_input);
- let baselines = context.derived_baselines_of_root_box();
- let result = ChildLayoutResult {
- automatic_content_block_size: context.automatic_content_block_size(),
- baselines,
- in_flow_children_margin_box_block_extent: context.in_flow_items_margin_box_block_extent(),
- ..ChildLayoutResult::default()
- };
- (result, own_root.get())
- }
- FormattingContextImplementation::Table(context) => {
- context.run(body_run, body_input, inline_layout_of_root_table_box.take());
- let baselines = context.derived_baselines_of_root_box();
- let result = ChildLayoutResult {
- automatic_content_block_size: context.automatic_content_block_size,
- baselines,
- ..ChildLayoutResult::default()
- };
- (result, context.table_box_cells())
- }
- FormattingContextImplementation::Svg(context) => (
- ChildLayoutResult::default(),
- context.run(body_run, body_input, own_root.get()),
- ),
- FormattingContextImplementation::Math(context) => {
- context.run(body_run, body_input);
- let baselines = context.derived_baselines_of_root_box();
- let result = ChildLayoutResult {
- automatic_content_block_size: context.automatic_content_block_size(),
- baselines,
- ..ChildLayoutResult::default()
- };
- (result, context.root_cells())
- }
- FormattingContextImplementation::ReplacedWithChildren => {
- let mut root_after_body = own_root.get();
- let result = replaced_with_children_formatting_context::layout_replaced_with_children(
- body_run,
- body_input,
- &mut root_after_body,
- );
- (result, root_after_body)
- }
- FormattingContextImplementation::InternalReplaced => (ChildLayoutResult::default(), own_root.get()),
- FormattingContextImplementation::FrameSet => {
- let mut root_after_body = own_root.get();
- let result = frameset_formatting_context::layout_frameset(body_run, &mut root_after_body);
- (result, root_after_body)
- }
- }
- };
+ let mut algorithms = FormattingContextAlgorithms::default();
+ let mut algorithm = None;
+ let AlgorithmOutput {
+ mut result,
+ root_cells: own_root_after_body,
+ mut root_lines,
+ laid_out_content_block_size,
+ } = if empty_root_needs_no_formatting_context_algorithm(&pass, box_, fc_type, input, request.mode) {
+ // An empty root context has no body output. Root sizing and finalization still
+ // run through the shared paths around this branch.
+ AlgorithmOutput::of_root(
+ result_of_empty_root(&pass, box_, fc_type, own_root.get()),
+ own_root.get(),
+ )
+ } else {
+ let algorithm = algorithm.insert(algorithms.create(
+ body_run,
+ own_root.get(),
+ subgrid_parent,
+ fc_type,
+ previous_line_data,
+ inline_layout_of_child_table_box,
+ ));
+ algorithm.lay_out(
+ body_run,
+ AlgorithmInput {
+ body_input,
+ root_input: input,
+ root_cells: own_root.get(),
+ flex_self_block_size_resolution_space,
+ table_inline_layout: inline_layout_of_root_table_box,
+ },
+ )
+ };
let baselines_in_own_frame = result.baselines;
// https://drafts.csswg.org/css-sizing-4/#intrinsic-size-override
// If an element has an explicit intrinsic inner size in an axis, then after laying out the element as normal for
@@ -2808,36 +2817,19 @@ fn lay_out_run_root_and_contents<'pass>(
);
return (outputs, root_cells_left_by_run);
}
- if let Some(implementation) = &implementation {
- match implementation {
- FormattingContextImplementation::Block(_) => {}
- FormattingContextImplementation::Table(_) => {
- register_table_abspos_descendants(body_run, run.box_);
- }
- FormattingContextImplementation::Flex(context) => {
- context.parent_did_dimension(root_in_own_frame_left_by_run);
- }
- FormattingContextImplementation::Grid(context) => {
- context.parent_did_dimension(root_in_own_frame_left_by_run);
- }
- FormattingContextImplementation::Math(context) => {
- context.parent_did_dimension(root_in_own_frame_left_by_run);
- }
- FormattingContextImplementation::Svg(_)
- | FormattingContextImplementation::ReplacedWithChildren
- | FormattingContextImplementation::FrameSet => {}
- FormattingContextImplementation::InternalReplaced => {
- let outputs = run.outputs(
- result,
- take_run_fragments(),
- root_cells_left_by_run,
- root_line_data,
- inputs_never_observed,
- parent_owns_root_input_cells,
- );
- return (outputs, root_cells_left_by_run);
- }
- }
+ if algorithm.as_ref().is_some_and(|algorithm| {
+ algorithm.after_root_is_dimensioned(body_run, root_in_own_frame_left_by_run)
+ == AfterDimensioning::LaysOutNothingMore
+ }) {
+ let outputs = run.outputs(
+ result,
+ take_run_fragments(),
+ root_cells_left_by_run,
+ root_line_data,
+ inputs_never_observed,
+ parent_owns_root_input_cells,
+ );
+ return (outputs, root_cells_left_by_run);
}
// NB: The absolutely positioned boxes whose containing block is the root are laid out against its final sizes and
// box sides, in the frame its contents are laid out in, so that the root's fragment holds them. The columns of
diff --git a/Libraries/LibWeb/Rust/src/layout/frameset_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/frameset_formatting_context.rs
index 66d6247cd0a..562894ba19b 100644
--- a/Libraries/LibWeb/Rust/src/layout/frameset_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/frameset_formatting_context.rs
@@ -230,7 +230,23 @@ fn layout_frameset_child(
}
// https://html.spec.whatwg.org/multipage/rendering.html#frames-and-framesets
-pub(super) fn layout_frameset(
+/// The formatting context of a frameset, which lays its frames out in the grid of its rows and columns.
+#[derive(Default)]
+pub(crate) struct FrameSetFormattingContext;
+
+impl<'pass> formatting_context::FormattingContextAlgorithm<'pass> for FrameSetFormattingContext {
+ fn lay_out(
+ &mut self,
+ run: &formatting_context::FormattingContextRun<'pass>,
+ input: formatting_context::AlgorithmInput<'_>,
+ ) -> formatting_context::AlgorithmOutput {
+ let mut root = input.root_cells;
+ let result = layout_frameset(run, &mut root);
+ formatting_context::AlgorithmOutput::of_root(result, root)
+ }
+}
+
+fn layout_frameset(
run: &formatting_context::FormattingContextRun,
root: &mut used_values::RootInputCells,
) -> formatting_context::ChildLayoutResult {
diff --git a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
index 50f2fce30d2..683651b9a05 100644
--- a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
@@ -1299,6 +1299,34 @@ pub(super) fn grid_template_declares_a_subgrid_axis(pass: &LayoutPass<'_>, box_:
grid_style.template_columns.is_subgrid || grid_style.template_rows.is_subgrid
}
+impl<'pass> formatting_context::FormattingContextAlgorithm<'pass> for GridFormattingContext<'pass> {
+ fn lay_out(
+ &mut self,
+ run: &FormattingContextRun<'pass>,
+ input: formatting_context::AlgorithmInput<'_>,
+ ) -> formatting_context::AlgorithmOutput {
+ self.run(run, input.body_input);
+ formatting_context::AlgorithmOutput::of_root(
+ formatting_context::ChildLayoutResult {
+ automatic_content_block_size: self.automatic_content_block_size(),
+ baselines: self.derived_baselines_of_root_box(),
+ in_flow_children_margin_box_block_extent: self.in_flow_items_margin_box_block_extent,
+ ..formatting_context::ChildLayoutResult::default()
+ },
+ input.root_cells,
+ )
+ }
+
+ fn after_root_is_dimensioned(
+ &self,
+ _run: &FormattingContextRun<'pass>,
+ root_cells_in_own_frame: used_values::RootInputCells,
+ ) -> formatting_context::AfterDimensioning {
+ self.parent_did_dimension(root_cells_in_own_frame);
+ formatting_context::AfterDimensioning::LaysOutAbsposContainedByRoot
+ }
+}
+
impl<'pass> GridFormattingContext<'pass> {
pub(crate) fn new(
run: &FormattingContextRun<'pass>,
@@ -5208,10 +5236,6 @@ impl<'pass> GridFormattingContext<'pass> {
}
/// For a container with a preferred aspect ratio, the block size of the margin boxes of its in-flow children.
- pub(crate) fn in_flow_items_margin_box_block_extent(&self) -> Option<CssPixels> {
- self.in_flow_items_margin_box_block_extent
- }
-
// https://drafts.csswg.org/css-grid-2/#grid-baselines
fn baseline_of_items(&self, baseline_set: formatting_context::BaselineSet) -> Option<CssPixels> {
if let Some(lanes) = self.lanes
@@ -5505,7 +5529,7 @@ impl<'pass> GridFormattingContext<'pass> {
self.track_sum(Axis::Row)
}
- pub(crate) fn run(&mut self, run: &FormattingContextRun<'pass>, input: LayoutInput) {
+ fn run(&mut self, run: &FormattingContextRun<'pass>, input: LayoutInput) {
let available = input.available_space;
let grid_style = self.place_items_and_size_tracks(input, true);
@@ -5558,15 +5582,15 @@ impl<'pass> GridFormattingContext<'pass> {
self.save_devtools_data(grid_style);
}
- pub(crate) fn derived_baselines_of_root_box(&self) -> DerivedBaselines {
+ fn derived_baselines_of_root_box(&self) -> DerivedBaselines {
self.derived_baselines_of_root_box
}
- pub(crate) fn automatic_content_block_size(&self) -> CssPixels {
+ fn automatic_content_block_size(&self) -> CssPixels {
self.automatic_content_block_size
}
- pub(crate) fn parent_did_dimension(&self, root_after_epilogue: used_values::RootInputCells) {
+ fn parent_did_dimension(&self, root_after_epilogue: used_values::RootInputCells) {
// NB: The static positions and containing blocks of the abspos children use the container as the run sized it.
self.container_cells.set(root_after_epilogue);
// https://drafts.csswg.org/css-grid-2/#static-position
diff --git a/Libraries/LibWeb/Rust/src/layout/math_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/math_formatting_context.rs
index 1247ffb672c..f0946e5f830 100644
--- a/Libraries/LibWeb/Rust/src/layout/math_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/math_formatting_context.rs
@@ -1470,6 +1470,33 @@ pub(super) struct MathFormattingContext<'pass> {
root_cells: Cell<used_values::RootInputCells>,
}
+impl<'pass> formatting_context::FormattingContextAlgorithm<'pass> for MathFormattingContext<'pass> {
+ fn lay_out(
+ &mut self,
+ run: &FormattingContextRun<'pass>,
+ input: formatting_context::AlgorithmInput<'_>,
+ ) -> formatting_context::AlgorithmOutput {
+ self.run(run, input.body_input);
+ formatting_context::AlgorithmOutput::of_root(
+ ChildLayoutResult {
+ automatic_content_block_size: self.automatic_content_block_size(),
+ baselines: self.derived_baselines_of_root_box,
+ ..ChildLayoutResult::default()
+ },
+ self.root_cells(),
+ )
+ }
+
+ fn after_root_is_dimensioned(
+ &self,
+ _run: &FormattingContextRun<'pass>,
+ root_cells_in_own_frame: used_values::RootInputCells,
+ ) -> formatting_context::AfterDimensioning {
+ self.parent_did_dimension(root_cells_in_own_frame);
+ formatting_context::AfterDimensioning::LaysOutAbsposContainedByRoot
+ }
+}
+
impl<'pass> MathFormattingContext<'pass> {
#[track_caller]
fn placed_child(&self, child: Node) -> formatting_context::PlacedChild {
@@ -2207,7 +2234,7 @@ impl<'pass> MathFormattingContext<'pass> {
}
}
- pub(super) fn run(&mut self, run: &FormattingContextRun<'pass>, layout_input: LayoutInput) {
+ fn run(&mut self, run: &FormattingContextRun<'pass>, layout_input: LayoutInput) {
let root = run.box_;
let kind = self.kind(root);
let constraints = run.sizing().constraints_for_child_context(
@@ -2642,7 +2669,7 @@ impl<'pass> MathFormattingContext<'pass> {
}
}
- pub(super) fn parent_did_dimension(&self, root_after_epilogue: used_values::RootInputCells) {
+ fn parent_did_dimension(&self, root_after_epilogue: used_values::RootInputCells) {
for child in self.run.pass.children(self.run.box_) {
let facts = self.facts(child);
if !facts.is_box() || !facts.is_absolutely_positioned() {
@@ -2671,11 +2698,7 @@ impl<'pass> MathFormattingContext<'pass> {
}
}
- pub(super) fn derived_baselines_of_root_box(&self) -> DerivedBaselines {
- self.derived_baselines_of_root_box
- }
-
- pub(super) fn automatic_content_block_size(&self) -> CssPixels {
+ fn automatic_content_block_size(&self) -> CssPixels {
self.content.ascent + self.content.descent
}
}
diff --git a/Libraries/LibWeb/Rust/src/layout/replaced_with_children_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/replaced_with_children_formatting_context.rs
index 650e1c1600b..436364fba39 100644
--- a/Libraries/LibWeb/Rust/src/layout/replaced_with_children_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/replaced_with_children_formatting_context.rs
@@ -6,7 +6,23 @@
use super::*;
-pub(super) fn layout_replaced_with_children(
+/// The formatting context of a replaced box with children, whose shadow content lays out in an anonymous wrapper.
+#[derive(Default)]
+pub(crate) struct ReplacedWithChildrenFormattingContext;
+
+impl<'pass> formatting_context::FormattingContextAlgorithm<'pass> for ReplacedWithChildrenFormattingContext {
+ fn lay_out(
+ &mut self,
+ run: &formatting_context::FormattingContextRun<'pass>,
+ input: formatting_context::AlgorithmInput<'_>,
+ ) -> formatting_context::AlgorithmOutput {
+ let mut root = input.root_cells;
+ let result = layout_replaced_with_children(run, input.body_input, &mut root);
+ formatting_context::AlgorithmOutput::of_root(result, root)
+ }
+}
+
+fn layout_replaced_with_children(
run: &formatting_context::FormattingContextRun,
layout_input: geometry::LayoutInput,
root: &mut used_values::RootInputCells,
diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
index 0f61cc48d03..5daa4c7e24c 100644
--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
@@ -598,6 +598,18 @@ pub(super) struct SvgFormattingContext<'pass> {
current_text_position: FfiFloatPoint,
}
+impl<'pass> formatting_context::FormattingContextAlgorithm<'pass> for SvgFormattingContext<'pass> {
+ fn lay_out(
+ &mut self,
+ run: &FormattingContextRun<'pass>,
+ input: formatting_context::AlgorithmInput<'_>,
+ ) -> formatting_context::AlgorithmOutput {
+ let root = Cell::new(input.root_cells);
+ self.lay_out_contents(run, input.body_input, &root);
+ formatting_context::AlgorithmOutput::of_root(formatting_context::ChildLayoutResult::default(), root.get())
+ }
+}
+
impl<'pass> SvgFormattingContext<'pass> {
pub(super) fn new(run: &FormattingContextRun<'pass>) -> Self {
Self::new_nested(run, run.box_, false)
@@ -1938,19 +1950,6 @@ impl<'pass> SvgFormattingContext<'pass> {
.find(|&child| self.node_kind(child) == kind)
}
- /// Lays out the run's root from `root_cells`, which hold it in the frame it lays its contents out in, and returns
- /// the root's cells after the layout.
- pub(super) fn run(
- &mut self,
- run: &FormattingContextRun<'pass>,
- input: LayoutInput,
- root_cells: used_values::RootInputCells,
- ) -> used_values::RootInputCells {
- let root = Cell::new(root_cells);
- self.lay_out_contents(run, input, &root);
- root.get()
- }
-
/// Lays out the contents of this context's box, which is laid out in `cells`.
fn lay_out_contents(
&mut self,
diff --git a/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs
index 7c30f22a2b8..38f8440a04c 100644
--- a/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs
@@ -1160,6 +1160,33 @@ impl TableFormattingContext<'_> {
}
}
+impl<'pass> formatting_context::FormattingContextAlgorithm<'pass> for TableFormattingContext<'pass> {
+ fn lay_out(
+ &mut self,
+ run: &FormattingContextRun<'pass>,
+ input: formatting_context::AlgorithmInput<'_>,
+ ) -> formatting_context::AlgorithmOutput {
+ self.run(run, input.body_input, input.table_inline_layout);
+ formatting_context::AlgorithmOutput::of_root(
+ formatting_context::ChildLayoutResult {
+ automatic_content_block_size: self.automatic_content_block_size,
+ baselines: self.derived_baselines_of_root_box(),
+ ..formatting_context::ChildLayoutResult::default()
+ },
+ self.table_box_cells(),
+ )
+ }
+
+ fn after_root_is_dimensioned(
+ &self,
+ run: &FormattingContextRun<'pass>,
+ _root_cells_in_own_frame: used_values::RootInputCells,
+ ) -> formatting_context::AfterDimensioning {
+ formatting_context::register_table_abspos_descendants(run, run.box_);
+ formatting_context::AfterDimensioning::LaysOutAbsposContainedByRoot
+ }
+}
+
impl<'pass> TableFormattingContext<'pass> {
fn node_facts(&self, node: Node) -> NodeFacts<'_> {
NodeFacts::new(&self.run.pass, node)
@@ -3813,12 +3840,7 @@ impl<'pass> TableFormattingContext<'pass> {
});
}
- pub(super) fn run(
- &mut self,
- run: &FormattingContextRun<'pass>,
- input: LayoutInput,
- inline_layout: Option<TableInlineLayout>,
- ) {
+ fn run(&mut self, run: &FormattingContextRun<'pass>, input: LayoutInput, inline_layout: Option<TableInlineLayout>) {
self.available_space = input.available_space;
self.min_border_box_block_size_from_flex_item = input.sizing.forced_min_border_box_block_size.get();
self.table_box_content_block_offset_in_wrapper = input
@@ -3865,7 +3887,7 @@ impl<'pass> TableFormattingContext<'pass> {
self.automatic_content_block_size = self.table_block_size;
}
- pub(super) fn derived_baselines_of_root_box(&self) -> DerivedBaselines {
+ fn derived_baselines_of_root_box(&self) -> DerivedBaselines {
self.derived_baselines_of_root_box.get()
}
}