-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
Trim whitespaces before control structures and variables #6540
Comments
This would be a breaking change because right now there's no requirement to have a whitespace between the brackets and the code. This makes it ambigouus to the minus sign. |
What about the only use case i know is to invert Bits and I guess nearly no one would use it in template/html code and for these who need it could use
|
It's still a breaking change. I like the idea. White space issues in Svelte come up frequently and there is no one-size-fits-all solution to make every use-case happy (I still have to fight inline-block with font-size: 0 because of that...). And this is a suggestion I like very much. Liquid does essentially the same https://shopify.github.io/liquid/basics/whitespace/ If the Svelte team thinks this is a viable solution to the white space issues, then let's find a syntax. It also needs to work for
I've seen this argument a number of times (e.g. in #6373), maybe it's time to plan for a v4? But I know absolutely zero about future plans for Svelte and if that's on the horizon. I think avoiding breaking changes at all cost slows innovation and might result in awkward APIs. I personally would even be fine with a Example: currently my <span class="host"
>{#if subdomain}<span class="subdomain">{subdomain}</span>.{/if}{#if sld}<span class="sld">{sld}</span>.{/if}<span
class="tld">{tld}</span
></span
> It could become this if both if-blocks would trim leading and trailing whitespace, with whatever syntax we end up. <span class="host">
{#if subdomain}
<span class="subdomain">{subdomain}</span>.
{/if}
{#if sld}
<span class="sld">{sld}</span>.
{/if}
<span class="tld">{tld}</span>
</span> But I think there are enough examples of why this is needed in other issues already 😅 @Kapsonfire-DE do you have an example where trimming of an Edit: I was just thinking that if something like this is introduced it would make things even better than expected. Because then by default all white-space could be 100% preserved as they appear in the HTML without touching it at all. It could be treated like any other content. This would make everything much more explicit than what Svelte is currently doing. And the white space can also be trimmed in the compile step. But of course keeping white-space 100% in-tact would be breaking because |
In fact I dont have a example where a control block won't work - but this is a solution where it works against the concept of writing short code. To make the code not breaking, this could be a flag-enabled feature, which are default off, in start template default on. The |
What would this give us that hugging open/close tags can't?
If it's "only" about "this looks ugly in certain cases", then that sounds like a case for better formatting, which is the job of |
Like I said this discussion is not new, there are plenty of arguments to be made #3080, #189 But skimming through those the proposed solution here would put us in jsx territory where you need
I think code readability and things like cyclomatic complexity are not about looks, but maintainability. Also consistency (see example below).
This is an interesting way to look at it. But prettier cannot offer a one-size-fits-all solution in the same way the compiler itself cannot. Because there are different problems. I'll post one more example to illustrate a point, but I think the other issues will contain much better and in depths examples. Let's take this as an example (formatted by prettier): <div>
{#each list as item}
{#if item.foo}
<div>{item.foo}</div>
{/if}
{#if item.bar}
<div>{item.bar}</div>
{/if}
{/each}
</div> Works as expected. But once we're talking about inline-block prettier would have to (know to) turn this into: <div><!--
-->{#each list as item}<!--
---->{#if item.foo}<!--
-----><div>{item.foo}</div><!--
--->{/if}<!--
--->{#if item.bar}<!--
-----><div>{item.bar}</div><!--
--->{/if}<!--
-->{/each}
</div> which looks a lot like what I've been doing 10 years ago in PHP to render a inline-block list. The alternative would be to completely change the code to let it hug. But I'd rather have consistently formatted code and a way to also control white-space, which the proposal here would (kind of) give us. I think if there was an easy and straight forward solution it would have already happened 😄 |
What do you think about my opinion? (Maybe this is almost the same as @Kapsonfire-DE)
My opinion is that users can set the option to In my understanding, if a user wants to output whitespace, a user should mention So I think in many cases, whitespace is just noise. (or should be noise.) And the good point of this idea is that users can maintain readability. Thank you for reading!🎉 |
whitespaces and are not the same pre-tag |
Ahhh right... (I forgot to think about pre tag ...) |
Svelte does not currently [1] remove purely whitespace text nodes like most other frameworks do, which causes noticeable performance impact when benchmarking creating thousands of rows. This commit manually fixes that so that the generated HTML is closer to the competing implementations, using a workaround mentioned in [1] On my system, all the benchmarks involving creation of rows run ~5% faster with this change and the overall geometric mean improves from around 1.28 to 1.25. [1]: sveltejs/svelte#6540
Svelte does not currently [1] remove purely whitespace text nodes like most other frameworks do, which causes noticeable performance impact when benchmarking creating thousands of rows. This commit manually fixes that so that the generated HTML is closer to the competing implementations, using a workaround mentioned in [1]. On my system, all the benchmarks involving creation of rows run ~5% faster with this change and the overall geometric mean improves from around 1.28 to 1.25. [1]: sveltejs/svelte#6540
Svelte 5 has introduced some changes to how whitespace in the components is handled. If these changes don't cover the exact case in this issue, I don't think a special syntax to let you avoid needing to run elements together in the component source makes sense. Closing. |
Describe the problem
Fixes #6486
Describe the proposed solution
PHP Template Engine
Twig
solves it by adding a - to the curly braces{{-variable }}
removes whitespaces before the variable/control structure
{{ variable -}}
removes whitespaces after the variable/control structure
combining both remove the whitespaces in front and end
Take a look at:
https://symfony.com/blog/better-white-space-control-in-twig-templates
Similiar solution for svelte could be nice as well
Alternatives considered
dont write whitespaces in your code - but makes the code ugly ;-)
Importance
nice to have
The text was updated successfully, but these errors were encountered: