0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-12-01 01:11:24 +01:00

feat: better <svelte:element> SSR output (#12339)

This commit is contained in:
Rich Harris 2024-07-07 20:02:11 -07:00 committed by GitHub
parent a59bc99782
commit bbf489504d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 29 additions and 15 deletions

View File

@ -0,0 +1,5 @@
---
'svelte': patch
---
feat: better `<svelte:element>` SSR output

View File

@ -1338,12 +1338,18 @@ const template_visitors = {
context.visit(node.fragment, state)
);
const body = b.stmt(
b.call('$.element', b.id('$$payload'), tag, b.thunk(attributes), b.thunk(children))
context.state.template.push(
b.stmt(
b.call(
'$.element',
b.id('$$payload'),
tag,
attributes.body.length > 0 && b.thunk(attributes),
children.body.length > 0 && b.thunk(children)
)
)
);
context.state.template.push(b.if(tag, body), block_anchor);
if (context.state.options.dev) {
context.state.template.push(b.stmt(b.call('$.pop_element')));
}

View File

@ -72,18 +72,22 @@ export function assign_payload(p1, p2) {
* @param {() => void} children_fn
* @returns {void}
*/
export function element(payload, tag, attributes_fn, children_fn) {
payload.out += `<${tag} `;
attributes_fn();
payload.out += `>`;
export function element(payload, tag, attributes_fn = noop, children_fn = noop) {
if (tag) {
payload.out += `<${tag} `;
attributes_fn();
payload.out += `>`;
if (!VoidElements.has(tag)) {
children_fn();
if (!RawTextElements.includes(tag)) {
payload.out += BLOCK_ANCHOR;
if (!VoidElements.has(tag)) {
children_fn();
if (!RawTextElements.includes(tag)) {
payload.out += BLOCK_ANCHOR;
}
payload.out += `</${tag}>`;
}
payload.out += `</${tag}>`;
}
payload.out += '<!---->';
}
/**

View File

@ -3,6 +3,5 @@ import * as $ from "svelte/internal/server";
export default function Svelte_element($$payload, $$props) {
let { tag = 'hr' } = $$props;
if (tag) $.element($$payload, tag, () => {}, () => {});
$$payload.out += `<!---->`;
$.element($$payload, tag);
}