1use super::{Property, PropertyId};
4use crate::context::PropertyHandlerContext;
5use crate::declaration::{DeclarationBlock, DeclarationList};
6use crate::error::{ParserError, PrinterError};
7use crate::macros::{define_shorthand, enum_property, shorthand_handler};
8use crate::targets::{Browsers, Targets};
9use crate::traits::{FallbackValues, IsCompatible, Parse, PropertyHandler, Shorthand, ToCss};
10use crate::values::string::CSSString;
11use crate::values::{ident::CustomIdent, image::Image};
12#[cfg(feature = "visitor")]
13use crate::visitor::Visit;
14use cssparser::*;
15
16#[derive(Debug, Clone, PartialEq, Parse, ToCss)]
18#[cfg_attr(feature = "visitor", derive(Visit))]
19#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
20#[cfg_attr(
21 feature = "serde",
22 derive(serde::Serialize, serde::Deserialize),
23 serde(tag = "type", content = "value", rename_all = "kebab-case")
24)]
25#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
26pub enum ListStyleType<'i> {
27 None,
29 #[cfg_attr(feature = "serde", serde(borrow))]
31 String(CSSString<'i>),
32 CounterStyle(CounterStyle<'i>),
34}
35
36impl Default for ListStyleType<'_> {
37 fn default() -> Self {
38 ListStyleType::CounterStyle(CounterStyle::Predefined(PredefinedCounterStyle::Disc))
39 }
40}
41
42impl IsCompatible for ListStyleType<'_> {
43 fn is_compatible(&self, browsers: Browsers) -> bool {
44 match self {
45 ListStyleType::CounterStyle(c) => c.is_compatible(browsers),
46 ListStyleType::String(..) => crate::compat::Feature::StringListStyleType.is_compatible(browsers),
47 ListStyleType::None => true,
48 }
49 }
50}
51
52#[derive(Debug, Clone, PartialEq)]
54#[cfg_attr(feature = "visitor", derive(Visit))]
55#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
56#[cfg_attr(
57 feature = "serde",
58 derive(serde::Serialize, serde::Deserialize),
59 serde(tag = "type", rename_all = "kebab-case")
60)]
61#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
62pub enum CounterStyle<'i> {
63 #[cfg_attr(
65 feature = "serde",
66 serde(with = "crate::serialization::ValueWrapper::<PredefinedCounterStyle>")
67 )]
68 Predefined(PredefinedCounterStyle),
69 #[cfg_attr(
71 feature = "serde",
72 serde(borrow, with = "crate::serialization::ValueWrapper::<CustomIdent>")
73 )]
74 Name(CustomIdent<'i>),
75 Symbols {
77 #[cfg_attr(feature = "serde", serde(default))]
79 system: SymbolsType,
80 symbols: Vec<Symbol<'i>>,
82 },
83}
84
85macro_rules! counter_styles {
86 (
87 $(#[$outer:meta])*
88 $vis:vis enum $name:ident {
89 $(
90 $(#[$meta: meta])*
91 $id: ident,
92 )+
93 }
94 ) => {
95 enum_property! {
96 #[allow(missing_docs)]
98 pub enum PredefinedCounterStyle {
99 $(
100 $(#[$meta])*
101 $id,
102 )+
103 }
104 }
105
106 impl IsCompatible for PredefinedCounterStyle {
107 fn is_compatible(&self, browsers: Browsers) -> bool {
108 match self {
109 $(
110 PredefinedCounterStyle::$id => pastey::paste! {
111 crate::compat::Feature::[<$id ListStyleType>].is_compatible(browsers)
112 },
113 )+
114 }
115 }
116 }
117 };
118}
119
120counter_styles! {
121 #[allow(missing_docs)]
123 pub enum PredefinedCounterStyle {
124 Decimal,
126 DecimalLeadingZero,
127 ArabicIndic,
128 Armenian,
129 UpperArmenian,
130 LowerArmenian,
131 Bengali,
132 Cambodian,
133 Khmer,
134 CjkDecimal,
135 Devanagari,
136 Georgian,
137 Gujarati,
138 Gurmukhi,
139 Hebrew,
140 Kannada,
141 Lao,
142 Malayalam,
143 Mongolian,
144 Myanmar,
145 Oriya,
146 Persian,
147 LowerRoman,
148 UpperRoman,
149 Tamil,
150 Telugu,
151 Thai,
152 Tibetan,
153
154 LowerAlpha,
156 LowerLatin,
157 UpperAlpha,
158 UpperLatin,
159 LowerGreek,
160 Hiragana,
161 HiraganaIroha,
162 Katakana,
163 KatakanaIroha,
164
165 Disc,
167 Circle,
168 Square,
169 DisclosureOpen,
170 DisclosureClosed,
171
172 CjkEarthlyBranch,
174 CjkHeavenlyStem,
175
176 JapaneseInformal,
178 JapaneseFormal,
179 KoreanHangulFormal,
180 KoreanHanjaInformal,
181 KoreanHanjaFormal,
182 SimpChineseInformal,
183 SimpChineseFormal,
184 TradChineseInformal,
185 TradChineseFormal,
186 EthiopicNumeric,
187 }
188}
189
190impl<'i> Parse<'i> for CounterStyle<'i> {
191 fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
192 if let Ok(predefined) = input.try_parse(PredefinedCounterStyle::parse) {
193 return Ok(CounterStyle::Predefined(predefined));
194 }
195
196 if input.try_parse(|input| input.expect_function_matching("symbols")).is_ok() {
197 return input.parse_nested_block(|input| {
198 let t = input.try_parse(SymbolsType::parse).unwrap_or_default();
199
200 let mut symbols = Vec::new();
201 while let Ok(s) = input.try_parse(Symbol::parse) {
202 symbols.push(s);
203 }
204
205 Ok(CounterStyle::Symbols { system: t, symbols })
206 });
207 }
208
209 let name = CustomIdent::parse(input)?;
210 Ok(CounterStyle::Name(name))
211 }
212}
213
214impl ToCss for CounterStyle<'_> {
215 fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
216 match self {
217 CounterStyle::Predefined(style) => style.to_css(dest),
218 CounterStyle::Name(name) => {
219 let source_index = dest.state().loc.source_index;
220 if let Some(css_module) = dest.state_mut().css_module.as_mut() {
221 css_module.reference(&name.0, source_index)
222 }
223 name.to_css(dest)
224 }
225 CounterStyle::Symbols { system: t, symbols } => {
226 dest.write_str("symbols(")?;
227 let mut needs_space = false;
228 if *t != SymbolsType::Symbolic {
229 t.to_css(dest)?;
230 needs_space = true;
231 }
232
233 for symbol in symbols {
234 if needs_space {
235 dest.write_char(' ')?;
236 }
237 symbol.to_css(dest)?;
238 needs_space = true;
239 }
240 dest.write_char(')')?;
241 Ok(())
242 }
243 }
244 }
245}
246
247impl IsCompatible for CounterStyle<'_> {
248 fn is_compatible(&self, browsers: Browsers) -> bool {
249 match self {
250 CounterStyle::Name(..) => true,
251 CounterStyle::Predefined(p) => p.is_compatible(browsers),
252 CounterStyle::Symbols { .. } => crate::compat::Feature::SymbolsListStyleType.is_compatible(browsers),
253 }
254 }
255}
256
257enum_property! {
258 #[allow(missing_docs)]
263 pub enum SymbolsType {
264 Cyclic,
265 Numeric,
266 Alphabetic,
267 Symbolic,
268 Fixed,
269 }
270}
271
272impl Default for SymbolsType {
273 fn default() -> Self {
274 SymbolsType::Symbolic
275 }
276}
277
278#[derive(Debug, Clone, PartialEq, Parse, ToCss)]
283#[cfg_attr(feature = "visitor", derive(Visit))]
284#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
285#[cfg_attr(
286 feature = "serde",
287 derive(serde::Serialize, serde::Deserialize),
288 serde(tag = "type", content = "value", rename_all = "kebab-case")
289)]
290#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
291pub enum Symbol<'i> {
292 #[cfg_attr(feature = "serde", serde(borrow))]
294 String(CSSString<'i>),
295 Image(Image<'i>),
297}
298
299enum_property! {
300 pub enum ListStylePosition {
302 Inside,
304 Outside,
306 }
307}
308
309impl Default for ListStylePosition {
310 fn default() -> ListStylePosition {
311 ListStylePosition::Outside
312 }
313}
314
315impl IsCompatible for ListStylePosition {
316 fn is_compatible(&self, _browsers: Browsers) -> bool {
317 true
318 }
319}
320
321enum_property! {
322 #[allow(missing_docs)]
324 pub enum MarkerSide {
325 MatchSelf,
326 MatchParent,
327 }
328}
329
330define_shorthand! {
331 pub struct ListStyle<'i> {
333 position: ListStylePosition(ListStylePosition),
335 #[cfg_attr(feature = "serde", serde(borrow))]
337 image: ListStyleImage(Image<'i>),
338 list_style_type: ListStyleType(ListStyleType<'i>),
340 }
341}
342
343impl<'i> Parse<'i> for ListStyle<'i> {
344 fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
345 let mut position = None;
346 let mut image = None;
347 let mut list_style_type = None;
348 let mut nones = 0;
349
350 loop {
351 if input.try_parse(|input| input.expect_ident_matching("none")).is_ok() {
353 nones += 1;
354 if nones > 2 {
355 return Err(input.new_custom_error(ParserError::InvalidValue));
356 }
357 continue;
358 }
359
360 if image.is_none() {
361 if let Ok(val) = input.try_parse(Image::parse) {
362 image = Some(val);
363 continue;
364 }
365 }
366
367 if position.is_none() {
368 if let Ok(val) = input.try_parse(ListStylePosition::parse) {
369 position = Some(val);
370 continue;
371 }
372 }
373
374 if list_style_type.is_none() {
375 if let Ok(val) = input.try_parse(ListStyleType::parse) {
376 list_style_type = Some(val);
377 continue;
378 }
379 }
380
381 break;
382 }
383
384 match (nones, image, list_style_type) {
387 (2, None, None) | (1, None, None) => Ok(ListStyle {
388 position: position.unwrap_or_default(),
389 image: Image::None,
390 list_style_type: ListStyleType::None,
391 }),
392 (1, Some(image), None) => Ok(ListStyle {
393 position: position.unwrap_or_default(),
394 image,
395 list_style_type: ListStyleType::None,
396 }),
397 (1, None, Some(list_style_type)) => Ok(ListStyle {
398 position: position.unwrap_or_default(),
399 image: Image::None,
400 list_style_type,
401 }),
402 (0, image, list_style_type) => Ok(ListStyle {
403 position: position.unwrap_or_default(),
404 image: image.unwrap_or_default(),
405 list_style_type: list_style_type.unwrap_or_default(),
406 }),
407 _ => Err(input.new_custom_error(ParserError::InvalidValue)),
408 }
409 }
410}
411
412impl<'i> ToCss for ListStyle<'i> {
413 fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
414 let mut needs_space = false;
415 if self.position != ListStylePosition::default() {
416 self.position.to_css(dest)?;
417 needs_space = true;
418 }
419
420 if self.image != Image::default() {
421 if needs_space {
422 dest.write_char(' ')?;
423 }
424 self.image.to_css(dest)?;
425 needs_space = true;
426 }
427
428 if self.list_style_type != ListStyleType::default() {
429 if needs_space {
430 dest.write_char(' ')?;
431 }
432 self.list_style_type.to_css(dest)?;
433 needs_space = true;
434 }
435
436 if !needs_space {
437 self.position.to_css(dest)?;
438 }
439
440 Ok(())
441 }
442}
443
444impl<'i> FallbackValues for ListStyle<'i> {
445 fn get_fallbacks(&mut self, targets: Targets) -> Vec<Self> {
446 self
447 .image
448 .get_fallbacks(targets)
449 .into_iter()
450 .map(|image| ListStyle { image, ..self.clone() })
451 .collect()
452 }
453}
454
455shorthand_handler!(ListStyleHandler -> ListStyle<'i> fallbacks: true {
456 image: ListStyleImage(Image<'i>, fallback: true, image: true),
457 list_style_type: ListStyleType(ListStyleType<'i>),
458 position: ListStylePosition(ListStylePosition),
459});