@@ -30,6 +30,7 @@ mod html;
30
30
mod parser;
31
31
32
32
pub use error:: InlineError ;
33
+ use html5ever:: tendril:: StrTendril ;
33
34
use indexmap:: IndexMap ;
34
35
use smallvec:: { smallvec, SmallVec } ;
35
36
use std:: {
@@ -257,7 +258,7 @@ impl<'a> CSSInliner<'a> {
257
258
let attributes = & mut element. attributes ;
258
259
if let Some ( existing_style) = attributes. get_style_mut ( ) {
259
260
styles. sort_unstable_by ( |_, ( a, _) , _, ( b, _) | a. cmp ( b) ) ;
260
- * existing_style = merge_styles ( existing_style, & styles) ?. into ( ) ;
261
+ merge_styles ( existing_style, styles) ?;
261
262
} else {
262
263
let mut final_styles = String :: with_capacity ( 128 ) ;
263
264
let mut styles = styles. iter ( ) . collect :: < Vec < _ > > ( ) ;
@@ -409,9 +410,9 @@ pub fn inline_to<W: Write>(html: &str, target: &mut W) -> Result<()> {
409
410
}
410
411
411
412
fn merge_styles (
412
- existing_style : & str ,
413
+ existing_style : & mut StrTendril ,
413
414
new_styles : & IndexMap < String , ( Specificity , String ) > ,
414
- ) -> Result < String > {
415
+ ) -> Result < ( ) > {
415
416
// Parse existing declarations in the "style" attribute
416
417
let mut input = cssparser:: ParserInput :: new ( existing_style) ;
417
418
let mut parser = cssparser:: Parser :: new ( & mut input) ;
@@ -423,7 +424,8 @@ fn merge_styles(
423
424
for declaration in declarations {
424
425
let ( name, value) = declaration?;
425
426
// Allocate enough space for the new style
426
- let mut style = String :: with_capacity ( name. len ( ) + value. len ( ) + 2 ) ;
427
+ let mut style =
428
+ String :: with_capacity ( name. len ( ) . saturating_add ( value. len ( ) ) . saturating_add ( 2 ) ) ;
427
429
style. push_str ( & name) ;
428
430
style. push_str ( ": " ) ;
429
431
replace_double_quotes ! ( style, name, value. trim( ) ) ;
@@ -456,5 +458,16 @@ fn merge_styles(
456
458
( None , Some ( _) ) => { }
457
459
}
458
460
}
459
- Ok ( final_styles. join ( ";" ) )
461
+ drop ( input) ;
462
+ existing_style. clear ( ) ;
463
+ let mut first = true ;
464
+ for style in & final_styles {
465
+ if first {
466
+ first = false
467
+ } else {
468
+ existing_style. push_char ( ';' ) ;
469
+ }
470
+ existing_style. push_slice ( style) ;
471
+ }
472
+ Ok ( ( ) )
460
473
}
0 commit comments