Skip to content

chore(html/formatter): better control over spacing #5604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .changeset/silver-groups-shout.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ An option `html.formatter.selfCloseVoidElements` allows to control whether the t

```diff
- <input />
+ <input >
+ <input>
```

If you come from Prettier and you want to keep the same formatting behaviour, you should set the option to `"always"`:
Expand All @@ -38,7 +38,7 @@ If you come from Prettier and you want to keep the same formatting behaviour, yo
```

```diff
- <input >
- <input>
+ <input />
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub(crate) struct FormatHtmlAttribute {
}

pub(crate) struct FormatHtmlAttributeOptions {
/// Whether or not this attribute belongs to a canonical tag.
/// Whether this attribute belongs to a canonical tag.
pub is_canonical_html_element: bool,

/// The name of the tag this attribute belongs to.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ use biome_html_syntax::{HtmlAttributeName, HtmlAttributeNameFields, HtmlTagName}

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatHtmlAttributeName {
/// Whether or not this attribute belongs to a canonical tag.
/// Whether this attribute belongs to a canonical tag.
pub is_canonical_html_element: bool,

/// The name of the tag this attribute belongs to.
pub tag_name: Option<HtmlTagName>,
}

pub(crate) struct FormatHtmlAttributeNameOptions {
/// Whether or not this attribute belongs to a canonical tag.
/// Whether this attribute belongs to a canonical tag.
pub is_canonical_html_element: bool,

/// The name of the tag this attribute belongs to.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl FormatNodeRule<HtmlSelfClosingElement> for FormatHtmlSelfClosingElement {
let name = name?;
let is_canonical_html_tag = is_canonical_html_tag(&name);

write!(f, [l_angle_token.format(), name.format(), space()])?;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This space doesn't make a difference, so I removed it

write!(f, [l_angle_token.format(), name.format()])?;

let attr_group_id = f.group_id("element-attr-group-id");
write!(
Expand All @@ -41,7 +41,7 @@ impl FormatNodeRule<HtmlSelfClosingElement> for FormatHtmlSelfClosingElement {
// When these tokens are borrowed, they are managed by the sibling `HtmlElementList` formatter.
if bracket_same_line {
write!(f, [hard_space()])?;
} else {
} else if self_close_void_elements.is_always() || attributes.len() >= 1 {
write!(f, [soft_line_break_or_space()])?;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub(crate) struct FormatHtmlAttributeList {
}

pub(crate) struct FormatHtmlAttributeListOptions {
/// Whether or not this attribute list belongs to a canonical tag.
/// Whether this attribute list belongs to a canonical tag.
pub is_canonical_html_element: bool,

/// The name of the tag this attribute list belongs to.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
Lorem ipsum<span />dolor sit amet, consectetur adipiscing elit. Sed eu diam vel
sem congue pulvinar at vitae purus. Morbi volutpat arcu massa, eu laoreet eros
feugiat ac.<b
attribute="really long for this example"
actually="so long it should break and become multiline"
>Etiam sit amet turpis blandit, volutpat magna nec, luctus justo. Nam nec
augue mauris. Nullam sit amet blandit massa, at finibus felis. Nunc ut
vestibulum nulla.</b
Lorem ipsum<b>dolor sit amet, consectetur adipiscing elit. Sed eu diam vel
sem congue pulvinar at vitae purus. Morbi volutpat arcu massa, eu laoreet eros
feugiat ac.</b><b
attribute="really long for this example"
actually="so long it should break and become multiline"
>Etiam sit amet turpis blandit, volutpat magna nec, luctus justo. Nam nec
augue mauris. Nullam sit amet blandit massa, at finibus felis. Nunc ut
vestibulum nulla.</b
>Donec maximus euismod egestas. Sed tempus semper efficitur. Suspendisse maximus
ut risus vel sollicitudin. Maecenas eu bibendum lorem.<span
attribute="really long for this example"
actually="so long it should break and become multiline"
/>Sed porttitor commodo commodo. Morbi luctus consequat maximus. Vestibulum
attribute="really long for this example"
actually="so long it should break and become multiline"
>Sed porttitor commodo commodo. Morbi luctus consequat maximus. Vestibulum
viverra libero quis lacus euismod, ut consequat ante convallis.
</span>
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ snapshot_kind: text
# Input

```html
Lorem ipsum<span />dolor sit amet, consectetur adipiscing elit. Sed eu diam vel
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to change this example with valid self-closing tags

sem congue pulvinar at vitae purus. Morbi volutpat arcu massa, eu laoreet eros
feugiat ac.<b
attribute="really long for this example"
actually="so long it should break and become multiline"
>Etiam sit amet turpis blandit, volutpat magna nec, luctus justo. Nam nec
augue mauris. Nullam sit amet blandit massa, at finibus felis. Nunc ut
vestibulum nulla.</b
Lorem ipsum<b>dolor sit amet, consectetur adipiscing elit. Sed eu diam vel
sem congue pulvinar at vitae purus. Morbi volutpat arcu massa, eu laoreet eros
feugiat ac.</b><b
attribute="really long for this example"
actually="so long it should break and become multiline"
>Etiam sit amet turpis blandit, volutpat magna nec, luctus justo. Nam nec
augue mauris. Nullam sit amet blandit massa, at finibus felis. Nunc ut
vestibulum nulla.</b
>Donec maximus euismod egestas. Sed tempus semper efficitur. Suspendisse maximus
ut risus vel sollicitudin. Maecenas eu bibendum lorem.<span
attribute="really long for this example"
actually="so long it should break and become multiline"
/>Sed porttitor commodo commodo. Morbi luctus consequat maximus. Vestibulum
attribute="really long for this example"
actually="so long it should break and become multiline"
>Sed porttitor commodo commodo. Morbi luctus consequat maximus. Vestibulum
viverra libero quis lacus euismod, ut consequat ante convallis.
</span>

```

Expand All @@ -43,9 +44,12 @@ Self close void elements: never
-----

```html
Lorem ipsum<span />dolor sit amet, consectetur adipiscing elit. Sed eu diam vel
sem congue pulvinar at vitae purus. Morbi volutpat arcu massa, eu laoreet eros
feugiat ac.<b
Lorem ipsum<b
>dolor sit amet, consectetur adipiscing elit. Sed eu diam vel sem congue
pulvinar at vitae purus. Morbi volutpat arcu massa, eu laoreet eros feugiat
ac.</b
>
<b
attribute="really long for this example"
actually="so long it should break and become multiline"
>Etiam sit amet turpis blandit, volutpat magna nec, luctus justo. Nam nec
Expand All @@ -55,6 +59,7 @@ feugiat ac.<b
ut risus vel sollicitudin. Maecenas eu bibendum lorem.<span
attribute="really long for this example"
actually="so long it should break and become multiline"
/>Sed porttitor commodo commodo. Morbi luctus consequat maximus. Vestibulum
viverra libero quis lacus euismod, ut consequat ante convallis.
>Sed porttitor commodo commodo. Morbi luctus consequat maximus. Vestibulum
viverra libero quis lacus euismod, ut consequat ante convallis.
</span>
```
30 changes: 15 additions & 15 deletions crates/biome_html_formatter/tests/specs/html/self-closing.html.snap
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,20 @@ Self close void elements: never
```html
<div>
<br >
<img >
<area >
<base >
<br >
<col >
<embed >
<hr >
<img >
<input >
<link >
<meta >
<source >
<track >
<wbr >
<br>
<img>
<area>
<base>
<br>
<col>
<embed>
<hr>
<img>
<input>
<link>
<meta>
<source>
<track>
<wbr>
</div>
```
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
<div>
<br>
<img>
<area>
<base>
<br>
<col>
<embed>
<hr>
<img>
<input>
<link>
<meta>
<source>
<track>
<wbr>
</div>
<br>
<img>
<area>
<base>
<col>
<embed>
<hr>
<img>
<input>
<link>
<meta>
<source>
<track>
<wbr>
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,20 @@ snapshot_kind: text
# Input

```html
<div>
<br>
<img>
<area>
<base>
<br>
<col>
<embed>
<hr>
<img>
<input>
<link>
<meta>
<source>
<track>
<wbr>
</div>
<br>
<img>
<area>
<base>
<col>
<embed>
<hr>
<img>
<input>
<link>
<meta>
<source>
<track>
<wbr>

```

Expand All @@ -46,23 +43,20 @@ Self close void elements: never
-----

```html
<div>
<br >
<img >
<area >
<base >
<br >
<col >
<embed >
<hr >
<img >
<input >
<link >
<meta >
<source >
<track >
<wbr >
</div>
<br>
<img>
<area>
<base>
<col>
<embed>
<hr>
<img>
<input>
<link>
<meta>
<source>
<track>
<wbr>
```

## Output 1
Expand All @@ -80,21 +74,18 @@ Self close void elements: always
-----

```html
<div>
<br />
<img />
<area />
<base />
<br />
<col />
<embed />
<hr />
<img />
<input />
<link />
<meta />
<source />
<track />
<wbr />
</div>
<br />
<img />
<area />
<base />
<col />
<embed />
<hr />
<img />
<input />
<link />
<meta />
<source />
<track />
<wbr />
```