Skip to content

feat: Parse/format HTML in {@html} on server-side #15528

Open
@adiguba

Description

@adiguba

Describe the problem

There no problem on client-side since the browser will fix the HTML code automatically, but using {@html} can be dangerous on server-side.

=> the HTML code will be written as is in the generated page, without any verification.
An invalid HTML can completely broke the result and cause strange behavior with hydration...

Example : https://www.sveltelab.dev/ba3rh3y3aek8ctc

Describe the proposed solution

Adding an HTML parser to Svelte would probably be disproportionate.

I think a better solution would be to add an htmlFormat option to the server-side render() function :

     htmlFormat?: (html: string) => string

When present, this function will be used by {@html} to render the HTML code on server-side.

And after that, SvelteKit could define a new server-hook for this.

// src/hooks.server.js

/**
 * @param {string} html
 * @returns {string}
 */
export function htmlFormat(html) {
    const parsed = // can use any HTML parser
    return parsed;
}

So the impact will be minimal, while allowing to have a full control over how the HTML will be formatted.

Importance

nice to have

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions