@@ -519,4 +519,84 @@ describe( 'Position', () => {
519519 document . destroy ( ) ;
520520 } ) ;
521521 } ) ;
522+
523+ describe ( 'getCommonAncestor()' , ( ) => {
524+ let div , ul , liUl1 , liUl2 , texts , section , article , ol , liOl1 , liOl2 , p ;
525+
526+ // |- div
527+ // |- ul
528+ // | |- li
529+ // | | |- foz
530+ // | |- li
531+ // | |- bar
532+ // |- section
533+ // |- Sed id libero at libero tristique
534+ // |- article
535+ // | |- ol
536+ // | | |- li
537+ // | | | |- Lorem ipsum dolor sit amet.
538+ // | | |- li
539+ // | | |- Mauris tincidunt tincidunt leo ac rutrum.
540+ // | |- p
541+ // | | |- Maecenas accumsan tellus.
542+
543+ beforeEach ( ( ) => {
544+ texts = {
545+ foz : new Text ( 'foz' ) ,
546+ bar : new Text ( 'bar' ) ,
547+ lorem : new Text ( 'Lorem ipsum dolor sit amet.' ) ,
548+ mauris : new Text ( 'Mauris tincidunt tincidunt leo ac rutrum.' ) ,
549+ maecenas : new Text ( 'Maecenas accumsan tellus.' ) ,
550+ sed : new Text ( 'Sed id libero at libero tristique.' )
551+ } ;
552+
553+ liUl1 = new Element ( 'li' , null , texts . foz ) ;
554+ liUl2 = new Element ( 'li' , null , texts . bar ) ;
555+ ul = new Element ( 'ul' , null , [ liUl1 , liUl2 ] ) ;
556+
557+ liOl1 = new Element ( 'li' , null , texts . lorem ) ;
558+ liOl2 = new Element ( 'li' , null , texts . mauris ) ;
559+ ol = new Element ( 'ol' , null , [ liOl1 , liOl2 ] ) ;
560+
561+ p = new Element ( 'p' , null , texts . maecenas ) ;
562+
563+ article = new Element ( 'article' , null , [ ol , p ] ) ;
564+ section = new Element ( 'section' , null , [ texts . sed , article ] ) ;
565+
566+ div = new Element ( 'div' , null , [ ul , section ] ) ;
567+ } ) ;
568+
569+ it ( 'for two the same positions returns the parent element' , ( ) => {
570+ const afterLoremPosition = new Position ( liOl1 , 5 ) ;
571+ const otherPosition = Position . createFromPosition ( afterLoremPosition ) ;
572+
573+ test ( afterLoremPosition , otherPosition , liOl1 ) ;
574+ } ) ;
575+
576+ it ( 'for two positions in the same element returns the element' , ( ) => {
577+ const startMaecenasPosition = Position . createAt ( liOl2 ) ;
578+ const beforeTellusPosition = new Position ( liOl2 , 18 ) ;
579+
580+ test ( startMaecenasPosition , beforeTellusPosition , liOl2 ) ;
581+ } ) ;
582+
583+ it ( 'works when one of the positions is nested deeper than the other #1' , ( ) => {
584+ const firstPosition = new Position ( liUl1 , 1 ) ;
585+ const secondPosition = new Position ( p , 3 ) ;
586+
587+ test ( firstPosition , secondPosition , div ) ;
588+ } ) ;
589+
590+ it ( 'works when one of the positions is nested deeper than the other #2' , ( ) => {
591+ const firstPosition = new Position ( liOl2 , 10 ) ;
592+ const secondPosition = new Position ( section , 1 ) ;
593+
594+ test ( firstPosition , secondPosition , section ) ;
595+ } ) ;
596+
597+ function test ( positionA , positionB , lca ) {
598+ expect ( positionA . getCommonAncestor ( positionB ) ) . to . equal ( lca ) ;
599+ expect ( positionB . getCommonAncestor ( positionA ) ) . to . equal ( lca ) ;
600+ }
601+ } ) ;
522602} ) ;
0 commit comments