New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How should Prettier handle unclosed tags in HTML? #5864
Comments
If I remember correctly, it's valid if it's the "last" element, which should be already the case: <!-- input -->
<div>
<!-- output -->
<div></div> <!-- input -->
<span>
<div>
</span>
<!-- output -->
SyntaxError: Unexpected closing tag "span".
Yes, it's invalid for HTML, but valid for other templates such as
It sounds like the same case for type annotations in |
According to validator.w3.org/nu, the following snippet:
… has errors:
Good point, need to think about that.
Also a very good point. |
One use case that was not brought up which favors Prettier to not do self-closing is markdown files. An example would be HTML code snippets in markdown: Prettier formats HTML code snippets by HTML rules and applies self-closing slash to void elements but I don't want users copying XHTML self-closing versions of it. Another example is what if the HTML code snippet intentionally had unclosed tags. I think this may be more of a markdown problem but I also think it's relevant to the issue at hand. |
@TimeDropsExp Can you share an example on the playground? |
Example of
|
Well, I would expect that: <custom-element/> would be transformed to: <custom-element></custom-element> Which is also what hyperHTML does. |
Mby it could be some kind of "flavor" property in |
Self-closing tags are confusing because they're mostly ignored ( Currently Prettier adds in the meaningless Eg, input like: <div />
<p></p> <div />
<p></p> but I think it should output: <div>
<p></p>
</div> Which is the same output Prettier gives for: <div>
<p></p> |
<div>
<div></div>
<div/>
<div />
</div>
SyntaxError: Unexpected closing tag "div".
As far as I know, leaving the end tag out for
<div>
is not allowed.<div/>
gets treated as<div>
(start tag only, the/
is ignored), but is still invalid.</div>
is invalid for sure.In other words – sometimes Prettier throws errors for invalid HTML (
</div>
), sometimes it preserves input (<div />
) and sometimes it … fixes the HTML(?) (<div>
).What should happen though?
The reason I bring this up is because I was thinking about #5246 which discusses whether void elements should have a slash or not (
<input>
vs<input />
). One argument brought up for<input>
was that the other syntax (<input />
) could fool developers into thinking that<div />
is valid syntax for an empty div. Then I was thinking – "Wait, that wouldn't be a problem, because Prettier would throw an error on<div />
" and went to the playground to try it out. Nope. No error.Related: #5665 (which discusses whether Prettier should add missing closing tags).
Notes:
<p>
should still be valid and become<p></p>
, since p tags have optional end tags.@ikatyang Since you know the HTML parser best – do you have any thoughts?
One idea is to throw errors for unclosed tags and illegal "self-closing" tags. I think that would be helpful, since mistakes would be pointed out early. Also, Prettier can't really know where to insert a missing end tag. But would this break stuff for somebody?
The text was updated successfully, but these errors were encountered: