@@ -463,25 +463,36 @@ fn merge_styles(
463
463
let mut parser = cssparser:: Parser :: new ( & mut input) ;
464
464
let declarations =
465
465
cssparser:: DeclarationListParser :: new ( & mut parser, parser:: CSSDeclarationListParser ) ;
466
- // New rules should not override old ones and we store selectors inline to check the old rules later
466
+ // New rules should not override old ones unless !important and we store selectors inline to check the old rules later
467
467
let mut buffer: SmallVec < [ String ; 8 ] > = smallvec ! [ ] ;
468
- let mut final_styles = String :: with_capacity ( 256 ) ;
468
+ let mut final_styles: Vec < String > = Vec :: new ( ) ;
469
469
for declaration in declarations {
470
470
let ( name, value) = declaration?;
471
- final_styles. push_str ( & name) ;
472
- final_styles. push ( ':' ) ;
473
- replace_double_quotes ! ( final_styles, name, value) ;
474
- final_styles. push ( ';' ) ;
475
- // This property won't be taken from new styles
471
+ let mut style = String :: with_capacity ( 256 ) ;
472
+ style. push_str ( & name) ;
473
+ style. push ( ':' ) ;
474
+ replace_double_quotes ! ( style, name, value) ;
475
+ final_styles. push ( style) ;
476
+ // This property won't be taken from new styles unless it's !important
476
477
buffer. push ( name. to_string ( ) ) ;
477
478
}
478
479
for ( property, ( _, value) ) in new_styles {
479
- if !buffer. contains ( property) {
480
- final_styles. push_str ( property) ;
481
- final_styles. push ( ':' ) ;
482
- replace_double_quotes ! ( final_styles, property, value) ;
483
- final_styles. push ( ';' ) ;
480
+ let index = buffer. iter ( ) . position ( |r| r == property) ;
481
+ let is_important = value. ends_with ( "!important" ) ;
482
+ if index == None || is_important {
483
+ let mut style = String :: with_capacity ( 256 ) ;
484
+ style. push_str ( & property) ;
485
+ style. push ( ':' ) ;
486
+ replace_double_quotes ! ( style, property, value) ;
487
+ // Strip !important so additional styles with !important can override this
488
+ style = style. replace ( "!important" , "" ) ;
489
+ style = style. trim ( ) . to_string ( ) ;
490
+ if index != None {
491
+ final_styles[ index. unwrap ( ) ] = style;
492
+ } else {
493
+ final_styles. push ( style) ;
494
+ }
484
495
}
485
496
}
486
- Ok ( final_styles)
497
+ Ok ( final_styles. join ( ";" ) )
487
498
}
0 commit comments