-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
frame.js
506 lines (451 loc) · 19 KB
/
frame.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
var path = require('path');
var fs = require('fs');
const assert = require("node:assert/strict");
const { describe, specify } = require("mocha-sugar-free");
const { JSDOM } = require("../..");
var toFileUrl = require('../util.js').toFileUrl(__dirname);
describe("frame", () => {
specify('frame_parent', (t) => {
var { window } = new JSDOM('<html><body>\
<script>\
aGlobal=1;\
var iframe = document.createElement("iframe");\
iframe.src = "' + toFileUrl('files/iframe.html') + '";\
document.body.appendChild(iframe);\
</script>',
{ resources: "usable", runScripts: "dangerously" });
window.iframe.onload = function() {
assert.equal(window.DONE, 1);
assert.equal(window.PARENT_IS_TOP, true);
//insert a script tag to make sure the global set in the iframe is visible
//in the parent window context
var doc = window.document;
var script = doc.createElement('script');
script.textContent = 'results=[aGlobal, DONE, PARENT_IS_TOP]';
doc.body.appendChild(script);
//the script is executed asynchronously after insertion to the document,
//so setTimeout is needed
setTimeout(function(){
assert.equal(window.results.length, 3);
assert.equal(window.results[0], 1);
assert.equal(window.results[1], 1);
assert.equal(window.results[2], true);
t.done();
}, 0);
};
}, {
async: true
});
specify('frame_src_relative_to_parent_doc', (t) => {
var { window } = new JSDOM('<html><body>\
<iframe src="./files/iframe.html"></iframe>\
</body></html>',
{
url: toFileUrl("test.html"),
resources: "usable",
runScripts: "dangerously"
});
window.document.onload = function(){
assert.equal(window.LOADED_FRAME, 1);
assert.equal(window.PARENT_IS_TOP, true);
t.done();
};
}, {
async: true
});
specify('test iframe element existence', () => {
var iframeParentPath = path.resolve(__dirname, 'files', 'iframe_parent.html');
var { window } = new JSDOM(fs.readFileSync(iframeParentPath, 'utf8'), {
resources: "usable",
url : toFileUrl(__filename)
});
var elem = window.document.getElementById('simpleIFrameID');
assert.notEqual(elem, null);
assert.equal(elem.name, 'simpleIFrame');
assert.equal(elem.id, 'simpleIFrameID');
});
specify('test iframe.contentDocument access', (t) => {
var iframeParentPath = path.resolve(__dirname, 'files', 'iframe_parent.html');
var { window } = new JSDOM(fs.readFileSync(iframeParentPath, 'utf8'), {
resources: "usable",
url : toFileUrl(__filename)
});
window.document.addEventListener('load', function () {
var iframeElem = window.document.getElementById('simpleIFrameID');
assert.notEqual(iframeElem, null);
var iframeDoc = iframeElem.contentDocument;
assert.notEqual(iframeDoc, null);
assert.notEqual(iframeDoc, window.document);
var iframeDiv = iframeDoc.getElementById('iframeDiv');
assert.notEqual(iframeDiv, null);
assert.equal(iframeDiv.innerHTML, "Initial Text");
t.done();
});
}, {
async: true
});
specify('test iframe load event', (t) => {
var { window } = new JSDOM(``, {
resources: "usable",
url : toFileUrl(__filename)
});
var iFrame = window.document.createElement('iframe');
iFrame.addEventListener('load', function () {
assert.notEqual(iFrame.contentDocument, null);
t.done();
});
iFrame.src = 'files/simple_iframe.html';
// Must insert into doc to force load.
window.document.documentElement.appendChild(iFrame);
}, {
async: true
});
specify('iframe loads blank document when src unspecified', (t) => {
var doc = (new JSDOM(``, { resources: "usable" })).window.document;
var iFrame = doc.createElement('iframe');
iFrame.addEventListener('load', function () {
assert.notEqual(iFrame.contentDocument, null);
assert.equal(iFrame.contentDocument.readyState, 'complete');
t.done();
});
doc.documentElement.appendChild(iFrame);
}, {
async: true
});
specify('test iframe.contentWindow acccess', (t) => {
var htmlPath = path.resolve(__dirname, 'files', 'iframe_parent.html');
var { window } = new JSDOM(fs.readFileSync(htmlPath, 'utf8'), {
resources: "usable",
url : toFileUrl(__filename)
});
window.document.addEventListener('load', function () {
var iframeElem = window.document.getElementById('simpleIFrameID');
assert.notEqual(iframeElem, null);
var iframeDoc = iframeElem.contentDocument;
assert.notEqual(iframeDoc, null);
var iframeWindow = iframeElem.contentWindow;
assert.notEqual(iframeWindow, window.document.defaultView);
assert.equal(iframeWindow, iframeDoc.defaultView);
t.done();
});
}, {
async: true
});
specify('get iframe window via indexed frames access', (t) => {
var htmlPath = path.resolve(__dirname, 'files', 'iframe_parent.html');
var { window } = new JSDOM(fs.readFileSync(htmlPath, 'utf8'), {
resources: "usable",
url : toFileUrl(__filename)
});
window.document.addEventListener('load', function () {
var iframeWindow = window.frames[0];
assert.notEqual(iframeWindow, null);
assert.notEqual(iframeWindow, window);
assert.equal(iframeWindow.parent, window);
var iframeDoc = iframeWindow.document;
assert.notEqual(iframeWindow.document, window.document);
assert.equal(iframeWindow.document, iframeDoc);
assert.notEqual(iframeWindow.document.getElementById('iframeDiv'), null);
t.done();
});
}, {
async: true
});
specify('get iframe window via indexed frames access with setAttributeNode', (t) => {
var { window } = new JSDOM("<html><head></head><body><iframe name='simpleIFrame' id='simpleIFrameID'></iframe></body></html>", {
resources: "usable",
url : toFileUrl(__filename)
});
const doc = window.document;
var iframe = doc.getElementById('simpleIFrameID');
var attr = doc.createAttribute('src');
attr.value = 'files/simple_iframe.html';
iframe.setAttributeNode(attr);
iframe.addEventListener('load', function () {
var window = doc.defaultView;
var iframeWindow = window.frames[0];
assert.notEqual(iframeWindow, null);
assert.notEqual(iframeWindow, window);
assert.equal(iframeWindow.parent, window);
var iframeDoc = iframeWindow.document;
assert.notEqual(iframeWindow.document, window.document);
assert.notEqual(iframeWindow.document.getElementById('iframeDiv'), null);
t.done();
});
}, {
async: true
});
specify('update named frames access on name change', (t) => {
var htmlPath = path.resolve(__dirname, 'files', 'iframe_parent.html');
var doc = (new JSDOM(fs.readFileSync(htmlPath, 'utf8'), {
resources: "usable",
url : toFileUrl(__filename)
})).window.document;
doc.addEventListener('load', function () {
var window = doc.defaultView;
var iframeWindow = window.frames['simpleIFrame'];
assert.notEqual(iframeWindow, null);
assert.notEqual(iframeWindow, window);
assert.equal(iframeWindow.parent, window);
doc.getElementById('simpleIFrameID').setAttribute('name', 'otherSimpleIFrame');
assert.ok(!window.frames['simpleIFrame'], 'remove old named property');
assert.ok(window.frames['otherSimpleIFrame'], 'add new named property');
t.done();
});
}, {
async: true
});
// See: http://www.whatwg.org/specs/web-apps/current-work/#dom-frames
specify('test frames array identity', (t) => {
var htmlPath = path.resolve(__dirname, 'files', 'iframe_parent.html');
var doc = (new JSDOM(fs.readFileSync(htmlPath, 'utf8'), {
resources: "usable",
url : toFileUrl(__filename)
})).window.document;
doc.addEventListener('load', function () {
var window = doc.defaultView;
assert.equal(window.frames, window);
t.done();
});
}, {
async: true
});
specify('test nested iframes', (t) => {
var htmlPath = path.resolve(__dirname, 'files', 'iframe_parent.html');
var { window } = new JSDOM(fs.readFileSync(htmlPath, 'utf8'), {
resources: "usable",
url : toFileUrl(__filename)
});
const doc = window.document;
doc.addEventListener('load', function () {
var topIFrameElem = doc.getElementById('simpleIFrameID');
var topIFrameDoc = topIFrameElem.contentDocument;
var topIFrameWindow = topIFrameElem.contentWindow;
var bottomIFrameElem = topIFrameDoc.createElement('iframe');
bottomIFrameElem.addEventListener('load', function () {
var bottomIFrameDoc = bottomIFrameElem.contentDocument;
assert.notEqual(bottomIFrameDoc, null);
var bottomIFrameWindow = bottomIFrameDoc.defaultView;
assert.notEqual(bottomIFrameWindow, null);
// The real tests
assert.equal(bottomIFrameWindow.parent, topIFrameWindow);
assert.equal(bottomIFrameWindow.top, window);
assert.equal(topIFrameWindow.parent, window);
assert.equal(topIFrameWindow.top, window);
assert.equal(window.frames[0], topIFrameWindow);
assert.equal(topIFrameWindow.frames[0], bottomIFrameWindow);
t.done();
});
bottomIFrameElem.src = 'simple_iframe.html';
topIFrameDoc.documentElement.appendChild(bottomIFrameElem);
});
}, {
async: true
});
specify('test multiple iframes', (t) => {
var htmlPath = path.resolve(__dirname, 'files', 'multiple_iframe_parent.html');
var { window } = new JSDOM(fs.readFileSync(htmlPath, 'utf8'), {
resources: "usable",
url : toFileUrl(__filename)
});
var doc = window.document;
doc.addEventListener('load', function () {
var iframe1 = doc.getElementById('frame1ID');
var iframe2 = doc.getElementById('frame2ID');
var iframe3 = doc.getElementById('frame3ID');
assert.notEqual(window, iframe1.contentWindow);
assert.notEqual(window, iframe2.contentWindow);
assert.notEqual(window, iframe3.contentWindow);
assert.equal(iframe1.contentWindow.parent, window);
assert.equal(iframe2.contentWindow.parent, window);
assert.equal(iframe3.contentWindow.parent, window);
assert.equal(iframe1.contentWindow.top, window);
assert.equal(iframe2.contentWindow.top, window);
assert.equal(iframe3.contentWindow.top, window);
t.done();
});
}, {
async: true
});
specify('test named lookup', (t) => {
var htmlPath = path.resolve(__dirname, 'files', 'multiple_iframe_parent.html');
var { window } = new JSDOM(fs.readFileSync(htmlPath, 'utf8'), {
resources: "usable",
url : toFileUrl(__filename)
});
var doc = window.document;
doc.addEventListener('load', function () {
var iframe1 = doc.getElementById('frame1ID');
var iframe2 = doc.getElementById('frame2ID');
var iframe3 = doc.getElementById('frame3ID');
assert.equal(window['frame1'], iframe1.contentWindow);
assert.equal(window['frame2'], iframe2.contentWindow);
assert.equal(window['frame3'], iframe3.contentWindow);
t.done();
});
}, {
async: true
});
// This is based off of a test from the jQuery test suite that was failing.
specify('test iframe without src', (t) => {
var { window } = new JSDOM(``, {
resources: "usable",
url : toFileUrl(__filename),
runScripts: "dangerously"
});
var doc = window.document;
window.loaded = function () {
assert.equal(window.testVal, 3);
t.done();
};
var iframe = doc.createElement('iframe');
doc.body.appendChild(iframe);
var iframeDoc = iframe.contentDocument;
assert.notEqual(iframeDoc, null);
iframeDoc.open();
iframeDoc.write("<body onload='window.top.testVal = 3;window.parent.loaded()'>");
iframeDoc.close();
}, {
async: true
});
specify('test setting src multiple times', (t) => {
var doc = (new JSDOM(null, {
resources: "usable",
url : toFileUrl(__filename)
})).window.document;
var iframe = doc.createElement('iframe');
iframe.addEventListener('load', function () {
assert.equal(iframe.src, toFileUrl('files/simple_iframe.html'));
t.done();
});
iframe.src = 'garbage';
iframe.src = 'files/simple_iframe.html';
doc.body.appendChild(iframe);
}, {
async: true
});
specify('test framesets', (t) => {
var htmlPath = path.resolve(__dirname, 'files', 'frameset_parent.html');
var { window } = new JSDOM(fs.readFileSync(htmlPath, 'utf8'), {
resources: "usable",
url : toFileUrl(htmlPath)
});
var doc = window.document;
doc.addEventListener('load', function () {
var frame1 = doc.getElementById('frame1ID');
var frame2 = doc.getElementById('frame2ID');
assert.notEqual(frame1, null);
assert.notEqual(frame2, null);
var frame1doc = frame1.contentDocument;
var frame2doc = frame2.contentDocument;
assert.notEqual(frame1doc, null);
assert.notEqual(frame2doc, null);
assert.equal(frame1.contentWindow, frame1doc.defaultView);
assert.equal(frame2.contentWindow, frame2doc.defaultView);
assert.equal(window.frames[0], frame1.contentWindow);
assert.equal(window.frames[1], frame2.contentWindow);
assert.equal(window.frames['frame1'], frame1.contentWindow);
assert.equal(window.frames['frame2'], frame2.contentWindow);
assert.equal(window, frame1.contentWindow.parent);
assert.equal(window, frame2.contentWindow.parent);
t.done();
});
}, {
async: true
});
specify('test frame references', () => {
var { window } = new JSDOM("<!doctype html><iframe name='foo'></iframe>");
assert.equal(window.length, 1, "frame should exist (.length)");
assert.notEqual(window[0], undefined, "frame should exist (undefined check)");
assert.equal(window[0], window.foo, "index access and name access should return same reference");
});
specify('remove frame', () => {
var { window } = new JSDOM("<!doctype html><iframe id='myFrame' name='foo'></iframe>");
assert.equal(window.length, 1, "frame should exist (.length)");
assert.notEqual(window[0], undefined, "frame should exist (undefined check)");
window.document.body.removeChild(window.document.getElementById("myFrame"));
assert.equal(window.length, 0, "frame shouldn't exist (.length)");
assert.equal(window[0], undefined, "frame shouldn't exist anymore (idx accessor)");
assert.equal(window.foo, undefined, "frame shouldn't exist anymore (name accessor)");
assert.ok(!('0' in window), "'0' should not be in window anymore");
});
specify('remove middle frame', () => {
var { window } = new JSDOM("<!doctype html><iframe id='myFrame1' name='foo1'></iframe>"+
"<iframe id='myFrame2' name='foo2'></iframe><iframe id='myFrame3' name='foo3'></iframe>");
assert.equal(window.length, 3, "frames should exist (.length)");
assert.notEqual(window[0], undefined, "frame1 should exist (undefined check)");
assert.notEqual(window[1], undefined, "frame2 should exist (undefined check)");
assert.notEqual(window[2], undefined, "frame3 should exist (undefined check)");
window.document.body.removeChild(window.document.getElementById("myFrame2"));
assert.equal(window.length, 2, "frame shouldn't exist (.length)");
assert.equal(window[2], undefined, "frame shouldn't exist anymore (idx accessor)");
assert.equal(window.foo2, undefined, "frame shouldn't exist anymore (name accessor)");
assert.equal(window.foo3, window[1], "frame index accessor should be moved down");
window.document.body.removeChild(window.document.getElementById("myFrame1"));
assert.equal(window.length, 1, "frame shouldn't exist (.length)");
assert.equal(window[1], undefined, "frame shouldn't exist anymore (idx accessor)");
assert.equal(window.foo1, undefined, "frame shouldn't exist anymore (name accessor)");
assert.equal(window.foo3, window[0], "frame index accessor should be moved down");
});
specify('accessor should not exist before append', () => {
var { document } = (new JSDOM()).window;
var el = document.createElement("iframe");
el.setAttribute("name", "foo");
assert.equal(document.defaultView.length, 0, "no frames should exist yet");
assert.equal(document.defaultView[0], undefined, "indexed access should fail");
assert.equal(document.defaultView.foo, undefined, "named access should fail");
document.body.appendChild(el);
assert.equal(document.defaultView.length, 1,
"appended frame should increase window.length");
assert.notEqual(document.defaultView[0], undefined,
"indexed access should succeed");
assert.notEqual(document.defaultView.foo, undefined,
"named access should succeed");
assert.equal(document.defaultView.foo, document.defaultView[0],
"named and indexed access should return same object");
document.body.removeChild(el);
assert.equal(document.defaultView.length, 0, "no frames should exist yet");
assert.equal(document.defaultView[0], undefined, "indexed access should fail");
assert.equal(document.defaultView.foo, undefined, "named access should fail");
document.body.appendChild(el);
assert.equal(document.defaultView.length, 1,
"appended frame should increase window.length");
assert.notEqual(document.defaultView[0], undefined,
"indexed access should succeed");
assert.notEqual(document.defaultView.foo, undefined,
"named access should succeed");
assert.equal(document.defaultView.foo, document.defaultView[0],
"named and indexed access should return same object");
});
specify('move frame', () => {
var { window } = new JSDOM("<!doctype html><iframe name='foo'></iframe>");
var document = window.document;
var frame = document.querySelector("iframe");
var beforeFrame = document.createElement("iframe");
beforeFrame.setAttribute("name", "bar");
document.body.insertBefore(beforeFrame, frame);
assert.equal(window.length, 2, "should load 2 iframes");
assert.equal(window.bar, window[0], "bar should be first frame");
assert.equal(window.foo, window[1], "foo should be second frame");
});
specify('frame should not be loaded and accessor should not exist until in document, even with a parent node', (t) => {
var { window } = new JSDOM("<!doctype html>", { url: toFileUrl(__filename) });
var document = window.document;
var frame = document.createElement("iframe");
frame.onload = function () {
assert.ok(false, "onload should not be called");
};
var parentNode = document.createElement("div");
parentNode.appendChild(frame);
frame.src = "files/simple_iframe.html";
frame.setAttribute("name", "foo");
assert.equal(window.length, 0, "window length should be zero (no frames)");
assert.equal(window[0], undefined, "window should not have a 0 property");
assert.equal(window.foo, undefined, "window should not have a property for the iframe name");
setTimeout(function () {
t.done();
}, 1000);
}, {
async: true
});
});