Struct lightningcss::stylesheet::StyleSheet
source · pub struct StyleSheet<'i, 'o, T: AtRuleParser<'i> = DefaultAtRuleParser> {
pub rules: CssRuleList<'i, T::AtRule>,
pub sources: Vec<String>,
/* private fields */
}
Expand description
A CSS style sheet, representing a .css
file or inline <style>
element.
Style sheets can be parsed from a string, constructed from scratch, or created using a 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");
Fields§
§rules: CssRuleList<'i, T::AtRule>
A list of top-level rules within the style sheet.
sources: Vec<String>
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.
Implementations§
source§impl<'i, 'o, T: AtRuleParser<'i>> StyleSheet<'i, 'o, T>where
T::AtRule: ToCss,
impl<'i, 'o, T: AtRuleParser<'i>> StyleSheet<'i, 'o, T>where
T::AtRule: ToCss,
sourcepub fn new(
sources: Vec<String>,
rules: CssRuleList<'i, T::AtRule>,
options: ParserOptions<'o, 'i, T>
) -> StyleSheet<'i, 'o, T>
pub fn new(
sources: Vec<String>,
rules: CssRuleList<'i, T::AtRule>,
options: ParserOptions<'o, 'i, T>
) -> StyleSheet<'i, 'o, T>
Creates a new style sheet with the given source filenames and rules.
sourcepub fn parse(
code: &'i str,
options: ParserOptions<'o, 'i, T>
) -> Result<Self, Error<ParserError<'i>>>
pub fn parse(
code: &'i str,
options: ParserOptions<'o, 'i, T>
) -> Result<Self, Error<ParserError<'i>>>
Parse a style sheet from a string.
sourcepub fn source_map_url(&self, source_index: usize) -> Option<&String>
pub fn source_map_url(&self, source_index: usize) -> Option<&String>
Returns the source map URL for the source at the given index.
sourcepub fn source_map(&self, source_index: usize) -> Option<SourceMap>
pub fn source_map(&self, source_index: usize) -> Option<SourceMap>
Returns the inline source map associated with the source at the given index.
sourcepub fn minify(
&mut self,
options: MinifyOptions
) -> Result<(), Error<MinifyErrorKind>>
pub fn minify(
&mut self,
options: MinifyOptions
) -> Result<(), Error<MinifyErrorKind>>
Minify and transform the style sheet for the provided browser targets.
sourcepub fn to_css(
&self,
options: PrinterOptions<'_>
) -> Result<ToCssResult, Error<PrinterErrorKind>>
pub fn to_css(
&self,
options: PrinterOptions<'_>
) -> Result<ToCssResult, Error<PrinterErrorKind>>
Serialize the style sheet to a CSS string.