rspack_lightningcss/rules/
property.rs1use super::Location;
4#[cfg(feature = "visitor")]
5use crate::visitor::Visit;
6use crate::{
7 error::{ParserError, PrinterError},
8 properties::custom::TokenList,
9 traits::{Parse, ToCss},
10 values::{
11 ident::DashedIdent,
12 syntax::{ParsedComponent, SyntaxString},
13 },
14};
15use cssparser::*;
16
17#[derive(Debug, PartialEq, Clone)]
19#[cfg_attr(feature = "visitor", derive(Visit))]
20#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
21#[cfg_attr(
22 feature = "serde",
23 derive(serde::Serialize, serde::Deserialize),
24 serde(rename_all = "camelCase")
25)]
26#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
27pub struct PropertyRule<'i> {
28 #[cfg_attr(feature = "serde", serde(borrow))]
30 pub name: DashedIdent<'i>,
31 #[cfg_attr(feature = "visitor", skip_visit)]
33 pub syntax: SyntaxString,
34 #[cfg_attr(feature = "visitor", skip_visit)]
36 pub inherits: bool,
37 #[cfg_attr(feature = "visitor", skip_visit)]
39 pub initial_value: Option<ParsedComponent<'i>>,
40 #[cfg_attr(feature = "visitor", skip_visit)]
42 pub loc: Location,
43}
44
45impl<'i> PropertyRule<'i> {
46 pub(crate) fn parse<'t>(
47 name: DashedIdent<'i>,
48 input: &mut Parser<'i, 't>,
49 loc: Location,
50 ) -> Result<Self, ParseError<'i, ParserError<'i>>> {
51 let mut parser = PropertyRuleDeclarationParser {
52 syntax: None,
53 inherits: None,
54 initial_value: None,
55 };
56
57 let mut decl_parser = RuleBodyParser::new(input, &mut parser);
58 while let Some(decl) = decl_parser.next() {
59 match decl {
60 Ok(()) => {}
61 Err((e, _)) => return Err(e),
62 }
63 }
64
65 let parser = decl_parser.parser;
67 let syntax = parser
68 .syntax
69 .clone()
70 .ok_or(decl_parser.input.new_custom_error(ParserError::AtRuleBodyInvalid))?;
71 let inherits = parser
72 .inherits
73 .clone()
74 .ok_or(decl_parser.input.new_custom_error(ParserError::AtRuleBodyInvalid))?;
75
76 let initial_value = match syntax {
78 SyntaxString::Universal => match parser.initial_value {
79 None => None,
80 Some(val) => {
81 let mut input = ParserInput::new(val);
82 let mut parser = Parser::new(&mut input);
83
84 if parser.is_exhausted() {
85 Some(ParsedComponent::TokenList(TokenList(vec![])))
86 } else {
87 Some(syntax.parse_value(&mut parser)?)
88 }
89 }
90 },
91 _ => {
92 let val = parser
93 .initial_value
94 .ok_or(input.new_custom_error(ParserError::AtRuleBodyInvalid))?;
95 let mut input = ParserInput::new(val);
96 let mut parser = Parser::new(&mut input);
97 Some(syntax.parse_value(&mut parser)?)
98 }
99 };
100
101 return Ok(PropertyRule {
102 name,
103 syntax,
104 inherits,
105 initial_value,
106 loc,
107 });
108 }
109}
110
111impl<'i> ToCss for PropertyRule<'i> {
112 fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
113 dest.add_mapping(self.loc);
114 dest.write_str("@property ")?;
115 self.name.to_css(dest)?;
116 dest.whitespace()?;
117 dest.write_char('{')?;
118 dest.indent();
119 dest.newline()?;
120
121 dest.write_str("syntax:")?;
122 dest.whitespace()?;
123 self.syntax.to_css(dest)?;
124 dest.write_char(';')?;
125 dest.newline()?;
126
127 dest.write_str("inherits:")?;
128 dest.whitespace()?;
129 match self.inherits {
130 true => dest.write_str("true")?,
131 false => dest.write_str("false")?,
132 }
133
134 if let Some(initial_value) = &self.initial_value {
135 dest.write_char(';')?;
136 dest.newline()?;
137
138 dest.write_str("initial-value:")?;
139 dest.whitespace()?;
140 initial_value.to_css(dest)?;
141
142 if !dest.options().minify {
143 dest.write_char(';')?;
144 }
145 }
146
147 dest.dedent();
148 dest.newline()?;
149 dest.write_char('}')?;
150 Ok(())
151 }
152}
153
154pub(crate) struct PropertyRuleDeclarationParser<'i> {
155 syntax: Option<SyntaxString>,
156 inherits: Option<bool>,
157 initial_value: Option<&'i str>,
158}
159
160impl<'i> cssparser::DeclarationParser<'i> for PropertyRuleDeclarationParser<'i> {
161 type Declaration = ();
162 type Error = ParserError<'i>;
163
164 fn parse_value<'t>(
165 &mut self,
166 name: CowRcStr<'i>,
167 input: &mut cssparser::Parser<'i, 't>,
168 _declaration_start: &ParserState,
169 ) -> Result<Self::Declaration, cssparser::ParseError<'i, Self::Error>> {
170 match_ignore_ascii_case! { &name,
171 "syntax" => {
172 let syntax = SyntaxString::parse(input)?;
173 self.syntax = Some(syntax);
174 },
175 "inherits" => {
176 let location = input.current_source_location();
177 let ident = input.expect_ident()?;
178 let inherits = match_ignore_ascii_case! {&*ident,
179 "true" => true,
180 "false" => false,
181 _ => return Err(location.new_unexpected_token_error(
182 cssparser::Token::Ident(ident.clone())
183 ))
184 };
185 self.inherits = Some(inherits);
186 },
187 "initial-value" => {
188 let start = input.position();
190 while input.next().is_ok() {}
191 let initial_value = input.slice_from(start);
192 self.initial_value = Some(initial_value);
193 },
194 _ => return Err(input.new_custom_error(ParserError::InvalidDeclaration))
195 }
196
197 return Ok(());
198 }
199}
200
201impl<'i> AtRuleParser<'i> for PropertyRuleDeclarationParser<'i> {
203 type Prelude = ();
204 type AtRule = ();
205 type Error = ParserError<'i>;
206}
207
208impl<'i> QualifiedRuleParser<'i> for PropertyRuleDeclarationParser<'i> {
209 type Prelude = ();
210 type QualifiedRule = ();
211 type Error = ParserError<'i>;
212}
213
214impl<'i> RuleBodyItemParser<'i, (), ParserError<'i>> for PropertyRuleDeclarationParser<'i> {
215 fn parse_qualified(&self) -> bool {
216 false
217 }
218
219 fn parse_declarations(&self) -> bool {
220 true
221 }
222}