Ver código fonte

Fixed `isWindow` helper for the Electron environment.

Maciej Bukowski 7 anos atrás
pai
commit
06659db796

+ 9 - 1
packages/ckeditor5-utils/src/dom/iswindow.js

@@ -14,5 +14,13 @@
  * @returns {Boolean}
  * @returns {Boolean}
  */
  */
 export default function isWindow( obj ) {
 export default function isWindow( obj ) {
-	return Object.prototype.toString.apply( obj ) == '[object Window]';
+	const stringifiedObject = Object.prototype.toString.apply( obj );
+
+	return (
+		// Returns `true` for the `window` object in browser environments.
+		stringifiedObject == '[object Window]' ||
+
+		// Returns `true` for the `window` object in the Electron environment.
+		stringifiedObject == '[object global]'
+	);
 }
 }

+ 11 - 1
packages/ckeditor5-utils/tests/dom/iswindow.js

@@ -8,7 +8,7 @@
 import isWindow from '../../src/dom/iswindow';
 import isWindow from '../../src/dom/iswindow';
 
 
 describe( 'isWindow()', () => {
 describe( 'isWindow()', () => {
-	it( 'detects DOM Window', () => {
+	it( 'detects DOM Window in browsers', () => {
 		expect( isWindow( window ) ).to.be.true;
 		expect( isWindow( window ) ).to.be.true;
 		expect( isWindow( {} ) ).to.be.false;
 		expect( isWindow( {} ) ).to.be.false;
 		expect( isWindow( null ) ).to.be.false;
 		expect( isWindow( null ) ).to.be.false;
@@ -16,4 +16,14 @@ describe( 'isWindow()', () => {
 		expect( isWindow( new Date() ) ).to.be.false;
 		expect( isWindow( new Date() ) ).to.be.false;
 		expect( isWindow( 42 ) ).to.be.false;
 		expect( isWindow( 42 ) ).to.be.false;
 	} );
 	} );
+
+	it( 'detects DOM Window in the Electron environment', () => {
+		const global = {
+			get [ Symbol.toStringTag ]() {
+				return 'global';
+			}
+		};
+
+		expect( isWindow( global ) ).to.be.true;
+	} );
 } );
 } );