Skip to content

fix: improve ssr output of dynamic textarea elements #10638

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nice-avocados-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: improve ssr output of dynamic textarea elements
16 changes: 16 additions & 0 deletions packages/svelte/src/compiler/phases/2-analyze/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,22 @@ const validation = {
error(arg, 'invalid-render-spread-argument');
}
}
const is_inside_textarea = context.path.find((n) => {
return (
n.type === 'SvelteElement' &&
n.name === 'svelte:element' &&
n.tag.type === 'Literal' &&
n.tag.value === 'textarea'
);
});
if (is_inside_textarea) {
error(
node,
'invalid-tag-placement',
'inside <textarea> or <svelte:element this="textarea">',
node.expression.name
);
}
},
SvelteHead(node) {
const attribute = node.attributes[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1274,36 +1274,32 @@ const template_visitors = {
context.state.init.push(el_anchor);
context.state.template.push(t_expression(anchor_id));

const [inner_anchor, inner_id] = serialize_anchor(context.state);
inner_context.state.init.push(inner_anchor);
inner_context.state.template.push(t_string('<'), t_expression(tag));
serialize_element_attributes(node, inner_context);
inner_context.state.template.push(t_string('>'));

const before = serialize_template(inner_context.state.template);
const main = create_block(node, node.fragment.nodes, {
...context,
state: { ...context.state, metadata }
});
const after = serialize_template([
t_expression(inner_id),
t_string('</'),
t_expression(tag),
t_string('>')
]);

serialize_element_attributes(node, inner_context);

context.state.template.push(
t_statement(
b.if(
tag,
b.block([
...inner_context.state.init,
...before,
b.if(
b.unary('!', b.call('$.VoidElements.has', tag)),
b.block([...serialize_template([t_expression(inner_id)]), ...main, ...after])

b.stmt(
b.call(
'$.element',
b.id('$$payload'),
tag,
b.thunk(
b.block([
...inner_context.state.init,
...serialize_template(inner_context.state.template)
])
),
b.thunk(b.block(main))
)
])
)
)
),
t_expression(anchor_id)
Expand Down
25 changes: 25 additions & 0 deletions packages/svelte/src/internal/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,31 @@ export function assign_payload(p1, p2) {
p1.anchor = p2.anchor;
}

/**
* @param {Payload} payload
* @param {string} tag
* @param {() => void} attributes_fn
* @param {() => void} children_fn
* @returns {void}
*/
export function element(payload, tag, attributes_fn, children_fn) {
payload.out += `<${tag} `;
attributes_fn();
payload.out += `>`;

if (!VoidElements.has(tag)) {
const anchor = tag !== 'textarea' ? create_anchor(payload) : null;
if (anchor !== null) {
payload.out += anchor;
}
children_fn();
if (anchor !== null) {
payload.out += anchor;
}
payload.out += `</${tag}>`;
}
}

/**
* Array of `onDestroy` callbacks that should be called at the end of the server render function
* @type {Function[]}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { test } from '../../test';

export default test({
html: '<textarea></textarea>',

test({ assert, target }) {
assert.htmlEqual(
target.innerHTML,
`
<textarea></textarea>
`
);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<svelte:element this="textarea"></svelte:element>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* index.svelte.js generated by Svelte VERSION */
import * as $ from "svelte/internal";

export const object = $.proxy({ ok: true });
export const object = $.proxy({ ok: true });
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* index.svelte.js generated by Svelte VERSION */
import * as $ from "svelte/internal/server";

export const object = { ok: true };
export const object = { ok: true };
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,7 @@ export default function Svelte_element($$payload, $$props) {
const anchor = $.create_anchor($$payload);

$$payload.out += `${anchor}`;

if (tag) {
const anchor_1 = $.create_anchor($$payload);

$$payload.out += `<${tag}>`;

if (!$.VoidElements.has(tag)) {
$$payload.out += `${anchor_1}`;
$$payload.out += `${anchor_1}</${tag}>`;
}
}

if (tag) $.element($$payload, tag, () => {}, () => {});
$$payload.out += `${anchor}`;
$.bind_props($$props, { tag });
$.pop();
Expand Down