Skip to content

Commit bbf6b04

Browse files
tborgStranger6667
authored andcommitted
fix: double quote when extending inline styles
1 parent 48cafa7 commit bbf6b04

File tree

7 files changed

+47
-7
lines changed

7 files changed

+47
-7
lines changed

CHANGELOG.md

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

77
- Bump MSRV to `1.70`.
88

9+
### Fixed
10+
11+
- Replace double quotes when merging styles. [#392](https://github.com/Stranger6667/css-inline/issues/392)
12+
913
## [0.14.1] - 2024-04-27
1014

1115
### Fixed

bindings/c/CHANGELOG.md

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

77
- Bump MSRV to `1.70`.
88

9+
### Fixed
10+
11+
- Replace double quotes when merging styles. [#392](https://github.com/Stranger6667/css-inline/issues/392)
12+
913
## [0.14.1] - 2024-04-27
1014

1115
### Fixed

bindings/javascript/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
### Fixed
1010

1111
- Build on `linux-arm-gnueabihf`
12+
- Replace double quotes when merging styles. [#392](https://github.com/Stranger6667/css-inline/issues/392)
1213

1314
## [0.14.1] - 2024-04-27
1415

bindings/python/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
- Update `PyO3` to `0.22.0`.
88
- Bump MSRV to `1.70`.
99

10+
### Fixed
11+
12+
- Replace double quotes when merging styles. [#392](https://github.com/Stranger6667/css-inline/issues/392)
13+
1014
## [0.14.1] - 2024-04-27
1115

1216
### Fixed

bindings/ruby/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
- Update `magnus` to `0.7`.
88
- Bump MSRV to `1.70`.
99

10+
### Fixed
11+
12+
- Replace double quotes when merging styles. [#392](https://github.com/Stranger6667/css-inline/issues/392)
13+
1014
## [0.14.1] - 2024-04-27
1115

1216
### Fixed

css-inline/src/html/serializer.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,11 @@ fn write_declaration<Wr: Write>(
399399
) -> Result<(), InlineError> {
400400
writer.write_all(name.as_bytes())?;
401401
writer.write_all(STYLE_SEPARATOR)?;
402+
write_declaration_value(writer, value)
403+
}
404+
405+
#[inline]
406+
fn write_declaration_value<Wr: Write>(writer: &mut Wr, value: &str) -> Result<(), InlineError> {
402407
let value = value.trim();
403408
if value.as_bytes().contains(&b'"') {
404409
// Roughly based on `str::replace`
@@ -429,9 +434,7 @@ macro_rules! push_or_update {
429434
($style_buffer:expr, $length:expr, $name: expr, $value:expr) => {{
430435
if let Some(style) = $style_buffer.get_mut($length) {
431436
style.clear();
432-
style.extend_from_slice($name.as_bytes());
433-
style.extend_from_slice(STYLE_SEPARATOR);
434-
style.extend_from_slice($value.trim().as_bytes());
437+
write_declaration(style, &$name, $value)?;
435438
} else {
436439
let value = $value.trim();
437440
let mut style = Vec::with_capacity(
@@ -440,9 +443,7 @@ macro_rules! push_or_update {
440443
.saturating_add(STYLE_SEPARATOR.len())
441444
.saturating_add(value.len()),
442445
);
443-
style.extend_from_slice($name.as_bytes());
444-
style.extend_from_slice(STYLE_SEPARATOR);
445-
style.extend_from_slice(value.as_bytes());
446+
write_declaration(&mut style, &$name, $value)?;
446447
$style_buffer.push(style);
447448
};
448449
$length = $length.saturating_add(1);
@@ -507,7 +508,7 @@ fn merge_styles<Wr: Write>(
507508
(Some(value), Some(buffer)) => {
508509
// We keep the rule name and the colon-space suffix - '<rule>: `
509510
buffer.truncate(property.len().saturating_add(STYLE_SEPARATOR.len()));
510-
buffer.extend_from_slice(value.trim().as_bytes());
511+
write_declaration_value(buffer, value)?;
511512
}
512513
// There's no existing rule with the same name, but the new rule is `!important`
513514
// In this case, we add the new rule with the `!important` suffix removed

css-inline/tests/test_inlining.rs

+22
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,28 @@ fn font_family_quoted() {
267267
)
268268
}
269269

270+
#[test]
271+
fn font_family_quoted_with_existing_inline_style() {
272+
// When property value contains double quotes
273+
assert_inlined!(
274+
style = r#"h1 { font-family: "Open Sans", sans-serif; }"#,
275+
body = r#"<h1 style="whitespace: nowrap">Hello world!</h1>"#,
276+
// Then it should be replaced with single quotes
277+
expected = r#"<h1 style="font-family: 'Open Sans', sans-serif;whitespace: nowrap">Hello world!</h1>"#
278+
)
279+
}
280+
281+
#[test]
282+
fn font_family_quoted_with_inline_style_override() {
283+
// When property value contains double quotes
284+
assert_inlined!(
285+
style = r#"h1 { font-family: "Open Sans", sans-serif !important; }"#,
286+
body = r#"<h1 style="font-family: Helvetica; whitespace: nowrap">Hello world!</h1>"#,
287+
// Then it should be replaced with single quotes
288+
expected = r#"<h1 style="font-family: 'Open Sans', sans-serif;whitespace: nowrap">Hello world!</h1>"#
289+
)
290+
}
291+
270292
#[test]
271293
fn other_property_quoted() {
272294
// When property value contains double quotes

0 commit comments

Comments
 (0)