8
0
Piotrek Koszuliński 8 лет назад
Родитель
Сommit
3c755611bd
1 измененных файлов с 19 добавлено и 16 удалено
  1. 19 16
      packages/ckeditor5-utils/src/dom/rect.js

+ 19 - 16
packages/ckeditor5-utils/src/dom/rect.js

@@ -11,8 +11,6 @@ import global from './global';
 import isRange from './isrange';
 import isElement from '../lib/lodash/isElement';
 
-const rectProperties = [ 'top', 'right', 'bottom', 'left', 'width', 'height' ];
-
 /**
  * A helper class representing a `ClientRect` object, e.g. value returned by
  * the native `object.getBoundingClientRect()` method. Provides a set of methods
@@ -54,38 +52,30 @@ export default class Rect {
 			enumerable: false
 		} );
 
-		// Acquires all the rect properties from the passed source.
-		//
-		// @private
-		// @param {ClientRect|module:utils/dom/rect~Rect|Object} source}
-		const setProperties = source => {
-			rectProperties.forEach( p => this[ p ] = source[ p ] );
-		};
-
 		if ( isElement( source ) ) {
-			setProperties( source.getBoundingClientRect() );
+			copyRectProperties( this, source.getBoundingClientRect() );
 		} else if ( isRange( source ) ) {
-			// Use getClientRects() when the range is collapsed
+			// Use getClientRects() when the range is collapsed.
 			// https://github.com/ckeditor/ckeditor5-utils/issues/153
 			if ( source.collapsed ) {
 				const rects = source.getClientRects();
 
 				if ( rects.length ) {
-					setProperties( rects[ 0 ] );
+					copyRectProperties( this, rects[ 0 ] );
 				}
 				// 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
 				else {
-					setProperties( source.startContainer.getBoundingClientRect() );
+					copyRectProperties( this, source.startContainer.getBoundingClientRect() );
 					this.width = 0;
 				}
 			} else {
-				setProperties( source.getBoundingClientRect() );
+				copyRectProperties( this, source.getBoundingClientRect() );
 			}
 		} else {
-			setProperties( source );
+			copyRectProperties( this, source );
 		}
 
 		/**
@@ -279,3 +269,16 @@ export default class Rect {
 		} );
 	}
 }
+
+const rectProperties = [ 'top', 'right', 'bottom', 'left', 'width', 'height' ];
+
+// Acquires all the rect properties from the passed source.
+//
+// @private
+// @param {module:utils/dom/rect~Rect} rect
+// @param {ClientRect|module:utils/dom/rect~Rect|Object} source
+function copyRectProperties( rect, source ) {
+	for ( const p of rectProperties ) {
+		rect[ p ] = source[ p ];
+	}
+}