-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Don't self-close void elements, because invalid when using HTML5 #8923
Comments
We're following Prettier's code style here which also does this. Where is that HTML output from, server side rendering with Svelte? In that case I think it would be better to adjust Svelte core to not do this. |
I see it within the head of my app.html and between svelte:head elements on my main +layout.svelte. All my routes are prerendered, if that helps. (I haven't inspected any body html elements.) |
It is a bit strange that W3C validator complains about that since |
This was on HN yesterday: https://news.ycombinator.com/item?id=36615691 |
From the post:
+1 thanks for sharing |
@jasongitmail Can you provide the source that produces these tags? It doesn't output the slash for me if I try to ssr The first two warnings come from the kit templates which should be trivial to change. |
@gtm-nayan I'm prerendering, not using SSR. That might be the difference there. This is Prettier related and Prettier wouldn't run on SSR-generated HTML. The code repro is to use any similar line from the screenshot in |
I also tried it on a prerendered page and it still didn't insert the slash. Are you using |
@gtm-nayan No, I'm not. This is about the prettier config, not the output, so you can see what we're talking about in the editor. On Stackblitz, you can repro with:
This demonstrates what Prettier is doing for both .html and .svelte files. It's not necessary to build it to see what Prettier is doing. But as a side note, if you're looking at browser's Elements tab in dev console, you're not looking at the HTML string sent over the wire. The Elements tab shows parsed HTML based on the doc type, and for SK's HTML5 doc type will remove the self close on items we're talking about because it's not supposed to exist. But, visit your SK website in Chrome and hit "command+option+u" (Mac) and you can see the HTML sent over the wire, including the trailing slashes added by Prettier. |
Hmm, I thought the warnings were for the HTML in the prerendered HTML files. Prettier shouldn't have any effect on those. Do you have a link to the page where this occurs? |
Apart from possibly cleaning up https://github.com/sveltejs/kit/blob/main/packages/create-svelte/templates/skeleton/src/app.html, this doesn't seem like a Svelte issue at all — the rendered HTML is correct. If Prettier is subsequently running on that HTML and introducing errors, that's a Prettier bug. So I'm pretty sure this issue can be closed? That said, I'm glad this was opened because that's how I learned that Svelte incorrectly parses self-closing non-void HTML elements. I've opened #11052 to address that. |
Technically, trailing slashes in void HTML elements are valid in HTML5. The validator doesn't actually complain about them (these messages aren't errors or even warnings, they are marked as just info), it only tries to tell the author that the meaning of this code might be different from what is intended (e.g. that these slashes don't make these elements "self-closed"). |
@Rich-Harris Yes, I think this can be closed, since there does not seem to be a config option exposed by Prettier to be changed. The Prettier repo has an open issue for this same topic, if anyone wants to follow it there. The comment thread links to a plugin that can clean this up until Prettier does so officially (prettier-plugin-void-html). |
The W3C validator complains about SvelteKit's built HTML, given it uses an HTML5 doc type.
These items clean up after removing the closing slash from sample HTML input.
Browsers also automatically convert it from that format into correct HTML format after interpreting it, but it'd be good to have prettier aligned with the correct version.
From MDN: https://developer.mozilla.org/en-US/docs/Glossary/Void_element "Self-closing tags (
<tag />
) do not exist in HTML. The void elements are as follows":The text was updated successfully, but these errors were encountered: