@@ -134,6 +134,7 @@ mod parser;
134
134
135
135
pub use error:: InlineError ;
136
136
use std:: collections:: HashMap ;
137
+ use std:: io:: Write ;
137
138
138
139
#[ derive( Debug ) ]
139
140
struct Rule < ' i > {
@@ -201,9 +202,19 @@ impl CSSInliner {
201
202
}
202
203
}
203
204
204
- /// Inline CSS styles from <style> tags to matching elements in the HTML tree.
205
+ /// Inline CSS styles from <style> tags to matching elements in the HTML tree and return a
206
+ /// string.
205
207
#[ inline]
206
208
pub fn inline ( & self , html : & str ) -> Result < String , InlineError > {
209
+ let mut out = vec ! [ ] ;
210
+ self . inline_to ( html, & mut out) ?;
211
+ Ok ( String :: from_utf8_lossy ( & out) . to_string ( ) )
212
+ }
213
+
214
+ /// Inline CSS & write the result to a generic writer. Use it if you want to write
215
+ /// the inlined document to a file.
216
+ #[ inline]
217
+ pub fn inline_to < W : Write > ( & self , html : & str , target : & mut W ) -> Result < ( ) , InlineError > {
207
218
let document = parse_html ( ) . one ( html) ;
208
219
for style_tag in document
209
220
. select ( "style" )
@@ -245,9 +256,8 @@ impl CSSInliner {
245
256
style_tag. as_node ( ) . detach ( )
246
257
}
247
258
}
248
- let mut out = vec ! [ ] ;
249
- document. serialize ( & mut out) ?;
250
- Ok ( String :: from_utf8_lossy ( & out) . to_string ( ) )
259
+ document. serialize ( target) ?;
260
+ Ok ( ( ) )
251
261
}
252
262
}
253
263
@@ -264,6 +274,12 @@ pub fn inline(html: &str) -> Result<String, InlineError> {
264
274
CSSInliner :: default ( ) . inline ( html)
265
275
}
266
276
277
+ /// Shortcut for inlining CSS with default parameters and writing the output to a generic writer.
278
+ #[ inline]
279
+ pub fn inline_to < W : Write > ( html : & str , target : & mut W ) -> Result < ( ) , InlineError > {
280
+ CSSInliner :: default ( ) . inline_to ( html, target)
281
+ }
282
+
267
283
fn merge_styles (
268
284
existing_style : & str ,
269
285
new_styles : & [ parser:: Declaration ] ,
0 commit comments