question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add an option to prefer void tags over self closing tags.

See original GitHub issue

Update:

I have change my opinion on this, and I don’t think I require this any more. If this is ever implemented I would probably use it, but I really don’t mind this behavior at all. If you want to read more, I wrote a post in dev.to


Original post:

Prettier 1.14.3 Playground link

# Options (if any):

Input:

<input type="button">

Output:

<input type="button" />

Expected behavior: I would like an option to prefer void element tags over the current implementation of self-closing element tags. I understand why is this the default behavior, as this is xml compatible, but several style guides suggest the usage of void tags as they are valid HTML5 tags. As I understand in the option philosophy, if there is enough usage from developers there is a justification to add the option.

Style Guides:

# Options:
--prefer-void-tags=true
<input type="button">

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:253
  • Comments:121 (34 by maintainers)

github_iconTop GitHub Comments

79reactions
alexander-akaitcommented, Oct 11, 2018

I don’t think we need option. I think we should print this as in source code, i.e. if you have <input type="button" /> print this as is.

Why:

  1. Compatibility with XHTML.
  2. It is out of scope prettier, here should be used linter to avoid HTML errors.
39reactions
olescommented, Nov 12, 2020

I went even deeper.

When searching for “html style guide” and “html best practice”, these were the serious results (and I am not nit-picking):

https://google.github.io/styleguide/htmlcssguide.html#Document_Type https://contribute.jquery.org/style-guide/html/ https://www.w3schools.com/html/html5_syntax.asp https://github.com/hail2u/html-best-practices#dont-mix-empty-element-format https://developer.mozilla.org/en-US/docs/MDN/Guidelines/Code_guidelines/HTML

and none of them use self-closing tags.

Even the same doc specifying it as valid doesn’t use it: https://html.spec.whatwg.org/multipage/syntax.html#attributes-2

Furthering my case of it being a weird thing to do.

Surely this is unexpected for beginners and whoever just trying out prettier. It sure was for me.

And surely the cost of this disruption, beginners and alike, is greater than to support XML / XHTML? Is there any point of XHTML nowadays?

It seems to me like it should be an option to have the old XML / XHTML behaviour, as it seems to be this way due to “because of history” and “compatibility reasons”, as said in the options philosophy.

It is also the third highest upvoted html issue now, if that has any weight.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What are the allowed ways to close self-closing tags for void ...
According to the specification you link to: "Void elements only have a start tag; end tags must not be specified for void elements",...
Read more >
I changed my opinion about Prettier not formatting void ...
I open an issue to give support to an option because I was against this behavior. Several guide styles about HTML recommend void...
Read more >
Restrict "self-closing" tags to only void elements in ... - Drupal
Updated: #174 Problem/Motivation drupal_pre_render_html_tag() is not specific in allowing which tags can be void (no closing tag).
Read more >
Self-Closing Tags in HTML [Guide] - Treehouse Blog
TL;DR: In HTML5 it is not strictly necessary to close certain HTML tags. ... Let's take a look at some more examples. Optional...
Read more >
vue/html-self-closing
enforce self-closing style. ... The style of well-known HTML elements except void elements. html.component ( "always" by default) .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found