Skip to content

Commit cdecc99

Browse files
committed
fix(format/html): preserve whitespace between a HtmlSingleTextExpression/HtmlElement and HtmlContent
1 parent 0196c0e commit cdecc99

18 files changed

+261
-59
lines changed

.changeset/evil-fans-live.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@biomejs/biome": patch
3+
---
4+
5+
Fixed [#8584](https://github.com/biomejs/biome/issues/8584): The HTML formatter will preserve whitespace after expressions, which now matches Svelte's Prettier plugin.
6+
7+
```diff
8+
- <h1>Hello, {framework}and Svelte!</h1>
9+
+ <h1>Hello, {framework} and Svelte!</h1>
10+
```

crates/biome_html_formatter/src/utils/children.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,29 @@ where
396396
} else {
397397
builder.entry(HtmlChild::NonText(child.clone()));
398398
}
399+
400+
// Check for trailing whitespace on embedded content elements (e.g., after `}` in `{framework} `)
401+
// This preserves spaces between expressions/elements and following text content.
402+
if matches!(
403+
&child,
404+
AnyHtmlElement::AnyHtmlContent(_)
405+
| AnyHtmlElement::HtmlElement(_)
406+
| AnyHtmlElement::HtmlSelfClosingElement(_)
407+
) && let Some(last_token) = child.syntax().last_token()
408+
&& last_token.has_trailing_whitespace()
409+
{
410+
// Check if trailing trivia contains a newline
411+
let has_newline = last_token
412+
.trailing_trivia()
413+
.pieces()
414+
.any(|piece| piece.is_newline());
415+
if has_newline {
416+
builder.entry(HtmlChild::Newline);
417+
} else {
418+
builder.entry(HtmlChild::Whitespace);
419+
}
420+
}
421+
399422
prev_child_was_content = false;
400423
}
401424
}

crates/biome_html_formatter/tests/specs/html/elements/inline/mixed-block-inline.html.snap

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
source: crates/biome_formatter_test/src/snapshot_builder.rs
33
info: elements/inline/mixed-block-inline.html
4-
snapshot_kind: text
54
---
65
# Input
76

@@ -30,11 +29,8 @@ Self close void elements: never
3029

3130
```html
3231
<span>
33-
hello <a>foo</a> <a>foo</a>
34-
<div>foo</div>
35-
<a>foo</a> <a>foo</a> <a>foo</a> <b>bar</b><b>bar</b><b>bar</b> <a>foo</a>
36-
<a>foo</a> <a>foo</a>
37-
<div>foo</div>
32+
hello <a>foo</a> <a>foo</a> <div>foo</div> <a>foo</a> <a>foo</a> <a>foo</a>
33+
<b>bar</b><b>bar</b><b>bar</b> <a>foo</a> <a>foo</a> <a>foo</a> <div>foo</div>
3834
<a>foo</a> <a>foo</a>
3935
</span>
4036
```

crates/biome_html_formatter/tests/specs/html/elements/inline/tags-hug-content-longer-w-attr.html.snap

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
source: crates/biome_formatter_test/src/snapshot_builder.rs
33
info: elements/inline/tags-hug-content-longer-w-attr.html
4-
snapshot_kind: text
54
---
65
# Input
76

@@ -30,7 +29,7 @@ Self close void elements: never
3029

3130
```html
3231
<b class="really long really long really long really long really long"
33-
>asdfasdf foo bar things <i>much more</i>longer things lorem ipsum or
32+
>asdfasdf foo bar things <i>much more</i> longer things lorem ipsum or
3433
something idk i put pineapple in strombolis</b
3534
>
3635
```

crates/biome_html_formatter/tests/specs/html/svelte/each_with_destructuring.svelte.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Self close void elements: never
4848
{/each}
4949
5050
{#each users as { name, email }, i}
51-
<div>{i}: {name}({email})</div>
51+
<div>{i}: {name} ({email})</div>
5252
{/each}
5353
5454
{#each products as [id, title]}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
const framework = "Astro";
3+
</script>
4+
5+
<h1>
6+
Hello, {framework}
7+
and Svelte!
8+
</h1>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
source: crates/biome_formatter_test/src/snapshot_builder.rs
3+
info: svelte/whitespace/issue-8584-w-newline.svelte
4+
---
5+
# Input
6+
7+
```svelte
8+
<script>
9+
const framework = "Astro";
10+
</script>
11+
12+
<h1>
13+
Hello, {framework}
14+
and Svelte!
15+
</h1>
16+
17+
```
18+
19+
20+
=============================
21+
22+
# Outputs
23+
24+
## Output 1
25+
26+
-----
27+
Indent style: Tab
28+
Indent width: 2
29+
Line ending: LF
30+
Line width: 80
31+
Attribute Position: Auto
32+
Bracket same line: false
33+
Whitespace sensitivity: css
34+
Indent script and style: false
35+
Self close void elements: never
36+
-----
37+
38+
```svelte
39+
<script>
40+
const framework = "Astro";
41+
</script>
42+
43+
<h1>
44+
Hello, {framework}
45+
and Svelte!
46+
</h1>
47+
```
48+
49+
50+
51+
## Unimplemented nodes/tokens
52+
53+
"\n\tconst framework = \"Astro\";\n" => 8..37
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
const framework = "Astro";
3+
</script>
4+
5+
<h1>Hello, {framework} and Svelte!</h1>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
source: crates/biome_formatter_test/src/snapshot_builder.rs
3+
info: svelte/whitespace/issue-8584.svelte
4+
---
5+
# Input
6+
7+
```svelte
8+
<script>
9+
const framework = "Astro";
10+
</script>
11+
12+
<h1>Hello, {framework} and Svelte!</h1>
13+
14+
```
15+
16+
17+
=============================
18+
19+
# Outputs
20+
21+
## Output 1
22+
23+
-----
24+
Indent style: Tab
25+
Indent width: 2
26+
Line ending: LF
27+
Line width: 80
28+
Attribute Position: Auto
29+
Bracket same line: false
30+
Whitespace sensitivity: css
31+
Indent script and style: false
32+
Self close void elements: never
33+
-----
34+
35+
```svelte
36+
<script>
37+
const framework = "Astro";
38+
</script>
39+
40+
<h1>Hello, {framework} and Svelte!</h1>
41+
```
42+
43+
44+
45+
## Unimplemented nodes/tokens
46+
47+
"\n\tconst framework = \"Astro\";\n" => 8..37
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<p>
2+
<span>Foo</span>
3+
Bar
4+
</p>

0 commit comments

Comments
 (0)