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.loc().source_index;
220 dest.with_css_module_mut(|css_module| css_module.reference(&name.0, source_index));
221 name.to_css(dest)
222 }
223 CounterStyle::Symbols { system: t, symbols } => {
224 dest.write_str("symbols(")?;
225 let mut needs_space = false;
226 if *t != SymbolsType::Symbolic {
227 t.to_css(dest)?;
228 needs_space = true;
229 }
230
231 for symbol in symbols {
232 if needs_space {
233 dest.write_char(' ')?;
234 }
235 symbol.to_css(dest)?;
236 needs_space = true;
237 }
238 dest.write_char(')')?;
239 Ok(())
240 }
241 }
242 }
243}
244
245impl IsCompatible for CounterStyle<'_> {
246 fn is_compatible(&self, browsers: Browsers) -> bool {
247 match self {
248 CounterStyle::Name(..) => true,
249 CounterStyle::Predefined(p) => p.is_compatible(browsers),
250 CounterStyle::Symbols { .. } => crate::compat::Feature::SymbolsListStyleType.is_compatible(browsers),
251 }
252 }
253}
254
255enum_property! {
256 #[allow(missing_docs)]
261 pub enum SymbolsType {
262 Cyclic,
263 Numeric,
264 Alphabetic,
265 Symbolic,
266 Fixed,
267 }
268}
269
270impl Default for SymbolsType {
271 fn default() -> Self {
272 SymbolsType::Symbolic
273 }
274}
275
276#[derive(Debug, Clone, PartialEq, Parse, ToCss)]
281#[cfg_attr(feature = "visitor", derive(Visit))]
282#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
283#[cfg_attr(
284 feature = "serde",
285 derive(serde::Serialize, serde::Deserialize),
286 serde(tag = "type", content = "value", rename_all = "kebab-case")
287)]
288#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
289pub enum Symbol<'i> {
290 #[cfg_attr(feature = "serde", serde(borrow))]
292 String(CSSString<'i>),
293 Image(Image<'i>),
295}
296
297enum_property! {
298 pub enum ListStylePosition {
300 Inside,
302 Outside,
304 }
305}
306
307impl Default for ListStylePosition {
308 fn default() -> ListStylePosition {
309 ListStylePosition::Outside
310 }
311}
312
313impl IsCompatible for ListStylePosition {
314 fn is_compatible(&self, _browsers: Browsers) -> bool {
315 true
316 }
317}
318
319enum_property! {
320 #[allow(missing_docs)]
322 pub enum MarkerSide {
323 MatchSelf,
324 MatchParent,
325 }
326}
327
328define_shorthand! {
329 pub struct ListStyle<'i> {
331 position: ListStylePosition(ListStylePosition),
333 #[cfg_attr(feature = "serde", serde(borrow))]
335 image: ListStyleImage(Image<'i>),
336 list_style_type: ListStyleType(ListStyleType<'i>),
338 }
339}
340
341impl<'i> Parse<'i> for ListStyle<'i> {
342 fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
343 let mut position = None;
344 let mut image = None;
345 let mut list_style_type = None;
346 let mut nones = 0;
347
348 loop {
349 if input.try_parse(|input| input.expect_ident_matching("none")).is_ok() {
351 nones += 1;
352 if nones > 2 {
353 return Err(input.new_custom_error(ParserError::InvalidValue));
354 }
355 continue;
356 }
357
358 if image.is_none() {
359 if let Ok(val) = input.try_parse(Image::parse) {
360 image = Some(val);
361 continue;
362 }
363 }
364
365 if position.is_none() {
366 if let Ok(val) = input.try_parse(ListStylePosition::parse) {
367 position = Some(val);
368 continue;
369 }
370 }
371
372 if list_style_type.is_none() {
373 if let Ok(val) = input.try_parse(ListStyleType::parse) {
374 list_style_type = Some(val);
375 continue;
376 }
377 }
378
379 break;
380 }
381
382 match (nones, image, list_style_type) {
385 (2, None, None) | (1, None, None) => Ok(ListStyle {
386 position: position.unwrap_or_default(),
387 image: Image::None,
388 list_style_type: ListStyleType::None,
389 }),
390 (1, Some(image), None) => Ok(ListStyle {
391 position: position.unwrap_or_default(),
392 image,
393 list_style_type: ListStyleType::None,
394 }),
395 (1, None, Some(list_style_type)) => Ok(ListStyle {
396 position: position.unwrap_or_default(),
397 image: Image::None,
398 list_style_type,
399 }),
400 (0, image, list_style_type) => Ok(ListStyle {
401 position: position.unwrap_or_default(),
402 image: image.unwrap_or_default(),
403 list_style_type: list_style_type.unwrap_or_default(),
404 }),
405 _ => Err(input.new_custom_error(ParserError::InvalidValue)),
406 }
407 }
408}
409
410impl<'i> ToCss for ListStyle<'i> {
411 fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
412 let mut needs_space = false;
413 if self.position != ListStylePosition::default() {
414 self.position.to_css(dest)?;
415 needs_space = true;
416 }
417
418 if self.image != Image::default() {
419 if needs_space {
420 dest.write_char(' ')?;
421 }
422 self.image.to_css(dest)?;
423 needs_space = true;
424 }
425
426 if self.list_style_type != ListStyleType::default() {
427 if needs_space {
428 dest.write_char(' ')?;
429 }
430 self.list_style_type.to_css(dest)?;
431 needs_space = true;
432 }
433
434 if !needs_space {
435 self.position.to_css(dest)?;
436 }
437
438 Ok(())
439 }
440}
441
442impl<'i> FallbackValues for ListStyle<'i> {
443 fn get_fallbacks(&mut self, targets: Targets) -> Vec<Self> {
444 self
445 .image
446 .get_fallbacks(targets)
447 .into_iter()
448 .map(|image| ListStyle { image, ..self.clone() })
449 .collect()
450 }
451}
452
453shorthand_handler!(ListStyleHandler -> ListStyle<'i> fallbacks: true {
454 image: ListStyleImage(Image<'i>, fallback: true, image: true),
455 list_style_type: ListStyleType(ListStyleType<'i>),
456 position: ListStylePosition(ListStylePosition),
457});