Skip to content

Commit ab2948e

Browse files
committed
fix(html/formatter): preserve closing > before self-closing elements
Exclude self-closing elements from the token borrowing pattern by only setting next_is_adjacent_inline when the next element is an HtmlElement.
1 parent 412a08d commit ab2948e

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

.changeset/sour-ducks-flash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@biomejs/biome": patch
3+
---
4+
5+
Fixed an issue where the HTML formatter mangled the closing `>` of inline elements when followed by self-closing elements like `<br>` or `<img>`.

crates/biome_html_formatter/src/html/lists/element_list.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,11 +763,17 @@ impl FormatHtmlElementList {
763763
// Take any borrowed `>` from the previous sibling element
764764
let current_borrowed_r_angle = borrowed_sibling_r_angle.take();
765765

766+
let next_can_borrow = next_is_adjacent_inline
767+
&& matches!(
768+
children_iter.peek(),
769+
Some(HtmlChild::NonText(AnyHtmlElement::HtmlElement(_)))
770+
);
771+
766772
// Create the element formatter with borrowing options
767773
let element_format = format_element_with_borrowing(
768774
non_text,
769775
current_borrowed_r_angle,
770-
next_is_adjacent_inline,
776+
next_can_borrow,
771777
);
772778

773779
if needs_outer_group {
@@ -864,7 +870,9 @@ impl FormatHtmlElementList {
864870
if next_is_adjacent_inline {
865871
prev_inline_group_id = Some(non_text_group_id);
866872
// Store the closing r_angle token from this element for the next sibling
867-
borrowed_sibling_r_angle = non_text.closing_r_angle_token();
873+
if next_can_borrow {
874+
borrowed_sibling_r_angle = non_text.closing_r_angle_token();
875+
}
868876
} else {
869877
prev_inline_group_id = None;
870878
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<code>cubing.js</code><br>
2+
<span>foo</span><br>
3+
<b>bar</b><br>
4+
<a>link</a><img>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
source: crates/biome_formatter_test/src/snapshot_builder.rs
3+
info: elements/inline/inline-before-self-closing.html
4+
---
5+
# Input
6+
7+
```html
8+
<code>cubing.js</code><br>
9+
<span>foo</span><br>
10+
<b>bar</b><br>
11+
<a>link</a><img>
12+
13+
```
14+
15+
16+
=============================
17+
18+
# Outputs
19+
20+
## Output 1
21+
22+
-----
23+
Indent style: Tab
24+
Indent width: 2
25+
Line ending: LF
26+
Line width: 80
27+
Attribute Position: Auto
28+
Bracket same line: false
29+
Whitespace sensitivity: css
30+
Indent script and style: false
31+
Self close void elements: never
32+
Trailing newline: true
33+
-----
34+
35+
```html
36+
<code>cubing.js</code><br>
37+
<span>foo</span><br>
38+
<b>bar</b><br>
39+
<a>link</a><img>
40+
41+
```

0 commit comments

Comments
 (0)