|
| 1 | +use cssparser::*; |
| 2 | +use crate::traits::{Parse, ToCss, PropertyHandler}; |
| 3 | +use super::Property; |
| 4 | +use crate::values::{image::Image, ident::CustomIdent}; |
| 5 | +use crate::declaration::DeclarationList; |
| 6 | +use crate::macros::{enum_property, shorthand_property, shorthand_handler}; |
| 7 | +use crate::printer::Printer; |
| 8 | +use std::fmt::Write; |
| 9 | + |
| 10 | +/// https://www.w3.org/TR/2020/WD-css-lists-3-20201117/#text-markers |
| 11 | +#[derive(Debug, Clone, PartialEq)] |
| 12 | +pub enum ListStyleType { |
| 13 | + None, |
| 14 | + CounterStyle(CounterStyle), |
| 15 | + String(String) |
| 16 | +} |
| 17 | + |
| 18 | +impl Default for ListStyleType { |
| 19 | + fn default() -> ListStyleType { |
| 20 | + ListStyleType::CounterStyle(CounterStyle::Name(CustomIdent("disc".into()))) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +impl Parse for ListStyleType { |
| 25 | + fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ()>> { |
| 26 | + if let Ok(val) = input.try_parse(CounterStyle::parse) { |
| 27 | + return Ok(ListStyleType::CounterStyle(val)) |
| 28 | + } |
| 29 | + |
| 30 | + if input.try_parse(|input| input.expect_ident_matching("none")).is_ok() { |
| 31 | + return Ok(ListStyleType::None) |
| 32 | + } |
| 33 | + |
| 34 | + let s = input.expect_string()?.as_ref().to_owned(); |
| 35 | + Ok(ListStyleType::String(s)) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl ToCss for ListStyleType { |
| 40 | + fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write { |
| 41 | + match self { |
| 42 | + ListStyleType::None => dest.write_str("none"), |
| 43 | + ListStyleType::CounterStyle(style) => style.to_css(dest), |
| 44 | + ListStyleType::String(s) => serialize_string(&s, dest) |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/// https://www.w3.org/TR/css-counter-styles-3/#typedef-counter-style |
| 50 | +#[derive(Debug, Clone, PartialEq)] |
| 51 | +pub enum CounterStyle { |
| 52 | + Name(CustomIdent), |
| 53 | + Symbols(SymbolsType, Vec<Symbol>) |
| 54 | +} |
| 55 | + |
| 56 | +impl Parse for CounterStyle { |
| 57 | + fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ()>> { |
| 58 | + if input.try_parse(|input| input.expect_function_matching("symbols")).is_ok() { |
| 59 | + return input.parse_nested_block(|input| { |
| 60 | + let t = input.try_parse(SymbolsType::parse).unwrap_or(SymbolsType::Symbolic); |
| 61 | + println!("{:?}", t); |
| 62 | + |
| 63 | + let mut symbols = Vec::new(); |
| 64 | + while let Ok(s) = input.try_parse(Symbol::parse) { |
| 65 | + symbols.push(s); |
| 66 | + } |
| 67 | + |
| 68 | + Ok(CounterStyle::Symbols(t, symbols)) |
| 69 | + }) |
| 70 | + } |
| 71 | + |
| 72 | + let name = CustomIdent::parse(input)?; |
| 73 | + Ok(CounterStyle::Name(name)) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl ToCss for CounterStyle { |
| 78 | + fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write { |
| 79 | + match self { |
| 80 | + CounterStyle::Name(name) => name.to_css(dest), |
| 81 | + CounterStyle::Symbols(t, symbols) => { |
| 82 | + dest.write_str("symbols(")?; |
| 83 | + let mut needs_space = false; |
| 84 | + if *t != SymbolsType::Symbolic { |
| 85 | + t.to_css(dest)?; |
| 86 | + needs_space = true; |
| 87 | + } |
| 88 | + |
| 89 | + for symbol in symbols { |
| 90 | + if needs_space { |
| 91 | + dest.write_char(' ')?; |
| 92 | + } |
| 93 | + symbol.to_css(dest)?; |
| 94 | + needs_space = true; |
| 95 | + } |
| 96 | + dest.write_char(')') |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +enum_property!(SymbolsType, |
| 103 | + Cyclic, |
| 104 | + Numeric, |
| 105 | + Alphabetic, |
| 106 | + Symbolic, |
| 107 | + Fixed |
| 108 | +); |
| 109 | + |
| 110 | +/// https://www.w3.org/TR/css-counter-styles-3/#funcdef-symbols |
| 111 | +#[derive(Debug, Clone, PartialEq)] |
| 112 | +pub enum Symbol { |
| 113 | + String(String), |
| 114 | + Image(Image) |
| 115 | +} |
| 116 | + |
| 117 | +impl Parse for Symbol { |
| 118 | + fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ()>> { |
| 119 | + if let Ok(img) = input.try_parse(Image::parse) { |
| 120 | + return Ok(Symbol::Image(img)) |
| 121 | + } |
| 122 | + |
| 123 | + let s = input.expect_string()?.as_ref().to_owned(); |
| 124 | + Ok(Symbol::String(s)) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +impl ToCss for Symbol { |
| 129 | + fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write { |
| 130 | + match self { |
| 131 | + Symbol::String(s) => serialize_string(&s, dest), |
| 132 | + Symbol::Image(img) => img.to_css(dest) |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +// https://www.w3.org/TR/2020/WD-css-lists-3-20201117/#list-style-position-property |
| 138 | +enum_property!(ListStylePosition, |
| 139 | + Inside, |
| 140 | + Outside |
| 141 | +); |
| 142 | + |
| 143 | +impl Default for ListStylePosition { |
| 144 | + fn default() -> ListStylePosition { |
| 145 | + ListStylePosition::Outside |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +// https://www.w3.org/TR/2020/WD-css-lists-3-20201117/#marker-side |
| 150 | +enum_property!(MarkerSide, |
| 151 | + ("match-self", MatchSelf), |
| 152 | + ("match-parent", MatchParent) |
| 153 | +); |
| 154 | + |
| 155 | +// https://www.w3.org/TR/2020/WD-css-lists-3-20201117/#list-style-property |
| 156 | +shorthand_property!(ListStyle { |
| 157 | + list_style_type: ListStyleType, |
| 158 | + image: Image, |
| 159 | + position: ListStylePosition, |
| 160 | +}); |
| 161 | + |
| 162 | +shorthand_handler!(ListStyleHandler -> ListStyle { |
| 163 | + list_style_type: ListStyleType(ListStyleType), |
| 164 | + image: ListStyleImage(Image), |
| 165 | + position: ListStylePosition(ListStylePosition), |
| 166 | +}); |
0 commit comments