1use crate::context::{DeclarationContext, PropertyHandlerContext};
7use crate::css_modules::{hash, CssModule, CssModuleExports, CssModuleReferences};
8use crate::declaration::{DeclarationBlock, DeclarationHandler};
9use crate::dependencies::Dependency;
10use crate::error::{Error, ErrorLocation, MinifyErrorKind, ParserError, PrinterError, PrinterErrorKind};
11use crate::parser::{DefaultAtRule, DefaultAtRuleParser, TopLevelRuleParser};
12use crate::printer::Printer;
13use crate::rules::{CssRule, CssRuleList, MinifyContext};
14use crate::targets::{should_compile, Targets};
15use crate::traits::{AtRuleParser, ToCss};
16use crate::values::string::CowArcStr;
17#[cfg(feature = "visitor")]
18use crate::visitor::{Visit, VisitTypes, Visitor};
19use cssparser::{Parser, ParserInput, StyleSheetParser};
20#[cfg(feature = "sourcemap")]
21use parcel_sourcemap::SourceMap;
22use std::collections::{HashMap, HashSet};
23
24pub use crate::parser::{ParserFlags, ParserOptions};
25pub use crate::printer::PrinterOptions;
26pub use crate::printer::PseudoClasses;
27
28#[derive(Debug)]
63#[cfg_attr(
64 feature = "serde",
65 derive(serde::Serialize, serde::Deserialize),
66 serde(rename_all = "camelCase")
67)]
68#[cfg_attr(
69 feature = "jsonschema",
70 derive(schemars::JsonSchema),
71 schemars(rename = "StyleSheet", bound = "T: schemars::JsonSchema")
72)]
73pub struct StyleSheet<'i, 'o, T = DefaultAtRule> {
74 #[cfg_attr(feature = "serde", serde(borrow))]
76 pub rules: CssRuleList<'i, T>,
77 pub sources: Vec<String>,
80 pub(crate) source_map_urls: Vec<Option<String>>,
82 pub license_comments: Vec<CowArcStr<'i>>,
84 #[cfg_attr(feature = "serde", serde(skip))]
87 pub(crate) content_hashes: Option<Vec<String>>,
88 #[cfg_attr(feature = "serde", serde(skip))]
89 options: ParserOptions<'o, 'i>,
91}
92
93#[derive(Default)]
96pub struct MinifyOptions {
97 pub targets: Targets,
99 pub unused_symbols: HashSet<String>,
102}
103
104#[derive(Debug)]
107pub struct ToCssResult {
108 pub code: String,
110 pub exports: Option<CssModuleExports>,
113 pub references: Option<CssModuleReferences>,
116 pub dependencies: Option<Vec<Dependency>>,
119}
120
121impl<'i, 'o> StyleSheet<'i, 'o, DefaultAtRule> {
122 pub fn parse(code: &'i str, options: ParserOptions<'o, 'i>) -> Result<Self, Error<ParserError<'i>>> {
124 Self::parse_with(code, options, &mut DefaultAtRuleParser)
125 }
126}
127
128impl<'i, 'o, T> StyleSheet<'i, 'o, T>
129where
130 T: ToCss + Clone,
131{
132 pub fn new(
134 sources: Vec<String>,
135 rules: CssRuleList<'i, T>,
136 options: ParserOptions<'o, 'i>,
137 ) -> StyleSheet<'i, 'o, T> {
138 StyleSheet {
139 sources,
140 source_map_urls: Vec::new(),
141 license_comments: Vec::new(),
142 content_hashes: None,
143 rules,
144 options,
145 }
146 }
147
148 pub fn parse_with<P: AtRuleParser<'i, AtRule = T>>(
150 code: &'i str,
151 mut options: ParserOptions<'o, 'i>,
152 at_rule_parser: &mut P,
153 ) -> Result<Self, Error<ParserError<'i>>> {
154 let mut input = ParserInput::new(&code);
155 let mut parser = Parser::new(&mut input);
156 let mut license_comments = Vec::new();
157
158 let mut content_hashes = None;
159 if let Some(config) = &options.css_modules {
160 if config.pattern.has_content_hash() {
161 content_hashes = Some(vec![hash(
162 &code,
163 matches!(config.pattern.segments[0], crate::css_modules::Segment::ContentHash),
164 )]);
165 }
166 }
167
168 let mut state = parser.state();
169 while let Ok(token) = parser.next_including_whitespace_and_comments() {
170 match token {
171 cssparser::Token::WhiteSpace(..) => {}
172 cssparser::Token::Comment(comment) if comment.starts_with('!') => {
173 license_comments.push((*comment).into());
174 }
175 _ => break,
176 }
177 state = parser.state();
178 }
179 parser.reset(&state);
180
181 let mut rules = CssRuleList(vec![]);
182 let mut rule_parser = TopLevelRuleParser::new(&mut options, at_rule_parser, &mut rules);
183 let mut rule_list_parser = StyleSheetParser::new(&mut parser, &mut rule_parser);
184
185 while let Some(rule) = rule_list_parser.next() {
186 match rule {
187 Ok(()) => {}
188 Err((e, _)) => {
189 let options = &mut rule_list_parser.parser.options;
190 if options.error_recovery {
191 options.warn(e);
192 continue;
193 }
194
195 return Err(Error::from(e, options.filename.clone()));
196 }
197 }
198 }
199
200 Ok(StyleSheet {
201 sources: vec![options.filename.clone()],
202 source_map_urls: vec![parser.current_source_map_url().map(|s| s.to_owned())],
203 content_hashes,
204 rules,
205 license_comments,
206 options,
207 })
208 }
209
210 pub fn source_map_url(&self, source_index: usize) -> Option<&String> {
212 self.source_map_urls.get(source_index)?.as_ref()
213 }
214
215 #[cfg(feature = "sourcemap")]
217 #[cfg_attr(docsrs, doc(cfg(feature = "sourcemap")))]
218 pub fn source_map(&self, source_index: usize) -> Option<SourceMap> {
219 SourceMap::from_data_url("/", self.source_map_url(source_index)?).ok()
220 }
221
222 pub fn minify(&mut self, options: MinifyOptions) -> Result<(), Error<MinifyErrorKind>> {
224 let context = PropertyHandlerContext::new(options.targets, &options.unused_symbols);
225 let mut handler = DeclarationHandler::default();
226 let mut important_handler = DeclarationHandler::default();
227
228 let custom_media = if self.options.flags.contains(ParserFlags::CUSTOM_MEDIA)
231 && should_compile!(options.targets, CustomMediaQueries)
232 {
233 let mut custom_media = HashMap::new();
234 for rule in &self.rules.0 {
235 if let CssRule::CustomMedia(rule) = rule {
236 custom_media.insert(rule.name.0.clone(), rule.clone());
237 }
238 }
239 Some(custom_media)
240 } else {
241 None
242 };
243
244 let mut ctx = MinifyContext {
245 targets: &options.targets,
246 handler: &mut handler,
247 important_handler: &mut important_handler,
248 handler_context: context,
249 unused_symbols: &options.unused_symbols,
250 custom_media,
251 css_modules: self.options.css_modules.is_some(),
252 pure_css_modules: self.options.css_modules.as_ref().map(|c| c.pure).unwrap_or_default(),
253 };
254
255 self.rules.minify(&mut ctx, false).map_err(|e| Error {
256 kind: e.kind,
257 loc: Some(ErrorLocation::new(
258 e.loc,
259 self.sources[e.loc.source_index as usize].clone(),
260 )),
261 })?;
262
263 Ok(())
264 }
265
266 pub fn to_css(&self, options: PrinterOptions) -> Result<ToCssResult, Error<PrinterErrorKind>> {
268 let mut dest = String::with_capacity(1);
270 let project_root = options.project_root.clone();
271 let mut printer = Printer::new(&mut dest, options);
272
273 #[cfg(feature = "sourcemap")]
274 {
275 printer.sources = Some(&self.sources);
276 }
277
278 #[cfg(feature = "sourcemap")]
279 if printer.source_map.is_some() {
280 printer.source_maps = self.sources.iter().enumerate().map(|(i, _)| self.source_map(i)).collect();
281 }
282
283 for comment in &self.license_comments {
284 printer.write_str("/*")?;
285 printer.write_str(comment)?;
286 printer.write_str("*/\n")?;
287 }
288
289 if let Some(config) = &self.options.css_modules {
290 let mut references = HashMap::new();
291 printer.css_module = Some(CssModule::new(
292 config,
293 &self.sources,
294 project_root,
295 &mut references,
296 &self.content_hashes,
297 ));
298
299 self.rules.to_css(&mut printer)?;
300 printer.newline()?;
301
302 Ok(ToCssResult {
303 dependencies: printer.dependencies,
304 exports: Some(std::mem::take(
305 &mut printer.css_module.unwrap().exports_by_source_index[0],
306 )),
307 code: dest,
308 references: Some(references),
309 })
310 } else {
311 self.rules.to_css(&mut printer)?;
312 printer.newline()?;
313
314 Ok(ToCssResult {
315 dependencies: printer.dependencies,
316 code: dest,
317 exports: None,
318 references: None,
319 })
320 }
321 }
322}
323
324#[cfg(feature = "visitor")]
325#[cfg_attr(docsrs, doc(cfg(feature = "visitor")))]
326impl<'i, 'o, T, V> Visit<'i, T, V> for StyleSheet<'i, 'o, T>
327where
328 T: Visit<'i, T, V>,
329 V: ?Sized + Visitor<'i, T>,
330{
331 const CHILD_TYPES: VisitTypes = VisitTypes::all();
332
333 fn visit(&mut self, visitor: &mut V) -> Result<(), V::Error> {
334 visitor.visit_stylesheet(self)
335 }
336
337 fn visit_children(&mut self, visitor: &mut V) -> Result<(), V::Error> {
338 self.rules.visit(visitor)
339 }
340}
341
342#[cfg_attr(feature = "visitor", derive(Visit))]
368#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
369pub struct StyleAttribute<'i> {
370 pub declarations: DeclarationBlock<'i>,
372 #[cfg_attr(feature = "visitor", skip_visit)]
373 sources: Vec<String>,
374}
375
376impl<'i> StyleAttribute<'i> {
377 pub fn parse(
379 code: &'i str,
380 options: ParserOptions<'_, 'i>,
381 ) -> Result<StyleAttribute<'i>, Error<ParserError<'i>>> {
382 let mut input = ParserInput::new(&code);
383 let mut parser = Parser::new(&mut input);
384 Ok(StyleAttribute {
385 declarations: DeclarationBlock::parse(&mut parser, &options).map_err(|e| Error::from(e, "".into()))?,
386 sources: vec![options.filename],
387 })
388 }
389
390 pub fn minify(&mut self, options: MinifyOptions) {
392 let mut context = PropertyHandlerContext::new(options.targets, &options.unused_symbols);
393 let mut handler = DeclarationHandler::default();
394 let mut important_handler = DeclarationHandler::default();
395 context.context = DeclarationContext::StyleAttribute;
396 self.declarations.minify(&mut handler, &mut important_handler, &mut context);
397 }
398
399 pub fn to_css(&self, options: PrinterOptions) -> Result<ToCssResult, PrinterError> {
401 #[cfg(feature = "sourcemap")]
402 assert!(
403 options.source_map.is_none(),
404 "Source maps are not supported for style attributes"
405 );
406
407 let mut dest = String::with_capacity(1);
409 let mut printer = Printer::new(&mut dest, options);
410 printer.sources = Some(&self.sources);
411
412 self.declarations.to_css(&mut printer)?;
413
414 Ok(ToCssResult {
415 dependencies: printer.dependencies,
416 code: dest,
417 exports: None,
418 references: None,
419 })
420 }
421}