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 #[cfg(feature = "custom_sourcemap")]
114 dest.add_mapping(self.loc);
115 dest.write_str("@property ")?;
116 self.name.to_css(dest)?;
117 dest.whitespace()?;
118 dest.write_char('{')?;
119 dest.indent();
120 dest.newline()?;
121
122 dest.write_str("syntax:")?;
123 dest.whitespace()?;
124 self.syntax.to_css(dest)?;
125 dest.write_char(';')?;
126 dest.newline()?;
127
128 dest.write_str("inherits:")?;
129 dest.whitespace()?;
130 match self.inherits {
131 true => dest.write_str("true")?,
132 false => dest.write_str("false")?,
133 }
134
135 if let Some(initial_value) = &self.initial_value {
136 dest.write_char(';')?;
137 dest.newline()?;
138
139 dest.write_str("initial-value:")?;
140 dest.whitespace()?;
141 initial_value.to_css(dest)?;
142
143 if !dest.options().minify {
144 dest.write_char(';')?;
145 }
146 }
147
148 dest.dedent();
149 dest.newline()?;
150 dest.write_char('}')?;
151 Ok(())
152 }
153}
154
155pub(crate) struct PropertyRuleDeclarationParser<'i> {
156 syntax: Option<SyntaxString>,
157 inherits: Option<bool>,
158 initial_value: Option<&'i str>,
159}
160
161impl<'i> cssparser::DeclarationParser<'i> for PropertyRuleDeclarationParser<'i> {
162 type Declaration = ();
163 type Error = ParserError<'i>;
164
165 fn parse_value<'t>(
166 &mut self,
167 name: CowRcStr<'i>,
168 input: &mut cssparser::Parser<'i, 't>,
169 _declaration_start: &ParserState,
170 ) -> Result<Self::Declaration, cssparser::ParseError<'i, Self::Error>> {
171 match_ignore_ascii_case! { &name,
172 "syntax" => {
173 let syntax = SyntaxString::parse(input)?;
174 self.syntax = Some(syntax);
175 },
176 "inherits" => {
177 let location = input.current_source_location();
178 let ident = input.expect_ident()?;
179 let inherits = match_ignore_ascii_case! {&*ident,
180 "true" => true,
181 "false" => false,
182 _ => return Err(location.new_unexpected_token_error(
183 cssparser::Token::Ident(ident.clone())
184 ))
185 };
186 self.inherits = Some(inherits);
187 },
188 "initial-value" => {
189 let start = input.position();
191 while input.next().is_ok() {}
192 let initial_value = input.slice_from(start);
193 self.initial_value = Some(initial_value);
194 },
195 _ => return Err(input.new_custom_error(ParserError::InvalidDeclaration))
196 }
197
198 return Ok(());
199 }
200}
201
202impl<'i> AtRuleParser<'i> for PropertyRuleDeclarationParser<'i> {
204 type Prelude = ();
205 type AtRule = ();
206 type Error = ParserError<'i>;
207}
208
209impl<'i> QualifiedRuleParser<'i> for PropertyRuleDeclarationParser<'i> {
210 type Prelude = ();
211 type QualifiedRule = ();
212 type Error = ParserError<'i>;
213}
214
215impl<'i> RuleBodyItemParser<'i, (), ParserError<'i>> for PropertyRuleDeclarationParser<'i> {
216 fn parse_qualified(&self) -> bool {
217 false
218 }
219
220 fn parse_declarations(&self) -> bool {
221 true
222 }
223}