@@ -72,21 +72,8 @@ export function template(content, flags) {
72
72
*/
73
73
/*#__NO_SIDE_EFFECTS__*/
74
74
export function template_with_script ( content , flags ) {
75
- var first = true ;
76
75
var fn = template ( content , flags ) ;
77
-
78
- return ( ) => {
79
- if ( hydrating ) return fn ( ) ;
80
-
81
- var node = /** @type {Element | DocumentFragment } */ ( fn ( ) ) ;
82
-
83
- if ( first ) {
84
- first = false ;
85
- run_scripts ( node ) ;
86
- }
87
-
88
- return node ;
89
- } ;
76
+ return ( ) => run_scripts ( /** @type {Element | DocumentFragment } */ ( fn ( ) ) ) ;
90
77
}
91
78
92
79
/**
@@ -151,21 +138,8 @@ export function ns_template(content, flags, ns = 'svg') {
151
138
*/
152
139
/*#__NO_SIDE_EFFECTS__*/
153
140
export function svg_template_with_script ( content , flags ) {
154
- var first = true ;
155
141
var fn = ns_template ( content , flags ) ;
156
-
157
- return ( ) => {
158
- if ( hydrating ) return fn ( ) ;
159
-
160
- var node = /** @type {Element | DocumentFragment } */ ( fn ( ) ) ;
161
-
162
- if ( first ) {
163
- first = false ;
164
- run_scripts ( node ) ;
165
- }
166
-
167
- return node ;
168
- } ;
142
+ return ( ) => run_scripts ( /** @type {Element | DocumentFragment } */ ( fn ( ) ) ) ;
169
143
}
170
144
171
145
/**
@@ -182,10 +156,11 @@ export function mathml_template(content, flags) {
182
156
* Creating a document fragment from HTML that contains script tags will not execute
183
157
* the scripts. We need to replace the script tags with new ones so that they are executed.
184
158
* @param {Element | DocumentFragment } node
159
+ * @returns {Node | Node[] }
185
160
*/
186
161
function run_scripts ( node ) {
187
162
// scripts were SSR'd, in which case they will run
188
- if ( hydrating ) return ;
163
+ if ( hydrating ) return node ;
189
164
190
165
const is_fragment = node . nodeType === 11 ;
191
166
const scripts =
@@ -202,28 +177,17 @@ function run_scripts(node) {
202
177
203
178
clone . textContent = script . textContent ;
204
179
205
- const replace = ( ) => {
206
- // The script has changed - if it's at the edges, the effect now points at dead nodes
207
- if ( is_fragment ? node . firstChild === script : node === script ) {
208
- effect . nodes_start = clone ;
209
- }
210
- if ( is_fragment ? node . lastChild === script : node === script ) {
211
- effect . nodes_end = clone ;
212
- }
213
-
214
- script . replaceWith ( clone ) ;
215
- } ;
216
-
217
- // If node === script tag, replaceWith will do nothing because there's no parent yet,
218
- // waiting until that's the case using an effect solves this.
219
- // Don't do it in other circumstances or we could accidentally execute scripts
220
- // in an adjacent @html tag that was instantiated in the meantime.
221
- if ( script === node ) {
222
- queue_micro_task ( replace ) ;
223
- } else {
224
- replace ( ) ;
180
+ // The script has changed - if it's at the edges, the effect now points at dead nodes
181
+ if ( is_fragment ? node . firstChild === script : node === script ) {
182
+ effect . nodes_start = clone ;
183
+ }
184
+ if ( is_fragment ? node . lastChild === script : node === script ) {
185
+ effect . nodes_end = clone ;
225
186
}
187
+
188
+ script . replaceWith ( clone ) ;
226
189
}
190
+ return node ;
227
191
}
228
192
229
193
/**
0 commit comments