Remove SizeKeyword, merge it into Size (#33844)

It's not really useful to have it.

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
This commit is contained in:
Oriol Brufau 2024-10-15 12:30:22 +02:00 committed by GitHub
parent c37fb2e453
commit 564478ef0d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 36 additions and 45 deletions

View file

@ -622,10 +622,10 @@ impl ToLogicalWithContainingBlock<LogicalRect<Au>> for PhysicalRect<Au> {
} }
} }
/// The possible keywords accepted by the sizing properties. /// The possible values accepted by the sizing properties.
/// <https://drafts.csswg.org/css-sizing/#sizing-properties> /// <https://drafts.csswg.org/css-sizing/#sizing-properties>
#[derive(Clone)] #[derive(Clone)]
pub(crate) enum SizeKeyword { pub(crate) enum Size<T> {
/// Represents an `auto` value for the preferred and minimum size properties, /// Represents an `auto` value for the preferred and minimum size properties,
/// or `none` for the maximum size properties. /// or `none` for the maximum size properties.
/// <https://drafts.csswg.org/css-sizing/#valdef-width-auto> /// <https://drafts.csswg.org/css-sizing/#valdef-width-auto>
@ -639,38 +639,32 @@ pub(crate) enum SizeKeyword {
FitContent, FitContent,
/// <https://drafts.csswg.org/css-sizing-4/#valdef-width-stretch> /// <https://drafts.csswg.org/css-sizing-4/#valdef-width-stretch>
Stretch, Stretch,
} /// Represents a numeric `<length-percentage>`, but resolved as a `T`.
/// <https://drafts.csswg.org/css-sizing/#valdef-width-length-percentage-0>
/// The possible values accepted by the sizing properties,
/// with numeric `<length-percentage>` resolved as a `T`.
/// <https://drafts.csswg.org/css-sizing/#sizing-properties>
#[derive(Clone)]
pub(crate) enum Size<T> {
Keyword(SizeKeyword),
Numeric(T), Numeric(T),
} }
impl<T> Default for Size<T> { impl<T> Default for Size<T> {
#[inline] #[inline]
fn default() -> Self { fn default() -> Self {
Self::Keyword(SizeKeyword::Initial) Self::Initial
} }
} }
impl<T> Size<T> { impl<T> Size<T> {
#[inline] #[inline]
pub(crate) fn fit_content() -> Self { pub(crate) fn fit_content() -> Self {
Self::Keyword(SizeKeyword::FitContent) Self::FitContent
} }
#[inline] #[inline]
pub(crate) fn is_keyword(&self) -> bool { pub(crate) fn is_keyword(&self) -> bool {
matches!(self, Self::Keyword(_)) !matches!(self, Self::Numeric(_))
} }
#[inline] #[inline]
pub(crate) fn is_initial(&self) -> bool { pub(crate) fn is_initial(&self) -> bool {
matches!(self, Self::Keyword(SizeKeyword::Initial)) matches!(self, Self::Initial)
} }
} }
@ -678,8 +672,8 @@ impl<T: Clone> Size<T> {
#[inline] #[inline]
pub(crate) fn to_numeric(&self) -> Option<T> { pub(crate) fn to_numeric(&self) -> Option<T> {
match self { match self {
Self::Keyword(_) => None,
Self::Numeric(numeric) => Some(numeric).cloned(), Self::Numeric(numeric) => Some(numeric).cloned(),
_ => None,
} }
} }
@ -692,7 +686,11 @@ impl<T: Clone> Size<T> {
#[inline] #[inline]
pub fn map<U>(&self, f: impl FnOnce(T) -> U) -> Size<U> { pub fn map<U>(&self, f: impl FnOnce(T) -> U) -> Size<U> {
match self { match self {
Size::Keyword(keyword) => Size::Keyword(keyword.clone()), Size::Initial => Size::Initial,
Size::MinContent => Size::MinContent,
Size::MaxContent => Size::MaxContent,
Size::FitContent => Size::FitContent,
Size::Stretch => Size::Stretch,
Size::Numeric(numeric) => Size::Numeric(f(numeric.clone())), Size::Numeric(numeric) => Size::Numeric(f(numeric.clone())),
} }
} }
@ -700,8 +698,8 @@ impl<T: Clone> Size<T> {
#[inline] #[inline]
pub fn maybe_map<U>(&self, f: impl FnOnce(T) -> Option<U>) -> Option<Size<U>> { pub fn maybe_map<U>(&self, f: impl FnOnce(T) -> Option<U>) -> Option<Size<U>> {
Some(match self { Some(match self {
Size::Keyword(keyword) => Size::Keyword(keyword.clone()),
Size::Numeric(numeric) => Size::Numeric(f(numeric.clone())?), Size::Numeric(numeric) => Size::Numeric(f(numeric.clone())?),
_ => self.map(|_| unreachable!("This shouldn't be called for keywords")),
}) })
} }
} }
@ -710,11 +708,11 @@ impl From<StyleSize> for Size<LengthPercentage> {
fn from(size: StyleSize) -> Self { fn from(size: StyleSize) -> Self {
match size { match size {
StyleSize::LengthPercentage(length) => Size::Numeric(length.0), StyleSize::LengthPercentage(length) => Size::Numeric(length.0),
StyleSize::Auto => Size::Keyword(SizeKeyword::Initial), StyleSize::Auto => Size::Initial,
StyleSize::MinContent => Size::Keyword(SizeKeyword::MinContent), StyleSize::MinContent => Size::MinContent,
StyleSize::MaxContent => Size::Keyword(SizeKeyword::MaxContent), StyleSize::MaxContent => Size::MaxContent,
StyleSize::FitContent => Size::Keyword(SizeKeyword::FitContent), StyleSize::FitContent => Size::FitContent,
StyleSize::Stretch => Size::Keyword(SizeKeyword::Stretch), StyleSize::Stretch => Size::Stretch,
} }
} }
} }
@ -723,11 +721,11 @@ impl From<StyleMaxSize> for Size<LengthPercentage> {
fn from(max_size: StyleMaxSize) -> Self { fn from(max_size: StyleMaxSize) -> Self {
match max_size { match max_size {
StyleMaxSize::LengthPercentage(length) => Size::Numeric(length.0), StyleMaxSize::LengthPercentage(length) => Size::Numeric(length.0),
StyleMaxSize::None => Size::Keyword(SizeKeyword::Initial), StyleMaxSize::None => Size::Initial,
StyleMaxSize::MinContent => Size::Keyword(SizeKeyword::MinContent), StyleMaxSize::MinContent => Size::MinContent,
StyleMaxSize::MaxContent => Size::Keyword(SizeKeyword::MaxContent), StyleMaxSize::MaxContent => Size::MaxContent,
StyleMaxSize::FitContent => Size::Keyword(SizeKeyword::FitContent), StyleMaxSize::FitContent => Size::FitContent,
StyleMaxSize::Stretch => Size::Keyword(SizeKeyword::Stretch), StyleMaxSize::Stretch => Size::Stretch,
} }
} }
} }
@ -803,13 +801,11 @@ impl Size<Au> {
get_content_size: &mut impl FnMut() -> ContentSizes, get_content_size: &mut impl FnMut() -> ContentSizes,
) -> Option<Au> { ) -> Option<Au> {
match self { match self {
Self::Keyword(SizeKeyword::Initial) => None, Self::Initial => None,
Self::Keyword(SizeKeyword::MinContent) => Some(get_content_size().min_content), Self::MinContent => Some(get_content_size().min_content),
Self::Keyword(SizeKeyword::MaxContent) => Some(get_content_size().max_content), Self::MaxContent => Some(get_content_size().max_content),
Self::Keyword(SizeKeyword::FitContent) => { Self::FitContent => Some(get_content_size().shrink_to_fit(stretch_size)),
Some(get_content_size().shrink_to_fit(stretch_size)) Self::Stretch => Some(stretch_size),
},
Self::Keyword(SizeKeyword::Stretch) => Some(stretch_size),
Self::Numeric(numeric) => Some(*numeric), Self::Numeric(numeric) => Some(*numeric),
} }
} }
@ -826,13 +822,8 @@ impl Size<Au> {
#[inline] #[inline]
pub(crate) fn maybe_resolve_extrinsic(&self, stretch_size: Option<Au>) -> Option<Au> { pub(crate) fn maybe_resolve_extrinsic(&self, stretch_size: Option<Au>) -> Option<Au> {
match self { match self {
Self::Keyword(keyword) => match keyword { Self::Initial | Self::MinContent | Self::MaxContent | Self::FitContent => None,
SizeKeyword::Initial | Self::Stretch => stretch_size,
SizeKeyword::MinContent |
SizeKeyword::MaxContent |
SizeKeyword::FitContent => None,
SizeKeyword::Stretch => stretch_size,
},
Self::Numeric(numeric) => Some(*numeric), Self::Numeric(numeric) => Some(*numeric),
} }
} }

View file

@ -29,7 +29,7 @@ use crate::dom_traversal::Contents;
use crate::fragment_tree::FragmentFlags; use crate::fragment_tree::FragmentFlags;
use crate::geom::{ use crate::geom::{
AuOrAuto, LengthPercentageOrAuto, LogicalSides, LogicalVec2, PhysicalSides, PhysicalSize, AuOrAuto, LengthPercentageOrAuto, LogicalSides, LogicalVec2, PhysicalSides, PhysicalSize,
PhysicalVec, Size, SizeKeyword, PhysicalVec, Size,
}; };
use crate::{ContainingBlock, IndefiniteContainingBlock}; use crate::{ContainingBlock, IndefiniteContainingBlock};
@ -528,7 +528,7 @@ impl ComputedValuesExt for ComputedValues {
// TODO: We are assuming that Size::Initial doesn't stretch. However, it may actually // TODO: We are assuming that Size::Initial doesn't stretch. However, it may actually
// stretch flex and grid items depending on the CSS Align properties, in that case // stretch flex and grid items depending on the CSS Align properties, in that case
// the caller needs to take care of it. // the caller needs to take care of it.
Size::Keyword(SizeKeyword::Stretch) => true, Size::Stretch => true,
Size::Numeric(length_percentage) => length_percentage.has_percentage(), Size::Numeric(length_percentage) => length_percentage.has_percentage(),
_ => false, _ => false,
} }

View file

@ -35,7 +35,7 @@ use crate::fragment_tree::{
}; };
use crate::geom::{ use crate::geom::{
AuOrAuto, LogicalRect, LogicalSides, LogicalVec2, PhysicalPoint, PhysicalRect, PhysicalSides, AuOrAuto, LogicalRect, LogicalSides, LogicalVec2, PhysicalPoint, PhysicalRect, PhysicalSides,
Size, SizeKeyword, ToLogical, ToLogicalWithContainingBlock, Size, ToLogical, ToLogicalWithContainingBlock,
}; };
use crate::positioned::{relative_adjustement, PositioningContext, PositioningContextLength}; use crate::positioned::{relative_adjustement, PositioningContext, PositioningContextLength};
use crate::sizing::{ContentSizes, InlineContentSizesResult}; use crate::sizing::{ContentSizes, InlineContentSizesResult};
@ -252,7 +252,7 @@ impl<'a> TableLayout<'a> {
TableLayoutMode::Fixed && TableLayoutMode::Fixed &&
!matches!( !matches!(
self.table.style.box_size(writing_mode).inline, self.table.style.box_size(writing_mode).inline,
Size::Keyword(SizeKeyword::Initial | SizeKeyword::MaxContent) Size::Initial | Size::MaxContent
); );
let row_measures = vec![LogicalVec2::zero(); self.table.size.width]; let row_measures = vec![LogicalVec2::zero(); self.table.size.width];
self.cell_measures = vec![row_measures; self.table.size.height]; self.cell_measures = vec![row_measures; self.table.size.height];