-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
script.js
105 lines (96 loc) · 3.91 KB
/
script.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"use strict";
const assert = require("node:assert/strict");
const { describe, specify } = require("mocha-sugar-free");
const { JSDOM } = require('../..');
const toFileUrl = require("../util.js").toFileUrl(__dirname);
describe("script", () => {
specify('scripts_share_a_global_context', () => {
var { window } = new JSDOM('\
<html><head>\
<script type="text/javascript">\
Object.prototype.a = 1;\
hello = "hello";\
window.bye = "good";\
var abc = 123;\
var localOnWindow = "look at me, im on a window";\
</script>\
\
<script type="text/javascript">\
window.object = new Object();\
hello += " world";\
bye = bye + "bye";\
window.confirmTheLocalIsOnTheWindow = localOnWindow;\
(function() {\
var hidden = "hidden";\
window.exposed = hidden;\
this.imOnAWindow = true;\
})();\
</script>\
</head><body></body></html>',
{ runScripts: "dangerously" }
);
assert.equal(window.confirmTheLocalIsOnTheWindow, window.localOnWindow, 'local variables should be attached to the window');
assert.equal(window.hello, "hello world", 'window should be the global context');
assert.equal(window.bye, "goodbye", 'window should be the global context');
assert.equal(window.abc, 123, 'local vars should not leak out to the window');
assert.equal(window.hidden, undefined, 'vars in a closure are safe');
assert.equal(window.exposed, 'hidden', 'vars exposed to the window are global');
assert.equal(window.imOnAWindow, true, 'setting this in the outer context should apply to the window');
assert.equal(window.object.a, 1, 'prototypes should be maintained across contexts');
});
specify('global_is_window_in_scripts', () => {
var { window } = new JSDOM('<html><head>\
<script type="text/javascript">\
var results=[window===this,\
window===this.window,\
window.window===this,\
document.defaultView===this];\
</script>\
</head><body></body></html>',
{ runScripts: "dangerously" });
assert.equal(window.results[0], true, "window should equal global this");
assert.equal(window.results[1], true, "window should equal this.window");
assert.equal(window.results[2], true, "this should equal window.window");
assert.equal(window.results[3], true, "this should equal document.defaultView");
assert.equal(window.document.defaultView, window, "outside window context, document.defaultView should be window as well");
});
specify('global_in_object_should_be_valid_in_other_scripts', () => {
var { window } = new JSDOM('<html><head>\
<script>\
aGlobal={win:this};\
</script>\
<script>\
appVersion = aGlobal.win.navigator.product\
</script>\
</head><body></body></html>',
{ runScripts: "dangerously"});
assert.equal(window.appVersion, "Gecko");
});
specify('window_functions', () => {
var { window } = new JSDOM('<html><head>\
<script>\
function handle(){};\
window.addEventListener("load", handle, false);\
window.removeEventListener("load", handle, false);\
var ev = document.createEvent("MouseEvents");\
ev.initEvent("click", true, true);\
window.dispatchEvent(ev);\
window.DONE=1;\
</script>\
</head><body></body></html>',
{ runScripts: "dangerously" });
assert.equal(window.DONE, 1);
});
specify('timer_executes_in_context', { async: true }, t => {
const { window } = new JSDOM(``, { runScripts: "dangerously", resources: "usable" });
const script = window.document.createElement("script");
script.src = toFileUrl('./files/timer_in_context.js');
script.onload = () => {
setTimeout(() => {
assert.equal(window.x, 1);
t.done();
}, 1);
};
window.document.body.appendChild(script);
});
});