@@ -463,25 +463,36 @@ fn merge_styles(
463463 let mut parser = cssparser:: Parser :: new ( & mut input) ;
464464 let declarations =
465465 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
467467 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 ( ) ;
469469 for declaration in declarations {
470470 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
476477 buffer. push ( name. to_string ( ) ) ;
477478 }
478479 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+ }
484495 }
485496 }
486- Ok ( final_styles)
497+ Ok ( final_styles. join ( ";" ) )
487498}
0 commit comments