diff --git a/.changeset/rotten-pans-ring.md b/.changeset/rotten-pans-ring.md new file mode 100644 index 000000000000..2f4d99ca4564 --- /dev/null +++ b/.changeset/rotten-pans-ring.md @@ -0,0 +1,10 @@ +--- +"@biomejs/biome": patch +--- + +Fixed an issue with the HTML formatter where it wouldn't add a space before the `/>` in self closing elements. This brings the HTML formatter more in line with Prettier. + +```diff +- ++ +``` diff --git a/crates/biome_html_formatter/src/html/auxiliary/self_closing_element.rs b/crates/biome_html_formatter/src/html/auxiliary/self_closing_element.rs index db1694a42da6..d250ffe28b80 100644 --- a/crates/biome_html_formatter/src/html/auxiliary/self_closing_element.rs +++ b/crates/biome_html_formatter/src/html/auxiliary/self_closing_element.rs @@ -56,9 +56,9 @@ impl FormatNodeRule for FormatHtmlSelfClosingElement { // To resolve this, these tokens either need to be passed to or deferred to sibling text elements when // whitespace sensitivity would require it. if slash_token.is_some() { - write!(f, [slash_token.format()])?; + write!(f, [if_group_fits_on_line(&space()), slash_token.format()])?; } else { - write!(f, [token("/")])?; + write!(f, [if_group_fits_on_line(&space()), token("/")])?; } } // We remove the slash only from void elements @@ -67,6 +67,10 @@ impl FormatNodeRule for FormatHtmlSelfClosingElement { write!(f, [format_removed(slash_token)])?; } } else { + if slash_token.is_some() { + // only add a space before the slash if it exists. + write!(f, [space()])?; + } write!(f, [slash_token.format()])?; } diff --git a/crates/biome_html_formatter/tests/specs/html/component-frameworks/astro-component-casing.astro.snap b/crates/biome_html_formatter/tests/specs/html/component-frameworks/astro-component-casing.astro.snap index 7b69b59a43b4..0b398e097ef0 100644 --- a/crates/biome_html_formatter/tests/specs/html/component-frameworks/astro-component-casing.astro.snap +++ b/crates/biome_html_formatter/tests/specs/html/component-frameworks/astro-component-casing.astro.snap @@ -42,8 +42,8 @@ import Button from './Button.astro'; import TextInput from './TextInput.astro'; --- - ``` diff --git a/crates/biome_html_formatter/tests/specs/html/component-frameworks/large-self-closing.svelte b/crates/biome_html_formatter/tests/specs/html/component-frameworks/large-self-closing.svelte new file mode 100644 index 000000000000..b7a5bf30a9cb --- /dev/null +++ b/crates/biome_html_formatter/tests/specs/html/component-frameworks/large-self-closing.svelte @@ -0,0 +1,6 @@ +