forked from Khan/perseus
-
Notifications
You must be signed in to change notification settings - Fork 34
/
common.js
65 lines (57 loc) · 2.04 KB
/
common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// This is a file of things that both the perseus environment and node-perseus
// need
// Helper function for loading react; see comment when we load react
var withNavigator = function(lambda) {
var oldNavigator = global.navigator;
global.navigator = { userAgent: "Node" };
var result = lambda();
global.navigator = oldNavigator;
return result;
};
// Fake being a web browser
global.document = global.jsdom.jsdom();
var window = global.window = document.defaultView; // @Nolint
// Mock out window.getSelection for react
// TODO(jack): Remove this once
// https://github.com/facebook/react/commit/2347abf75c2acb40b4b6ba10750f0461a5b837ad
// makes it into our version of react
if (!window.getSelection) {
window.getSelection = function() {
return {
rangeCount: 0,
};
};
}
var _ = require("../lib/underscore.js");
global._ = window._ = _;
// Create a function to copy globals from `window` to `global`
var jsdomWindowProps = _.clone(global.window);
var updateGlobals = function() {
// Update only things we add to window, but not properties created by
// jsdom, since making those globals can confuse jsdom.
// We can't use a plain _.each(global.window) here because window has
// a .length property (set to 0), which makes _ think it's array-like
_.each(_.keys(global.window), function(key) {
if (!_.has(jsdomWindowProps, key)) {
global[key] = global.window[key];
}
});
};
// Third-party global dependencies
global.React = withNavigator(function() {
// react-with-addons requires global.navigator to be defined,
// but some other deps (including requirejs) require it to be
// undefined to detect that we are running in Node.
// We therefore define it only while loading React.
require("../lib/react-with-addons.js");
return window.React;
});
require("../lib/jquery.js");
updateGlobals();
// First-party global dependencies
require("../ke/local-only/i18n.js");
updateGlobals();
module.exports = {
updateGlobals: updateGlobals,
window: window,
};