lightningcss/
stylesheet.rs

1//! CSS style sheets and style attributes.
2//!
3//! A [StyleSheet](StyleSheet) represents a `.css` file or `<style>` element in HTML.
4//! A [StyleAttribute](StyleAttribute) represents an inline `style` attribute in HTML.
5
6use 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/// A CSS style sheet, representing a `.css` file or inline `<style>` element.
29///
30/// Style sheets can be parsed from a string, constructed from scratch,
31/// or created using a [Bundler](super::bundler::Bundler). Then, they can be
32/// minified and transformed for a set of target browsers, and serialied to a string.
33///
34/// # Example
35///
36/// ```
37/// use lightningcss::stylesheet::{
38///   StyleSheet, ParserOptions, MinifyOptions, PrinterOptions
39/// };
40///
41/// // Parse a style sheet from a string.
42/// let mut stylesheet = StyleSheet::parse(
43///   r#"
44///   .foo {
45///     color: red;
46///   }
47///
48///   .bar {
49///     color: red;
50///   }
51///   "#,
52///   ParserOptions::default()
53/// ).unwrap();
54///
55/// // Minify the stylesheet.
56/// stylesheet.minify(MinifyOptions::default()).unwrap();
57///
58/// // Serialize it to a string.
59/// let res = stylesheet.to_css(PrinterOptions::default()).unwrap();
60/// assert_eq!(res.code, ".foo, .bar {\n  color: red;\n}\n");
61/// ```
62#[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  /// A list of top-level rules within the style sheet.
75  #[cfg_attr(feature = "serde", serde(borrow))]
76  pub rules: CssRuleList<'i, T>,
77  /// A list of file names for all source files included within the style sheet.
78  /// Sources are referenced by index in the `loc` property of each rule.
79  pub sources: Vec<String>,
80  /// The source map URL extracted from the original style sheet.
81  pub(crate) source_map_urls: Vec<Option<String>>,
82  /// The license comments that appeared at the start of the file.
83  pub license_comments: Vec<CowArcStr<'i>>,
84  /// A list of content hashes for all source files included within the style sheet.
85  /// This is only set if CSS modules are enabled and the pattern includes [content-hash].
86  #[cfg_attr(feature = "serde", serde(skip))]
87  pub(crate) content_hashes: Option<Vec<String>>,
88  #[cfg_attr(feature = "serde", serde(skip))]
89  /// The options the style sheet was originally parsed with.
90  options: ParserOptions<'o, 'i>,
91}
92
93/// Options for the `minify` function of a [StyleSheet](StyleSheet)
94/// or [StyleAttribute](StyleAttribute).
95#[derive(Default)]
96pub struct MinifyOptions {
97  /// Targets to compile the CSS for.
98  pub targets: Targets,
99  /// A list of known unused symbols, including CSS class names,
100  /// ids, and `@keyframe` names. The declarations of these will be removed.
101  pub unused_symbols: HashSet<String>,
102}
103
104/// A result returned from `to_css`, including the serialize CSS
105/// and other metadata depending on the input options.
106#[derive(Debug)]
107pub struct ToCssResult {
108  /// Serialized CSS code.
109  pub code: String,
110  /// A map of CSS module exports, if the `css_modules` option was
111  /// enabled during parsing.
112  pub exports: Option<CssModuleExports>,
113  /// A map of CSS module references, if the `css_modules` config
114  /// had `dashed_idents` enabled.
115  pub references: Option<CssModuleReferences>,
116  /// A list of dependencies (e.g. `@import` or `url()`) found in
117  /// the style sheet, if the `analyze_dependencies` option is enabled.
118  pub dependencies: Option<Vec<Dependency>>,
119}
120
121impl<'i, 'o> StyleSheet<'i, 'o, DefaultAtRule> {
122  /// Parse a style sheet from a string.
123  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  /// Creates a new style sheet with the given source filenames and rules.
133  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  /// Parse a style sheet from a string.
149  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  /// Returns the source map URL for the source at the given index.
211  pub fn source_map_url(&self, source_index: usize) -> Option<&String> {
212    self.source_map_urls.get(source_index)?.as_ref()
213  }
214
215  /// Returns the inline source map associated with the source at the given index.
216  #[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  /// Minify and transform the style sheet for the provided browser targets.
223  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    // @custom-media rules may be defined after they are referenced, but may only be defined at the top level
229    // of a stylesheet. Do a pre-scan here and create a lookup table by name.
230    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  /// Serialize the style sheet to a CSS string.
267  pub fn to_css(&self, options: PrinterOptions) -> Result<ToCssResult, Error<PrinterErrorKind>> {
268    // Make sure we always have capacity > 0: https://github.com/napi-rs/napi-rs/issues/1124.
269    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/// An inline style attribute, as in HTML or SVG.
343///
344/// Style attributes can be parsed from a string, minified and transformed
345/// for a set of target browsers, and serialied to a string.
346///
347/// # Example
348///
349/// ```
350/// use lightningcss::stylesheet::{
351///   StyleAttribute, ParserOptions, MinifyOptions, PrinterOptions
352/// };
353///
354/// // Parse a style sheet from a string.
355/// let mut style = StyleAttribute::parse(
356///   "color: yellow; font-family: 'Helvetica';",
357///   ParserOptions::default()
358/// ).unwrap();
359///
360/// // Minify the stylesheet.
361/// style.minify(MinifyOptions::default());
362///
363/// // Serialize it to a string.
364/// let res = style.to_css(PrinterOptions::default()).unwrap();
365/// assert_eq!(res.code, "color: #ff0; font-family: Helvetica");
366/// ```
367#[cfg_attr(feature = "visitor", derive(Visit))]
368#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
369pub struct StyleAttribute<'i> {
370  /// The declarations in the style attribute.
371  pub declarations: DeclarationBlock<'i>,
372  #[cfg_attr(feature = "visitor", skip_visit)]
373  sources: Vec<String>,
374}
375
376impl<'i> StyleAttribute<'i> {
377  /// Parses a style attribute from a string.
378  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  /// Minify and transform the style attribute for the provided browser targets.
391  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  /// Serializes the style attribute to a CSS string.
400  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    // Make sure we always have capacity > 0: https://github.com/napi-rs/napi-rs/issues/1124.
408    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}