-
Notifications
You must be signed in to change notification settings - Fork 791
/
Copy pathvdom-render.ts
698 lines (604 loc) · 25.5 KB
/
vdom-render.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/**
* Virtual DOM patching algorithm based on Snabbdom by
* Simon Friis Vindum (@paldepind)
* Licensed under the MIT License
* https://github.com/snabbdom/snabbdom/blob/master/LICENSE
*
* Modified for Stencil's renderer and slot projection
*/
import type * as d from '../../declarations';
import { BUILD } from '@app-data';
import { CMP_FLAGS, HTML_NS, SVG_NS, isDef } from '@utils';
import { consoleDevError, doc, plt, supportsShadow } from '@platform';
import { h, isHost, newVNode } from './h';
import { NODE_TYPE, PLATFORM_FLAGS, VNODE_FLAGS } from '../runtime-constants';
import { updateElement } from './update-element';
let scopeId: string;
let contentRef: d.RenderNode;
let hostTagName: string;
let useNativeShadowDom = false;
let checkSlotFallbackVisibility = false;
let checkSlotRelocate = false;
let isSvgMode = false;
const createElm = (oldParentVNode: d.VNode, newParentVNode: d.VNode, childIndex: number, parentElm: d.RenderNode) => {
// tslint:disable-next-line: prefer-const
let newVNode = newParentVNode.$children$[childIndex];
let i = 0;
let elm: d.RenderNode;
let childNode: d.RenderNode;
let oldVNode: d.VNode;
if (BUILD.slotRelocation && !useNativeShadowDom) {
// remember for later we need to check to relocate nodes
checkSlotRelocate = true;
if (newVNode.$tag$ === 'slot') {
if (scopeId) {
// scoped css needs to add its scoped id to the parent element
parentElm.classList.add(scopeId + '-s');
}
newVNode.$flags$ |= newVNode.$children$
? // slot element has fallback content
// still create an element that "mocks" the slot element
VNODE_FLAGS.isSlotFallback
: // slot element does not have fallback content
// create an html comment we'll use to always reference
// where actual slot content should sit next to
VNODE_FLAGS.isSlotReference;
}
}
if (BUILD.isDev && newVNode.$elm$) {
consoleDevError(
`The JSX ${
newVNode.$text$ !== null ? `"${newVNode.$text$}" text` : `"${newVNode.$tag$}" element`
} node should not be shared within the same renderer. The renderer caches element lookups in order to improve performance. However, a side effect from this is that the exact same JSX node should not be reused. For more information please see https://stenciljs.com/docs/templating-jsx#avoid-shared-jsx-nodes`,
);
}
if (BUILD.vdomText && newVNode.$text$ !== null) {
// create text node
elm = newVNode.$elm$ = doc.createTextNode(newVNode.$text$) as any;
} else if (BUILD.slotRelocation && newVNode.$flags$ & VNODE_FLAGS.isSlotReference) {
// create a slot reference node
elm = newVNode.$elm$ = BUILD.isDebug || BUILD.hydrateServerSide ? slotReferenceDebugNode(newVNode) : (doc.createTextNode('') as any);
} else {
if (BUILD.svg && !isSvgMode) {
isSvgMode = newVNode.$tag$ === 'svg';
}
// create element
elm = newVNode.$elm$ = (BUILD.svg
? doc.createElementNS(isSvgMode ? SVG_NS : HTML_NS, BUILD.slotRelocation && newVNode.$flags$ & VNODE_FLAGS.isSlotFallback ? 'slot-fb' : (newVNode.$tag$ as string))
: doc.createElement(BUILD.slotRelocation && newVNode.$flags$ & VNODE_FLAGS.isSlotFallback ? 'slot-fb' : (newVNode.$tag$ as string))) as any;
if (BUILD.svg && isSvgMode && newVNode.$tag$ === 'foreignObject') {
isSvgMode = false;
}
// add css classes, attrs, props, listeners, etc.
if (BUILD.vdomAttribute) {
updateElement(null, newVNode, isSvgMode);
}
if ((BUILD.shadowDom || BUILD.scoped) && isDef(scopeId) && elm['s-si'] !== scopeId) {
// if there is a scopeId and this is the initial render
// then let's add the scopeId as a css class
elm.classList.add((elm['s-si'] = scopeId));
}
if (newVNode.$children$) {
for (i = 0; i < newVNode.$children$.length; ++i) {
// create the node
childNode = createElm(oldParentVNode, newVNode, i, elm);
// return node could have been null
if (childNode) {
// append our new node
elm.appendChild(childNode);
}
}
}
if (BUILD.svg) {
if (newVNode.$tag$ === 'svg') {
// Only reset the SVG context when we're exiting <svg> element
isSvgMode = false;
} else if (elm.tagName === 'foreignObject') {
// Reenter SVG context when we're exiting <foreignObject> element
isSvgMode = true;
}
}
}
if (BUILD.slotRelocation) {
elm['s-hn'] = hostTagName;
if (newVNode.$flags$ & (VNODE_FLAGS.isSlotFallback | VNODE_FLAGS.isSlotReference)) {
// remember the content reference comment
elm['s-sr'] = true;
// remember the content reference comment
elm['s-cr'] = contentRef;
// remember the slot name, or empty string for default slot
elm['s-sn'] = newVNode.$name$ || '';
// check if we've got an old vnode for this slot
oldVNode = oldParentVNode && oldParentVNode.$children$ && oldParentVNode.$children$[childIndex];
if (oldVNode && oldVNode.$tag$ === newVNode.$tag$ && oldParentVNode.$elm$) {
// we've got an old slot vnode and the wrapper is being replaced
// so let's move the old slot content back to it's original location
putBackInOriginalLocation(oldParentVNode.$elm$, false);
}
}
}
return elm;
};
const putBackInOriginalLocation = (parentElm: Node, recursive: boolean) => {
plt.$flags$ |= PLATFORM_FLAGS.isTmpDisconnected;
const oldSlotChildNodes = parentElm.childNodes;
for (let i = oldSlotChildNodes.length - 1; i >= 0; i--) {
const childNode = oldSlotChildNodes[i] as any;
if (childNode['s-hn'] !== hostTagName && childNode['s-ol']) {
// // this child node in the old element is from another component
// // remove this node from the old slot's parent
// childNode.remove();
// and relocate it back to it's original location
parentReferenceNode(childNode).insertBefore(childNode, referenceNode(childNode));
// remove the old original location comment entirely
// later on the patch function will know what to do
// and move this to the correct spot in need be
childNode['s-ol'].remove();
childNode['s-ol'] = undefined;
checkSlotRelocate = true;
}
if (recursive) {
putBackInOriginalLocation(childNode, recursive);
}
}
plt.$flags$ &= ~PLATFORM_FLAGS.isTmpDisconnected;
};
const addVnodes = (parentElm: d.RenderNode, before: d.RenderNode, parentVNode: d.VNode, vnodes: d.VNode[], startIdx: number, endIdx: number) => {
let containerElm = ((BUILD.slotRelocation && parentElm['s-cr'] && parentElm['s-cr'].parentNode) || parentElm) as any;
let childNode: Node;
if (BUILD.shadowDom && (containerElm as any).shadowRoot && containerElm.tagName === hostTagName) {
containerElm = (containerElm as any).shadowRoot;
}
for (; startIdx <= endIdx; ++startIdx) {
if (vnodes[startIdx]) {
childNode = createElm(null, parentVNode, startIdx, parentElm);
if (childNode) {
vnodes[startIdx].$elm$ = childNode as any;
containerElm.insertBefore(childNode, BUILD.slotRelocation ? referenceNode(before) : before);
}
}
}
};
const removeVnodes = (vnodes: d.VNode[], startIdx: number, endIdx: number, vnode?: d.VNode, elm?: d.RenderNode) => {
for (; startIdx <= endIdx; ++startIdx) {
if ((vnode = vnodes[startIdx])) {
elm = vnode.$elm$;
callNodeRefs(vnode);
if (BUILD.slotRelocation) {
// we're removing this element
// so it's possible we need to show slot fallback content now
checkSlotFallbackVisibility = true;
if (elm['s-ol']) {
// remove the original location comment
elm['s-ol'].remove();
} else {
// it's possible that child nodes of the node
// that's being removed are slot nodes
putBackInOriginalLocation(elm, true);
}
}
// remove the vnode's element from the dom
elm.remove();
}
}
};
const updateChildren = (parentElm: d.RenderNode, oldCh: d.VNode[], newVNode: d.VNode, newCh: d.VNode[]) => {
let oldStartIdx = 0;
let newStartIdx = 0;
let idxInOld = 0;
let i = 0;
let oldEndIdx = oldCh.length - 1;
let oldStartVnode = oldCh[0];
let oldEndVnode = oldCh[oldEndIdx];
let newEndIdx = newCh.length - 1;
let newStartVnode = newCh[0];
let newEndVnode = newCh[newEndIdx];
let node: Node;
let elmToMove: d.VNode;
while (oldStartIdx <= oldEndIdx && newStartIdx <= newEndIdx) {
if (oldStartVnode == null) {
// Vnode might have been moved left
oldStartVnode = oldCh[++oldStartIdx];
} else if (oldEndVnode == null) {
oldEndVnode = oldCh[--oldEndIdx];
} else if (newStartVnode == null) {
newStartVnode = newCh[++newStartIdx];
} else if (newEndVnode == null) {
newEndVnode = newCh[--newEndIdx];
} else if (isSameVnode(oldStartVnode, newStartVnode)) {
patch(oldStartVnode, newStartVnode);
oldStartVnode = oldCh[++oldStartIdx];
newStartVnode = newCh[++newStartIdx];
} else if (isSameVnode(oldEndVnode, newEndVnode)) {
patch(oldEndVnode, newEndVnode);
oldEndVnode = oldCh[--oldEndIdx];
newEndVnode = newCh[--newEndIdx];
} else if (isSameVnode(oldStartVnode, newEndVnode)) {
// Vnode moved right
if (BUILD.slotRelocation && (oldStartVnode.$tag$ === 'slot' || newEndVnode.$tag$ === 'slot')) {
putBackInOriginalLocation(oldStartVnode.$elm$.parentNode, false);
}
patch(oldStartVnode, newEndVnode);
parentElm.insertBefore(oldStartVnode.$elm$, oldEndVnode.$elm$.nextSibling as any);
oldStartVnode = oldCh[++oldStartIdx];
newEndVnode = newCh[--newEndIdx];
} else if (isSameVnode(oldEndVnode, newStartVnode)) {
// Vnode moved left
if (BUILD.slotRelocation && (oldStartVnode.$tag$ === 'slot' || newEndVnode.$tag$ === 'slot')) {
putBackInOriginalLocation(oldEndVnode.$elm$.parentNode, false);
}
patch(oldEndVnode, newStartVnode);
parentElm.insertBefore(oldEndVnode.$elm$, oldStartVnode.$elm$);
oldEndVnode = oldCh[--oldEndIdx];
newStartVnode = newCh[++newStartIdx];
} else {
// createKeyToOldIdx
idxInOld = -1;
if (BUILD.vdomKey) {
for (i = oldStartIdx; i <= oldEndIdx; ++i) {
if (oldCh[i] && oldCh[i].$key$ !== null && oldCh[i].$key$ === newStartVnode.$key$) {
idxInOld = i;
break;
}
}
}
if (BUILD.vdomKey && idxInOld >= 0) {
elmToMove = oldCh[idxInOld];
if (elmToMove.$tag$ !== newStartVnode.$tag$) {
node = createElm(oldCh && oldCh[newStartIdx], newVNode, idxInOld, parentElm);
} else {
patch(elmToMove, newStartVnode);
oldCh[idxInOld] = undefined;
node = elmToMove.$elm$;
}
newStartVnode = newCh[++newStartIdx];
} else {
// new element
node = createElm(oldCh && oldCh[newStartIdx], newVNode, newStartIdx, parentElm);
newStartVnode = newCh[++newStartIdx];
}
if (node) {
if (BUILD.slotRelocation) {
parentReferenceNode(oldStartVnode.$elm$).insertBefore(node, referenceNode(oldStartVnode.$elm$));
} else {
oldStartVnode.$elm$.parentNode.insertBefore(node, oldStartVnode.$elm$);
}
}
}
}
if (oldStartIdx > oldEndIdx) {
addVnodes(parentElm, newCh[newEndIdx + 1] == null ? null : newCh[newEndIdx + 1].$elm$, newVNode, newCh, newStartIdx, newEndIdx);
} else if (BUILD.updatable && newStartIdx > newEndIdx) {
removeVnodes(oldCh, oldStartIdx, oldEndIdx);
}
};
export const isSameVnode = (vnode1: d.VNode, vnode2: d.VNode) => {
// compare if two vnode to see if they're "technically" the same
// need to have the same element tag, and same key to be the same
if (vnode1.$tag$ === vnode2.$tag$) {
if (BUILD.slotRelocation && vnode1.$tag$ === 'slot') {
return vnode1.$name$ === vnode2.$name$;
}
if (BUILD.vdomKey) {
return vnode1.$key$ === vnode2.$key$;
}
return true;
}
return false;
};
const referenceNode = (node: d.RenderNode) => {
// this node was relocated to a new location in the dom
// because of some other component's slot
// but we still have an html comment in place of where
// it's original location was according to it's original vdom
return (node && node['s-ol']) || node;
};
const parentReferenceNode = (node: d.RenderNode) => (node['s-ol'] ? node['s-ol'] : node).parentNode;
export const patch = (oldVNode: d.VNode, newVNode: d.VNode) => {
const elm = (newVNode.$elm$ = oldVNode.$elm$);
const oldChildren = oldVNode.$children$;
const newChildren = newVNode.$children$;
const tag = newVNode.$tag$;
const text = newVNode.$text$;
let defaultHolder: Comment;
if (!BUILD.vdomText || text === null) {
if (BUILD.svg) {
// test if we're rendering an svg element, or still rendering nodes inside of one
// only add this to the when the compiler sees we're using an svg somewhere
isSvgMode = tag === 'svg' ? true : tag === 'foreignObject' ? false : isSvgMode;
}
// element node
if (BUILD.vdomAttribute || BUILD.reflect) {
if (BUILD.slot && tag === 'slot') {
// minifier will clean this up
} else {
// either this is the first render of an element OR it's an update
// AND we already know it's possible it could have changed
// this updates the element's css classes, attrs, props, listeners, etc.
updateElement(oldVNode, newVNode, isSvgMode);
}
}
if (BUILD.updatable && oldChildren !== null && newChildren !== null) {
// looks like there's child vnodes for both the old and new vnodes
updateChildren(elm, oldChildren, newVNode, newChildren);
} else if (newChildren !== null) {
// no old child vnodes, but there are new child vnodes to add
if (BUILD.updatable && BUILD.vdomText && oldVNode.$text$ !== null) {
// the old vnode was text, so be sure to clear it out
elm.textContent = '';
}
// add the new vnode children
addVnodes(elm, null, newVNode, newChildren, 0, newChildren.length - 1);
} else if (BUILD.updatable && oldChildren !== null) {
// no new child vnodes, but there are old child vnodes to remove
removeVnodes(oldChildren, 0, oldChildren.length - 1);
}
if (BUILD.svg && isSvgMode && tag === 'svg') {
isSvgMode = false;
}
} else if (BUILD.vdomText && BUILD.slotRelocation && (defaultHolder = elm['s-cr'] as any)) {
// this element has slotted content
defaultHolder.parentNode.textContent = text;
} else if (BUILD.vdomText && oldVNode.$text$ !== text) {
// update the text content for the text only vnode
// and also only if the text is different than before
elm.data = text;
}
};
const updateFallbackSlotVisibility = (elm: d.RenderNode) => {
// tslint:disable-next-line: prefer-const
let childNodes: d.RenderNode[] = elm.childNodes as any;
let childNode: d.RenderNode;
let i: number;
let ilen: number;
let j: number;
let slotNameAttr: string;
let nodeType: number;
for (i = 0, ilen = childNodes.length; i < ilen; i++) {
childNode = childNodes[i];
if (childNode.nodeType === NODE_TYPE.ElementNode) {
if (childNode['s-sr']) {
// this is a slot fallback node
// get the slot name for this slot reference node
slotNameAttr = childNode['s-sn'];
// by default always show a fallback slot node
// then hide it if there are other slots in the light dom
childNode.hidden = false;
for (j = 0; j < ilen; j++) {
nodeType = childNodes[j].nodeType;
if (childNodes[j]['s-hn'] !== childNode['s-hn'] || slotNameAttr !== '') {
// this sibling node is from a different component OR is a named fallback slot node
if (nodeType === NODE_TYPE.ElementNode && slotNameAttr === childNodes[j].getAttribute('slot')) {
childNode.hidden = true;
break;
}
} else {
// this is a default fallback slot node
// any element or text node (with content)
// should hide the default fallback slot node
if (
nodeType === NODE_TYPE.ElementNode ||
(nodeType === NODE_TYPE.TextNode && childNodes[j].textContent.trim() !== '')
) {
childNode.hidden = true;
break;
}
}
}
}
// keep drilling down
updateFallbackSlotVisibility(childNode);
}
}
};
const relocateNodes: RelocateNodeData[] = [];
const relocateSlotContent = (elm: d.RenderNode) => {
// tslint:disable-next-line: prefer-const
let childNode: d.RenderNode;
let node: d.RenderNode;
let hostContentNodes: NodeList;
let slotNameAttr: string;
let relocateNodeData: RelocateNodeData;
let j;
let i = 0;
let childNodes: d.RenderNode[] = elm.childNodes as any;
let ilen = childNodes.length;
for (; i < ilen; i++) {
childNode = childNodes[i];
if (childNode['s-sr'] && (node = childNode['s-cr'])) {
// first got the content reference comment node
// then we got it's parent, which is where all the host content is in now
hostContentNodes = node.parentNode.childNodes;
slotNameAttr = childNode['s-sn'];
for (j = hostContentNodes.length - 1; j >= 0; j--) {
node = hostContentNodes[j] as d.RenderNode;
if (!node['s-cn'] && !node['s-nr'] && node['s-hn'] !== childNode['s-hn']) {
// let's do some relocating to its new home
// but never relocate a content reference node
// that is suppose to always represent the original content location
if (isNodeLocatedInSlot(node, slotNameAttr)) {
// it's possible we've already decided to relocate this node
relocateNodeData = relocateNodes.find(r => r.$nodeToRelocate$ === node);
// made some changes to slots
// let's make sure we also double check
// fallbacks are correctly hidden or shown
checkSlotFallbackVisibility = true;
node['s-sn'] = node['s-sn'] || slotNameAttr;
if (relocateNodeData) {
// previously we never found a slot home for this node
// but turns out we did, so let's remember it now
relocateNodeData.$slotRefNode$ = childNode;
} else {
// add to our list of nodes to relocate
relocateNodes.push({
$slotRefNode$: childNode,
$nodeToRelocate$: node,
});
}
if (node['s-sr']) {
relocateNodes.map(relocateNode => {
if (isNodeLocatedInSlot(relocateNode.$nodeToRelocate$, node['s-sn'])) {
relocateNodeData = relocateNodes.find(r => r.$nodeToRelocate$ === node);
if (relocateNodeData && !relocateNode.$slotRefNode$) {
relocateNode.$slotRefNode$ = relocateNodeData.$slotRefNode$;
}
}
});
}
} else if (!relocateNodes.some(r => r.$nodeToRelocate$ === node)) {
// so far this element does not have a slot home, not setting slotRefNode on purpose
// if we never find a home for this element then we'll need to hide it
relocateNodes.push({
$nodeToRelocate$: node,
});
}
}
}
}
if (childNode.nodeType === NODE_TYPE.ElementNode) {
relocateSlotContent(childNode);
}
}
};
const isNodeLocatedInSlot = (nodeToRelocate: d.RenderNode, slotNameAttr: string) => {
if (nodeToRelocate.nodeType === NODE_TYPE.ElementNode) {
if (nodeToRelocate.getAttribute('slot') === null && slotNameAttr === '') {
return true;
}
if (nodeToRelocate.getAttribute('slot') === slotNameAttr) {
return true;
}
return false;
}
if (nodeToRelocate['s-sn'] === slotNameAttr) {
return true;
}
return slotNameAttr === '';
};
export const callNodeRefs = (vNode: d.VNode) => {
if (BUILD.vdomRef) {
vNode.$attrs$ && vNode.$attrs$.ref && vNode.$attrs$.ref(null);
vNode.$children$ && vNode.$children$.map(callNodeRefs);
}
};
interface RelocateNodeData {
$slotRefNode$?: d.RenderNode;
$nodeToRelocate$: d.RenderNode;
}
export const renderVdom = (hostRef: d.HostRef, renderFnResults: d.VNode | d.VNode[]) => {
const hostElm = hostRef.$hostElement$;
const cmpMeta = hostRef.$cmpMeta$;
const oldVNode: d.VNode = hostRef.$vnode$ || newVNode(null, null);
const rootVnode = isHost(renderFnResults) ? renderFnResults : h(null, null, renderFnResults as any);
hostTagName = hostElm.tagName;
// <Host> runtime check
if (BUILD.isDev && Array.isArray(renderFnResults) && renderFnResults.some(isHost)) {
throw new Error(`The <Host> must be the single root component.
Looks like the render() function of "${hostTagName.toLowerCase()}" is returning an array that contains the <Host>.
The render() function should look like this instead:
render() {
// Do not return an array
return (
<Host>{content}</Host>
);
}
`);
}
if (BUILD.reflect && cmpMeta.$attrsToReflect$) {
rootVnode.$attrs$ = rootVnode.$attrs$ || {};
cmpMeta.$attrsToReflect$.map(([propName, attribute]) => (rootVnode.$attrs$[attribute] = (hostElm as any)[propName]));
}
rootVnode.$tag$ = null;
rootVnode.$flags$ |= VNODE_FLAGS.isHost;
hostRef.$vnode$ = rootVnode;
rootVnode.$elm$ = oldVNode.$elm$ = (BUILD.shadowDom ? hostElm.shadowRoot || hostElm : hostElm) as any;
if (BUILD.scoped || BUILD.shadowDom) {
scopeId = hostElm['s-sc'];
}
if (BUILD.slotRelocation) {
contentRef = hostElm['s-cr'];
useNativeShadowDom = supportsShadow && (cmpMeta.$flags$ & CMP_FLAGS.shadowDomEncapsulation) !== 0;
// always reset
checkSlotFallbackVisibility = false;
}
// synchronous patch
patch(oldVNode, rootVnode);
if (BUILD.slotRelocation) {
// while we're moving nodes around existing nodes, temporarily disable
// the disconnectCallback from working
plt.$flags$ |= PLATFORM_FLAGS.isTmpDisconnected;
if (checkSlotRelocate) {
relocateSlotContent(rootVnode.$elm$);
let relocateData: RelocateNodeData;
let nodeToRelocate: d.RenderNode;
let orgLocationNode: d.RenderNode;
let parentNodeRef: Node;
let insertBeforeNode: Node;
let refNode: d.RenderNode;
let i = 0;
for (; i < relocateNodes.length; i++) {
relocateData = relocateNodes[i];
nodeToRelocate = relocateData.$nodeToRelocate$;
if (!nodeToRelocate['s-ol']) {
// add a reference node marking this node's original location
// keep a reference to this node for later lookups
orgLocationNode = BUILD.isDebug || BUILD.hydrateServerSide ? originalLocationDebugNode(nodeToRelocate) : (doc.createTextNode('') as any);
orgLocationNode['s-nr'] = nodeToRelocate;
nodeToRelocate.parentNode.insertBefore((nodeToRelocate['s-ol'] = orgLocationNode), nodeToRelocate);
}
}
for (i = 0; i < relocateNodes.length; i++) {
relocateData = relocateNodes[i];
nodeToRelocate = relocateData.$nodeToRelocate$;
if (relocateData.$slotRefNode$) {
// by default we're just going to insert it directly
// after the slot reference node
parentNodeRef = relocateData.$slotRefNode$.parentNode;
insertBeforeNode = relocateData.$slotRefNode$.nextSibling;
orgLocationNode = nodeToRelocate['s-ol'] as any;
while ((orgLocationNode = orgLocationNode.previousSibling as any)) {
refNode = orgLocationNode['s-nr'];
if (refNode && refNode['s-sn'] === nodeToRelocate['s-sn'] && parentNodeRef === refNode.parentNode) {
refNode = refNode.nextSibling as any;
if (!refNode || !refNode['s-nr']) {
insertBeforeNode = refNode;
break;
}
}
}
if ((!insertBeforeNode && parentNodeRef !== nodeToRelocate.parentNode) || nodeToRelocate.nextSibling !== insertBeforeNode) {
// we've checked that it's worth while to relocate
// since that the node to relocate
// has a different next sibling or parent relocated
if (nodeToRelocate !== insertBeforeNode) {
if (!nodeToRelocate['s-hn'] && nodeToRelocate['s-ol']) {
// probably a component in the index.html that doesn't have it's hostname set
nodeToRelocate['s-hn'] = nodeToRelocate['s-ol'].parentNode.nodeName;
}
// add it back to the dom but in its new home
parentNodeRef.insertBefore(nodeToRelocate, insertBeforeNode);
}
}
} else {
// this node doesn't have a slot home to go to, so let's hide it
if (nodeToRelocate.nodeType === NODE_TYPE.ElementNode) {
nodeToRelocate.hidden = true;
}
}
}
}
if (checkSlotFallbackVisibility) {
updateFallbackSlotVisibility(rootVnode.$elm$);
}
// done moving nodes around
// allow the disconnect callback to work again
plt.$flags$ &= ~PLATFORM_FLAGS.isTmpDisconnected;
// always reset
relocateNodes.length = 0;
}
};
// slot comment debug nodes only created with the `--debug` flag
// otherwise these nodes are text nodes w/out content
const slotReferenceDebugNode = (slotVNode: d.VNode) => doc.createComment(`<slot${slotVNode.$name$ ? ' name="' + slotVNode.$name$ + '"' : ''}> (host=${hostTagName.toLowerCase()})`);
const originalLocationDebugNode = (nodeToRelocate: d.RenderNode): any =>
doc.createComment(`org-location for ` + (nodeToRelocate.localName ? `<${nodeToRelocate.localName}> (host=${nodeToRelocate['s-hn']})` : `[${nodeToRelocate.textContent}]`));