Skip to content

Commit 12e8b84

Browse files
committed
fix: Precedence of element styles over other styles
Signed-off-by: Dmitry Dygalo <[email protected]>
1 parent 8680a9a commit 12e8b84

File tree

7 files changed

+50
-12
lines changed

7 files changed

+50
-12
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Fixed
6+
7+
- Precedence of element styles over other styles. [#364](https://github.com/Stranger6667/css-inline/issues/364)
8+
59
## [0.14.0] - 2024-04-01
610

711
### Added

bindings/c/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Fixed
6+
7+
- Precedence of element styles over other styles. [#364](https://github.com/Stranger6667/css-inline/issues/364)
8+
59
## [0.14.0] - 2024-04-01
610

711
### Added

bindings/javascript/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Fixed
6+
7+
- Precedence of element styles over other styles. [#364](https://github.com/Stranger6667/css-inline/issues/364)
8+
59
## [0.14.0] - 2024-04-01
610

711
### Added

bindings/python/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Fixed
6+
7+
- Precedence of element styles over other styles. [#364](https://github.com/Stranger6667/css-inline/issues/364)
8+
59
## [0.14.0] - 2024-04-01
610

711
### Added

bindings/ruby/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Fixed
6+
7+
- Precedence of element styles over other styles. [#364](https://github.com/Stranger6667/css-inline/issues/364)
8+
59
## [0.14.0] - 2024-04-01
610

711
### Added

css-inline/src/html/serializer.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,8 @@ fn merge_styles<Wr: Write>(
486486
declarations_buffer.push(buffer);
487487
};
488488
}
489+
// Keep the number of current declarations to write them last as they have the precedence
490+
let current_declarations_count = parsed_declarations_count;
489491
// Next, we iterate over the new styles and merge them into our existing set
490492
// New rules will not override old ones unless they are marked as `!important`
491493
for (property, (_, value)) in new_styles {
@@ -532,13 +534,20 @@ fn merge_styles<Wr: Write>(
532534
}
533535

534536
let mut first = true;
535-
for declaration in &declarations_buffer[..parsed_declarations_count] {
536-
if first {
537-
first = false;
538-
} else {
539-
writer.write_all(b";")?;
537+
for range in [
538+
// First, write the new rules
539+
current_declarations_count..parsed_declarations_count,
540+
// Then, write the current rules
541+
0..current_declarations_count,
542+
] {
543+
for declaration in &declarations_buffer[range] {
544+
if first {
545+
first = false;
546+
} else {
547+
writer.write_all(b";")?;
548+
}
549+
writer.write_all(declaration)?;
540550
}
541-
writer.write_all(declaration)?;
542551
}
543552
Ok(())
544553
}

css-inline/tests/test_inlining.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ fn specificity_same_selector() {
157157
padding: 10px;
158158
padding-left: 12px;
159159
}"#,
160-
body = r#"<a class="test-class" href="https://example.com">Test</a>"#,
160+
body = r#"<a class="test-class">Test</a>"#,
161161
// Then the final style should come from the more specific selector
162-
expected = r#"<a class="test-class" href="https://example.com" style="padding-top: 15px;padding: 10px;padding-left: 12px;">Test</a>"#
162+
expected = r#"<a class="test-class" style="padding-top: 15px;padding: 10px;padding-left: 12px;">Test</a>"#
163163
)
164164
}
165165

@@ -181,7 +181,16 @@ fn specificity_different_selectors_existing_style() {
181181
.test { padding-left: 16px; }
182182
h1 { padding: 0; }"#,
183183
body = r#"<h1 class="test" style="color: blue;"></h1>"#,
184-
expected = r#"<h1 class="test" style="color: blue;padding: 0;padding-left: 16px"></h1>"#
184+
expected = r#"<h1 class="test" style="padding: 0;padding-left: 16px;color: blue"></h1>"#
185+
)
186+
}
187+
188+
#[test]
189+
fn specificity_merge_with_existing_style() {
190+
assert_inlined!(
191+
style = ".test { padding: 0; }",
192+
body = r#"<h1 class="test" style="padding-left: 16px"></h1>"#,
193+
expected = r#"<h1 class="test" style="padding: 0;padding-left: 16px"></h1>"#
185194
)
186195
}
187196

@@ -243,7 +252,7 @@ fn important_no_rule_exists() {
243252
assert_inlined!(
244253
style = "h1 { color: blue !important; }",
245254
body = r#"<h1 style="margin:0">Big Text</h1>"#,
246-
expected = r#"<h1 style="margin: 0;color: blue">Big Text</h1>"#
255+
expected = r#"<h1 style="color: blue;margin: 0">Big Text</h1>"#
247256
)
248257
}
249258

@@ -382,7 +391,7 @@ fn existing_styles_with_merge() {
382391
body = r#"<h1 style="color: blue">Hello world!</h1>"#,
383392
// Then the existing rule should be preferred
384393
// And the new style should be merged
385-
expected = r#"<h1 style="color: blue;font-size: 14px">Hello world!</h1>"#
394+
expected = r#"<h1 style="font-size: 14px;color: blue">Hello world!</h1>"#
386395
)
387396
}
388397

@@ -397,7 +406,7 @@ fn existing_styles_with_merge_multiple_tags() {
397406
r#"<h1 style="color: blue">Hello world!</h1><h1 style="color: blue">Hello world!</h1>"#,
398407
// Then the existing rule should be preferred
399408
// And the new style should be merged
400-
expected = r#"<h1 style="color: blue;font-size: 14px">Hello world!</h1><h1 style="color: blue;font-size: 14px">Hello world!</h1>"#
409+
expected = r#"<h1 style="font-size: 14px;color: blue">Hello world!</h1><h1 style="font-size: 14px;color: blue">Hello world!</h1>"#
401410
)
402411
}
403412

0 commit comments

Comments
 (0)