@@ -19,6 +19,8 @@ var first_child_getter;
19
19
/** @type {() => Node | null } */
20
20
var next_sibling_getter ;
21
21
22
+ var renderer ;
23
+
22
24
/**
23
25
* Initialize these lazily to avoid issues when using the runtime in a server context
24
26
* where these globals are not available while avoiding a separate server entry point
@@ -28,20 +30,23 @@ export function init_operations() {
28
30
return ;
29
31
}
30
32
31
- $window = window ;
32
- $document = document ;
33
- is_firefox = / F i r e f o x / . test ( navigator . userAgent ) ;
33
+ $window = typeof window === 'undefined' ? /** @type { Window } */ ( { } ) : window ;
34
+ $document = typeof document === 'undefined' ? /** @type { Document } */ ( { } ) : document ;
35
+ is_firefox = typeof navigator === 'undefined' ? true : / F i r e f o x / . test ( navigator . userAgent ) ;
34
36
35
- var element_prototype = Element . prototype ;
36
- var node_prototype = Node . prototype ;
37
- var text_prototype = Text . prototype ;
37
+ var element_prototype ;
38
+ if ( window . Element ) element_prototype = Element . prototype ;
39
+ var node_prototype ;
40
+ if ( window . Node ) node_prototype = Node . prototype ;
41
+ var text_prototype ;
42
+ if ( window . Text ) text_prototype = Text . prototype ;
38
43
39
44
// @ts -ignore
40
- first_child_getter = get_descriptor ( node_prototype , 'firstChild' ) . get ;
45
+ first_child_getter = get_descriptor ( node_prototype , 'firstChild' ) ? .get ;
41
46
// @ts -ignore
42
- next_sibling_getter = get_descriptor ( node_prototype , 'nextSibling' ) . get ;
47
+ next_sibling_getter = get_descriptor ( node_prototype , 'nextSibling' ) ? .get ;
43
48
44
- if ( is_extensible ( element_prototype ) ) {
49
+ if ( element_prototype && is_extensible ( element_prototype ) ) {
45
50
// the following assignments improve perf of lookups on DOM nodes
46
51
// @ts -expect-error
47
52
element_prototype . __click = undefined ;
@@ -73,6 +78,9 @@ export function init_operations() {
73
78
* @returns {Text }
74
79
*/
75
80
export function create_text ( value = '' ) {
81
+ if ( renderer ) {
82
+ return renderer . document . createTextNode ( value ) ;
83
+ }
76
84
return document . createTextNode ( value ) ;
77
85
}
78
86
@@ -83,6 +91,9 @@ export function create_text(value = '') {
83
91
*/
84
92
/*@__NO_SIDE_EFFECTS__ */
85
93
export function get_first_child ( node ) {
94
+ if ( renderer_first_child ) {
95
+ return renderer_first_child . call ( node ) ;
96
+ }
86
97
return first_child_getter . call ( node ) ;
87
98
}
88
99
@@ -93,6 +104,9 @@ export function get_first_child(node) {
93
104
*/
94
105
/*@__NO_SIDE_EFFECTS__ */
95
106
export function get_next_sibling ( node ) {
107
+ if ( renderer_next_sibling ) {
108
+ return renderer_next_sibling . call ( node ) ;
109
+ }
96
110
return next_sibling_getter . call ( node ) ;
97
111
}
98
112
@@ -213,6 +227,9 @@ export function clear_text_content(node) {
213
227
* @returns
214
228
*/
215
229
export function create_element ( tag , namespace , is ) {
230
+ if ( renderer ) {
231
+ return renderer . document . createElement ( tag ) ;
232
+ }
216
233
let options = is ? { is } : undefined ;
217
234
if ( namespace ) {
218
235
return document . createElementNS ( namespace , tag , options ) ;
@@ -221,6 +238,9 @@ export function create_element(tag, namespace, is) {
221
238
}
222
239
223
240
export function create_fragment ( ) {
241
+ if ( renderer ) {
242
+ return renderer . document . createDocumentFragment ( ) ;
243
+ }
224
244
return document . createDocumentFragment ( ) ;
225
245
}
226
246
@@ -229,6 +249,9 @@ export function create_fragment() {
229
249
* @returns
230
250
*/
231
251
export function create_comment ( data = '' ) {
252
+ if ( renderer ) {
253
+ return renderer . document . createComment ( data ) ;
254
+ }
232
255
return document . createComment ( data ) ;
233
256
}
234
257
@@ -245,3 +268,24 @@ export function set_attribute(element, key, value = '') {
245
268
}
246
269
return element . setAttribute ( key , value ) ;
247
270
}
271
+
272
+ var renderer_next_sibling ;
273
+ var renderer_first_child ;
274
+
275
+ export function push_renderer ( custom_renderer ) {
276
+ var old_next_sibling = renderer_next_sibling ;
277
+ var old_first_child = renderer_first_child ;
278
+ var old_renderer = renderer ;
279
+ renderer_next_sibling = get_descriptor ( custom_renderer . Node . prototype , 'nextSibling' ) ?. get ;
280
+ renderer_first_child = get_descriptor ( custom_renderer . Node . prototype , 'firstChild' ) ?. get ;
281
+ renderer = custom_renderer ;
282
+ return ( ) => {
283
+ renderer_next_sibling = old_next_sibling ;
284
+ renderer_first_child = old_first_child ;
285
+ renderer = old_renderer ;
286
+ } ;
287
+ }
288
+
289
+ export function get_renderer ( ) {
290
+ return renderer ;
291
+ }
0 commit comments