Browse Source

Don't use jsdom private API features to get global properties

Before we were manually grabbing `window._core` which was a bit of a hack. This sets the correct settings to allow us to get all globals with `Object.getOwnPropertyNames(window)`.

We also need to clone the settings object before passing it to jsdom otherwise it tacks loads of stuff on to the referenced object.
pull/19/head
Luke Childs 8 years ago
parent
commit
f6b5e9ff3b
  1. 30
      src/index.js

30
src/index.js

@ -1,16 +1,19 @@
import { jsdom } from 'jsdom';
import jsdom from 'jsdom';
function getWindowPropertyKeys(window) {
return Object
.keys(window)
.concat(Object.keys(window._core))
.filter(prop => prop.substring(0, 1) !== '_');
}
const defaultJsdomConfig = {
features: {
FetchExternalResources: false,
ProcessExternalResources: false
}
};
const cloneObject = obj => JSON.parse(JSON.stringify(obj));
const protectedproperties = (() => {
const window = jsdom('<html><body></body></html>').defaultView;
const window = jsdom.jsdom('<html><body></body></html>', cloneObject(defaultJsdomConfig)).defaultView;
return getWindowPropertyKeys(window)
return Object
.getOwnPropertyNames(window)
.filter(prop => global[prop]);
})();
@ -18,11 +21,14 @@ const getType = val => Object.prototype.toString.call(val);
module.exports = (...args) => {
const properties = args.filter(arg => getType(arg) === '[object Array]')[0];
const jsdomConfig = args.filter(arg => getType(arg) === '[object Object]')[0];
const userJsdomConfig = args.filter(arg => getType(arg) === '[object Object]')[0];
const jsdomConfig = Object.assign({}, userJsdomConfig, defaultJsdomConfig);
const window = jsdom('<html><body></body></html>', jsdomConfig).defaultView;
const window = jsdom.jsdom('<html><body></body></html>', cloneObject(jsdomConfig)).defaultView;
getWindowPropertyKeys(window)
Object
.getOwnPropertyNames(window)
.filter(prop => protectedproperties.indexOf(prop) === -1)
.filter(prop => !(properties && properties.indexOf(prop) === -1))
.forEach(prop => global[prop] = window[prop]);

Loading…
Cancel
Save