1+ //! CSS serialization and source map generation.
2+
13use crate :: css_modules:: CssModule ;
24use crate :: dependencies:: Dependency ;
35use crate :: error:: { Error , ErrorLocation , PrinterError , PrinterErrorKind } ;
@@ -7,24 +9,54 @@ use crate::vendor_prefix::VendorPrefix;
79use cssparser:: { serialize_identifier, SourceLocation } ;
810use parcel_sourcemap:: { OriginalLocation , SourceMap } ;
911
12+ /// Options that control how CSS is serialized to a string.
1013#[ derive( Default ) ]
1114pub struct PrinterOptions < ' a > {
15+ /// Whether to minify the CSS, i.e. remove white space.
1216 pub minify : bool ,
17+ /// An optional reference to a source map to write mappings into.
1318 pub source_map : Option < & ' a mut SourceMap > ,
19+ /// Browser targets to output the CSS for.
1420 pub targets : Option < Browsers > ,
21+ /// Whether to analyze dependencies (i.e. `@import` and `url()`).
22+ /// If true, the dependencies are returned as part of the
23+ /// [ToCssResult](super::stylesheet::ToCssResult).
24+ ///
25+ /// When enabled, `@import` rules are removed, and `url()` dependencies
26+ /// are replaced with hashed placeholders that can be replaced with the final
27+ /// urls later (after bundling).
1528 pub analyze_dependencies : bool ,
29+ /// A mapping of pseudo classes to replace with class names that can be applied
30+ /// from JavaScript. Useful for polyfills, for example.
1631 pub pseudo_classes : Option < PseudoClasses < ' a > > ,
1732}
1833
34+ /// A mapping of user action pseudo classes to replace with class names.
35+ ///
36+ /// See [PrinterOptions](PrinterOptions).
1937#[ derive( Default , Debug ) ]
2038pub struct PseudoClasses < ' a > {
39+ /// The class name to replace `:hover` with.
2140 pub hover : Option < & ' a str > ,
41+ /// The class name to replace `:active` with.
2242 pub active : Option < & ' a str > ,
43+ /// The class name to replace `:focus` with.
2344 pub focus : Option < & ' a str > ,
45+ /// The class name to replace `:focus-visible` with.
2446 pub focus_visible : Option < & ' a str > ,
47+ /// The class name to replace `:focus-within` with.
2548 pub focus_within : Option < & ' a str > ,
2649}
2750
51+ /// A `Printer` represents a destination to output serialized CSS, as used in
52+ /// the [ToCss](super::traits::ToCss) trait. It can wrap any destination that
53+ /// implements [std::fmt::Write](std::fmt::Write), such as a [String](String).
54+ ///
55+ /// A `Printer` keeps track of the current line and column position, and uses
56+ /// this to generate a source map if provided in the options.
57+ ///
58+ /// `Printer` also includes helper functions that assist with writing output
59+ /// that respects options such as `minify`, and `css_modules`.
2860pub struct Printer < ' a , W > {
2961 pub ( crate ) sources : Option < & ' a Vec < String > > ,
3062 dest : & ' a mut W ,
@@ -45,6 +77,7 @@ pub struct Printer<'a, W> {
4577}
4678
4779impl < ' a , W : std:: fmt:: Write + Sized > Printer < ' a , W > {
80+ /// Create a new Printer wrapping the given destination.
4881 pub fn new ( dest : & ' a mut W , options : PrinterOptions < ' a > ) -> Printer < ' a , W > {
4982 Printer {
5083 sources : None ,
@@ -68,6 +101,7 @@ impl<'a, W: std::fmt::Write + Sized> Printer<'a, W> {
68101 }
69102 }
70103
104+ /// Returns the current source filename that is being printed.
71105 pub fn filename ( & self ) -> & str {
72106 if let Some ( sources) = self . sources {
73107 if let Some ( f) = sources. get ( self . source_index as usize ) {
@@ -80,12 +114,17 @@ impl<'a, W: std::fmt::Write + Sized> Printer<'a, W> {
80114 }
81115 }
82116
117+ /// Writes a raw string to the underlying destination.
118+ ///
119+ /// NOTE: Is is assumed that the string does not contain any newline characters.
120+ /// If such a string is written, it will break source maps.
83121 pub fn write_str ( & mut self , s : & str ) -> Result < ( ) , PrinterError > {
84122 self . col += s. len ( ) as u32 ;
85123 self . dest . write_str ( s) ?;
86124 Ok ( ( ) )
87125 }
88126
127+ /// Write a single character to the underlying destination.
89128 pub fn write_char ( & mut self , c : char ) -> Result < ( ) , PrinterError > {
90129 if c == '\n' {
91130 self . line += 1 ;
@@ -97,6 +136,10 @@ impl<'a, W: std::fmt::Write + Sized> Printer<'a, W> {
97136 Ok ( ( ) )
98137 }
99138
139+ /// Writes a single whitespace character, unless the `minify` option is enabled.
140+ ///
141+ /// Use `write_char` instead if you wish to force a space character to be written,
142+ /// regardless of the `minify` option.
100143 pub fn whitespace ( & mut self ) -> Result < ( ) , PrinterError > {
101144 if self . minify {
102145 return Ok ( ( ) ) ;
@@ -105,6 +148,8 @@ impl<'a, W: std::fmt::Write + Sized> Printer<'a, W> {
105148 self . write_char ( ' ' )
106149 }
107150
151+ /// Writes a delimeter character, followed by whitespace (depending on the `minify` option).
152+ /// If `ws_before` is true, then whitespace is also written before the delimeter.
108153 pub fn delim ( & mut self , delim : char , ws_before : bool ) -> Result < ( ) , PrinterError > {
109154 if ws_before {
110155 self . whitespace ( ) ?;
@@ -113,6 +158,8 @@ impl<'a, W: std::fmt::Write + Sized> Printer<'a, W> {
113158 self . whitespace ( )
114159 }
115160
161+ /// Writes a newline character followed by indentation.
162+ /// If the `minify` option is enabled, then nothing is printed.
116163 pub fn newline ( & mut self ) -> Result < ( ) , PrinterError > {
117164 if self . minify {
118165 return Ok ( ( ) ) ;
@@ -126,26 +173,32 @@ impl<'a, W: std::fmt::Write + Sized> Printer<'a, W> {
126173 Ok ( ( ) )
127174 }
128175
176+ /// Increases the current indent level.
129177 pub fn indent ( & mut self ) {
130178 self . indent += 2 ;
131179 }
132180
181+ /// Decreases the current indent level.
133182 pub fn dedent ( & mut self ) {
134183 self . indent -= 2 ;
135184 }
136185
186+ /// Increases the current indent level by the given number of characters.
137187 pub fn indent_by ( & mut self , amt : u8 ) {
138188 self . indent += amt;
139189 }
140190
191+ /// Decreases the current indent level by the given number of characters.
141192 pub fn dedent_by ( & mut self , amt : u8 ) {
142193 self . indent -= amt;
143194 }
144195
196+ /// Returns whether the indent level is greater than one.
145197 pub fn is_nested ( & self ) -> bool {
146198 self . indent > 2
147199 }
148200
201+ /// Adds a mapping to the source map, if any.
149202 pub fn add_mapping ( & mut self , loc : Location ) {
150203 self . source_index = loc. source_index ;
151204 if let Some ( map) = & mut self . source_map {
@@ -162,6 +215,9 @@ impl<'a, W: std::fmt::Write + Sized> Printer<'a, W> {
162215 }
163216 }
164217
218+ /// Writes a CSS identifier to the underlying destination, escaping it
219+ /// as appropriate. If the `css_modules` option was enabled, then a hash
220+ /// is added, and the mapping is added to the CSS module.
165221 pub fn write_ident ( & mut self , ident : & str ) -> Result < ( ) , PrinterError > {
166222 let hash = if let Some ( css_module) = & self . css_module {
167223 Some ( css_module. hash )
@@ -183,6 +239,7 @@ impl<'a, W: std::fmt::Write + Sized> Printer<'a, W> {
183239 Ok ( ( ) )
184240 }
185241
242+ /// Returns an error of the given kind at the provided location in the current source file.
186243 pub fn error ( & self , kind : PrinterErrorKind , loc : SourceLocation ) -> Error < PrinterErrorKind > {
187244 Error {
188245 kind,
0 commit comments