Skip to content

Commit

Permalink
fix(format/html): keep single quotes if string contains a double quote
Browse files Browse the repository at this point in the history
  • Loading branch information
dyc3 committed Sep 17, 2024
1 parent 1fe6f78 commit 9b37249
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 13 deletions.
34 changes: 22 additions & 12 deletions crates/biome_html_formatter/src/html/auxiliary/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,32 @@ impl FormatNodeRule<HtmlString> for FormatHtmlString {
let value_text = value.text().trim();

if !(value_text.starts_with('"') && value_text.ends_with('"')) {
let range = if value_text.starts_with('\'') && value_text.ends_with('\'') {
let contains_double_quote = value_text.contains('"');

let range = if value_text.starts_with('\'')
&& value_text.ends_with('\'')
&& !contains_double_quote
{
value.text_range().add_start(1.into()).sub_end(1.into())
} else {
value.text_range()
};
write!(
f,
[format_replaced(
value,
&group(&format_args![
text("\""),
located_token_text(value, range),
text("\""),
])
)]
)?;

if !contains_double_quote {
write!(
f,
[format_replaced(
value,
&group(&format_args![
text("\""),
located_token_text(value, range),
text("\""),
])
)]
)?;
} else {
value.format().fmt(f)?;
}
return Ok(());
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_html_formatter/tests/spec_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ mod spec_test;
mod formatter {

mod html {
tests_macros::gen_tests! {"tests/specs/**/*.html", crate::spec_test::run, "unknown"}
tests_macros::gen_tests! {"tests/specs/**/*.html", crate::spec_test::run, ""}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<img src='foo.png' alt='should keep "these" quotes' />
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: attributes-single-quotes.html
---
# Input

```html
<img src='foo.png' alt='should keep "these" quotes' />
```


=============================

# Outputs

## Output 1

-----
Indent style: Tab
Indent width: 2
Line ending: LF
Line width: 80
Attribute Position: Auto
-----

```html
<img src="foo.png" alt='should keep "these" quotes' />```

0 comments on commit 9b37249

Please sign in to comment.