Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit bfa55e9

Browse files
authored
Merge pull request #213 from ckeditor/t/ckeditor5-ui/317
Fix: `Rect.getDomRangeRects()` should not throw if the provided DOM range starts in a text node. Closes ckeditor/ckeditor5-ui#317.
2 parents d29307a + 4e1291a commit bfa55e9

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/dom/rect.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* @module utils/dom/rect
88
*/
99

10+
/* global Node */
11+
1012
import isRange from './isrange';
1113
import isWindow from './iswindow';
1214
import 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;

tests/dom/rect.js

+21
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)