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

Commit

Permalink
Merge pull request #213 from ckeditor/t/ckeditor5-ui/317
Browse files Browse the repository at this point in the history
Fix: `Rect.getDomRangeRects()` should not throw if the provided DOM range starts in a text node. Closes ckeditor/ckeditor5-ui#317.
  • Loading branch information
oleq authored Dec 19, 2017
2 parents d29307a + 4e1291a commit bfa55e9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/dom/rect.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @module utils/dom/rect
*/

/* global Node */

import isRange from './isrange';
import isWindow from './iswindow';
import isElement from '../lib/lodash/isElement';
Expand Down Expand Up @@ -361,12 +363,19 @@ export default class Rect {
// If there's no client rects for the Range, use parent container's bounding rect
// instead and adjust rect's width to simulate the actual geometry of such range.
// https://github.com/ckeditor/ckeditor5-utils/issues/153
// https://github.com/ckeditor/ckeditor5-ui/issues/317
else {
const startContainerRect = new Rect( range.startContainer.getBoundingClientRect() );
startContainerRect.right = startContainerRect.left;
startContainerRect.width = 0;
let startContainer = range.startContainer;

if ( startContainer.nodeType === Node.TEXT_NODE ) {
startContainer = startContainer.parentNode;
}

const rect = new Rect( startContainer.getBoundingClientRect() );
rect.right = rect.left;
rect.width = 0;

rects.push( startContainerRect );
rects.push( rect );
}

return rects;
Expand Down
21 changes: 21 additions & 0 deletions tests/dom/rect.js
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,27 @@ describe( 'Rect', () => {
expect( rects ).to.have.length( 1 );
assertRect( rects[ 0 ], expectedGeometry );
} );

// https://github.com/ckeditor/ckeditor5-ui/issues/317
it( 'should return rects for a text node\'s parent (collapsed, no Range rects available)', () => {
const range = document.createRange();
const element = document.createElement( 'div' );
const textNode = document.createTextNode( 'abc' );
element.appendChild( textNode );

range.setStart( textNode, 3 );
range.collapse();
testUtils.sinon.stub( range, 'getClientRects' ).returns( [] );
testUtils.sinon.stub( element, 'getBoundingClientRect' ).returns( geometry );

const expectedGeometry = Object.assign( {}, geometry );
expectedGeometry.right = expectedGeometry.left;
expectedGeometry.width = 0;

const rects = Rect.getDomRangeRects( range );
expect( rects ).to.have.length( 1 );
assertRect( rects[ 0 ], expectedGeometry );
} );
} );
} );

Expand Down

0 comments on commit bfa55e9

Please sign in to comment.