@@ -45,7 +45,7 @@ describe( 'Rect', () => {
4545 const range = document . createRange ( ) ;
4646
4747 range . selectNode ( document . body ) ;
48- testUtils . sinon . stub ( range , 'getBoundingClientRect ' ) . returns ( geometry ) ;
48+ testUtils . sinon . stub ( range , 'getClientRects ' ) . returns ( [ geometry ] ) ;
4949
5050 assertRect ( new Rect ( range ) , geometry ) ;
5151 } ) ;
@@ -71,6 +71,7 @@ describe( 'Rect', () => {
7171 testUtils . sinon . stub ( element , 'getBoundingClientRect' ) . returns ( geometry ) ;
7272
7373 const expectedGeometry = Object . assign ( { } , geometry ) ;
74+ expectedGeometry . right = expectedGeometry . left ;
7475 expectedGeometry . width = 0 ;
7576
7677 assertRect ( new Rect ( range ) , expectedGeometry ) ;
@@ -510,14 +511,14 @@ describe( 'Rect', () => {
510511 range . setStart ( ancestorA , 0 ) ;
511512 range . setEnd ( ancestorA , 1 ) ;
512513
513- testUtils . sinon . stub ( range , 'getBoundingClientRect ' ) . returns ( {
514+ testUtils . sinon . stub ( range , 'getClientRects ' ) . returns ( [ {
514515 top : 0 ,
515516 right : 100 ,
516517 bottom : 100 ,
517518 left : 0 ,
518519 width : 100 ,
519520 height : 100
520- } ) ;
521+ } ] ) ;
521522
522523 testUtils . sinon . stub ( ancestorA , 'getBoundingClientRect' ) . returns ( {
523524 top : 50 ,
@@ -584,6 +585,53 @@ describe( 'Rect', () => {
584585 } ) ;
585586 } ) ;
586587 } ) ;
588+
589+ describe ( 'getDomRangeRects() ' , ( ) => {
590+ it ( 'should return rects for a Range (non–collapsed)' , ( ) => {
591+ const range = document . createRange ( ) ;
592+
593+ range . selectNode ( document . body ) ;
594+ testUtils . sinon . stub ( range , 'getClientRects' ) . returns ( [ geometry ] ) ;
595+
596+ const rects = Rect . getDomRangeRects ( range ) ;
597+ expect ( rects ) . to . have . length ( 1 ) ;
598+ assertRect ( rects [ 0 ] , geometry ) ;
599+ } ) ;
600+
601+ // https://github.com/ckeditor/ckeditor5-utils/issues/153
602+ it ( 'should return rects for a Range (collapsed)' , ( ) => {
603+ const range = document . createRange ( ) ;
604+ const secondGeometry = { top : 20 , right : 80 , bottom : 60 , left : 40 , width : 40 , height : 40 } ;
605+
606+ range . collapse ( ) ;
607+ testUtils . sinon . stub ( range , 'getClientRects' ) . returns ( [ geometry , secondGeometry ] ) ;
608+
609+ const rects = Rect . getDomRangeRects ( range ) ;
610+ expect ( rects ) . to . have . length ( 2 ) ;
611+
612+ assertRect ( rects [ 0 ] , geometry ) ;
613+ assertRect ( rects [ 1 ] , secondGeometry ) ;
614+ } ) ;
615+
616+ // https://github.com/ckeditor/ckeditor5-utils/issues/153
617+ it ( 'should return rects for a Range (collapsed, no Range rects available)' , ( ) => {
618+ const range = document . createRange ( ) ;
619+ const element = document . createElement ( 'div' ) ;
620+
621+ range . setStart ( element , 0 ) ;
622+ range . collapse ( ) ;
623+ testUtils . sinon . stub ( range , 'getClientRects' ) . returns ( [ ] ) ;
624+ testUtils . sinon . stub ( element , 'getBoundingClientRect' ) . returns ( geometry ) ;
625+
626+ const expectedGeometry = Object . assign ( { } , geometry ) ;
627+ expectedGeometry . right = expectedGeometry . left ;
628+ expectedGeometry . width = 0 ;
629+
630+ const rects = Rect . getDomRangeRects ( range ) ;
631+ expect ( rects ) . to . have . length ( 1 ) ;
632+ assertRect ( rects [ 0 ] , expectedGeometry ) ;
633+ } ) ;
634+ } ) ;
587635} ) ;
588636
589637function assertRect ( rect , expected ) {
0 commit comments