use crate::style::{
StyleError, box_values::unsupported, component::Component, declaration, grammar::Keyword,
grammar::Property,
};
const STACKING: &[Property] = &[
Property::Opacity,
Property::Transform,
Property::OffsetPath,
Property::Translate,
Property::Rotate,
Property::Scale,
Property::Perspective,
Property::TransformStyle,
Property::Filter,
Property::BackdropFilter,
Property::MixBlendMode,
Property::Isolation,
Property::MaskImage,
Property::MaskBorderSource,
Property::ClipPath,
Property::Contain,
Property::ContainerType,
Property::ContentVisibility,
Property::Position,
Property::ViewTransitionName,
];
const BACKDROP_ROOT: &[Property] = &[
Property::Filter,
Property::Opacity,
Property::MaskImage,
Property::MaskBorderSource,
Property::ClipPath,
Property::BackdropFilter,
Property::MixBlendMode,
];
const FIXED_CONTAINING: &[Property] = &[
Property::Transform,
Property::OffsetPath,
Property::Translate,
Property::Rotate,
Property::Scale,
Property::Perspective,
Property::TransformStyle,
Property::Filter,
Property::BackdropFilter,
Property::Contain,
Property::ContainerType,
Property::ContentVisibility,
];
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, serde::Serialize)]
pub(crate) struct WillChange {
pub(crate) stacking_context: bool,
pub(crate) backdrop_root: bool,
pub(crate) z_index: bool,
pub(crate) absolute_containing_block: bool,
pub(crate) fixed_containing_block: bool,
}
impl WillChange {
pub(super) fn from_component(value: &Component) -> Result<Self, StyleError> {
let mut hints = Self::default();
if value.keyword() == Some(Keyword::Auto) {
return Ok(hints);
}
for feature in value.items() {
match feature.unwrap() {
Component::Keyword(Keyword::ScrollPosition | Keyword::Contents) => {}
Component::Ident(name) => {
let Some(property) = Property::from_name(&name.to_ascii_lowercase()) else {
continue;
};
for longhand in declaration::longhands(property) {
hints.stacking_context |= STACKING.contains(&longhand);
hints.backdrop_root |= BACKDROP_ROOT.contains(&longhand);
hints.z_index |= longhand == Property::ZIndex;
hints.fixed_containing_block |= FIXED_CONTAINING.contains(&longhand);
hints.absolute_containing_block |=
FIXED_CONTAINING.contains(&longhand) || longhand == Property::Position;
}
}
_ => return Err(unsupported(feature)),
}
}
Ok(hints)
}
}