@@ -16,15 +16,19 @@ export class ContainerNode extends AbstractNode {
1616 children < T extends VNode > ( predicate ?: Predicate < T > ) : T [ ] ;
1717 children ( predicate ?: Predicate ) : VNode [ ] ;
1818 children ( predicate ?: Predicate ) : VNode [ ] {
19- return this . childVNodes . filter ( child => {
20- return child . tangible && child . test ( predicate ) ;
19+ const children : VNode [ ] = [ ] ;
20+ this . childVNodes . forEach ( child => {
21+ if ( child . tangible && ( ! predicate || child . test ( predicate ) ) ) {
22+ children . push ( child ) ;
23+ }
2124 } ) ;
25+ return children ;
2226 }
2327 /**
2428 * See {@link AbstractNode.hasChildren}.
2529 */
2630 hasChildren ( ) : boolean {
27- return this . children ( ) . length > 0 ;
31+ return ! ! this . childVNodes . find ( child => child . tangible ) ;
2832 }
2933 /**
3034 * See {@link AbstractNode.nthChild}.
@@ -39,7 +43,7 @@ export class ContainerNode extends AbstractNode {
3943 firstChild ( predicate ?: Predicate ) : VNode ;
4044 firstChild ( predicate ?: Predicate ) : VNode {
4145 let child = this . childVNodes [ 0 ] ;
42- while ( child && ! ( child . tangible && child . test ( predicate ) ) ) {
46+ while ( child && ! ( child . tangible && ( ! predicate || child . test ( predicate ) ) ) ) {
4347 child = child . nextSibling ( ) ;
4448 }
4549 return child ;
@@ -51,7 +55,7 @@ export class ContainerNode extends AbstractNode {
5155 lastChild ( predicate ?: Predicate ) : VNode ;
5256 lastChild ( predicate ?: Predicate ) : VNode {
5357 let child = this . childVNodes [ this . childVNodes . length - 1 ] ;
54- while ( child && ! ( child . tangible && child . test ( predicate ) ) ) {
58+ while ( child && ! ( child . tangible && ( ! predicate || child . test ( predicate ) ) ) ) {
5559 child = child . previousSibling ( ) ;
5660 }
5761 return child ;
@@ -63,7 +67,7 @@ export class ContainerNode extends AbstractNode {
6367 firstLeaf ( predicate ?: Predicate ) : VNode ;
6468 firstLeaf ( predicate ?: Predicate ) : VNode {
6569 const isValidLeaf = ( node : VNode ) : boolean => {
66- return isLeaf ( node ) && node . test ( predicate ) ;
70+ return isLeaf ( node ) && ( ! predicate || node . test ( predicate ) ) ;
6771 } ;
6872 if ( isValidLeaf ( this ) ) {
6973 return this ;
@@ -78,7 +82,7 @@ export class ContainerNode extends AbstractNode {
7882 lastLeaf ( predicate ?: Predicate ) : VNode ;
7983 lastLeaf ( predicate ?: Predicate ) : VNode {
8084 const isValidLeaf = ( node : VNode ) : boolean => {
81- return isLeaf ( node ) && node . test ( predicate ) ;
85+ return isLeaf ( node ) && ( ! predicate || node . test ( predicate ) ) ;
8286 } ;
8387 if ( isValidLeaf ( this ) ) {
8488 return this ;
@@ -93,7 +97,7 @@ export class ContainerNode extends AbstractNode {
9397 firstDescendant ( predicate ?: Predicate ) : VNode ;
9498 firstDescendant ( predicate ?: Predicate ) : VNode {
9599 let firstDescendant = this . firstChild ( ) ;
96- while ( firstDescendant && ! firstDescendant . test ( predicate ) ) {
100+ while ( firstDescendant && predicate && ! firstDescendant . test ( predicate ) ) {
97101 firstDescendant = this . _descendantAfter ( firstDescendant ) ;
98102 }
99103 return firstDescendant ;
@@ -108,7 +112,7 @@ export class ContainerNode extends AbstractNode {
108112 while ( lastDescendant && lastDescendant . hasChildren ( ) ) {
109113 lastDescendant = lastDescendant . lastChild ( ) ;
110114 }
111- while ( lastDescendant && ! lastDescendant . test ( predicate ) ) {
115+ while ( lastDescendant && predicate && ! lastDescendant . test ( predicate ) ) {
112116 lastDescendant = this . _descendantBefore ( lastDescendant ) ;
113117 }
114118 return lastDescendant ;
@@ -123,7 +127,7 @@ export class ContainerNode extends AbstractNode {
123127 const stack = [ ...this . childVNodes ] ;
124128 while ( stack . length ) {
125129 const node = stack . shift ( ) ;
126- if ( node . tangible && node . test ( predicate ) ) {
130+ if ( node . tangible && ( ! predicate || node . test ( predicate ) ) ) {
127131 descendants . push ( node ) ;
128132 }
129133 if ( node instanceof ContainerNode ) {
@@ -226,9 +230,11 @@ export class ContainerNode extends AbstractNode {
226230 return this ;
227231 }
228232 const duplicate = this . clone ( ) ;
229- const index = child . parent . childVNodes . indexOf ( child ) ;
230- while ( this . childVNodes . length > index ) {
231- duplicate . append ( this . childVNodes [ index ] ) ;
233+ const index = this . childVNodes . indexOf ( child ) ;
234+ const children = this . childVNodes . splice ( index ) ;
235+ duplicate . childVNodes . push ( ...children ) ;
236+ for ( const child of children ) {
237+ child . parent = duplicate ;
232238 }
233239 this . after ( duplicate ) ;
234240 return duplicate ;
0 commit comments