Skip to content

Commit 7d1551b

Browse files
committed
use function composition
1 parent 408955d commit 7d1551b

File tree

3 files changed

+11
-51
lines changed

3 files changed

+11
-51
lines changed

packages/svelte/src/compiler/phases/3-transform/client/transform-template/index.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,12 @@ import * as b from '../../../../utils/builders.js';
1212
* @returns
1313
*/
1414
function get_template_function(namespace, state) {
15-
const contains_script_tag = state.template.contains_script_tag;
1615
return (
1716
namespace === 'svg'
18-
? contains_script_tag
19-
? '$.svg_template_with_script'
20-
: '$.ns_template'
17+
? '$.ns_template'
2118
: namespace === 'mathml'
2219
? '$.mathml_template'
23-
: contains_script_tag
24-
? '$.template_with_script'
25-
: '$.template'
20+
: '$.template'
2621
).concat(state.options.templatingMode === 'functional' ? '_fn' : '');
2722
}
2823

@@ -67,6 +62,10 @@ export function transform_template(state, namespace, flags) {
6762
flags ? b.literal(flags) : undefined
6863
);
6964

65+
if (state.template.contains_script_tag) {
66+
call = b.call(`$.with_script`, call);
67+
}
68+
7069
if (dev) {
7170
return b.call(
7271
'$.add_locations',

packages/svelte/src/internal/client/dom/template.js

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -165,24 +165,10 @@ export function template_fn(structure, flags) {
165165
}
166166

167167
/**
168-
* @param {string} content
169-
* @param {number} flags
170-
* @returns {() => Node | Node[]}
168+
* @param {() => Element | DocumentFragment} fn
171169
*/
172-
/*#__NO_SIDE_EFFECTS__*/
173-
export function template_with_script(content, flags) {
174-
var fn = template(content, flags);
175-
return () => run_scripts(/** @type {Element | DocumentFragment} */ (fn()));
176-
}
177-
178-
/**
179-
* @param {Array<TemplateStructure>} structure
180-
* @param {number} flags
181-
* @returns {() => Node | Node[]}
182-
*/ /*#__NO_SIDE_EFFECTS__*/
183-
export function template_with_script_fn(structure, flags) {
184-
var templated_fn = template_fn(structure, flags);
185-
return () => run_scripts(/** @type {Element | DocumentFragment} */ (templated_fn()));
170+
export function with_script(fn) {
171+
return () => run_scripts(fn());
186172
}
187173

188174
/**
@@ -290,28 +276,6 @@ export function ns_template_fn(structure, flags, ns = 'svg') {
290276
};
291277
}
292278

293-
/**
294-
* @param {string} content
295-
* @param {number} flags
296-
* @returns {() => Node | Node[]}
297-
*/
298-
/*#__NO_SIDE_EFFECTS__*/
299-
export function svg_template_with_script(content, flags) {
300-
var fn = ns_template(content, flags);
301-
return () => run_scripts(/** @type {Element | DocumentFragment} */ (fn()));
302-
}
303-
304-
/**
305-
* @param {Array<TemplateStructure>} structure
306-
* @param {number} flags
307-
* @returns {() => Node | Node[]}
308-
*/
309-
/*#__NO_SIDE_EFFECTS__*/
310-
export function svg_template_with_script_fn(structure, flags) {
311-
var templated_fn = ns_template_fn(structure, flags);
312-
return () => run_scripts(/** @type {Element | DocumentFragment} */ (templated_fn()));
313-
}
314-
315279
/**
316280
* @param {string} content
317281
* @param {number} flags

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,13 @@ export {
9090
comment,
9191
ns_template,
9292
ns_template_fn,
93-
svg_template_with_script,
94-
svg_template_with_script_fn,
9593
mathml_template,
9694
mathml_template_fn,
9795
template,
9896
template_fn,
99-
template_with_script,
100-
template_with_script_fn,
10197
text,
102-
props_id
98+
props_id,
99+
with_script
103100
} from './dom/template.js';
104101
export { user_derived as derived, derived_safe_equal } from './reactivity/deriveds.js';
105102
export {

0 commit comments

Comments
 (0)