From b2b3ea992c2f045a49d7264df18f5f85b2c913fb Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Tue, 26 Nov 2019 11:22:23 +0100 Subject: Make IndependentFormattingContext a struct that owns styles MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … and has a private enum for its contents. Privacy forces the rest of the code to go through methods rather than matching on the enum, reducing accidental layout-mode-specific behavior. --- components/layout_2020/lib.rs | 89 ++----------------------------------------- 1 file changed, 4 insertions(+), 85 deletions(-) (limited to 'components/layout_2020/lib.rs') diff --git a/components/layout_2020/lib.rs b/components/layout_2020/lib.rs index c0892553f35..06db3b65acb 100644 --- a/components/layout_2020/lib.rs +++ b/components/layout_2020/lib.rs @@ -12,16 +12,13 @@ #[macro_use] extern crate serde; -use style::properties::ComputedValues; -use style::values::computed::{Length, LengthOrAuto}; -use style::Zero; - pub mod context; pub mod data; pub mod display_list; mod dom_traversal; mod element_data; mod flow; +mod formatting_contexts; mod fragments; mod geom; mod opaque_node; @@ -43,87 +40,9 @@ use crate::replaced::ReplacedContent; use crate::style_ext::{ComputedValuesExt, Direction, DisplayInside, Position, WritingMode}; use servo_arc::Arc; use std::convert::TryInto; -use style::context::SharedStyleContext; - -/// https://drafts.csswg.org/css-display/#independent-formatting-context -#[derive(Debug)] -enum IndependentFormattingContext { - Flow(BlockFormattingContext), - - // Not called FC in specs, but behaves close enough - Replaced(ReplacedContent), - // Other layout modes go here -} - -enum NonReplacedIFC<'a> { - Flow(&'a BlockFormattingContext), -} - -impl IndependentFormattingContext { - fn construct<'dom, 'style>( - context: &SharedStyleContext<'style>, - style: &Arc, - display_inside: DisplayInside, - contents: Contents>, - ) -> Self { - match contents.try_into() { - Ok(non_replaced) => match display_inside { - DisplayInside::Flow | DisplayInside::FlowRoot => { - IndependentFormattingContext::Flow(BlockFormattingContext::construct( - context, - style, - non_replaced, - )) - }, - }, - Err(replaced) => IndependentFormattingContext::Replaced(replaced), - } - } - - fn as_replaced(&self) -> Result<&ReplacedContent, NonReplacedIFC> { - match self { - IndependentFormattingContext::Replaced(r) => Ok(r), - IndependentFormattingContext::Flow(f) => Err(NonReplacedIFC::Flow(f)), - } - } - - fn layout<'a>( - &'a self, - layout_context: &LayoutContext, - containing_block: &ContainingBlock, - tree_rank: usize, - absolutely_positioned_fragments: &mut Vec>, - ) -> FlowChildren { - match self.as_replaced() { - Ok(replaced) => match *replaced {}, - Err(ifc) => ifc.layout( - layout_context, - containing_block, - tree_rank, - absolutely_positioned_fragments, - ), - } - } -} - -impl<'a> NonReplacedIFC<'a> { - fn layout( - &self, - layout_context: &LayoutContext, - containing_block: &ContainingBlock, - tree_rank: usize, - absolutely_positioned_fragments: &mut Vec>, - ) -> FlowChildren { - match self { - NonReplacedIFC::Flow(bfc) => bfc.layout( - layout_context, - containing_block, - tree_rank, - absolutely_positioned_fragments, - ), - } - } -} +use style::properties::ComputedValues; +use style::values::computed::{Length, LengthOrAuto}; +use style::Zero; struct ContainingBlock { inline_size: Length, -- cgit v1.2.3 From 80eec48d373f89415e95e01ab9aaf6d07ac01c3e Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Tue, 26 Nov 2019 11:24:53 +0100 Subject: Use std::mem::take instead of defining it --- components/layout_2020/lib.rs | 9 --------- 1 file changed, 9 deletions(-) (limited to 'components/layout_2020/lib.rs') diff --git a/components/layout_2020/lib.rs b/components/layout_2020/lib.rs index 06db3b65acb..f1b3c308be2 100644 --- a/components/layout_2020/lib.rs +++ b/components/layout_2020/lib.rs @@ -81,12 +81,3 @@ fn relative_adjustement( block: adjust(box_offsets.block_start, box_offsets.block_end), } } - -// FIXME: use std::mem::take when it’s stable. -// https://github.com/rust-lang/rust/issues/61129 -fn take(x: &mut T) -> T -where - T: Default, -{ - std::mem::replace(x, Default::default()) -} -- cgit v1.2.3 From 858bc5aca681fae70ce640d70d5fa80f7edd3dc9 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Tue, 26 Nov 2019 16:21:11 +0100 Subject: Split FlowChildren in IndependentLayout and FlowLayout The result of doing the layout of an independent formatting context should be unconcerned with margin collapsing. --- components/layout_2020/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'components/layout_2020/lib.rs') diff --git a/components/layout_2020/lib.rs b/components/layout_2020/lib.rs index f1b3c308be2..07956417087 100644 --- a/components/layout_2020/lib.rs +++ b/components/layout_2020/lib.rs @@ -33,7 +33,7 @@ pub use flow::{BoxTreeRoot, FragmentTreeRoot}; use crate::context::LayoutContext; use crate::dom_traversal::{Contents, NodeExt}; -use crate::flow::{BlockFormattingContext, FlowChildren}; +use crate::flow::BlockFormattingContext; use crate::geom::flow_relative::Vec2; use crate::positioned::AbsolutelyPositionedFragment; use crate::replaced::ReplacedContent; -- cgit v1.2.3