Skip to content

feat: put element content on its own lines instead of hugging the tags - #554

Open
Stadly wants to merge 1 commit into
sveltejs:mainfrom
Stadly:feat/unhug-element-content
Open

Stadly wants to merge 1 commit into
sveltejs:mainfrom
Stadly:feat/unhug-element-content

Conversation

@Stadly

@Stadly Stadly commented Oct 26, 2026

Copy link
Copy Markdown

Closes #547.

Svelte 5 removes whitespace at the start and end of an element's content (migration guide). Since 4.0 the plugin supports only Svelte 5, so a line break after > or before </ can't change what renders. The plugin still hugs those tags to avoid adding that whitespace, which gives this for #547's example:

<span class="highlight"
    ><strong>Some text that is long</strong><small
        >Some more text</small
    ></span
>

With this change it prints:

<span class="highlight">
    <strong>Some text that is long</strong><small>
        Some more text
    </small>
</span>

Whitespace between siblings still renders, so </strong><small> and </span>, stay together as before. Content inside <pre> and <textarea> keeps hugging, and so does htmlWhitespaceSensitivity: "strict".

Two smaller changes make the output match what you'd write by hand. When the source has no whitespace at an edge, the separator is a softline, so <b>Bold</b> stays as it is when it fits instead of becoming <b> Bold </b>. A tag without attributes no longer breaks before its >, so an overflowing line doesn't end in <span with the > alone on the next line.

The README FAQ about hugging described the Svelte 4 behavior, so I rewrote it.

Testing

16 fixtures have new expected output. I compiled the old and the new version of each with svelte/compiler, client and server, and got identical code for every one. Three of them don't compile as whole files (animate: outside a keyed {#each}, <svelte:fragment slot> without a parent), so for those I compiled only the elements that changed.

Two fixtures are new. One holds #547's example and text right after an element. The other covers <pre> and <textarea>, and its output is the same as before this change.

I also ran the plugin over a SvelteKit app with 828 formatted components. It changed only the 27 files that still had the hugged layout, and each of them compiles to the same code as before.

Svelte 5 removes whitespace at the start and end of an element's content, so a line break after `>` or before `</` can't change what renders. Tags keep hugging inside `pre` and `textarea` and with strict whitespace sensitivity.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Line break incorrectly placed before >, not after

1 participant