|
| 1 | +use cssparser::*; |
| 2 | +use crate::{ |
| 3 | + values::{syntax::{SyntaxString, ParsedComponent}, string::CowArcStr}, |
| 4 | + error::{ParserError, PrinterError}, |
| 5 | + printer::Printer, |
| 6 | + traits::{Parse, ToCss} |
| 7 | +}; |
| 8 | +use super::Location; |
| 9 | + |
| 10 | +/// https://drafts.css-houdini.org/css-properties-values-api/#at-property-rule |
| 11 | +#[derive(Debug, PartialEq, Clone)] |
| 12 | +pub struct PropertyRule<'i> { |
| 13 | + name: CowArcStr<'i>, |
| 14 | + syntax: SyntaxString, |
| 15 | + inherits: bool, |
| 16 | + initial_value: Option<ParsedComponent<'i>>, |
| 17 | + loc: Location |
| 18 | +} |
| 19 | + |
| 20 | +impl<'i> PropertyRule<'i> { |
| 21 | + pub fn parse<'t>(name: CowArcStr<'i>, input: &mut Parser<'i, 't>, loc: Location) -> Result<Self, ParseError<'i, ParserError<'i>>> { |
| 22 | + let parser = PropertyRuleDeclarationParser { |
| 23 | + syntax: None, |
| 24 | + inherits: None, |
| 25 | + initial_value: None |
| 26 | + }; |
| 27 | + |
| 28 | + let mut decl_parser = DeclarationListParser::new(input, parser); |
| 29 | + while let Some(decl) = decl_parser.next() { |
| 30 | + match decl { |
| 31 | + Ok(()) => {}, |
| 32 | + Err((e, _)) => return Err(e) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + // `syntax` and `inherits` are always required. |
| 37 | + let parser = decl_parser.parser; |
| 38 | + let syntax = parser.syntax.ok_or(input.new_custom_error(ParserError::AtRuleBodyInvalid))?; |
| 39 | + let inherits = parser.inherits.ok_or(input.new_custom_error(ParserError::AtRuleBodyInvalid))?; |
| 40 | + |
| 41 | + // `initial-value` is required unless the syntax is a universal definition. |
| 42 | + let initial_value = match syntax { |
| 43 | + SyntaxString::Universal => match parser.initial_value { |
| 44 | + None => None, |
| 45 | + Some(val) => { |
| 46 | + let mut input = ParserInput::new(val); |
| 47 | + let mut parser = Parser::new(&mut input); |
| 48 | + Some(syntax.parse_value(&mut parser)?) |
| 49 | + } |
| 50 | + }, |
| 51 | + _ => { |
| 52 | + let val = parser.initial_value.ok_or(input.new_custom_error(ParserError::AtRuleBodyInvalid))?; |
| 53 | + let mut input = ParserInput::new(val); |
| 54 | + let mut parser = Parser::new(&mut input); |
| 55 | + Some(syntax.parse_value(&mut parser)?) |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + return Ok(PropertyRule { |
| 60 | + name, |
| 61 | + syntax, |
| 62 | + inherits, |
| 63 | + initial_value, |
| 64 | + loc |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl<'i> ToCss for PropertyRule<'i> { |
| 70 | + fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError> where W: std::fmt::Write { |
| 71 | + dest.add_mapping(self.loc); |
| 72 | + dest.write_str("@property ")?; |
| 73 | + serialize_identifier(&self.name, dest)?; |
| 74 | + dest.whitespace()?; |
| 75 | + dest.write_char('{')?; |
| 76 | + dest.indent(); |
| 77 | + dest.newline()?; |
| 78 | + |
| 79 | + dest.write_str("syntax:")?; |
| 80 | + dest.whitespace()?; |
| 81 | + self.syntax.to_css(dest)?; |
| 82 | + dest.write_char(';')?; |
| 83 | + dest.newline()?; |
| 84 | + |
| 85 | + dest.write_str("inherits:")?; |
| 86 | + dest.whitespace()?; |
| 87 | + match self.inherits { |
| 88 | + true => dest.write_str("true")?, |
| 89 | + false => dest.write_str("false")? |
| 90 | + } |
| 91 | + |
| 92 | + if let Some(initial_value) = &self.initial_value { |
| 93 | + dest.write_char(';')?; |
| 94 | + dest.newline()?; |
| 95 | + |
| 96 | + dest.write_str("initial-value:")?; |
| 97 | + dest.whitespace()?; |
| 98 | + initial_value.to_css(dest)?; |
| 99 | + if !dest.minify { |
| 100 | + dest.write_char(';')?; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + dest.dedent(); |
| 105 | + dest.newline()?; |
| 106 | + dest.write_char('}') |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +pub(crate) struct PropertyRuleDeclarationParser<'i> { |
| 111 | + syntax: Option<SyntaxString>, |
| 112 | + inherits: Option<bool>, |
| 113 | + initial_value: Option<&'i str> |
| 114 | +} |
| 115 | + |
| 116 | +impl<'i> cssparser::DeclarationParser<'i> for PropertyRuleDeclarationParser<'i> { |
| 117 | + type Declaration = (); |
| 118 | + type Error = ParserError<'i>; |
| 119 | + |
| 120 | + fn parse_value<'t>( |
| 121 | + &mut self, |
| 122 | + name: CowRcStr<'i>, |
| 123 | + input: &mut cssparser::Parser<'i, 't>, |
| 124 | + ) -> Result<Self::Declaration, cssparser::ParseError<'i, Self::Error>> { |
| 125 | + match_ignore_ascii_case! { &name, |
| 126 | + "syntax" => { |
| 127 | + let syntax = SyntaxString::parse(input)?; |
| 128 | + self.syntax = Some(syntax); |
| 129 | + }, |
| 130 | + "inherits" => { |
| 131 | + let location = input.current_source_location(); |
| 132 | + let ident = input.expect_ident()?; |
| 133 | + let inherits = match_ignore_ascii_case! {&*ident, |
| 134 | + "true" => true, |
| 135 | + "false" => false, |
| 136 | + _ => return Err(location.new_unexpected_token_error( |
| 137 | + cssparser::Token::Ident(ident.clone()) |
| 138 | + )) |
| 139 | + }; |
| 140 | + self.inherits = Some(inherits); |
| 141 | + }, |
| 142 | + "initial-value" => { |
| 143 | + // Buffer the value into a string. We will parse it later. |
| 144 | + let start = input.position(); |
| 145 | + while input.next().is_ok() {} |
| 146 | + let initial_value = input.slice_from(start); |
| 147 | + self.initial_value = Some(initial_value); |
| 148 | + }, |
| 149 | + _ => return Err(input.new_custom_error(ParserError::InvalidDeclaration)) |
| 150 | + } |
| 151 | + |
| 152 | + return Ok(()) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +/// Default methods reject all at rules. |
| 157 | +impl<'i> AtRuleParser<'i> for PropertyRuleDeclarationParser<'i> { |
| 158 | + type Prelude = (); |
| 159 | + type AtRule = (); |
| 160 | + type Error = ParserError<'i>; |
| 161 | +} |
0 commit comments