//! CSS style sheets and style attributes. //! //! A [StyleSheet](StyleSheet) represents a `.css` file or `` element in HTML. //! A [StyleAttribute](StyleAttribute) represents an inline `style` attribute in HTML. use crate::context::{DeclarationContext, PropertyHandlerContext}; use crate::css_modules::{hash, CssModule, CssModuleExports, CssModuleReferences}; use crate::declaration::{DeclarationBlock, DeclarationHandler}; use crate::dependencies::Dependency; use crate::error::{Error, ErrorLocation, MinifyErrorKind, ParserError, PrinterError, PrinterErrorKind}; use crate::parser::{DefaultAtRule, DefaultAtRuleParser, TopLevelRuleParser}; use crate::printer::Printer; use crate::rules::{CssRule, CssRuleList, MinifyContext}; use crate::targets::{should_compile, Targets}; use crate::traits::{AtRuleParser, ToCss}; use crate::values::string::CowArcStr; #[cfg(feature = "visitor")] use crate::visitor::{Visit, VisitTypes, Visitor}; use cssparser::{Parser, ParserInput, StyleSheetParser}; #[cfg(feature = "sourcemap")] use parcel_sourcemap::SourceMap; use std::collections::{HashMap, HashSet}; pub use crate::parser::{ParserFlags, ParserOptions}; pub use crate::printer::PrinterOptions; pub use crate::printer::PseudoClasses; /// A CSS style sheet, representing a `.css` file or inline `` element. /// /// Style sheets can be parsed from a string, constructed from scratch, /// or created using a [Bundler](super::bundler::Bundler). Then, they can be /// minified and transformed for a set of target browsers, and serialied to a string. /// /// # Example /// /// ``` /// use lightningcss::stylesheet::{ /// StyleSheet, ParserOptions, MinifyOptions, PrinterOptions /// }; /// /// // Parse a style sheet from a string. /// let mut stylesheet = StyleSheet::parse( /// r#" /// .foo { /// color: red; /// } /// /// .bar { /// color: red; /// } /// "#, /// ParserOptions::default() /// ).unwrap(); /// /// // Minify the stylesheet. /// stylesheet.minify(MinifyOptions::default()).unwrap(); /// /// // Serialize it to a string. /// let res = stylesheet.to_css(PrinterOptions::default()).unwrap(); /// assert_eq!(res.code, ".foo, .bar {\n color: red;\n}\n"); /// ``` #[derive(Debug)] #[cfg_attr( feature = "serde", derive(serde::Serialize, serde::Deserialize), serde(rename_all = "camelCase") )] #[cfg_attr( feature = "jsonschema", derive(schemars::JsonSchema), schemars(rename = "StyleSheet", bound = "T: schemars::JsonSchema") )] pub struct StyleSheet<'i, 'o, T = DefaultAtRule> { /// A list of top-level rules within the style sheet. #[cfg_attr(feature = "serde", serde(borrow))] pub rules: CssRuleList<'i, T>, /// A list of file names for all source files included within the style sheet. /// Sources are referenced by index in the `loc` property of each rule. pub sources: Vec, /// The source map URL extracted from the original style sheet. pub(crate) source_map_urls: Vec