-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathindex.js
46 lines (36 loc) · 994 Bytes
/
index.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
/*
* enables jsdom globally.
*/
var KEYS = require('./keys')
var defaultHtml = '<!doctype html><html><head><meta charset="utf-8">' +
'</head><body></body></html>'
module.exports = function globalJsdom (html, options) {
if (html === undefined) {
html = defaultHtml
}
if (options === undefined) {
options = {}
}
// Idempotency
if (global.navigator &&
global.navigator.userAgent &&
global.navigator.userAgent.indexOf('Node.js') > -1 &&
global.document &&
typeof global.document.destroy === 'function') {
return global.document.destroy
}
var jsdom = require('jsdom')
var document = new jsdom.JSDOM(html, options)
var window = document.window
KEYS.forEach(function (key) {
global[key] = window[key]
})
global.document = window.document
global.window = window
window.console = global.console
document.destroy = cleanup
function cleanup () {
KEYS.forEach(function (key) { delete global[key] })
}
return cleanup
}