Add option to allow self-closing void tags#100
Conversation
|
Sorry for abruptly closing a PR that contains work you've done, but I don't intend to support breaking away from the HTML spec for no real reason. Prettier is doing the world a disservice by helping perpetuate the wrong fact that self closing tags are a thing in HTML. I do understand that functionality-wise self-closing tags on void elements are not harmful in any way, but they are harmful in the fact that they create confusion about what's what in HTML. Additionally, editors like VSCode have simplistic formatters that seem to suggest that self-closing tags have semantic meaning, causing even more confusion and mistakes: In VSCode (and any editor that uses vscode's extracted LSPs) this: <body>
<div />
<div>
</body>Is formatted like this (divs seem siblings): <body>
<div />
<div>
</body>While in reality the second div is nested into the first div (because html elements can't self-close). One of the goals of SuperHTML is to teach its users about HTML and, while the HTML spec can be put into question, there has to be a practical reason for breaking away from it. I will open an Issue for adding automatic removal of self-closing tags by fmt on void elements. |
|
I will note that these changes only affect behavior for void elements. Constructs like |
|
I've added the behavior I mentioned above in 2bf8f4d in case you're interested. Note that contributions are welcome but I'm generally against adding configuration options. I would recommend opening an issue for discussion before jumping into coding in the future. |
|
@kristoff-it, technically, I think if I'm not mistaken, HTML allows void elements to have the self-closing tag (which would only be forbidden for non-void elements). |
|
If you want to entertain a discussion about what HTML allows or not, you must start from quoting a passage of the WHATWG Living HTML spec that corroborates your understanding. Additionally, you need to differentiate between things that the HTML spec considers allowed and things that the spec offers fallback behavior for. For example some passages about parsing attribute value sublanguages offer procedures for ignoring certain kinds of syntax errors, which does not mean that those are part of the spec. My understanding is that self-closing elements are not a concept that exists in HTML and, as a fallback, self-closing slashes are ignored by the parser. Additionally, I'm actively against allowing self-closing slashes because, as stated above, it causes confusion, resulting in unexpected resulting nesting, which is something that a tool like SuperHTML should provide guaranteed protection against. |
https://html.spec.whatwg.org/multipage/syntax.html#start-tags 13.1.2.1 Start tagsStart tags must have the following format:
|
à la Pretter.
For
lspandfmt, there are three modes:never: Treat<br />as invalid, and convert to a void tag when doing an LSP format. (Default. Should match status quo.)preserve: Accept<br />, but do not automatically add or remove the slashes.always: Automatically convert all void elements into self-closing tags.To do:
(This is my first time writing any Zig. I've probably done something quite dubious.)