Prettier and closing slashes in HTML #242
-
We're starting to use Prettier to format code samples on MDN, and one issue in particular has come up. Prettier wants to add closing slashes to empty HTML elements: <input type="text" /> ...rather than: <input type="text"> This was in conflict with our coding guidelines, which have since been updated (mdn/content#20528). It seems that many people do not like this Prettier behavior:
It's not that clear what the best option here is though. Options include:
(Edited: "noone likes"->"many people do not like") |
Beta Was this translation helpful? Give feedback.
Replies: 10 comments 14 replies
-
I think one thing that worries me is: perhaps the use case for doc samples is different from that for production code. For production code, you can just write the code any way you like and the formatter will make everything consistent. In a way, the choice doesn't matter much (see https://dev.to/michaeljota/i-changed-my-opinion-about-prettier-not-formatting-void-elements-as-is-and-this-is-why-50hk). But code samples are designed to be read not written, so maybe these formatting choices are more important for us. |
Beta Was this translation helpful? Give feedback.
-
There is option to fork Prettier and include the changes that would be beneficial if it comes to it. I already tried this and was able to add some features that could be useful:
```html no-format
<input type="text">
```
So, it's easy to create fork and implement these changes for Prettier. However, forking Prettier would mean that someone needs to host and maintain the fork and keep it up-to-date. :) |
Beta Was this translation helpful? Give feedback.
-
This is AFAIK possible. You can only override Prettier based on file name, but you can't say that "don't format any html". This would also need forking. :( |
Beta Was this translation helpful? Give feedback.
-
This is just self-selection bias. In the last meeting, I think we re-affirmed consensus that editors find trailing slashes more readable. |
Beta Was this translation helpful? Give feedback.
-
I think the following points are relevant:
I think Prettier is, therefore, unsuited for teaching materials and, if Prettier won't budge, I think MDN (or other teaching materials) either shouldn't use Prettier or should use a version with the trailing slash on void elements patched out. |
Beta Was this translation helpful? Give feedback.
-
I have no strong feelings here. Nevertheless: I'm curious to know the advantages and disadvantages of both solutions, adding slashes or not adding slashes. Like Adding slashes makes the code XML compatible, or Not making them write the code easier to write, … This would allow us to know the best solution for our readership, decoupling this problem from the "How do you reach this ideal solution" question. |
Beta Was this translation helpful? Give feedback.
-
Let's read what the W3C people who maintain the HTML validator have to say about this: Trailing slashes in void-element start tags do not mark the start tags as self-closing. The whole page is a good read. |
Beta Was this translation helpful? Give feedback.
-
What are the next steps on this topic? |
Beta Was this translation helpful? Give feedback.
-
The google style guides are almost an antithesis of what we are teaching at MDN. But Google doesn't hire n00bs; so their target market is not ours. On the contrary. As someone who teaches new-to-dev people all the time, the /> is super helpful. Some internal “best practices”, like the one for Google devs, tell developers not to close out any tags that don’t need to be closed out; such as The mishmash of what is seen on viewsource, between self-closing, closing all, closing only the required, and closing only those that would actually break prod makes “view source” not a good teacher in this area. I recommend “erring” on showing examples showing the strictest method, and allowing people to slack as they learn, rather than showing the opposite. The google recommends committing optional tags, like So, yeah, as someone who focuses on teaching new-to-dev people HTML, I always close empty elements, always include optional closing tags, and always include optional tags. Best practices for teaching are not the same as "reduce every single byte possible" best practices. |
Beta Was this translation helpful? Give feedback.
-
This discussion has turned into quite a topic. Some people have strong feelings about this and wish to encourage debate, while others want a conclusion above all. There are a couple of items to address. The first is forking Prettier to make the changes needed to adopt this change regarding self-closing tags and possibly even broader optional use of the forward slash in HTML. The MDN Web Docs team has decided not to fork Prettier as it will introduce a big maintenance burden that is not sustainable. Anyone is, of course, welcome to take this conversation to the Prettier projects, but we would like you to keep the following in mind: There are more and more places where you can use JSX, and the file extension only sometimes clarifies this. Even in projects such as React, authors only occasionally use the.jsx extension for files that uses JSX. There is also a problem with formatting code blocks with no file extension. Here, annotating your code fences can help, but Prettier must rely on the author. Finally, there is a backward compatibility problem. That is a long way of saying that we understand why this is no simple thing for Prettier to adopt. Ultimately, these changes must be evangelized with authors and educators using a slow and methodical approach. With this in mind, we propose to review the Learn section and update it with clear guidance on the elements in question. |
Beta Was this translation helpful? Give feedback.
This discussion has turned into quite a topic. Some people have strong feelings about this and wish to encourage debate, while others want a conclusion above all. There are a couple of items to address. The first is forking Prettier to make the changes needed to adopt this change regarding self-closing tags and possibly even broader optional use of the forward slash in HTML.
The MDN Web Docs team has decided not to fork Prettier as it will introduce a big maintenance burden that is not sustainable. Anyone is, of course, welcome to take this conversation to the Prettier projects, but we would like you to keep the following in mind:
Prettier is not adopting this because of the complexity it…