File tree Expand file tree Collapse file tree 7 files changed +53
-2
lines changed Expand file tree Collapse file tree 7 files changed +53
-2
lines changed Original file line number Diff line number Diff line change 22
33## [ Unreleased]
44
5+ - Prioritize ` !important ` rules when computing element styles. [ #398 ] ( https://github.com/Stranger6667/css-inline/pull/398 )
6+
57## [ 0.14.2] - 2024-11-11
68
79### Changed
Original file line number Diff line number Diff line change 22
33## [ Unreleased]
44
5+ - Prioritize ` !important ` rules when computing element styles. [ #398 ] ( https://github.com/Stranger6667/css-inline/pull/398 )
6+
57## [ 0.14.2] - 2024-11-11
68
79### Changed
Original file line number Diff line number Diff line change 22
33## [ Unreleased]
44
5+ - Prioritize ` !important ` rules when computing element styles. [ #398 ] ( https://github.com/Stranger6667/css-inline/pull/398 )
6+
57## [ 0.14.2] - 2024-11-11
68
79### Changed
Original file line number Diff line number Diff line change 22
33## [ Unreleased]
44
5+ - Prioritize ` !important ` rules when computing element styles. [ #398 ] ( https://github.com/Stranger6667/css-inline/pull/398 )
6+
57## [ 0.14.2] - 2024-11-11
68
79### Changed
Original file line number Diff line number Diff line change 22
33## [ Unreleased]
44
5+ - Prioritize ` !important ` rules when computing element styles. [ #398 ] ( https://github.com/Stranger6667/css-inline/pull/398 )
6+
57## [ 0.14.2] - 2024-11-11
68
79### Changed
Original file line number Diff line number Diff line change @@ -459,8 +459,23 @@ impl<'a> CSSInliner<'a> {
459459 for ( name, value) in & declarations[ * start..* end] {
460460 match element_styles. entry ( name. as_ref ( ) ) {
461461 indexmap:: map:: Entry :: Occupied ( mut entry) => {
462- if entry. get ( ) . 0 <= specificity {
463- entry. insert ( ( specificity, * value) ) ;
462+ match (
463+ value. contains ( "!important" ) ,
464+ entry. get ( ) . 1 . contains ( "!important" ) ,
465+ ) {
466+ // Equal importance; the higher specificity wins.
467+ ( false , false ) | ( true , true ) => {
468+ if entry. get ( ) . 0 <= specificity {
469+ entry. insert ( ( specificity, * value) ) ;
470+ }
471+ }
472+ // Only the new value is important; it wins.
473+ ( true , false ) => {
474+ entry. insert ( ( specificity, * value) ) ;
475+ }
476+ // The old value is important and the new one is not; keep
477+ // the old value.
478+ ( false , true ) => { }
464479 }
465480 }
466481 indexmap:: map:: Entry :: Vacant ( entry) => {
Original file line number Diff line number Diff line change @@ -256,6 +256,32 @@ fn important_no_rule_exists() {
256256 )
257257}
258258
259+ #[ test]
260+ fn important_multiple_rules ( ) {
261+ // `!important` rules should override other rules with the same specificity.
262+ assert_inlined ! (
263+ style = ".blue { color: blue !important; } .reset { color: unset }" ,
264+ body = r#"<h1 class="blue reset">Big Text</h1>"# ,
265+ expected = r#"<h1 class="blue reset" style="color: blue !important;">Big Text</h1>"#
266+ ) ;
267+ // check in both directions
268+ assert_inlined ! (
269+ style = ".reset { color: unset } .blue { color: blue !important; }" ,
270+ body = r#"<h1 class="blue reset">Big Text</h1>"# ,
271+ expected = r#"<h1 class="blue reset" style="color: blue !important;">Big Text</h1>"#
272+ ) ;
273+ }
274+
275+ #[ test]
276+ fn important_more_specific ( ) {
277+ // `!important` rules should override other important rules with less specificity.
278+ assert_inlined ! (
279+ style = "h1 { color: unset !important } #title { color: blue !important; }" ,
280+ body = r#"<h1 id="title">Big Text</h1>"# ,
281+ expected = r#"<h1 id="title" style="color: blue !important;">Big Text</h1>"#
282+ ) ;
283+ }
284+
259285#[ test]
260286fn font_family_quoted ( ) {
261287 // When property value contains double quotes
You can’t perform that action at this time.
0 commit comments