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