style: Part 2: Support fit-content() in style

Support fit-content for preferred size, min size, and max size. This
patch only implement the style system. For layout part, we will do that
in the following patches.

Differential Revision: https://phabricator.services.mozilla.com/D107161
This commit is contained in:
Boris Chiou 2023-05-21 22:29:27 +02:00 committed by Oriol Brufau
parent 77cab0edc3
commit d103785c4b
3 changed files with 34 additions and 1 deletions

View file

@ -188,7 +188,8 @@ impl Size {
GenericSize::MinContent | GenericSize::MinContent |
GenericSize::MaxContent | GenericSize::MaxContent |
GenericSize::MozFitContent | GenericSize::MozFitContent |
GenericSize::MozAvailable => false GenericSize::MozAvailable |
GenericSize::FitContentFunction(_) => false
} }
} }
} }

View file

@ -161,6 +161,9 @@ pub enum GenericSize<LengthPercent> {
#[cfg(feature = "gecko")] #[cfg(feature = "gecko")]
#[animation(error)] #[animation(error)]
MozAvailable, MozAvailable,
#[animation(error)]
#[css(function = "fit-content")]
FitContentFunction(LengthPercent)
} }
pub use self::GenericSize as Size; pub use self::GenericSize as Size;
@ -215,6 +218,9 @@ pub enum GenericMaxSize<LengthPercent> {
#[cfg(feature = "gecko")] #[cfg(feature = "gecko")]
#[animation(error)] #[animation(error)]
MozAvailable, MozAvailable,
#[animation(error)]
#[css(function = "fit-content")]
FitContentFunction(LengthPercent),
} }
pub use self::GenericMaxSize as MaxSize; pub use self::GenericMaxSize as MaxSize;

View file

@ -1247,6 +1247,30 @@ macro_rules! parse_size_non_length {
}}; }};
} }
#[cfg(feature = "gecko")]
fn is_fit_content_function_enabled() -> bool {
static_prefs::pref!("layout.css.fit-content-function.enabled")
}
#[cfg(feature = "servo")]
fn is_fit_content_function_enabled() -> bool {
false
}
macro_rules! parse_fit_content_function {
($size:ident, $input:expr, $context:expr, $allow_quirks:expr) => {
if is_fit_content_function_enabled() {
if let Ok(length) = $input.try_parse(|input| {
input.expect_function_matching("fit-content")?;
input.parse_nested_block(|i| {
NonNegativeLengthPercentage::parse_quirky($context, i, $allow_quirks)
})
}) {
return Ok($size::FitContentFunction(length));
}
}
};
}
impl Size { impl Size {
/// Parses, with quirks. /// Parses, with quirks.
pub fn parse_quirky<'i, 't>( pub fn parse_quirky<'i, 't>(
@ -1255,6 +1279,7 @@ impl Size {
allow_quirks: AllowQuirks, allow_quirks: AllowQuirks,
) -> Result<Self, ParseError<'i>> { ) -> Result<Self, ParseError<'i>> {
parse_size_non_length!(Size, input, "auto" => Auto); parse_size_non_length!(Size, input, "auto" => Auto);
parse_fit_content_function!(Size, input, context, allow_quirks);
let length = NonNegativeLengthPercentage::parse_quirky(context, input, allow_quirks)?; let length = NonNegativeLengthPercentage::parse_quirky(context, input, allow_quirks)?;
Ok(GenericSize::LengthPercentage(length)) Ok(GenericSize::LengthPercentage(length))
@ -1287,6 +1312,7 @@ impl MaxSize {
allow_quirks: AllowQuirks, allow_quirks: AllowQuirks,
) -> Result<Self, ParseError<'i>> { ) -> Result<Self, ParseError<'i>> {
parse_size_non_length!(MaxSize, input, "none" => None); parse_size_non_length!(MaxSize, input, "none" => None);
parse_fit_content_function!(MaxSize, input, context, allow_quirks);
let length = NonNegativeLengthPercentage::parse_quirky(context, input, allow_quirks)?; let length = NonNegativeLengthPercentage::parse_quirky(context, input, allow_quirks)?;
Ok(GenericMaxSize::LengthPercentage(length)) Ok(GenericMaxSize::LengthPercentage(length))