diff --git a/packages/ckeditor5-utils/src/dom/rect.js b/packages/ckeditor5-utils/src/dom/rect.js index f9b411c5d66..4d1774e3ae9 100644 --- a/packages/ckeditor5-utils/src/dom/rect.js +++ b/packages/ckeditor5-utils/src/dom/rect.js @@ -78,7 +78,7 @@ export default class Rect { // @if CK_DEBUG // } if ( isSourceRange ) { - copyRectProperties( this, Rect.getDomRangeRects( source )[ 0 ] ); + copyRangeRectProperties( this, source ); } else { copyRectProperties( this, source.getBoundingClientRect() ); } @@ -406,3 +406,25 @@ function isBody( elementOrRange ) { return elementOrRange === elementOrRange.ownerDocument.body; } + +// Copies size properties from the `source` to the `rect`. +// +// @private +// @param {module:utils/dom/rect~Rect} rect Target rect. +// @param {Range} source +function copyRangeRectProperties( rect, source ) { + const rangeRects = Rect.getDomRangeRects( source ); + const combinedRect = rangeRects[ 0 ]; + + for ( let i = 1; i < rangeRects.length; i++ ) { + combinedRect.right = Math.max( combinedRect.right, rangeRects[ i ].right ); + combinedRect.left = Math.min( combinedRect.left, rangeRects[ i ].left ); + combinedRect.bottom = Math.max( combinedRect.bottom, rangeRects[ i ].bottom ); + combinedRect.top = Math.min( combinedRect.top, rangeRects[ i ].top ); + } + + combinedRect.width = combinedRect.right - combinedRect.left; + combinedRect.height = combinedRect.bottom - combinedRect.top; + + copyRectProperties( rect, combinedRect ); +}