diff options
author | Martin Robinson <mrobinson@igalia.com> | 2024-06-21 12:13:31 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-21 10:13:31 +0000 |
commit | 66edef806579fd0b386f4ceba473e6a9f7d0ca34 (patch) | |
tree | 830c4bb55c77f933ccd7808c91491c76c3ee85b1 /components/layout_2020 | |
parent | 44064b14392838fd7da148000b58c9a3cc07d4e7 (diff) | |
download | servo-66edef806579fd0b386f4ceba473e6a9f7d0ca34.tar.gz servo-66edef806579fd0b386f4ceba473e6a9f7d0ca34.zip |
layout: Simplify `Contents` a little (#32487)
Instead of duplicating some of `NonReplacedContents` in `Contents`,
divide it into either replaced and non-replaced content, since this is
how the layout system processes `Contents` always. In addition, stop
using `TryInto` to match replaced or non-replaced contents, as it is
quite confusing to handle an `Err` as a success case.
Diffstat (limited to 'components/layout_2020')
-rw-r--r-- | components/layout_2020/dom_traversal.rs | 47 | ||||
-rw-r--r-- | components/layout_2020/flow/construct.rs | 10 | ||||
-rw-r--r-- | components/layout_2020/flow/root.rs | 6 | ||||
-rw-r--r-- | components/layout_2020/formatting_contexts.rs | 8 |
4 files changed, 32 insertions, 39 deletions
diff --git a/components/layout_2020/dom_traversal.rs b/components/layout_2020/dom_traversal.rs index 10ae9ae3978..7d9be02634e 100644 --- a/components/layout_2020/dom_traversal.rs +++ b/components/layout_2020/dom_traversal.rs @@ -122,20 +122,19 @@ where #[derive(Debug)] pub(super) enum Contents { - /// Refers to a DOM subtree, plus `::before` and `::after` pseudo-elements. - OfElement, - + /// Any kind of content that is not replaced, including the contents of pseudo-elements. + NonReplaced(NonReplacedContents), /// Example: an `<img src=…>` element. /// <https://drafts.csswg.org/css2/conform.html#replaced-element> Replaced(ReplacedContent), - - /// Content of a `::before` or `::after` pseudo-element that is being generated. - /// <https://drafts.csswg.org/css2/generate.html#content> - OfPseudoElement(Vec<PseudoElementContentItem>), } +#[derive(Debug)] pub(super) enum NonReplacedContents { + /// Refers to a DOM subtree, plus `::before` and `::after` pseudo-elements. OfElement, + /// Content of a `::before` or `::after` pseudo-element that is being generated. + /// <https://drafts.csswg.org/css2/generate.html#content> OfPseudoElement(Vec<PseudoElementContentItem>), } @@ -221,7 +220,8 @@ fn traverse_element<'dom, Node>( } }, Display::GeneratingBox(display) => { - let contents = replaced.map_or(Contents::OfElement, Contents::Replaced); + let contents = + replaced.map_or(NonReplacedContents::OfElement.into(), Contents::Replaced); let display = display.used_value_for_contents(&contents); let box_slot = element.element_box_slot(); let info = NodeAndStyleInfo::new(element, style); @@ -251,7 +251,7 @@ fn traverse_pseudo_element<'dom, Node>( Display::GeneratingBox(display) => { let items = generate_pseudo_element_content(&info.style, element, context); let box_slot = element.pseudo_element_box_slot(which); - let contents = Contents::OfPseudoElement(items); + let contents = NonReplacedContents::OfPseudoElement(items).into(); handler.handle_element(&info, display, contents, box_slot); }, } @@ -310,30 +310,25 @@ fn traverse_pseudo_element_contents<'dom, Node>( impl Contents { /// Returns true iff the `try_from` impl below would return `Err(_)` pub fn is_replaced(&self) -> bool { - match self { - Contents::OfElement | Contents::OfPseudoElement(_) => false, - Contents::Replaced(_) => true, - } + matches!(self, Contents::Replaced(_)) } } -impl std::convert::TryFrom<Contents> for NonReplacedContents { - type Error = ReplacedContent; - - fn try_from(contents: Contents) -> Result<Self, Self::Error> { - match contents { - Contents::OfElement => Ok(NonReplacedContents::OfElement), - Contents::OfPseudoElement(items) => Ok(NonReplacedContents::OfPseudoElement(items)), - Contents::Replaced(replaced) => Err(replaced), - } +impl From<NonReplacedContents> for Contents { + fn from(non_replaced_contents: NonReplacedContents) -> Self { + Contents::NonReplaced(non_replaced_contents) } } -impl From<NonReplacedContents> for Contents { - fn from(contents: NonReplacedContents) -> Self { +impl std::convert::TryFrom<Contents> for NonReplacedContents { + type Error = &'static str; + + fn try_from(contents: Contents) -> Result<Self, Self::Error> { match contents { - NonReplacedContents::OfElement => Contents::OfElement, - NonReplacedContents::OfPseudoElement(items) => Contents::OfPseudoElement(items), + Contents::NonReplaced(non_replaced_contents) => Ok(non_replaced_contents), + Contents::Replaced(_) => { + Err("Tried to covnert a `Contents::Replaced` into `NonReplacedContent`") + }, } } } diff --git a/components/layout_2020/flow/construct.rs b/components/layout_2020/flow/construct.rs index 5a53bbd12d9..986ae3e46d7 100644 --- a/components/layout_2020/flow/construct.rs +++ b/components/layout_2020/flow/construct.rs @@ -3,7 +3,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ use std::borrow::Cow; -use std::convert::{TryFrom, TryInto}; +use std::convert::TryFrom; use rayon::iter::{IntoParallelIterator, ParallelIterator}; use servo_arc::Arc; @@ -401,7 +401,7 @@ where DisplayInside::Flow { is_list_item: false, }, - Contents::OfPseudoElement(contents), + NonReplacedContents::OfPseudoElement(contents).into(), BoxSlot::dummy(), ); } @@ -494,8 +494,8 @@ where } let propagated_text_decoration_line = self.text_decoration_line; - let kind = match contents.try_into() { - Ok(contents) => match display_inside { + let kind = match contents { + Contents::NonReplaced(contents) => match display_inside { DisplayInside::Flow { is_list_item } if !info.style.establishes_block_formatting_context() => { @@ -513,7 +513,7 @@ where propagated_text_decoration_line, }, }, - Err(contents) => { + Contents::Replaced(contents) => { let contents = Contents::Replaced(contents); BlockLevelCreator::Independent { display_inside, diff --git a/components/layout_2020/flow/root.rs b/components/layout_2020/flow/root.rs index cea4063592c..8a1865621cb 100644 --- a/components/layout_2020/flow/root.rs +++ b/components/layout_2020/flow/root.rs @@ -18,7 +18,7 @@ use webrender_traits::display_list::ScrollSensitivity; use crate::cell::ArcRefCell; use crate::context::LayoutContext; use crate::dom::{LayoutBox, NodeExt}; -use crate::dom_traversal::{iter_child_nodes, Contents, NodeAndStyleInfo}; +use crate::dom_traversal::{iter_child_nodes, Contents, NodeAndStyleInfo, NonReplacedContents}; use crate::flexbox::FlexLevelBox; use crate::flow::float::FloatBox; use crate::flow::inline::InlineItem; @@ -206,7 +206,7 @@ impl BoxTree { loop { if let Some((primary_style, display_inside, update_point)) = update_point(dirty_node) { let contents = ReplacedContent::for_element(dirty_node, context) - .map_or(Contents::OfElement, Contents::Replaced); + .map_or_else(|| NonReplacedContents::OfElement.into(), Contents::Replaced); let info = NodeAndStyleInfo::new(dirty_node, Arc::clone(&primary_style)); let out_of_flow_absolutely_positioned_box = ArcRefCell::new( AbsolutelyPositionedBox::construct(context, &info, display_inside, contents), @@ -264,7 +264,7 @@ fn construct_for_root_element<'dom>( }; let contents = ReplacedContent::for_element(root_element, context) - .map_or(Contents::OfElement, Contents::Replaced); + .map_or_else(|| NonReplacedContents::OfElement.into(), Contents::Replaced); let root_box = if box_style.position.is_absolutely_positioned() { BlockLevelBox::OutOfFlowAbsolutelyPositionedBox(ArcRefCell::new( AbsolutelyPositionedBox::construct(context, &info, display_inside, contents), diff --git a/components/layout_2020/formatting_contexts.rs b/components/layout_2020/formatting_contexts.rs index 1b15baa7eca..f3c51e9579b 100644 --- a/components/layout_2020/formatting_contexts.rs +++ b/components/layout_2020/formatting_contexts.rs @@ -2,8 +2,6 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -use std::convert::TryInto; - use app_units::Au; use serde::Serialize; use servo_arc::Arc; @@ -92,8 +90,8 @@ impl IndependentFormattingContext { contents: Contents, propagated_text_decoration_line: TextDecorationLine, ) -> Self { - match contents.try_into() { - Ok(non_replaced_contents) => { + match contents { + Contents::NonReplaced(non_replaced_contents) => { let contents = match display_inside { DisplayInside::Flow { is_list_item } | DisplayInside::FlowRoot { is_list_item } => { @@ -131,7 +129,7 @@ impl IndependentFormattingContext { contents, }) }, - Err(contents) => { + Contents::Replaced(contents) => { let mut base_fragment_info: BaseFragmentInfo = node_and_style_info.into(); base_fragment_info.flags.insert(FragmentFlags::IS_REPLACED); Self::Replaced(ReplacedFormattingContext { |