/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
//! Generic types for CSS values related to length.
usecrate::parser::{Parse, ParserContext}; usecrate::values::generics::box_::PositionProperty; usecrate::values::generics::Optional; usecrate::values::DashedIdent; #[cfg(feature = "gecko")] usecrate::Zero; use cssparser::Parser; use std::fmt::Write; use style_traits::ParseError; use style_traits::StyleParseErrorKind; use style_traits::ToCss; use style_traits::{CssWriter, SpecifiedValueInfo};
/// Whether this is the `auto` value. #[inline] pubfn is_auto(&self) -> bool {
matches!(*self, LengthPercentageOrAuto::Auto)
}
/// A helper function to parse this with quirks or not and so forth. pubfn parse_with<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
parser: impl FnOnce(
&ParserContext,
&mut Parser<'i, 't>,
) -> Result<LengthPercentage, ParseError<'i>>,
) -> Result<Self, ParseError<'i>> { if input.try_parse(|i| i.expect_ident_matching("auto")).is_ok() { return Ok(LengthPercentageOrAuto::Auto);
}
/// A generic `<length>` | `<number>` value for the `tab-size` property. #[derive(
Animate,
Clone,
ComputeSquaredDistance,
Copy,
Debug,
MallocSizeOf,
Parse,
PartialEq,
SpecifiedValueInfo,
ToAnimatedValue,
ToAnimatedZero,
ToComputedValue,
ToCss,
ToResolvedValue,
ToShmem,
)] #[repr(C, u8)] pubenum GenericLengthOrNumber<L, N> { /// A number. /// /// NOTE: Numbers need to be before lengths, in order to parse them /// first, since `0` should be a number, not the `0px` length.
Number(N), /// A length.
Length(L),
}
pubuseself::GenericLengthOrNumber as LengthOrNumber;
impl<L, N: Zero> Zero for LengthOrNumber<L, N> { fn zero() -> Self {
LengthOrNumber::Number(Zero::zero())
}
pubuseself::GenericLengthPercentageOrNormal as LengthPercentageOrNormal;
impl<LengthPercent> LengthPercentageOrNormal<LengthPercent> { /// Returns the normal value. #[inline] pubfn normal() -> Self {
LengthPercentageOrNormal::Normal
}
}
/// Anchor size function used by sizing, margin and inset properties. /// This resolves to the size of the anchor at computed time. /// /// https://drafts.csswg.org/css-anchor-position-1/#funcdef-anchor-size #[derive(
Animate,
Clone,
ComputeSquaredDistance,
Debug,
MallocSizeOf,
PartialEq,
SpecifiedValueInfo,
ToShmem,
ToAnimatedValue,
ToAnimatedZero,
ToComputedValue,
ToResolvedValue,
Serialize,
Deserialize,
)] #[repr(C)] pubstruct GenericAnchorSizeFunction<LengthPercentage> { /// Anchor name of the element to anchor to. /// If omitted (i.e. empty), selects the implicit anchor element. #[animation(constant)] pub target_element: DashedIdent, /// Size of the positioned element, expressed in that of the anchor element. /// If omitted, defaults to the axis of the property the function is used in. pub size: AnchorSizeKeyword, /// Value to use in case the anchor function is invalid. pub fallback: Optional<LengthPercentage>,
}
/// Result of resolving an anchor function. pubenum AnchorResolutionResult<'a, LengthPercentage> { /// Function resolved to a valid anchor.
Resolved(LengthPercentage), /// Referenced anchor is invalid, but fallback is used.
Fallback(&'a LengthPercentage), /// Referenced anchor is invalid.
Invalid,
}
impl<'a, LengthPercentage> AnchorResolutionResult<'a, LengthPercentage> { /// Return result for an invalid anchor function, depending on if it has any fallback. pubfn new_anchor_invalid(fallback: Option<&'a LengthPercentage>) -> Self { iflet Some(fb) = fallback { returnSelf::Fallback(fb);
} Self::Invalid
}
}
impl<LengthPercentage> GenericAnchorSizeFunction<LengthPercentage>
{ /// Parse the inner part of `anchor-size()`, after the parser has consumed "anchor-size(". pubfn parse_inner<'i, 't, F>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
f: F,
) -> Result<Self, ParseError<'i>> where
F: FnOnce(&mut Parser<'i, '_>) -> Result<LengthPercentage, ParseError<'i>>,
{
input.parse_nested_block(|i| { letmut target_element = i
.try_parse(|i| DashedIdent::parse(context, i))
.unwrap_or(DashedIdent::empty()); let size = i.try_parse(AnchorSizeKeyword::parse).unwrap_or(AnchorSizeKeyword::None); if target_element.is_empty() {
target_element = i
.try_parse(|i| DashedIdent::parse(context, i))
.unwrap_or(DashedIdent::empty());
} let previous_parsed = !target_element.is_empty() || size != AnchorSizeKeyword::None; let fallback = i
.try_parse(|i| { if previous_parsed {
i.expect_comma()?;
}
f(i)
})
.ok();
Ok(GenericAnchorSizeFunction {
target_element,
size: size.into(),
fallback: fallback.into(),
})
})
}
/// Resolve the anchor size function. On failure, return reference to fallback, if exists. pubfn resolve<'a>(
&'a self,
position_property: PositionProperty,
) -> AnchorResolutionResult<'a, LengthPercentage> { if !position_property.is_absolutely_positioned() { return AnchorResolutionResult::new_anchor_invalid(self.fallback.as_ref());
}
// TODO(dshin): Do the actual anchor resolution here.
AnchorResolutionResult::new_anchor_invalid(self.fallback.as_ref())
}
}
/// Keyword values for the anchor size function. #[derive(
Animate,
Clone,
ComputeSquaredDistance,
Copy,
Debug,
MallocSizeOf,
PartialEq,
Parse,
SpecifiedValueInfo,
ToCss,
ToShmem,
ToAnimatedValue,
ToAnimatedZero,
ToComputedValue,
ToResolvedValue,
Serialize,
Deserialize,
)] #[repr(u8)] pubenum AnchorSizeKeyword { /// Magic value for nothing. #[css(skip)]
None, /// Width of the anchor element.
Width, /// Height of the anchor element.
Height, /// Block size of the anchor element.
Block, /// Inline size of the anchor element.
Inline, /// Same as `Block`, resolved against the positioned element's writing mode.
SelfBlock, /// Same as `Inline`, resolved against the positioned element's writing mode.
SelfInline,
}
/// Specified type for `margin` properties, which allows /// the use of the `anchor-size()` function. #[derive(
Animate,
Clone,
ComputeSquaredDistance,
Debug,
MallocSizeOf,
PartialEq,
ToCss,
ToShmem,
ToAnimatedValue,
ToAnimatedZero,
ToComputedValue,
ToResolvedValue,
)] #[repr(C)] pubenum GenericMargin<LP> { /// A `<length-percentage>` value.
LengthPercentage(LP), /// An `auto` value.
Auto, /// Margin size defined by the anchor element. /// /// https://drafts.csswg.org/css-anchor-position-1/#funcdef-anchor-size
AnchorSizeFunction( #[animation(field_bound)] #[distance(field_bound)] Box<GenericAnchorSizeFunction<LP>>,
),
}
impl<LP> SpecifiedValueInfo for GenericMargin<LP> where
LP: SpecifiedValueInfo,
{ fn collect_completion_keywords(f: style_traits::KeywordsCollectFn) {
LP::collect_completion_keywords(f);
f(&["auto"]); if static_prefs::pref!("layout.css.anchor-positioning.enabled") {
f(&["anchor-size"]);
}
}
}
impl<LP> Zero for GenericMargin<LP> where
LP: Zero,
{ fn is_zero(&self) -> bool { matchself { Self::LengthPercentage(l) => l.is_zero(), Self::Auto | Self::AnchorSizeFunction(_) => false,
}
}
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.13Bemerkung:
(vorverarbeitet am 2026-06-18)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.