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 #247 from ckeditor/t/ckeditor5/879
Browse files Browse the repository at this point in the history
Fix: The `isWindow` helper should work in the Electron environment. Closes ckeditor/ckeditor5#879.
  • Loading branch information
oleq authored Jul 5, 2018
2 parents 7467c4e + 4ced195 commit d561151
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/dom/iswindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,17 @@
* @returns {Boolean}
*/
export default function isWindow( obj ) {
return Object.prototype.toString.apply( obj ) == '[object Window]';
const stringifiedObject = Object.prototype.toString.apply( obj );

// Returns `true` for the `window` object in browser environments.
if ( stringifiedObject == '[object Window]' ) {
return true;
}

// Returns `true` for the `window` object in the Electron environment.
if ( stringifiedObject == '[object global]' ) {
return true;
}

return false;
}
12 changes: 11 additions & 1 deletion tests/dom/iswindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,22 @@
import isWindow from '../../src/dom/iswindow';

describe( 'isWindow()', () => {
it( 'detects DOM Window', () => {
it( 'detects DOM Window in browsers', () => {
expect( isWindow( window ) ).to.be.true;
expect( isWindow( {} ) ).to.be.false;
expect( isWindow( null ) ).to.be.false;
expect( isWindow( undefined ) ).to.be.false;
expect( isWindow( new Date() ) ).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;
} );
} );

0 comments on commit d561151

Please sign in to comment.