This repository was archived by the owner on Jun 26, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +34
-4
lines changed
Expand file tree Collapse file tree 2 files changed +34
-4
lines changed Original file line number Diff line number Diff line change 77 * @module utils/dom/rect
88 */
99
10+ /* global Node */
11+
1012import isRange from './isrange' ;
1113import isWindow from './iswindow' ;
1214import isElement from '../lib/lodash/isElement' ;
@@ -361,12 +363,19 @@ export default class Rect {
361363 // If there's no client rects for the Range, use parent container's bounding rect
362364 // instead and adjust rect's width to simulate the actual geometry of such range.
363365 // https://github.com/ckeditor/ckeditor5-utils/issues/153
366+ // https://github.com/ckeditor/ckeditor5-ui/issues/317
364367 else {
365- const startContainerRect = new Rect ( range . startContainer . getBoundingClientRect ( ) ) ;
366- startContainerRect . right = startContainerRect . left ;
367- startContainerRect . width = 0 ;
368+ let startContainer = range . startContainer ;
369+
370+ if ( startContainer . nodeType === Node . TEXT_NODE ) {
371+ startContainer = startContainer . parentNode ;
372+ }
373+
374+ const rect = new Rect ( startContainer . getBoundingClientRect ( ) ) ;
375+ rect . right = rect . left ;
376+ rect . width = 0 ;
368377
369- rects . push ( startContainerRect ) ;
378+ rects . push ( rect ) ;
370379 }
371380
372381 return rects ;
Original file line number Diff line number Diff line change @@ -965,6 +965,27 @@ describe( 'Rect', () => {
965965 expect ( rects ) . to . have . length ( 1 ) ;
966966 assertRect ( rects [ 0 ] , expectedGeometry ) ;
967967 } ) ;
968+
969+ // https://github.com/ckeditor/ckeditor5-ui/issues/317
970+ it ( 'should return rects for a text node\'s parent (collapsed, no Range rects available)' , ( ) => {
971+ const range = document . createRange ( ) ;
972+ const element = document . createElement ( 'div' ) ;
973+ const textNode = document . createTextNode ( 'abc' ) ;
974+ element . appendChild ( textNode ) ;
975+
976+ range . setStart ( textNode , 3 ) ;
977+ range . collapse ( ) ;
978+ testUtils . sinon . stub ( range , 'getClientRects' ) . returns ( [ ] ) ;
979+ testUtils . sinon . stub ( element , 'getBoundingClientRect' ) . returns ( geometry ) ;
980+
981+ const expectedGeometry = Object . assign ( { } , geometry ) ;
982+ expectedGeometry . right = expectedGeometry . left ;
983+ expectedGeometry . width = 0 ;
984+
985+ const rects = Rect . getDomRangeRects ( range ) ;
986+ expect ( rects ) . to . have . length ( 1 ) ;
987+ assertRect ( rects [ 0 ] , expectedGeometry ) ;
988+ } ) ;
968989 } ) ;
969990} ) ;
970991
You can’t perform that action at this time.
0 commit comments