-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
children.ts
56 lines (47 loc) · 1.26 KB
/
children.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
namespace $ {
export function $mol_dom_render_children (
el : Element | DocumentFragment ,
childNodes : NodeList | Array< Node | string | null >
) {
const node_set = new Set< Node | string | null >( childNodes )
let nextNode : Node | null = el.firstChild
for( let view of childNodes ) {
if( view == null ) continue
if( view instanceof $mol_dom_context.Node ) {
while( true ) {
if( !nextNode ) {
el.appendChild( view )
break
}
if( nextNode == view ) {
nextNode = nextNode.nextSibling
break
} else {
if( node_set.has( nextNode ) ) {
el.insertBefore( view , nextNode )
break
} else {
const nn = nextNode.nextSibling
el.removeChild( nextNode )
nextNode = nn
}
}
}
} else {
if( nextNode && nextNode.nodeName === '#text' ) {
const str = String( view )
if( nextNode.nodeValue !== str ) nextNode.nodeValue = str
nextNode = nextNode.nextSibling
} else {
const textNode = $mol_dom_context.document.createTextNode( String( view ) )
el.insertBefore( textNode , nextNode )
}
}
}
while( nextNode ) {
const currNode = nextNode
nextNode = currNode.nextSibling
el.removeChild( currNode )
}
}
}