Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix getActiveElement so that it does not throw error when document
…does not exist

While using great library `react-unit` to test the React components I discovered that this function is the only part that needs `document` global otherwise it crashes (variable not defined). This is an attempt to make it work even without document.
  • Loading branch information
Kamil Jopek committed Sep 25, 2015
commit 4fc5b4951501b4a5ca03578cb0689962f14a81e7
22 changes: 22 additions & 0 deletions src/core/__tests__/dom/getActiveElement-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright 2013-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

'use strict';

jest.dontMock('getActiveElement');

var getActiveElement = require('getActiveElement');

describe('getActiveElement', () => {
it('returns body when there is no activeElement', () => {
var element = getActiveElement();
expect(element.tagName).toEqual('BODY');
});
});
6 changes: 5 additions & 1 deletion src/core/dom/getActiveElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
* Same as document.activeElement but wraps in a try-catch block. In IE it is
* not safe to call document.activeElement if there is nothing focused.
*
* The activeElement will be null only if the document body is not yet defined.
* The activeElement will be null only if the document or document body is not yet defined.
*/
function getActiveElement() /*?DOMElement*/ {
if (typeof document === 'undefined') {
return null;
}

try {
return document.activeElement || document.body;
} catch (e) {
Expand Down