//! 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::compat::Feature; use crate::context::{DeclarationContext, PropertyHandlerContext}; use crate::css_modules::{CssModule, CssModuleExports, CssModuleReferences}; use crate::declaration::{DeclarationBlock, DeclarationHandler}; use crate::dependencies::Dependency; use crate::error::{Error, ErrorLocation, MinifyErrorKind, ParserError, PrinterError, PrinterErrorKind}; use crate::parser::{DefaultAtRuleParser, TopLevelRuleParser}; use crate::printer::Printer; use crate::rules::{CssRule, CssRuleList, MinifyContext}; use crate::targets::Browsers; use crate::traits::ToCss; #[cfg(feature = "visitor")] use crate::visitor::{Visit, VisitTypes, Visitor}; use cssparser::{AtRuleParser, Parser, ParserInput, RuleListParser}; #[cfg(feature = "sourcemap")] use parcel_sourcemap::SourceMap; use std::collections::{HashMap, HashSet}; pub use crate::parser::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, T::AtRule: schemars::JsonSchema" ) )] pub struct StyleSheet<'i, 'o, T: AtRuleParser<'i> = DefaultAtRuleParser> { /// A list of top-level rules within the style sheet. #[cfg_attr( feature = "serde", serde( borrow, bound( serialize = "T::AtRule: serde::Serialize", deserialize = "T::AtRule: serde::Deserialize<'de>" ) ) )] pub rules: CssRuleList<'i, T::AtRule>, /// 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