11import { VNode , RelativePosition , Predicate , Typeguard , isLeaf } from './VNode' ;
22import { Constructor , nodeLength } from '../../../utils/src/utils' ;
3+ import { ContainerNode } from './ContainerNode' ;
4+ import { AtomicNode } from './AtomicNode' ;
35
46let id = 0 ;
57export abstract class AbstractNode {
@@ -14,9 +16,9 @@ export abstract class AbstractNode {
1416 *
1517 * @param predicate The predicate to check.
1618 */
17- static isConstructor < T extends VNode > (
18- predicate : Constructor < T > | Predicate ,
19- ) : predicate is Constructor < T > {
19+ static isConstructor (
20+ predicate : Predicate ,
21+ ) : predicate is Constructor < ContainerNode > | Constructor < AtomicNode > {
2022 return predicate . prototype instanceof AbstractNode ;
2123 }
2224
@@ -78,7 +80,9 @@ export abstract class AbstractNode {
7880 *
7981 * @param predicate The subclass of VNode to test this node against.
8082 */
81- is < T extends VNode > ( predicate : Constructor < T > | Typeguard < T > ) : this is T {
83+ is < T extends VNode > ( predicate : Constructor < T > | Typeguard < T > ) : this is T ;
84+ is ( predicate : Predicate ) : false ;
85+ is ( predicate : Predicate ) : boolean {
8286 if ( AbstractNode . isConstructor ( predicate ) ) {
8387 return this instanceof predicate ;
8488 } else {
@@ -96,11 +100,11 @@ export abstract class AbstractNode {
96100 *
97101 * @param predicate The predicate to test this node against.
98102 */
99- test < T extends VNode > ( predicate ?: Constructor < T > | Predicate ) : boolean {
103+ test ( predicate ?: Predicate ) : boolean {
100104 if ( ! predicate ) {
101105 return true ;
102106 } else if ( AbstractNode . isConstructor ( predicate ) ) {
103- return this . is ( predicate ) ;
107+ return this instanceof predicate ;
104108 } else {
105109 return predicate ( this as VNode ) ;
106110 }
@@ -161,8 +165,8 @@ export abstract class AbstractNode {
161165 * @param predicate
162166 */
163167 closest < T extends VNode > ( predicate : Predicate < T > ) : T ;
164- closest < T > ( predicate : Predicate < T > ) : VNode ;
165- closest < T > ( predicate : Predicate < T > ) : VNode {
168+ closest ( predicate : Predicate ) : VNode ;
169+ closest ( predicate : Predicate ) : VNode {
166170 if ( this . test ( predicate ) ) {
167171 return this as VNode ;
168172 } else {
@@ -176,8 +180,8 @@ export abstract class AbstractNode {
176180 * @param [predicate]
177181 */
178182 ancestor < T extends VNode > ( predicate ?: Predicate < T > ) : T ;
179- ancestor < T > ( predicate ?: Predicate < T > ) : VNode ;
180- ancestor < T > ( predicate ?: Predicate < T > ) : VNode {
183+ ancestor ( predicate ?: Predicate ) : VNode ;
184+ ancestor ( predicate ?: Predicate ) : VNode {
181185 let ancestor = this . parent ;
182186 while ( ancestor && ! ancestor . test ( predicate ) ) {
183187 ancestor = ancestor . parent ;
@@ -192,8 +196,8 @@ export abstract class AbstractNode {
192196 * @param [predicate]
193197 */
194198 ancestors < T extends VNode > ( predicate ?: Predicate < T > ) : T [ ] ;
195- ancestors < T > ( predicate ?: Predicate < T > ) : VNode [ ] ;
196- ancestors < T > ( predicate ?: Predicate < T > ) : VNode [ ] {
199+ ancestors ( predicate ?: Predicate ) : VNode [ ] ;
200+ ancestors ( predicate ?: Predicate ) : VNode [ ] {
197201 const ancestors : VNode [ ] = [ ] ;
198202 let parent = this . parent ;
199203 while ( parent ) {
@@ -210,8 +214,8 @@ export abstract class AbstractNode {
210214 * @param node
211215 */
212216 commonAncestor < T extends VNode > ( node : VNode , predicate ?: Predicate < T > ) : T ;
213- commonAncestor < T > ( node : VNode , predicate ?: Predicate < T > ) : VNode ;
214- commonAncestor < T > ( node : VNode , predicate ?: Predicate < T > ) : VNode {
217+ commonAncestor ( node : VNode , predicate ?: Predicate ) : VNode ;
218+ commonAncestor ( node : VNode , predicate ?: Predicate ) : VNode {
215219 if ( ! this . parent ) {
216220 return ;
217221 } else if ( this . parent === node . parent && this . parent . test ( predicate ) ) {
@@ -231,8 +235,8 @@ export abstract class AbstractNode {
231235 * @param [predicate]
232236 */
233237 siblings < T extends VNode > ( predicate ?: Predicate < T > ) : T [ ] ;
234- siblings < T > ( predicate ?: Predicate < T > ) : VNode [ ] ;
235- siblings < T > ( predicate ?: Predicate < T > ) : VNode [ ] {
238+ siblings ( predicate ?: Predicate ) : VNode [ ] ;
239+ siblings ( predicate ?: Predicate ) : VNode [ ] {
236240 const siblings : VNode [ ] = [ ] ;
237241 let sibling : VNode = this . previousSibling ( ) ;
238242 while ( sibling ) {
@@ -254,8 +258,8 @@ export abstract class AbstractNode {
254258 * Return the nodes adjacent to this VNode that satisfy the given predicate.
255259 */
256260 adjacents < T extends VNode > ( predicate ?: Predicate < T > ) : T [ ] ;
257- adjacents < T > ( predicate ?: Predicate < T > ) : VNode [ ] ;
258- adjacents < T > ( predicate ?: Predicate < T > ) : VNode [ ] {
261+ adjacents ( predicate ?: Predicate ) : VNode [ ] ;
262+ adjacents ( predicate ?: Predicate ) : VNode [ ] {
259263 const adjacents : VNode [ ] = [ ] ;
260264 let sibling : VNode = this . previousSibling ( ) ;
261265 while ( sibling && sibling . test ( predicate ) ) {
@@ -276,8 +280,8 @@ export abstract class AbstractNode {
276280 * @param [predicate]
277281 */
278282 previousSibling < T extends VNode > ( predicate ?: Predicate < T > ) : T ;
279- previousSibling < T > ( predicate ?: Predicate < T > ) : VNode ;
280- previousSibling < T > ( predicate ?: Predicate < T > ) : VNode {
283+ previousSibling ( predicate ?: Predicate ) : VNode ;
284+ previousSibling ( predicate ?: Predicate ) : VNode {
281285 if ( ! this . parent ) return ;
282286 const index = this . parent . childVNodes . indexOf ( this as VNode ) ;
283287 let sibling = this . parent . childVNodes [ index - 1 ] ;
@@ -294,8 +298,8 @@ export abstract class AbstractNode {
294298 * @param [predicate]
295299 */
296300 nextSibling < T extends VNode > ( predicate ?: Predicate < T > ) : T ;
297- nextSibling < T > ( predicate ?: Predicate < T > ) : VNode ;
298- nextSibling < T > ( predicate ?: Predicate < T > ) : VNode {
301+ nextSibling ( predicate ?: Predicate ) : VNode ;
302+ nextSibling ( predicate ?: Predicate ) : VNode {
299303 if ( ! this . parent ) return ;
300304 const index = this . parent . childVNodes . indexOf ( this as VNode ) ;
301305 let sibling = this . parent . childVNodes [ index + 1 ] ;
@@ -313,8 +317,8 @@ export abstract class AbstractNode {
313317 * @param [predicate]
314318 */
315319 previous < T extends VNode > ( predicate ?: Predicate < T > ) : T ;
316- previous < T > ( predicate ?: Predicate < T > ) : VNode ;
317- previous < T > ( predicate ?: Predicate < T > ) : VNode {
320+ previous ( predicate ?: Predicate ) : VNode ;
321+ previous ( predicate ?: Predicate ) : VNode {
318322 let previous = this . previousSibling ( ) ;
319323 if ( previous ) {
320324 // The previous node is the last leaf of the previous sibling.
@@ -336,8 +340,8 @@ export abstract class AbstractNode {
336340 * @param [predicate]
337341 */
338342 next < T extends VNode > ( predicate ?: Predicate < T > ) : T ;
339- next < T > ( predicate ?: Predicate < T > ) : VNode ;
340- next < T > ( predicate ?: Predicate < T > ) : VNode {
343+ next ( predicate ?: Predicate ) : VNode ;
344+ next ( predicate ?: Predicate ) : VNode {
341345 // The node after node is its first child.
342346 let next = this . firstChild ( ) ;
343347 if ( ! next ) {
@@ -366,8 +370,8 @@ export abstract class AbstractNode {
366370 * @param [predicate]
367371 */
368372 previousLeaf < T extends VNode > ( predicate ?: Predicate < T > ) : T ;
369- previousLeaf < T > ( predicate ?: Predicate < T > ) : VNode ;
370- previousLeaf < T > ( predicate ?: Predicate < T > ) : VNode {
373+ previousLeaf ( predicate ?: Predicate ) : VNode ;
374+ previousLeaf ( predicate ?: Predicate ) : VNode {
371375 return this . previous ( ( node : VNode ) : boolean => {
372376 return isLeaf ( node ) && node . test ( predicate ) ;
373377 } ) ;
@@ -380,8 +384,8 @@ export abstract class AbstractNode {
380384 * @param [predicate]
381385 */
382386 nextLeaf < T extends VNode > ( predicate ?: Predicate < T > ) : T ;
383- nextLeaf < T > ( predicate ?: Predicate < T > ) : VNode ;
384- nextLeaf < T > ( predicate ?: Predicate < T > ) : VNode {
387+ nextLeaf ( predicate ?: Predicate ) : VNode ;
388+ nextLeaf ( predicate ?: Predicate ) : VNode {
385389 return this . next ( ( node : VNode ) : boolean => {
386390 return isLeaf ( node ) && node . test ( predicate ) ;
387391 } ) ;
@@ -394,8 +398,8 @@ export abstract class AbstractNode {
394398 * @param [predicate]
395399 */
396400 previousSiblings < T extends VNode > ( predicate ?: Predicate < T > ) : T [ ] ;
397- previousSiblings < T > ( predicate ?: Predicate < T > ) : VNode [ ] ;
398- previousSiblings < T > ( predicate ?: Predicate < T > ) : VNode [ ] {
401+ previousSiblings ( predicate ?: Predicate ) : VNode [ ] ;
402+ previousSiblings ( predicate ?: Predicate ) : VNode [ ] {
399403 const previousSiblings : VNode [ ] = [ ] ;
400404 let sibling = this . previousSibling ( ) ;
401405 while ( sibling ) {
@@ -414,8 +418,8 @@ export abstract class AbstractNode {
414418 * @param [predicate]
415419 */
416420 nextSiblings < T extends VNode > ( predicate ?: Predicate < T > ) : T [ ] ;
417- nextSiblings < T > ( predicate ?: Predicate < T > ) : VNode [ ] ;
418- nextSiblings < T > ( predicate ?: Predicate < T > ) : VNode [ ] {
421+ nextSiblings ( predicate ?: Predicate ) : VNode [ ] ;
422+ nextSiblings ( predicate ?: Predicate ) : VNode [ ] {
419423 const nextSiblings : VNode [ ] = [ ] ;
420424 let sibling = this . nextSibling ( ) ;
421425 while ( sibling ) {
@@ -518,31 +522,31 @@ export abstract class AbstractNode {
518522 * @param [predicate]
519523 */
520524 abstract firstChild < T extends VNode > ( predicate ?: Predicate < T > ) : T ;
521- abstract firstChild < T > ( predicate ?: Predicate < T > ) : VNode ;
525+ abstract firstChild ( predicate ?: Predicate ) : VNode ;
522526 /**
523527 * Return the last child of this VNode that satisfies the given predicate.
524528 * If no predicate is given, return the last child of this VNode.
525529 *
526530 * @param [predicate]
527531 */
528532 abstract lastChild < T extends VNode > ( predicate ?: Predicate < T > ) : T ;
529- abstract lastChild < T > ( predicate ?: Predicate < T > ) : VNode ;
533+ abstract lastChild ( predicate ?: Predicate ) : VNode ;
530534 /**
531535 * Return the first leaf of this VNode that satisfies the given predicate.
532536 * If no predicate is given, return the first leaf of this VNode.
533537 *
534538 * @param [predicate]
535539 */
536540 abstract firstLeaf < T extends VNode > ( predicate ?: Predicate < T > ) : T ;
537- abstract firstLeaf < T > ( predicate ?: Predicate < T > ) : VNode ;
541+ abstract firstLeaf ( predicate ?: Predicate ) : VNode ;
538542 /**
539543 * Return the last leaf of this VNode that satisfies the given predicate.
540544 * If no predicate is given, return the last leaf of this VNode.
541545 *
542546 * @param [predicate]
543547 */
544548 abstract lastLeaf < T extends VNode > ( predicate ?: Predicate < T > ) : T ;
545- abstract lastLeaf < T > ( predicate ?: Predicate < T > ) : VNode ;
549+ abstract lastLeaf ( predicate ?: Predicate ) : VNode ;
546550 /**
547551 * Return all descendants of the current node that satisfy the given
548552 * predicate. If no predicate is given return all the ancestors of the
@@ -551,23 +555,23 @@ export abstract class AbstractNode {
551555 * @param [predicate]
552556 */
553557 abstract descendants < T extends VNode > ( predicate ?: Predicate < T > ) : T [ ] ;
554- abstract descendants < T > ( predicate ?: Predicate < T > ) : VNode [ ] ;
558+ abstract descendants ( predicate ?: Predicate ) : VNode [ ] ;
555559 /**
556560 * Return the first descendant of this VNode that satisfies the predicate.
557561 * If no predicate is given, return the first descendant of this VNode.
558562 *
559563 * @param [predicate]
560564 */
561565 abstract firstDescendant < T extends VNode > ( predicate ?: Predicate < T > ) : T ;
562- abstract firstDescendant < T > ( predicate ?: Predicate < T > ) : VNode ;
566+ abstract firstDescendant ( predicate ?: Predicate ) : VNode ;
563567 /**
564568 * Return the last descendant of this VNode that satisfies the predicate.
565569 * If no predicate is given, return the last descendant of this VNode.
566570 *
567571 * @param [predicate]
568572 */
569573 abstract lastDescendant < T extends VNode > ( predicate ?: Predicate < T > ) : T ;
570- abstract lastDescendant < T > ( predicate ?: Predicate < T > ) : VNode ;
574+ abstract lastDescendant ( predicate ?: Predicate ) : VNode ;
571575
572576 //--------------------------------------------------------------------------
573577 // Updating children. To be implemented by the concrete subclass.
0 commit comments