@@ -4,7 +4,6 @@ import { ChildError } from '../../../utils/src/errors';
44import { VersionableArray } from '../Memory/VersionableArray' ;
55
66export class ContainerNode extends AbstractNode {
7- parent : ContainerNode ;
87 readonly childVNodes : VNode [ ] = new VersionableArray < VNode > ( ) ;
98
109 //--------------------------------------------------------------------------
@@ -17,15 +16,19 @@ export class ContainerNode extends AbstractNode {
1716 children < T extends VNode > ( predicate ?: Predicate < T > ) : T [ ] ;
1817 children ( predicate ?: Predicate ) : VNode [ ] ;
1918 children ( predicate ?: Predicate ) : VNode [ ] {
20- return this . childVNodes . filter ( child => {
21- 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+ }
2224 } ) ;
25+ return children ;
2326 }
2427 /**
2528 * See {@link AbstractNode.hasChildren}.
2629 */
2730 hasChildren ( ) : boolean {
28- return this . children ( ) . length > 0 ;
31+ return ! ! this . childVNodes . find ( child => child . tangible ) ;
2932 }
3033 /**
3134 * See {@link AbstractNode.nthChild}.
@@ -40,7 +43,7 @@ export class ContainerNode extends AbstractNode {
4043 firstChild ( predicate ?: Predicate ) : VNode ;
4144 firstChild ( predicate ?: Predicate ) : VNode {
4245 let child = this . childVNodes [ 0 ] ;
43- while ( child && ! ( child . tangible && child . test ( predicate ) ) ) {
46+ while ( child && ! ( child . tangible && ( ! predicate || child . test ( predicate ) ) ) ) {
4447 child = child . nextSibling ( ) ;
4548 }
4649 return child ;
@@ -52,7 +55,7 @@ export class ContainerNode extends AbstractNode {
5255 lastChild ( predicate ?: Predicate ) : VNode ;
5356 lastChild ( predicate ?: Predicate ) : VNode {
5457 let child = this . childVNodes [ this . childVNodes . length - 1 ] ;
55- while ( child && ! ( child . tangible && child . test ( predicate ) ) ) {
58+ while ( child && ! ( child . tangible && ( ! predicate || child . test ( predicate ) ) ) ) {
5659 child = child . previousSibling ( ) ;
5760 }
5861 return child ;
@@ -64,7 +67,7 @@ export class ContainerNode extends AbstractNode {
6467 firstLeaf ( predicate ?: Predicate ) : VNode ;
6568 firstLeaf ( predicate ?: Predicate ) : VNode {
6669 const isValidLeaf = ( node : VNode ) : boolean => {
67- return isLeaf ( node ) && node . test ( predicate ) ;
70+ return isLeaf ( node ) && ( ! predicate || node . test ( predicate ) ) ;
6871 } ;
6972 if ( isValidLeaf ( this ) ) {
7073 return this ;
@@ -79,7 +82,7 @@ export class ContainerNode extends AbstractNode {
7982 lastLeaf ( predicate ?: Predicate ) : VNode ;
8083 lastLeaf ( predicate ?: Predicate ) : VNode {
8184 const isValidLeaf = ( node : VNode ) : boolean => {
82- return isLeaf ( node ) && node . test ( predicate ) ;
85+ return isLeaf ( node ) && ( ! predicate || node . test ( predicate ) ) ;
8386 } ;
8487 if ( isValidLeaf ( this ) ) {
8588 return this ;
@@ -94,7 +97,7 @@ export class ContainerNode extends AbstractNode {
9497 firstDescendant ( predicate ?: Predicate ) : VNode ;
9598 firstDescendant ( predicate ?: Predicate ) : VNode {
9699 let firstDescendant = this . firstChild ( ) ;
97- while ( firstDescendant && ! firstDescendant . test ( predicate ) ) {
100+ while ( firstDescendant && predicate && firstDescendant . test ( predicate ) ) {
98101 firstDescendant = this . _descendantAfter ( firstDescendant ) ;
99102 }
100103 return firstDescendant ;
@@ -109,7 +112,7 @@ export class ContainerNode extends AbstractNode {
109112 while ( lastDescendant && lastDescendant . hasChildren ( ) ) {
110113 lastDescendant = lastDescendant . lastChild ( ) ;
111114 }
112- while ( lastDescendant && ! lastDescendant . test ( predicate ) ) {
115+ while ( lastDescendant && predicate && ! lastDescendant . test ( predicate ) ) {
113116 lastDescendant = this . _descendantBefore ( lastDescendant ) ;
114117 }
115118 return lastDescendant ;
@@ -124,7 +127,7 @@ export class ContainerNode extends AbstractNode {
124127 const stack = [ ...this . childVNodes ] ;
125128 while ( stack . length ) {
126129 const node = stack . shift ( ) ;
127- if ( node . tangible && node . test ( predicate ) ) {
130+ if ( node . tangible && ( ! predicate || node . test ( predicate ) ) ) {
128131 descendants . push ( node ) ;
129132 }
130133 if ( node instanceof ContainerNode ) {
@@ -227,9 +230,11 @@ export class ContainerNode extends AbstractNode {
227230 return this ;
228231 }
229232 const duplicate = this . clone ( ) ;
230- const index = child . parent . childVNodes . indexOf ( child ) ;
231- while ( this . childVNodes . length > index ) {
232- 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 ;
233238 }
234239 this . after ( duplicate ) ;
235240 return duplicate ;
0 commit comments