biome: đ Self-closing tags do not exist in HTML
Environment information
I don't have it locally, tried in playground instead.
https://biomejs.dev/playground/?enabledLinting=false&files.main.html=PABpAG4AcAB1AHQAPgAKADwAaQBtAGcAPgAKADwAYgByAD4ACgA%3D
What happened?
- Create valid .html file with void elements.
- Disable JSX syntax support in Biome
- Format the file with Biome
- Observe that Biome corrupted valid void HTML elements by making them self-closing.
Expected result
Since there are no self-closing tags in HMTL, Biome should keep valid void elements formatting. Leading slash should be added only where self-closing tags are valid: within XML-like content such as svg, xml or JSX.
For these who want to keep having broken Prettier formatting of void HTML tags, there could be a configuration option.
Code of Conduct
- I agree to follow Biomeâs Code of Conduct
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 21
- Comments: 25 (10 by maintainers)
When formatting HTML, you donât need to follow Prettier, you need to follow HTML.
While aligning with Prettier, you can make it even better: offer an option similar to what prettier-plugin-void-html plugin does, but natively. Iâd prefer Biome to be HTML-compatible by default and optionally Prettier-compatible, but thatâs just me đ
Thatâs really cool, because it allows to easily switch from Prettier to Biome in the future (and Iâd do it!).
Nevertheless, please consider that there are Prettier plugins aimed to fix Prettierâs void tag corruption like prettier-plugin-void-html. And if we use the plugin now, and we switch to Biome, our code will break.
Also, the wide presence of Prettier made a lot of new-comming front end developers to think that this syntax is valid.
So my suggestion would be to make this behavior configurable. So people who want to seamlessly migrate from Prettier can simply turn it on. The rest will have the valid HTML in their projects by default. And these who just join the front-end community using Biome will not be confused.
I had a small chat on Discord with the other maintainers, and they generally agree to the following plan:
I donât deny it, and I also think that having Prettier compatibility is what has made Biome what it is.
Yes, thatâs the plan. The option is mostly to make the transition from Prettier easier for users. We can bikeshed the name and semantically explain it as a divergence from the spec. While all the arguments of this issue are valid, if Biome makes the transition as painless as possible (effort, resources, code review times, etc.), itâs a big win for Biome as a project.
And another argument is that users may need Prettier alignment just once, when they migrate. Then they can forget about it, and enjoy beautifully formatted code made by Biome, aligned with standards đ
Seriously, I canât wait for the HTML + Svelte support to migrate đ
Itâs not just you, many of us are baffled at Prettierâs obstinate refusal to emit correct HTML code, and instead force XHTML syntax.
Being opinionated is okay when there are options, but when a tool is contradicting standards itâs obnoxious and inarguably wrong.
its unfortunate youâve hit this landmine by trying to align with prettier. the discussion over there on this very same topic has been going on for 7+ years now.
all of it drills down to the fact that HTML certainly should not have self-closing tags printed, but non-HTML (though HTML-like) template languages may.
JSX and vue in particular use self-closing tags. so your printer should treat them differently to HTML.
this shouldnât really be a configuration option, it should be a conditional behaviour based on the language youâre formatting. the mistake here is treating them all as if theyâre HTML when theyâre not.
if you do this right, i suspect itâll actually lead to more people switching to biome because prettier wonât fix it. this is one case where purposely not aligning will benefit you.
Fortunately, XHTML doesnât exist on the web. You need to work really hard to force browsers to parse your markup as XHTML. And then youâd end up breaking the whole page in a parsing error when a stray ampersand, missing closing tag, or quote is detected. Nobody wants that. So, this concern is purely theoretical.
As for the XML-based languages you can embed into HTML, like SVG or MathML, they have their own parsing rules that browsers apply when they detect
<svg>or<math>elements. Disregarding the difference in parsing languages is not a very smart thing to do. Yes, they look the same, but they work differently. You wouldnât format CSS inside of<style>or JS inside of<script>as HTML just because theyâre in the .html files, wouldnât you?I think language formatters should accept the reality of how languages and browsers work, not invent it.
The thing is, self-closing tags donât exist in HTML, but they are required for void elements in XML, XHTML and SVG.
Which means, if a
.htmlfile adds the XHTML doc type metadata*, the self closing tag on void elements becomes mandatory.https://developer.mozilla.org/en-US/docs/Glossary/Void_element#self-closing_tags
*And thatâs when the âissueâ arises, knowing when a
.htmlfile is an actual XHTML file spec, which can only be inferred by the metadata. Maybe prettier went for the convenient way, to add a self-closing tag every time. It doesnât harm HTML files, and XHTML files donât have parsing errorsThere are two different cases we are conflating a bit:
Take input like this:
If you use JSX, you would expect output to be:
but browser actually parses this as:
Prettier happily leaves this as is, even though in HTML this produces unexpected results. Even Svelte creator was surprised to learn about this behavior last year, so regular users wonât do any better.
Biomeâs objective is to align with prettierâs formatting. You are right in that technically self closing tags donât exist in HTML. However, they do exist in HTML-ish templating languages, which we do plan to support. Regardless of correctness, we still need to handle self closing syntax because its a frequently used pattern.
The behavior displayed in your playground is intentional and not a bug. Biome correctly determines that these are void elements and uses self-closing syntax when formatting.
Doing it the âprettierâ i.e. incorrect way contradicts Biomeâs stated goal:
@dyc3
Thank you for the consideration.
Also, sorry, the option shouldnât be to diverge from prettier. The option should be to diverge from the spec.
In other words, the default should emphatically not be what prettier does.
Call the option
no-html-void-tagsorprettier-style-void-tagsor something.This is because otherwise you are encouraging and misinforming new and novice web developers.
Of course đ
Not exactly: the HTML spec has detailed algorithms for dealing with incorrect markup, and all browser parsers are based on that. So, whatever you do, all browsers will give you the same result. But most of the developers have no idea how complex those algorithms are. I wrote an article about jumping HTML tags you might find interesting.
Resources that talk about the spec? I already linked the one. I can link the actual spec, too: https://html.spec.whatwg.org/#start-tags
(emphasis mine).
It permits them, but recommends against using them because they can break attributes.
Looking at it again, I was wrong about browsers being able to do literally anything, as they still have to ignore it in the case that it doesnât mess with attributes. Even so.
Svelte does not support self closing tags for regular HTML elements as of Svelte 5.0. Components do support self closing tags tho.
This is incorrect. The HTML spec advises against using them. In other words, it is against the spec. At best, they are ignored. At worst, they change the expected meaning.
Weâre not bikeshedding here, by the way. Prettier refuses to put aside their arrogance. Biome shouldnât follow suit.
FYI Biome doesnât support HTML yet. The playground allows HTML because we are adding its support via an experimental flag while we work on it.
We added the
/because we follow Prettier formatting. What should we do instead?This is about aligning with Prettier. Whatâs your suggestion?