Skip to content

Commit

Permalink
fix: prioritize important styles
Browse files Browse the repository at this point in the history
  • Loading branch information
tborg authored and Stranger6667 committed Nov 14, 2024
1 parent 143cd63 commit 606cc93
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [Unreleased]

- Prioritize `!important` rules when computing element styles. [#398](https://github.com/Stranger6667/css-inline/pull/398)

## [0.14.2] - 2024-11-11

### Changed
Expand Down
2 changes: 2 additions & 0 deletions bindings/c/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [Unreleased]

- Prioritize `!important` rules when computing element styles. [#398](https://github.com/Stranger6667/css-inline/pull/398)

## [0.14.2] - 2024-11-11

### Changed
Expand Down
2 changes: 2 additions & 0 deletions bindings/javascript/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [Unreleased]

- Prioritize `!important` rules when computing element styles. [#398](https://github.com/Stranger6667/css-inline/pull/398)

## [0.14.2] - 2024-11-11

### Changed
Expand Down
2 changes: 2 additions & 0 deletions bindings/python/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [Unreleased]

- Prioritize `!important` rules when computing element styles. [#398](https://github.com/Stranger6667/css-inline/pull/398)

## [0.14.2] - 2024-11-11

### Changed
Expand Down
2 changes: 2 additions & 0 deletions bindings/ruby/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [Unreleased]

- Prioritize `!important` rules when computing element styles. [#398](https://github.com/Stranger6667/css-inline/pull/398)

## [0.14.2] - 2024-11-11

### Changed
Expand Down
19 changes: 17 additions & 2 deletions css-inline/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,23 @@ impl<'a> CSSInliner<'a> {
for (name, value) in &declarations[*start..*end] {
match element_styles.entry(name.as_ref()) {
indexmap::map::Entry::Occupied(mut entry) => {
if entry.get().0 <= specificity {
entry.insert((specificity, *value));
match (
value.contains("!important"),
entry.get().1.contains("!important"),
) {
// Equal importance; the higher specificity wins.
(false, false) | (true, true) => {
if entry.get().0 <= specificity {
entry.insert((specificity, *value));
}
}
// Only the new value is important; it wins.
(true, false) => {
entry.insert((specificity, *value));
}
// The old value is important and the new one is not; keep
// the old value.
(false, true) => {}
}
}
indexmap::map::Entry::Vacant(entry) => {
Expand Down
26 changes: 26 additions & 0 deletions css-inline/tests/test_inlining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,32 @@ fn important_no_rule_exists() {
)
}

#[test]
fn important_multiple_rules() {
// `!important` rules should override other rules with the same specificity.
assert_inlined!(
style = ".blue { color: blue !important; } .reset { color: unset }",
body = r#"<h1 class="blue reset">Big Text</h1>"#,
expected = r#"<h1 class="blue reset" style="color: blue !important;">Big Text</h1>"#
);
// check in both directions
assert_inlined!(
style = ".reset { color: unset } .blue { color: blue !important; }",
body = r#"<h1 class="blue reset">Big Text</h1>"#,
expected = r#"<h1 class="blue reset" style="color: blue !important;">Big Text</h1>"#
);
}

#[test]
fn important_more_specific() {
// `!important` rules should override other important rules with less specificity.
assert_inlined!(
style = "h1 { color: unset !important } #title { color: blue !important; }",
body = r#"<h1 id="title">Big Text</h1>"#,
expected = r#"<h1 id="title" style="color: blue !important;">Big Text</h1>"#
);
}

#[test]
fn font_family_quoted() {
// When property value contains double quotes
Expand Down

0 comments on commit 606cc93

Please sign in to comment.