use crate::style::{
Display, Style, StyleError, box_values::unsupported, component::Component, grammar::Keyword,
units::Relative,
};
use serde::Serialize;
use std::sync::Arc;
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize)]
pub(crate) struct Contain {
pub size: bool,
pub inline_size: bool,
pub layout: bool,
pub style: bool,
pub paint: bool,
}
impl Contain {
const LAYOUT: usize = 1;
const STYLE: usize = 2;
const PAINT: usize = 3;
pub(super) fn from_component(value: &Component) -> Result<Self, StyleError> {
let content = Self {
layout: true,
style: true,
paint: true,
..Self::default()
};
match value.keyword() {
Some(Keyword::None) => return Ok(Self::default()),
Some(Keyword::Strict) => {
return Ok(Self {
size: true,
..content
});
}
Some(Keyword::Content) => return Ok(content),
Some(_) => return Err(unsupported(value)),
None => {}
}
let sizes = value.slot(0);
Ok(Self {
size: sizes.and_then(|sizes| sizes.slot(0)).is_some(),
inline_size: sizes.and_then(|sizes| sizes.slot(1)).is_some(),
layout: value.slot(Self::LAYOUT).is_some(),
style: value.slot(Self::STYLE).is_some(),
paint: value.slot(Self::PAINT).is_some(),
})
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize)]
pub(crate) struct ContainerType {
pub size: bool,
pub inline_size: bool,
pub scroll_state: bool,
}
impl ContainerType {
pub(super) fn from_component(value: &Component) -> Self {
if value.keyword() == Some(Keyword::Normal) {
return Self::default();
}
let axes = value.slot(0).and_then(Component::keyword);
Self {
size: axes == Some(Keyword::Size),
inline_size: axes == Some(Keyword::InlineSize),
scroll_state: value.slot(1).is_some(),
}
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize)]
pub(crate) enum ContentVisibility {
#[default]
Visible,
Auto,
Hidden,
}
impl ContentVisibility {
pub(super) fn from_component(value: &Component) -> Result<Self, StyleError> {
match value.keyword() {
Some(Keyword::Visible) => Ok(Self::Visible),
Some(Keyword::Auto) => Ok(Self::Auto),
Some(Keyword::Hidden) => Ok(Self::Hidden),
_ => Err(unsupported(value)),
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize)]
pub(crate) struct ContainIntrinsicSize {
pub auto: bool,
pub length: Option<f32>,
}
impl ContainIntrinsicSize {
pub(super) fn from_component(
value: &Component,
relative: &Relative<'_>,
) -> Result<Self, StyleError> {
if value.keyword() == Some(Keyword::None) {
return Ok(Self::default());
}
let [auto, size] = value.items() else {
return Err(unsupported(value));
};
let length = match size.keyword() {
Some(Keyword::None) => None,
_ => Some(crate::style::values::length(size, relative)?),
};
Ok(Self {
auto: !auto.items().is_empty(),
length,
})
}
pub(crate) fn explicit(self) -> Option<f32> {
self.length
}
}
#[derive(Clone, Debug, Default, PartialEq, Serialize)]
pub(crate) struct ContainmentStyle {
pub contain: Contain,
pub container_type: ContainerType,
pub container_names: Arc<[Arc<str>]>,
pub content_visibility: ContentVisibility,
pub intrinsic: [ContainIntrinsicSize; 2],
}
pub(super) fn container_names(value: &Component) -> Result<Arc<[Arc<str>]>, StyleError> {
if value.keyword() == Some(Keyword::None) {
return Ok(Arc::from([]));
}
value
.items()
.iter()
.map(|name| match name.unwrap() {
Component::Ident(name) => Ok(Arc::from(name.as_ref())),
other => Err(unsupported(other)),
})
.collect()
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub(crate) struct Containment {
pub size: [bool; 2],
pub layout: bool,
pub style: bool,
pub paint: bool,
pub query_container: bool,
}
impl Style {
pub(crate) fn containment(&self) -> Containment {
let ContainmentStyle {
contain,
container_type,
content_visibility,
..
} = &self.containment;
let visibility = *content_visibility != ContentVisibility::Visible;
let skips = self.skips_contents();
let size = contain.size || container_type.size || skips;
let inline_size = size || contain.inline_size || container_type.inline_size;
let display = &self.display;
let internal = display.is_table_internal();
let sizes = !display.is_table() && !internal && *display != Display::None;
let boxes = (!internal || *display == Display::TableCell) && *display != Display::None;
Containment {
size: [inline_size && sizes, size && sizes],
layout: (contain.layout || visibility) && boxes,
style: contain.style || visibility || container_type.size || container_type.inline_size,
paint: (contain.paint || visibility) && boxes,
query_container: (container_type.size || container_type.inline_size) && sizes,
}
}
pub(crate) fn skips_contents(&self) -> bool {
self.containment.content_visibility == ContentVisibility::Hidden
}
pub(super) fn contains_positioned(&self) -> bool {
let containment = self.containment();
containment.layout || containment.paint
}
}