Skip to content

Plugin is incorrectly rewriting empty non-void HTML tags in svelte files #377

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

Closed
milne-dev opened this issue Jul 10, 2023 · 8 comments
Closed

Comments

@milne-dev
Copy link

milne-dev commented Jul 10, 2023

Within .svelte files, this plugin rewrites empty <div></div> to <div /> which is an HTML error according to the whatwg spec:

non-void-html-element-start-tag-with-trailing-solidus

This error occurs if the parser encounters a start tag for an element that is not in the list of void elements or is not a part of foreign content (i.e., not an SVG or MathML element) that has a U+002F (/) code point right before the closing U+003E (>) code point.

The list of valid void elements:
area, base, br, col, embed, hr, img, input, link, meta, source, track, wbr

@dummdidumm
Copy link
Member

dummdidumm commented Jul 11, 2023

We will adjust the options in the next major version to make this possible, though we're not sure yet what the default should be. Closely related to #238 / #348.

@milne-dev
Copy link
Author

Please consider making the defaults align with the HTML standard as well as the behavior of prettier core.

Further, my initial motivation to report this issue was because I found it frustrating to write layout structure in HTML only to have several elements being rewritten as <tag /> before I gave them content.

dummdidumm added a commit that referenced this issue Jul 14, 2023
- add `svelteSelfCloseComponents` and `svelteSelfCloseElements` to specify whether or not empty elements/components should self-close. `svelteStrictMode` no longer has any effect on this behavior
- `svelteAllowShorthand` now takes precedence over `svelteStrictMode`, which no longer has any effect on that behavior
- `svelteStrictMode` is deprecated as future Svelte versions might coerce numbers coming from mustache tags inside strings into strings

closes #348
closes #284
closes #279
closes #238
related to #377
dummdidumm added a commit that referenced this issue Jul 14, 2023
- whether or not empty elements/components should self-close is now left to the user - in other words, if you write `<div />` or `<Component />` that stays as is, and so does `<div></div>`/`<Component></Component>`. `svelteStrictMode` no longer has any effect on this behavior
- `svelteAllowShorthand` now takes precedence over `svelteStrictMode`, which no longer has any effect on that behavior
- `svelteStrictMode` is deprecated as future Svelte versions might coerce numbers coming from mustache tags inside strings into strings

closes #348
closes #284
closes #279
closes #238
related to #377
superseeds / closes #379 as less options = better
dummdidumm added a commit that referenced this issue Jul 17, 2023
- whether or not empty elements/components should self-close is now left to the user - in other words, if you write `<div />` or `<Component />` that stays as is, and so does `<div></div>`/`<Component></Component>`. `svelteStrictMode` only has effect on elements (not allowing self-closing non-void-ones)
- `svelteAllowShorthand` now takes precedence over `svelteStrictMode`, which no longer has any effect on that behavior

closes #284
closes #279
closes #238
related to #348
related to #377
superseeds / closes #379 as less options = better
@dummdidumm
Copy link
Member

Version 3 of this plugin will leave tags as-is, so writing <div></div> won't be reformatted to <div /> anymore. We keep formatting <input> as <input /> to align with Prettier.

@danielniccoli
Copy link

I'm not in favour of aligning with Prettier in case of void elements. They're explicitly mentioned as not self-closing. Prettier takes an opinionated stance on the matter and imposes its choice on the developer. I wouldn't argue, if it were up to the developer, but the HTML specs are very clear on void elements.

For now, I can enable svelteStrictMode, but as I understand it's deprecated.

@danielniccoli
Copy link

Related sveltejs/svelte#11052

@mustafa0x
Copy link

Related: https://github.com/awmottaz/prettier-plugin-void-html

Doesn't work with .svelte files thought.

@shadow-identity
Copy link

Rich mentioned that the latest version on prettier-plugin-svelte should prevent prettier from braking html by converting html tags into void tags. But as I can see, 3.2.3 still replaces <br> with <br />. Is it planned to fix it?

@dummdidumm
Copy link
Member

dummdidumm commented May 17, 2024

No this will stay as is for the foreseeable future to align with Prettier's HTML formatting. <br> is a void tag, and it's fine to write <br /> as well (browsers handle it). What is not fine, and which was changed (and accounted for in the prettier plugin) is writing <div /> instead of <div></div>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants