Comments (13)
I should have a fix for this ...PRing now
from svelte.
I don't think it's HMR i think is simply hydration...the reproduction works in the REPL too!
from svelte.
Ah, good to know! I originally couldn't reproduce it in the REPL, but didn't try again once I figured out what was happening.
from svelte.
@dummdidumm This is the intended behavior, isn't it? Isn't this just the new {#each}
behavior in v5? https://svelte-5-preview.vercel.app/docs/breaking-changes#whitespace-handling-changed
from svelte.
This case doesn't seem to fall into the listed cases there:
- "Whitespace between nodes is collapsed to one whitespace"
- These whitespace text nodes are not being collapsed into a single space, they are being set to null strings.
- "Whitespace at the start and end of a tag is removed completely"
- These are whitespace text nodes, not at the beginning or start of a tag
Certain exceptions apply such as keeping whitespace inside pre tags
- These are whitespace text nodes, not at the beginning or start of a tag
And the behavior on full reload versus dynamic reload differ, which presumably is not intended in any case.
from svelte.
Btw i don't even think this is hydration...is probably the each loop thing with a bug
from svelte.
It does look like <svelte:options preserveWhitespace={true} />
solves the problem, so it's no longer blocking my use case.
However, I did find this behavior to be surprising and, more importantly, very difficult to pin down. Searching for related terms in GitHub Issues and more generally didn't yield any answers. Maybe that'll be improved once the Svelte 5 docs are more discoverable, though the docs don't actually indicate that this behavior should happen.
from svelte.
Crap, I take it back. <svelte:options preserveWhitespace={true} />
does NOT solve the problem for my case. If I've split the original string on any non-word character, I end up with new spaces inserted between every text node: REPL
For clarity, my use case is that I have a "spellcheck" component that splits a string up into labeled parts (words, template variables, other). It then loops through those parts and either renders them directly with {substring}
or wraps them in some HTML. It looks something like this:
{#each parsed as part (part.start)}
{#if part.isVariable}
<code>{part.substr}</code>
{:else if !part.isWord && part.substr === '\n'}
<br />
{:else if !part.isWord || part.valid}
{part.substr}
{:else if part.isWord}
<span
role="button"
class="spelling-error"
onclick={async (event) => {
if (event.ctrlKey) await glossary.addTerm(part.substr);
}}
>
{part.substr}
</span>
{/if}
{/each}
from svelte.
When preserveWhitespace
mode is on, then the whitespace between the {#each words as word}
and the {word}
and the {/each}
is now significant, so I'd say that behavior is expected.
If, after you've split the string, you've lost the information about which elements should have whitespace displayed between them, then there's not anything Svelte is going to be able to do. If you want to conditionally display whitespace, and you know what that condition is, then you could do something like {string + (condition ? ' ' : ''}
or something like {string}{#if condition}{' '}{/if}
.
from svelte.
The information is not lost. I'm still rendering the whitespace characters via {theWhitespaceChar}
. They just become ""
text nodes once rendered instead of " "
text nodes.
You can see in this REPL that the split text includes entries that are just whitespace strings, and these are not being excluded by the loop. You can also see each entry in the text nodes of the dom, but that their data is set to ""
instead of " "
.
from svelte.
Gotcha, okay, yeah, it looks like the core of the bug, then is something like: {#each ['foo', ' ', 'bar'] as str}{str}{/each}
Apologies, I had missed that the original REPL had .split(/( )/)
rather than .split(' ')
/ .split(/ /)
. That's actually a new bit of JS regex behavior to me.
from svelte.
No worries! .split(/( )/)
definitely has unexpected behavior if you haven't used it before 🙂
from svelte.
And yep, your minimal case is exactly right: REPL
from svelte.
Related Issues (20)
- duplicate component issue is caused by using the fade transition. HOT 2
- Svelte 5 exports in rune mode to expose variables for programmatic use (for instance updates) HOT 3
- Svelte 5: Incorrect IfBlock Start Position in Modern Mode Parsing HOT 5
- Svelte 5: Maximum update depth exceeded. (effect_update_depth_exceeded) HOT 14
- hydrate and mount error out in browser, saying 'hydrate(...)' is not available on the server HOT 4
- Add setter to let variables accessors HOT 3
- Svelte 5: cannot bind to a `<svelte:component/>` element HOT 2
- Svelte 5: Flickering CSS transitions/animations starting in `next-144` HOT 1
- Svelte 5: Imported component applied global style HOT 2
- Svelte 5: Invalid namespace of elements within SVG HOT 5
- `render` function from `svelte/server` not properly typed
- preventDefault not working HOT 1
- Svelte 5: Can not reassign $derived variable HOT 8
- false positive a11y warning for label with control inside component HOT 6
- Uneasy bouncing in `transition:slide` HOT 12
- bug: getting $Ci not a constructor for imports of svg , png in static builds HOT 2
- SSR code regression between 152 and 153
- Svelte5: Snippet block name conflict with component name causes error HOT 1
- [Svelte5] Svelte mount duplicate issue HOT 7
- Svelte 5: events created with the "on" function will bubble separately from one another
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from svelte.