-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Feature request: Option to trim whitespace between tags #3080
Comments
I suppose it's duplicate of #189 |
I think that |
sometimes those text nodes are out of control https://svelte.dev/repl/902d4ccfcc5745d0b927b6db6be4f441?version=3 |
I've created an issue for empty text nodes here #3586 |
Ok, who can implement this proposal? Personal for me I want to add a feature when you can control space trimming per nodes, like |
Anything new on this? |
It's possible to work around this by creating a html comment tag wherever unneeded whitespace appears. |
I tried to fix it but, it was too complicated. Need a new attempt. |
Also need this for building markdown renderers that might affect whitespace. Having a Its nice to be able to indent svelte instead of having to put everything on one line because of whitespace |
Yeah, now we have a better codebase, maybe it will be simpler, but the team is probably focused on Svelte5 now. |
I'm going to close this, as Svelte 5 has much more sensible whitespace handling — whitespace is preserved between siblings, but collapsed at the edges of nodes. In practice, this is what you want 99% of the time, and there are escape hatches ( |
Problem
Svelte's handling of whitespace in templates is supposed to be consistent with HTML. However, the way HTML handles whitespace makes styling of inline blocks notoriously difficult in some cases, with no satisfactory workaround. There are basically two types of workarounds, each with their own frustrating problems:
Strategy 2 is arguably even trickier in a Svelte template, where spaces may or may not appear inside other components or slots, or around
{#if}
or{#each}
blocks. Blocks appear to already have some heuristics to avoid unnecessary spaces, but that also makes it harder to predict where spaces will need to be avoided or not.I'm used to JSX, where whitespace is automatically trimmed, and you have to insert it manually with
{" "}
when you want it. While this can cause its own occasional oddities, it's always much easier to add spaces explicitly than to remove them, and it allows the formatting of a template to be optimised for readability.Suggestion
It would be very helpful if there was a way to opt in to similar behaviour for a Svelte component. I think the natural way to do it would be by using
<svelte:options>
:I don't understand the details of how whitespace is handled in the compiler, but I think the compiler would just need to remove any calls to
space()
fromsvelte/internal
.The text was updated successfully, but these errors were encountered: