@@ -72,25 +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
- 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 ( ) ) ) ;
94
77
}
95
78
96
79
/**
@@ -155,25 +138,8 @@ export function ns_template(content, flags, ns = 'svg') {
155
138
*/
156
139
/*#__NO_SIDE_EFFECTS__*/
157
140
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 ( ) ) ) ;
177
143
}
178
144
179
145
/**
@@ -189,15 +155,21 @@ export function mathml_template(content, flags) {
189
155
/**
190
156
* Creating a document fragment from HTML that contains script tags will not execute
191
157
* 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[] }
193
160
*/
194
- function run_scripts ( fragment ) {
161
+ function run_scripts ( node ) {
195
162
// scripts were SSR'd, in which case they will run
196
- if ( hydrating ) return ;
163
+ if ( hydrating ) return node ;
197
164
165
+ const is_fragment = node . nodeType === 11 ;
166
+ const scripts =
167
+ /** @type {HTMLElement } */ ( node ) . tagName === 'SCRIPT'
168
+ ? [ /** @type {HTMLScriptElement } */ ( node ) ]
169
+ : node . querySelectorAll ( 'script' ) ;
198
170
const effect = /** @type {Effect } */ ( active_effect ) ;
199
171
200
- for ( const script of fragment . querySelectorAll ( 'script' ) ) {
172
+ for ( const script of scripts ) {
201
173
const clone = document . createElement ( 'script' ) ;
202
174
for ( var attribute of script . attributes ) {
203
175
clone . setAttribute ( attribute . name , attribute . value ) ;
@@ -206,15 +178,22 @@ function run_scripts(fragment) {
206
178
clone . textContent = script . textContent ;
207
179
208
180
// 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 ) {
210
182
effect . nodes_start = clone ;
211
183
}
212
- if ( fragment . lastChild === script ) {
184
+ if ( is_fragment ? node . lastChild === script : node === script ) {
213
185
effect . nodes_end = clone ;
214
186
}
215
187
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
+ }
217
195
}
196
+ return node ;
218
197
}
219
198
220
199
/**
0 commit comments