@@ -73,19 +73,23 @@ export function template(content, flags) {
73
73
/*#__NO_SIDE_EFFECTS__*/
74
74
export function template_with_script ( content , flags ) {
75
75
var first = true ;
76
- var fn = template ( content , flags ) ;
76
+ var fn = template ( content , flags | TEMPLATE_FRAGMENT ) ;
77
77
78
78
return ( ) => {
79
79
if ( hydrating ) return fn ( ) ;
80
80
81
- var node = /** @type {Element | DocumentFragment } */ ( fn ( ) ) ;
81
+ var fragment = /** @type {DocumentFragment } */ ( fn ( ) ) ;
82
82
83
83
if ( first ) {
84
84
first = false ;
85
- run_scripts ( node ) ;
85
+ run_scripts ( fragment ) ;
86
86
}
87
87
88
- return node ;
88
+ if ( ( flags & TEMPLATE_FRAGMENT ) !== 0 ) {
89
+ return fragment ;
90
+ }
91
+
92
+ return /** @type {Node } */ ( fragment . firstChild ) ;
89
93
} ;
90
94
}
91
95
@@ -152,19 +156,23 @@ export function ns_template(content, flags, ns = 'svg') {
152
156
/*#__NO_SIDE_EFFECTS__*/
153
157
export function svg_template_with_script ( content , flags ) {
154
158
var first = true ;
155
- var fn = ns_template ( content , flags ) ;
159
+ var fn = ns_template ( content , flags | TEMPLATE_FRAGMENT ) ;
156
160
157
161
return ( ) => {
158
162
if ( hydrating ) return fn ( ) ;
159
163
160
- var node = /** @type {Element | DocumentFragment } */ ( fn ( ) ) ;
164
+ var fragment = /** @type {DocumentFragment } */ ( fn ( ) ) ;
161
165
162
166
if ( first ) {
163
167
first = false ;
164
- run_scripts ( node ) ;
168
+ run_scripts ( fragment ) ;
169
+ }
170
+
171
+ if ( ( flags & TEMPLATE_FRAGMENT ) !== 0 ) {
172
+ return fragment ;
165
173
}
166
174
167
- return node ;
175
+ return /** @type { Node } */ ( fragment . firstChild ) ;
168
176
} ;
169
177
}
170
178
@@ -181,48 +189,31 @@ export function mathml_template(content, flags) {
181
189
/**
182
190
* Creating a document fragment from HTML that contains script tags will not execute
183
191
* the scripts. We need to replace the script tags with new ones so that they are executed.
184
- * @param {Element | DocumentFragment } node
192
+ * @param {DocumentFragment } fragment
185
193
*/
186
- function run_scripts ( node ) {
194
+ function run_scripts ( fragment ) {
187
195
// scripts were SSR'd, in which case they will run
188
196
if ( hydrating ) return ;
189
197
190
- const is_fragment = node . nodeType === 11 ;
191
- const scripts =
192
- /** @type {HTMLElement } */ ( node ) . tagName === 'SCRIPT'
193
- ? [ /** @type {HTMLScriptElement } */ ( node ) ]
194
- : node . querySelectorAll ( 'script' ) ;
195
198
const effect = /** @type {Effect } */ ( active_effect ) ;
196
199
197
- for ( const script of scripts ) {
200
+ for ( const script of fragment . querySelectorAll ( 'script' ) ) {
198
201
const clone = document . createElement ( 'script' ) ;
199
202
for ( var attribute of script . attributes ) {
200
203
clone . setAttribute ( attribute . name , attribute . value ) ;
201
204
}
202
205
203
206
clone . textContent = script . textContent ;
204
207
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 ( ) ;
208
+ // The script has changed - if it's at the edges, the effect now points at dead nodes
209
+ if ( fragment . firstChild === script ) {
210
+ effect . nodes_start = clone ;
225
211
}
212
+ if ( fragment . lastChild === script ) {
213
+ effect . nodes_end = clone ;
214
+ }
215
+
216
+ script . replaceWith ( clone ) ;
226
217
}
227
218
}
228
219
0 commit comments