Skip to content

Commit c0d670d

Browse files
committed
simplify a bit (pass callback direct to valueless_option)
1 parent ca824ff commit c0d670d

File tree

4 files changed

+32
-36
lines changed

4 files changed

+32
-36
lines changed

packages/svelte/src/compiler/phases/3-transform/server/visitors/RegularElement.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,16 @@ export function RegularElement(node, context) {
127127
}
128128
}
129129

130-
if (body === null) {
130+
const is_option_with_implicit_value =
131+
node.name === 'option' &&
132+
!node.attributes.some(
133+
(attribute) =>
134+
attribute.type === 'SpreadAttribute' ||
135+
((attribute.type === 'Attribute' || attribute.type === 'BindDirective') &&
136+
attribute.name === 'value')
137+
);
138+
139+
if (body === null && !is_option_with_implicit_value) {
131140
process_children(trimmed, { ...context, state });
132141
} else {
133142
// we need the body if:
@@ -137,14 +146,15 @@ export function RegularElement(node, context) {
137146
const inner_state = { ...state, template: [], init: [] };
138147
process_children(trimmed, { ...context, state: inner_state });
139148

140-
if (node.name === 'option') {
149+
if (is_option_with_implicit_value) {
141150
// in case of a valueless `<option>` element, $$body is a function that accepts the children...internally it
142151
// will run the children to get the value of the body at runtime since it's needed to for the implicit value
143152
// selection
144153
state.template.push(
145154
b.stmt(
146155
b.call(
147-
body,
156+
'$.valueless_option',
157+
b.id('$$payload'),
148158
b.thunk(b.block([...inner_state.init, ...build_template(inner_state.template)]))
149159
)
150160
)

packages/svelte/src/compiler/phases/3-transform/server/visitors/shared/element.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,6 @@ export function build_element_attributes(node, context) {
200200
}
201201
}
202202

203-
let is_option_with_implicit_value =
204-
node.name === 'option' &&
205-
!attributes.some(
206-
(attribute) => attribute.type === 'SpreadAttribute' || attribute.name === 'value'
207-
);
208-
209203
if (has_spread) {
210204
build_element_spread_attributes(node, attributes, style_directives, class_directives, context);
211205
if (node.name === 'option') {
@@ -306,10 +300,6 @@ export function build_element_attributes(node, context) {
306300
}
307301
}
308302

309-
if (is_option_with_implicit_value) {
310-
content = b.call('$.valueless_option', b.id('$$payload'));
311-
}
312-
313303
if (events_to_capture.size !== 0) {
314304
for (const event of events_to_capture) {
315305
context.state.template.push(b.literal(` ${event}="this.__e=event"`));

packages/svelte/src/internal/server/index.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -547,21 +547,23 @@ export function maybe_selected(payload, value) {
547547

548548
/**
549549
* @param {Payload} payload
550-
* @returns {(child: () => void) => void}
550+
* @param {() => void} children
551+
* @returns {void}
551552
*/
552-
export function valueless_option(payload) {
553-
return (child) => {
554-
// store the initial payload before executing the child
555-
let initial_payload = payload.out;
556-
// execute the child to get the runtime body of the option
557-
child();
558-
// remove the initial payload and eventual hydration comments
559-
let body = payload.out.substring(initial_payload.length).replace(/<!---->/g, '');
560-
// check the value of the body with the select_value
561-
if (body === payload.select_value) {
562-
// substring the initial payload to remove the last character (the closing `>`)
563-
// append selected and the body (the closing tag will be added later)
564-
payload.out = initial_payload.substring(0, initial_payload.length - 1) + ' selected>' + body;
565-
}
566-
};
553+
export function valueless_option(payload, children) {
554+
// store the initial payload before rendering children
555+
let initial_payload = payload.out;
556+
557+
// execute the child to get the runtime body of the option
558+
children();
559+
560+
// remove the initial payload and eventual hydration comments
561+
let body = payload.out.substring(initial_payload.length).replace(/<!---->/g, '');
562+
563+
// check the value of the body with the select_value
564+
if (body === payload.select_value) {
565+
// substring the initial payload to remove the last character (the closing `>`)
566+
// append selected and the body (the closing tag will be added later)
567+
payload.out = initial_payload.substring(0, initial_payload.length - 1) + ' selected>' + body;
568+
}
567569
}

packages/svelte/tests/snapshot/samples/skip-static-subtree/_expected/server/index.svelte.js

Lines changed: 1 addition & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)