rspack_lightningcss/properties/
contain.rs1#![allow(non_upper_case_globals)]
4
5use cssparser::*;
6use smallvec::SmallVec;
7
8#[cfg(feature = "visitor")]
9use crate::visitor::Visit;
10use crate::{
11 context::PropertyHandlerContext,
12 declaration::{DeclarationBlock, DeclarationList},
13 error::{ParserError, PrinterError},
14 macros::{define_shorthand, enum_property, shorthand_handler},
15 properties::{Property, PropertyId},
16 rules::container::ContainerName as ContainerIdent,
17 targets::Browsers,
18 traits::{IsCompatible, Parse, PropertyHandler, Shorthand, ToCss},
19};
20
21enum_property! {
22 pub enum ContainerType {
25 Normal,
28 InlineSize,
30 Size,
32 ScrollState,
34 }
35}
36
37impl Default for ContainerType {
38 fn default() -> Self {
39 ContainerType::Normal
40 }
41}
42
43impl IsCompatible for ContainerType {
44 fn is_compatible(&self, _browsers: Browsers) -> bool {
45 true
46 }
47}
48
49#[derive(Debug, Clone, PartialEq)]
51#[cfg_attr(feature = "visitor", derive(Visit))]
52#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
53#[cfg_attr(
54 feature = "serde",
55 derive(serde::Serialize, serde::Deserialize),
56 serde(tag = "type", content = "value", rename_all = "kebab-case")
57)]
58#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
59pub enum ContainerNameList<'i> {
60 None,
62 #[cfg_attr(feature = "serde", serde(borrow))]
64 Names(SmallVec<[ContainerIdent<'i>; 1]>),
65}
66
67impl<'i> Default for ContainerNameList<'i> {
68 fn default() -> Self {
69 ContainerNameList::None
70 }
71}
72
73impl<'i> Parse<'i> for ContainerNameList<'i> {
74 fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
75 if input.try_parse(|input| input.expect_ident_matching("none")).is_ok() {
76 return Ok(ContainerNameList::None);
77 }
78
79 let mut names = SmallVec::new();
80 while let Ok(name) = input.try_parse(ContainerIdent::parse) {
81 names.push(name);
82 }
83
84 if names.is_empty() {
85 return Err(input.new_error_for_next_token());
86 } else {
87 return Ok(ContainerNameList::Names(names));
88 }
89 }
90}
91
92impl<'i> ToCss for ContainerNameList<'i> {
93 fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
94 match self {
95 ContainerNameList::None => {
96 dest.write_str("none")?;
97 Ok(())
98 }
99 ContainerNameList::Names(names) => {
100 let mut first = true;
101 for name in names {
102 if first {
103 first = false;
104 } else {
105 dest.write_char(' ')?;
106 }
107 name.to_css(dest)?;
108 }
109 Ok(())
110 }
111 }
112 }
113}
114
115impl IsCompatible for ContainerNameList<'_> {
116 fn is_compatible(&self, _browsers: Browsers) -> bool {
117 true
118 }
119}
120
121define_shorthand! {
122 pub struct Container<'i> {
124 #[cfg_attr(feature = "serde", serde(borrow))]
126 name: ContainerName(ContainerNameList<'i>),
127 container_type: ContainerType(ContainerType),
129 }
130}
131
132impl<'i> Parse<'i> for Container<'i> {
133 fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
134 let name = ContainerNameList::parse(input)?;
135 let container_type = if input.try_parse(|input| input.expect_delim('/')).is_ok() {
136 ContainerType::parse(input)?
137 } else {
138 ContainerType::default()
139 };
140 Ok(Container { name, container_type })
141 }
142}
143
144impl<'i> ToCss for Container<'i> {
145 fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
146 self.name.to_css(dest)?;
147 if self.container_type != ContainerType::default() {
148 dest.delim('/', true)?;
149 self.container_type.to_css(dest)?;
150 }
151 Ok(())
152 }
153}
154
155shorthand_handler!(ContainerHandler -> Container<'i> {
156 name: ContainerName(ContainerNameList<'i>),
157 container_type: ContainerType(ContainerType),
158});