Code Monkey home page Code Monkey logo

Comments (13)

paoloricciuti avatar paoloricciuti commented on July 18, 2024 1

I should have a fix for this ...PRing now

from svelte.

paoloricciuti avatar paoloricciuti commented on July 18, 2024

I don't think it's HMR i think is simply hydration...the reproduction works in the REPL too!

from svelte.

adam-coster avatar adam-coster commented on July 18, 2024

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.

Conduitry avatar Conduitry commented on July 18, 2024

@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.

adam-coster avatar adam-coster commented on July 18, 2024

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

And the behavior on full reload versus dynamic reload differ, which presumably is not intended in any case.

from svelte.

paoloricciuti avatar paoloricciuti commented on July 18, 2024

Btw i don't even think this is hydration...is probably the each loop thing with a bug

REPL

from svelte.

adam-coster avatar adam-coster commented on July 18, 2024

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.

adam-coster avatar adam-coster commented on July 18, 2024

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.

Conduitry avatar Conduitry commented on July 18, 2024

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.

adam-coster avatar adam-coster commented on July 18, 2024

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.

Conduitry avatar Conduitry commented on July 18, 2024

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.

adam-coster avatar adam-coster commented on July 18, 2024

No worries! .split(/( )/) definitely has unexpected behavior if you haven't used it before 🙂

from svelte.

adam-coster avatar adam-coster commented on July 18, 2024

And yep, your minimal case is exactly right: REPL

from svelte.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.