@@ -5,85 +5,69 @@ export * from './dom/patchProp'
5
5
export * from './dom/templateRef'
6
6
export * from './dom/on'
7
7
8
- export function insert ( block : Block , parent : Node , anchor : Node | null = null ) {
8
+ function normalizeBlock ( block : Block ) : Node [ ] {
9
+ const nodes : Node [ ] = [ ]
9
10
if ( block instanceof Node ) {
10
- parent . insertBefore ( block , anchor )
11
+ nodes . push ( block )
11
12
} else if ( isArray ( block ) ) {
12
- for ( const child of block ) insert ( child , parent , anchor )
13
- } else {
14
- insert ( block . nodes , parent , anchor )
15
- block . anchor && parent . insertBefore ( block . anchor , anchor )
13
+ block . forEach ( child => nodes . push ( ... normalizeBlock ( child ) ) )
14
+ } else if ( block ) {
15
+ nodes . push ( ... normalizeBlock ( block . nodes ) )
16
+ block . anchor && nodes . push ( block . anchor )
16
17
}
18
+ return nodes
17
19
}
18
20
19
- export function prepend ( parent : ParentBlock , ...blocks : Block [ ] ) {
20
- const nodes : Node [ ] = [ ]
21
-
22
- for ( const block of blocks ) {
23
- if ( block instanceof Node ) {
24
- nodes . push ( block )
25
- } else if ( isArray ( block ) ) {
26
- prepend ( parent , ...block )
21
+ export function insert (
22
+ block : Block ,
23
+ parent : ParentBlock ,
24
+ anchor : Node | null = null ,
25
+ ) {
26
+ if ( isArray ( parent ) ) {
27
+ const index = anchor ? parent . indexOf ( anchor ) : - 1
28
+ if ( index > - 1 ) {
29
+ parent . splice ( index , 0 , block )
27
30
} else {
28
- prepend ( parent , block . nodes )
29
- block . anchor && prepend ( parent , block . anchor )
31
+ parent . push ( block )
30
32
}
33
+ } else {
34
+ normalizeBlock ( block ) . forEach ( node => parent . insertBefore ( node , anchor ) )
31
35
}
36
+ }
32
37
33
- if ( ! nodes . length ) return
34
-
35
- if ( parent instanceof Node ) {
36
- // TODO use insertBefore for better performance https://jsbench.me/rolpg250hh/1
37
- parent . prepend ( ...nodes )
38
- } else if ( isArray ( parent ) ) {
39
- parent . unshift ( ...nodes )
38
+ export function prepend ( parent : ParentBlock , ...blocks : Block [ ] ) {
39
+ if ( isArray ( parent ) ) {
40
+ parent . unshift ( ...blocks )
41
+ } else {
42
+ parent . prepend ( ...normalizeBlock ( blocks ) )
40
43
}
41
44
}
42
45
43
46
export function append ( parent : ParentBlock , ...blocks : Block [ ] ) {
44
- const nodes : Node [ ] = [ ]
45
-
46
- for ( const block of blocks ) {
47
- if ( block instanceof Node ) {
48
- nodes . push ( block )
49
- } else if ( isArray ( block ) ) {
50
- append ( parent , ...block )
51
- } else {
52
- append ( parent , block . nodes )
53
- block . anchor && append ( parent , block . anchor )
54
- }
55
- }
56
-
57
- if ( ! nodes . length ) return
58
-
59
- if ( parent instanceof Node ) {
60
- // TODO use insertBefore for better performance
61
- parent . append ( ...nodes )
62
- } else if ( isArray ( parent ) ) {
63
- parent . push ( ...nodes )
47
+ if ( isArray ( parent ) ) {
48
+ parent . push ( ...blocks )
49
+ } else {
50
+ parent . append ( ...normalizeBlock ( blocks ) )
64
51
}
65
52
}
66
53
67
- export function remove ( block : Block , parent : ParentNode ) {
68
- if ( block instanceof DocumentFragment ) {
69
- remove ( Array . from ( block . childNodes ) , parent )
70
- } else if ( block instanceof Node ) {
71
- parent . removeChild ( block )
72
- } else if ( isArray ( block ) ) {
73
- for ( const child of block ) remove ( child , parent )
54
+ export function remove ( block : Block , parent : ParentBlock ) {
55
+ if ( isArray ( parent ) ) {
56
+ const index = parent . indexOf ( block )
57
+ if ( index > - 1 ) {
58
+ parent . splice ( index , 1 )
59
+ }
74
60
} else {
75
- remove ( block . nodes , parent )
76
- block . anchor && parent . removeChild ( block . anchor )
61
+ normalizeBlock ( block ) . forEach ( node => parent . removeChild ( node ) )
77
62
}
78
63
}
79
64
80
65
type Children = Record < number , [ ChildNode , Children ] >
81
- export function children ( n : Node ) : Children {
66
+ export function children ( nodes : ChildNode [ ] ) : Children {
82
67
const result : Children = { }
83
- const array = Array . from ( n . childNodes )
84
- for ( let i = 0 ; i < array . length ; i ++ ) {
85
- const n = array [ i ]
86
- result [ i ] = [ n , children ( n ) ]
68
+ for ( let i = 0 ; i < nodes . length ; i ++ ) {
69
+ const n = nodes [ i ]
70
+ result [ i ] = [ n , children ( Array . from ( n . childNodes ) ) ]
87
71
}
88
72
return result
89
73
}
0 commit comments