Skip to content

Commit e119e8a

Browse files
committed
fix: Inline !important styles being overwritten by stylesheet !important styles
Signed-off-by: Dmitry Dygalo <dmitry@dygalo.dev>
1 parent 23a7244 commit e119e8a

File tree

9 files changed

+47
-4
lines changed

9 files changed

+47
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Fixed
6+
7+
- Inline `!important` styles being overwritten by stylesheet `!important` styles. [#637](https://github.com/Stranger6667/css-inline/issues/637)
8+
59
## [0.19.0] - 2025-12-29
610

711
### Added

bindings/c/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Fixed
6+
7+
- Inline `!important` styles being overwritten by stylesheet `!important` styles. [#637](https://github.com/Stranger6667/css-inline/issues/637)
8+
59
## [0.19.0] - 2025-12-29
610

711
### Added

bindings/java/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Fixed
6+
7+
- Inline `!important` styles being overwritten by stylesheet `!important` styles. [#637](https://github.com/Stranger6667/css-inline/issues/637)
8+
59
## [0.19.0] - 2025-12-29
610

711
### Added

bindings/javascript/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Fixed
6+
7+
- Inline `!important` styles being overwritten by stylesheet `!important` styles. [#637](https://github.com/Stranger6667/css-inline/issues/637)
8+
59
## [0.19.1] - 2025-12-29
610

711
### Fixed

bindings/php/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Fixed
6+
7+
- Inline `!important` styles being overwritten by stylesheet `!important` styles. [#637](https://github.com/Stranger6667/css-inline/issues/637)
8+
59
## [0.19.0] - 2025-12-29
610

711
- Initial public release

bindings/python/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Fixed
6+
7+
- Inline `!important` styles being overwritten by stylesheet `!important` styles. [#637](https://github.com/Stranger6667/css-inline/issues/637)
8+
59
## [0.19.0] - 2025-12-29
610

711
### Added

bindings/ruby/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Fixed
6+
7+
- Inline `!important` styles being overwritten by stylesheet `!important` styles. [#637](https://github.com/Stranger6667/css-inline/issues/637)
8+
59
## [0.19.0] - 2025-12-29
610

711
### Added

css-inline/src/html/serializer.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -557,11 +557,14 @@ fn merge_styles<Wr: Write>(
557557
}),
558558
) {
559559
// The new rule is `!important` and there's an existing rule with the same name
560-
// In this case, we override the existing rule with the new one
560+
// Only override if the existing inline rule is NOT `!important`.
561+
// Per CSS spec: inline `!important` takes precedence over stylesheet `!important`
561562
(Some(value), Some(buffer)) => {
562-
// We keep the rule name and the colon-space suffix - '<rule>: `
563-
buffer.truncate(property.len().saturating_add(STYLE_SEPARATOR.len()));
564-
write_declaration_value(buffer, value)?;
563+
if !buffer.ends_with(b"!important") {
564+
// We keep the rule name and the colon-space suffix - '<rule>: `
565+
buffer.truncate(property.len().saturating_add(STYLE_SEPARATOR.len()));
566+
write_declaration_value(buffer, value)?;
567+
}
565568
}
566569
// There's no existing rule with the same name, but the new rule is `!important`
567570
// In this case, we add the new rule with the `!important` suffix removed

css-inline/tests/test_inlining.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,18 @@ fn important_more_specific() {
291291
);
292292
}
293293

294+
#[test]
295+
fn important_inline_wins_over_stylesheet_important() {
296+
// Inline `!important` rules should override stylesheet `!important` rules.
297+
// Per CSS spec: "Inline important styles take precedence over all other important author styles"
298+
// See: https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values/important#inline_styles
299+
assert_inlined!(
300+
style = "h1 { color: blue !important; }",
301+
body = r#"<h1 style="color: red !important;">Big Text</h1>"#,
302+
expected = r#"<h1 style="color: red !important">Big Text</h1>"#
303+
);
304+
}
305+
294306
#[test]
295307
fn font_family_quoted() {
296308
// When property value contains double quotes

0 commit comments

Comments
 (0)