Sfoglia il codice sorgente

Made Rect class independent of global#window and document. Using rect source's window instead.

Aleksander Nowodzinski 8 anni fa
parent
commit
ebf5029b4b

+ 21 - 8
packages/ckeditor5-utils/src/dom/rect.js

@@ -7,8 +7,8 @@
  * @module utils/dom/rect
  */
 
-import global from './global';
 import isRange from './isrange';
+import isWindow from './iswindow';
 import isElement from '../lib/lodash/isElement';
 import getBorderWidths from './getborderwidths';
 import log from '../log';
@@ -90,8 +90,8 @@ export default class Rect {
 			} else {
 				copyRectProperties( this, source.getBoundingClientRect() );
 			}
-		} else if ( source === global.window ) {
-			const { innerWidth, innerHeight } = global.window;
+		} else if ( isWindow( source ) ) {
+			const { innerWidth, innerHeight } = source;
 
 			copyRectProperties( this, {
 				top: 0,
@@ -253,11 +253,11 @@ export default class Rect {
 		let visibleRect = this.clone();
 
 		// There's no ancestor to crop <body> with the overflow.
-		if ( source != global.document.body ) {
+		if ( !isBody( source ) ) {
 			let parent = source.parentNode || source.commonAncestorContainer;
 
 			// Check the ancestors all the way up to the <body>.
-			while ( parent && parent != global.document.body ) {
+			while ( parent && !isBody( parent ) ) {
 				const parentRect = new Rect( parent );
 				const intersectionRect = visibleRect.getIntersection( parentRect );
 
@@ -320,9 +320,9 @@ export default class Rect {
 		const source = this._source;
 		let scrollBarWidth, scrollBarHeight;
 
-		if ( source === global.window ) {
-			scrollBarWidth = global.window.innerWidth - global.document.documentElement.clientWidth;
-			scrollBarHeight = global.window.innerHeight - global.document.documentElement.clientHeight;
+		if ( isWindow( source ) ) {
+			scrollBarWidth = source.innerWidth - source.document.documentElement.clientWidth;
+			scrollBarHeight = source.innerHeight - source.document.documentElement.clientHeight;
 		} else {
 			const borderWidths = getBorderWidths( this._source );
 
@@ -385,3 +385,16 @@ function copyRectProperties( rect, source ) {
 		rect[ p ] = source[ p ];
 	}
 }
+
+// Checks if provided object is a <body> HTML element.
+//
+// @private
+// @param {HTMLElement|Range} elementOrRange
+// @returns {Boolean}
+function isBody( elementOrRange ) {
+	if ( !isElement( elementOrRange ) ) {
+		return false;
+	}
+
+	return elementOrRange === elementOrRange.ownerDocument.body;
+}

+ 16 - 23
packages/ckeditor5-utils/tests/dom/rect.js

@@ -3,9 +3,8 @@
  * For licensing, see LICENSE.md.
  */
 
-/* global document */
+/* global window, document */
 
-import global from '../../src/dom/global';
 import Rect from '../../src/dom/rect';
 import log from '../../src/log';
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
@@ -81,14 +80,12 @@ describe( 'Rect', () => {
 		} );
 
 		it( 'should accept the window (viewport)', () => {
-			testUtils.sinon.stub( global, 'window' ).value( {
-				innerWidth: 1000,
-				innerHeight: 500,
-				scrollX: 100,
-				scrollY: 200
-			} );
+			testUtils.sinon.stub( window, 'innerWidth' ).value( 1000 );
+			testUtils.sinon.stub( window, 'innerHeight' ).value( 500 );
+			testUtils.sinon.stub( window, 'scrollX' ).value( 100 );
+			testUtils.sinon.stub( window, 'scrollY' ).value( 200 );
 
-			assertRect( new Rect( global.window ), {
+			assertRect( new Rect( window ), {
 				top: 0,
 				right: 1000,
 				bottom: 500,
@@ -426,7 +423,7 @@ describe( 'Rect', () => {
 		} );
 
 		it( 'should return a new rect', () => {
-			const rect = new Rect( {} );
+			const rect = new Rect( element );
 			const visible = rect.getVisible();
 
 			expect( visible ).to.not.equal( rect );
@@ -755,7 +752,7 @@ describe( 'Rect', () => {
 			const element = document.createElement( 'div' );
 
 			testUtils.sinon.stub( element, 'getBoundingClientRect' ).returns( geometry );
-			testUtils.sinon.stub( global.window, 'getComputedStyle' ).returns( {
+			testUtils.sinon.stub( window, 'getComputedStyle' ).returns( {
 				borderTopWidth: '5px',
 				borderRightWidth: '10px',
 				borderLeftWidth: '5px',
@@ -789,21 +786,17 @@ describe( 'Rect', () => {
 		} );
 
 		it( 'should exclude scrollbars from viewport\'s rect', () => {
-			testUtils.sinon.stub( global, 'window' ).value( {
-				innerWidth: 1000,
-				innerHeight: 500,
-				scrollX: 100,
-				scrollY: 200
-			} );
+			testUtils.sinon.stub( window, 'innerWidth' ).value( 1000 );
+			testUtils.sinon.stub( window, 'innerHeight' ).value( 500 );
+			testUtils.sinon.stub( window, 'scrollX' ).value( 100 );
+			testUtils.sinon.stub( window, 'scrollY' ).value( 200 );
 
-			testUtils.sinon.stub( global, 'document' ).value( {
-				documentElement: {
-					clientWidth: 990,
-					clientHeight: 490
-				}
+			testUtils.sinon.stub( document, 'documentElement' ).value( {
+				clientWidth: 990,
+				clientHeight: 490
 			} );
 
-			assertRect( new Rect( global.window ).excludeScrollbarsAndBorders(), {
+			assertRect( new Rect( window ).excludeScrollbarsAndBorders(), {
 				top: 0,
 				right: 990,
 				bottom: 490,