Skip to content

Commit b919898

Browse files
committed
simpler and removal of the "first" trick
1 parent df5064b commit b919898

File tree

1 file changed

+24
-45
lines changed

1 file changed

+24
-45
lines changed

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

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,8 @@ export function template(content, flags) {
7272
*/
7373
/*#__NO_SIDE_EFFECTS__*/
7474
export function template_with_script(content, flags) {
75-
var first = true;
76-
var fn = template(content, flags | TEMPLATE_FRAGMENT);
77-
78-
return () => {
79-
if (hydrating) return fn();
80-
81-
var fragment = /** @type {DocumentFragment} */ (fn());
82-
83-
if (first) {
84-
first = false;
85-
run_scripts(fragment);
86-
}
87-
88-
if ((flags & TEMPLATE_FRAGMENT) !== 0) {
89-
return fragment;
90-
}
91-
92-
return /** @type {Node} */ (fragment.firstChild);
93-
};
75+
var fn = template(content, flags);
76+
return () => run_scripts(/** @type {Element | DocumentFragment} */ (fn()));
9477
}
9578

9679
/**
@@ -155,25 +138,8 @@ export function ns_template(content, flags, ns = 'svg') {
155138
*/
156139
/*#__NO_SIDE_EFFECTS__*/
157140
export function svg_template_with_script(content, flags) {
158-
var first = true;
159-
var fn = ns_template(content, flags | TEMPLATE_FRAGMENT);
160-
161-
return () => {
162-
if (hydrating) return fn();
163-
164-
var fragment = /** @type {DocumentFragment} */ (fn());
165-
166-
if (first) {
167-
first = false;
168-
run_scripts(fragment);
169-
}
170-
171-
if ((flags & TEMPLATE_FRAGMENT) !== 0) {
172-
return fragment;
173-
}
174-
175-
return /** @type {Node} */ (fragment.firstChild);
176-
};
141+
var fn = ns_template(content, flags);
142+
return () => run_scripts(/** @type {Element | DocumentFragment} */ (fn()));
177143
}
178144

179145
/**
@@ -189,15 +155,21 @@ export function mathml_template(content, flags) {
189155
/**
190156
* Creating a document fragment from HTML that contains script tags will not execute
191157
* the scripts. We need to replace the script tags with new ones so that they are executed.
192-
* @param {DocumentFragment} fragment
158+
* @param {Element | DocumentFragment} node
159+
* @returns {Node | Node[]}
193160
*/
194-
function run_scripts(fragment) {
161+
function run_scripts(node) {
195162
// scripts were SSR'd, in which case they will run
196-
if (hydrating) return;
163+
if (hydrating) return node;
197164

165+
const is_fragment = node.nodeType === 11;
166+
const scripts =
167+
/** @type {HTMLElement} */ (node).tagName === 'SCRIPT'
168+
? [/** @type {HTMLScriptElement} */ (node)]
169+
: node.querySelectorAll('script');
198170
const effect = /** @type {Effect} */ (active_effect);
199171

200-
for (const script of fragment.querySelectorAll('script')) {
172+
for (const script of scripts) {
201173
const clone = document.createElement('script');
202174
for (var attribute of script.attributes) {
203175
clone.setAttribute(attribute.name, attribute.value);
@@ -206,15 +178,22 @@ function run_scripts(fragment) {
206178
clone.textContent = script.textContent;
207179

208180
// The script has changed - if it's at the edges, the effect now points at dead nodes
209-
if (fragment.firstChild === script) {
181+
if (is_fragment ? node.firstChild === script : node === script) {
210182
effect.nodes_start = clone;
211183
}
212-
if (fragment.lastChild === script) {
184+
if (is_fragment ? node.lastChild === script : node === script) {
213185
effect.nodes_end = clone;
214186
}
215187

216-
script.replaceWith(clone);
188+
// If node === script tag, replaceWith will do nothing because there's no parent yet
189+
if (script === node) {
190+
// but we can returns the cloned <script> immediately
191+
return clone;
192+
} else {
193+
script.replaceWith(clone);
194+
}
217195
}
196+
return node;
218197
}
219198

220199
/**

0 commit comments

Comments
 (0)