-
Notifications
You must be signed in to change notification settings - Fork 886
Expand file tree
/
Copy pathcontext.js
More file actions
1072 lines (960 loc) · 32.3 KB
/
context.js
File metadata and controls
1072 lines (960 loc) · 32.3 KB
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*eslint no-new:0*/
describe('Context', () => {
const { Context } = axe._thisWillBeDeletedDoNotUse.base;
const { createNestedShadowDom } = axe.testUtils;
const fixture = document.getElementById('fixture');
function $id(id) {
return document.getElementById(id);
}
const selectors = elms =>
elms.map(elm => {
const domNode = elm?.actualNode || elm;
return domNode.id ? `#${domNode.id}` : domNode.nodeName.toLowerCase();
});
it('should not mutate exclude in input', () => {
fixture.innerHTML = '<div id="foo"></div>';
const context = { exclude: [['iframe', '#foo']] };
new Context(context);
assert.deepEqual(context, { exclude: [['iframe', '#foo']] });
});
it('should not mutate its include input', () => {
fixture.innerHTML = '<div id="foo"></div>';
const context = { include: [['#foo']] };
new Context(context);
assert.deepEqual(context, { include: [['#foo']] });
});
it('should not share memory with complex object', () => {
fixture.innerHTML = '<div id="foo"><a href="">Click me</a></div>';
const spec = {
include: [['#foo'], ['a']],
exclude: [['iframe', '#foo2']],
size: { width: 100, height: 100 }
};
const context = new Context(spec);
assert.notStrictEqual(spec.include, context.include);
spec.include.forEach((_, index) => {
assert.notStrictEqual(spec.include[index], context.include[index]);
});
assert.notStrictEqual(spec.exclude, context.exclude);
spec.exclude.forEach((_, index) => {
assert.notStrictEqual(spec.exclude[index], context.exclude[index]);
});
assert.notStrictEqual(spec.size, context.size);
});
it('should not share memory with simple array', () => {
fixture.innerHTML = '<div id="foo"></div>';
const spec = [['#foo']];
const context = new Context(spec);
assert.notStrictEqual(spec, context.include);
});
describe('include', () => {
it('accepts a single selector', () => {
fixture.innerHTML = '<div id="foo"></div>';
const result = new Context('#foo');
assert.deepEqual(selectors(result.include), ['#foo']);
assert.isEmpty(result.exclude);
});
it('accepts frame selectors', () => {
fixture.innerHTML = '<div id="foo"><div id="bar"></div></div>';
const result = new Context([['#foo'], ['#bar']]);
assert.deepEqual(selectors(result.include), ['#foo', '#bar']);
assert.isEmpty(result.exclude);
});
it('accepts an array of strings as selectors', () => {
fixture.innerHTML = '<div id="foo"><div id="bar"></div></div>';
const context = new Context(['#foo', '#bar']);
assert.deepEqual(selectors(context.include), ['#foo', '#bar']);
assert.isEmpty(context.exclude);
});
it('accepts a node reference', () => {
const div = document.createElement('div');
fixture.appendChild(div);
const result = new Context(div);
assert.deepEqual([result.include[0].actualNode], [div]);
});
it('does not match shadow DOM nodes with light DOM selection', () => {
createNestedShadowDom(
fixture,
`<p id="p1">Light DOM</p>
<article id="shadowHost">
<p id="p2">Slotted light DOM</p>
</article>`,
`<section id="shadowHost"> <slot /> </section>
<p id="p3">Shadow DOM</p>`
);
const result = new Context([[['p']]]);
assert.deepEqual(selectors(result.include), ['#p1', '#p2']);
});
it('accepts shadow DOM selectors', () => {
createNestedShadowDom(
fixture,
'<article id="shadowHost"></article>',
'<div><section id="shadowHost"></section></div>',
'<h1 id="target">Hello world</h1>'
);
const result = new Context([[['#fixture > article', 'section', 'h1']]]);
assert.equal(result.include[0].props.id, 'target');
});
it('accepts a reference to a ShadowRoot', () => {
createNestedShadowDom(
fixture,
'<article id="shadowHost"></article>',
`<h1 id="h1">Heading</h1>
<p id="p">Content</p>`
);
const shadowHost = fixture.querySelector('#shadowHost');
const shadowRoot = shadowHost.shadowRoot;
const result = new Context(shadowRoot);
assert.deepEqual(selectors(result.include), ['#h1', '#p']);
});
it('accepts a node reference consisting of nested divs', () => {
const div1 = document.createElement('div');
const div2 = document.createElement('div');
div1.appendChild(div2);
fixture.appendChild(div1);
const result = new Context(div1);
assert.deepEqual([result.include[0].actualNode], [div1]);
});
it('accepts a node reference consisting of a form with nested controls', () => {
const form = document.createElement('form');
const input = document.createElement('input');
form.appendChild(input);
fixture.appendChild(form);
const result = new Context(form);
assert.deepEqual(result.include[0].actualNode, form);
});
it('accepts an array of node references', () => {
fixture.innerHTML = '<div id="foo"><div id="bar"></div></div>';
const result = new Context([$id('foo'), $id('bar')]);
assert.deepEqual(
[result.include[0].actualNode, result.include[1].actualNode],
[$id('foo'), $id('bar')]
);
});
it('removes any non-matched reference', () => {
fixture.innerHTML = '<div id="foo"><div id="bar"></div></div>';
const result = new Context([['#foo'], ['#baz'], ['#bar']]);
assert.deepEqual(selectors(result.include), ['#foo', '#bar']);
});
it('sorts the include nodes in document order', () => {
fixture.innerHTML =
'<div id="foo"><div id="bar"></div></div><div id="baz"></div>';
const result = new Context([['#foo'], ['#baz'], ['#bar']]);
assert.deepEqual(selectors(result.include), ['#foo', '#bar', '#baz']);
});
it('removes any null reference', () => {
fixture.innerHTML = '<div id="foo"><div id="bar"></div></div>';
const result = new Context([$id('foo'), $id('bar'), null]);
assert.deepEqual(selectors(result.include), ['#foo', '#bar']);
});
it('accepts an array of mixed selectors and nodes', () => {
fixture.innerHTML = '<div id="foo"><div id="bar"></div></div>';
const div = document.createElement('div');
div.id = 'baz';
fixture.appendChild(div);
const result = new Context([['#foo'], '#bar', div]);
assert.deepEqual(selectors(result.include), ['#foo', '#bar', '#baz']);
});
it('accepts jQuery-like objects', () => {
fixture.innerHTML =
'<div id="foo"></div><div id="bar"></div><div id="baz"></div>';
const $test = {
0: $id('foo'),
1: $id('bar'),
2: $id('baz'),
length: 3
};
const result = new Context($test);
assert.deepEqual(selectors(result.include), ['#foo', '#bar', '#baz']);
});
describe('throwing errors', () => {
let isInFrame;
beforeEach(() => {
isInFrame = axe.utils.respondable.isInFrame;
});
afterEach(() => {
axe.utils.respondable.isInFrame = isInFrame;
});
it('should throw when no elements match the context', () => {
fixture.innerHTML = '<div id="foo"></div>';
assert.throws(
() => {
new Context('#notAnElement');
},
Error,
'No elements found for include in page Context'
);
});
it.skip('should throw when no elements match the context inside a frame', () => {
axe.utils.respondable.isInFrame = () => {
return true;
};
fixture.innerHTML = '<div id="foo"></div>';
assert.throws(
() => {
new Context('#notAnElement');
},
Error,
'No elements found for include in frame Context'
);
});
});
it('creates a flatTree property', () => {
const context = new Context({ include: [document] });
assert.isArray(context.flatTree);
assert.isAtLeast(context.flatTree.length, 1);
});
});
describe('object definition', () => {
it('should assign include/exclude', () => {
const context = new Context({
include: [['#fixture']],
exclude: [['#mocha']]
});
assert.isNotNull(context);
assert.hasAllKeys(context, [
'include',
'exclude',
'flatTree',
'initiator',
'page',
'frames',
'focusable',
'size'
]);
assert.isArray(context.flatTree);
assert.isAtLeast(context.flatTree.length, 1);
});
it('should disregard bad input, non-matching selectors', () => {
const flatTree = axe.utils.getFlattenedTree(document);
const context = new Context({
include: [['#fixture'], ['#monkeys']],
exclude: [['#bananas']]
});
assert.isNotNull(context);
assert.hasAllKeys(context, [
'include',
'exclude',
'flatTree',
'initiator',
'page',
'frames',
'focusable',
'size'
]);
assert.lengthOf(context.include, 1);
assert.equal(
context.include[0].actualNode.id,
axe.utils.querySelectorAll(flatTree, '#fixture')[0].actualNode.id
);
});
it('should disregard bad input (null)', () => {
const result = new Context();
assert.lengthOf(result.include, 1);
assert.equal(result.include[0].actualNode, document.documentElement);
assert.isEmpty(result.exclude);
assert.isTrue(result.initiator);
assert.isTrue(result.page);
assert.isEmpty(result.frames);
});
it('should default include to document', () => {
const result = new Context({ exclude: [['#fixture']] });
assert.lengthOf(result.include, 1);
assert.equal(result.include[0].actualNode, document.documentElement);
assert.lengthOf(result.exclude, 1);
assert.equal(result.exclude[0].actualNode, $id('fixture'));
assert.isTrue(result.initiator);
assert.isTrue(result.page);
assert.isEmpty(result.frames);
});
it('should default empty include to document', () => {
const result = new Context({ include: [], exclude: [] });
assert.lengthOf(result.include, 1);
assert.equal(result.include[0].actualNode, document.documentElement);
});
it('throws when it has a fromFrames prop', () => {
assert.throws(() => {
new Context({
include: [],
fromFrames: ['frame', '#fixture']
});
});
});
it('throws when it has a fromShadowDom prop', () => {
assert.throws(() => {
new Context({
include: [],
fromShadowDom: ['frame', '#fixture']
});
});
});
});
describe('initiator', () => {
it('should not be clobbered', () => {
const result = new Context({
initiator: false
});
assert.lengthOf(result.include, 1);
assert.equal(result.include[0].actualNode, document.documentElement);
assert.isEmpty(result.exclude);
assert.isFalse(result.initiator);
assert.isTrue(result.page);
assert.isEmpty(result.frames);
});
// document.hasOwnProperty is undefined in Firefox content scripts
it('should not throw given really weird circumstances when hasOwnProperty is deleted from a document node?', () => {
const spec = document.implementation.createHTMLDocument('ie is dumb');
spec.hasOwnProperty = undefined;
const result = new Context(spec);
assert.lengthOf(result.include, 1);
assert.equal(result.include[0].actualNode, spec.documentElement);
assert.isEmpty(result.exclude);
assert.isTrue(result.initiator);
assert.isFalse(result.page);
assert.isEmpty(result.frames);
});
});
describe('page', () => {
it('takes the page argument as default', () => {
assert.isTrue(new Context({ page: true }).page);
assert.isFalse(new Context({ page: false }).page);
});
it('is true if the document element is included', () => {
assert.isTrue(new Context(document).page);
assert.isTrue(new Context(document.documentElement).page);
assert.isTrue(new Context('html').page);
assert.isTrue(new Context(':root').page);
});
it('is true, with exclude used', () => {
// What matters is that the documentElement is included
// not that parts within that are excluded
assert.isTrue(
new Context({
include: document,
exclude: [['#mocha']]
}).page
);
});
it('is false if the context does not include documentElement', () => {
assert.isFalse(new Context(fixture).page);
assert.isFalse(new Context('#fixture').page);
assert.isFalse(new Context([['#fixture']]).page);
assert.isFalse(new Context({ include: [['#fixture']] }).page);
});
});
describe('focusable', () => {
it('should default to true', () => {
const result = new Context();
assert.isTrue(result.focusable);
});
it('should use passed in value', () => {
const result = new Context({
focusable: false
});
assert.isFalse(result.focusable);
});
it('should reject bad values', () => {
const result = new Context({
focusable: 'hello'
});
assert.isTrue(result.focusable);
});
});
describe('size', () => {
it('should default to empty object', () => {
const result = new Context();
assert.deepEqual(result.size, {});
});
it('should use passed in value', () => {
const result = new Context({
size: {
width: 10,
height: 20
}
});
assert.deepEqual(result.size, {
width: 10,
height: 20
});
});
it('should reject bad values', () => {
const result = new Context({
size: 'hello'
});
assert.deepEqual(result.size, {});
});
});
describe('frames', () => {
function iframeReady(src, context, id, cb, done) {
const iframe = document.createElement('iframe');
iframe.addEventListener('load', () => {
try {
cb(iframe);
done();
} catch (e) {
done(e);
}
});
iframe.src = src;
iframe.id = id;
context.appendChild(iframe);
}
it('adds frames that are explicitly included', function (done) {
fixture.innerHTML = '<div id="outer"></div>';
iframeReady(
'../mock/frames/context.html',
$id('outer'),
'target',
() => {
const result = new Context('#target');
assert.lengthOf(result.frames, 1);
assert.deepEqual(result.frames[0].node, $id('target'));
},
done
);
});
it('adds frames that are implicitly included', function (done) {
fixture.innerHTML = '<div id="outer"></div>';
iframeReady(
'../mock/frames/context.html',
$id('outer'),
'target',
() => {
const result = new Context('#outer');
assert.lengthOf(result.frames, 1);
assert.deepEqual(result.frames[0].node, $id('target'));
},
done
);
});
it('sets include', function (done) {
fixture.innerHTML = '<div id="outer"></div>';
iframeReady(
'../mock/frames/context.html',
$id('outer'),
'target',
() => {
const result = new Context([['#target', '#foo']]);
assert.lengthOf(result.frames, 1);
assert.deepEqual(result.frames[0].node, $id('target'));
assert.deepEqual(result.frames[0].include, [['#foo']]);
assert.deepEqual(result.frames[0].exclude, []);
},
done
);
});
it('sets exclude', function (done) {
fixture.innerHTML = '<div id="outer"></div>';
iframeReady(
'../mock/frames/context.html',
$id('outer'),
'target',
() => {
const result = new Context({
exclude: [['#target', '#foo']]
});
assert.lengthOf(result.frames, 1);
assert.deepEqual(result.frames[0].node, $id('target'));
assert.deepEqual(result.frames[0].include, []);
assert.deepEqual(result.frames[0].exclude, [['#foo']]);
},
done
);
});
it('sets initiator: false', function (done) {
iframeReady(
'../mock/frames/context.html',
$id('fixture'),
'target',
() => {
const result = new Context();
assert.isTrue(result.initiator);
assert.lengthOf(result.frames, 1);
assert.isFalse(result.frames[0].initiator);
},
done
);
});
it('finds frames inside shadow DOM trees', () => {
createNestedShadowDom(
fixture,
'<article id="shadowHost"></article>',
'<iframe id="target" srcdoc="<h1>My iframe page</h1>"></iframe>'
);
const result = new Context();
assert.equal(result.frames[0].node.id, 'target');
});
it('accepts frames inside shadow DOM selectors', () => {
createNestedShadowDom(
fixture,
'<article id="shadowHost"></article>',
'<div><section id="shadowHost"></section></div>',
'<iframe id="target" srcdoc="<h1>My iframe page</h1>"></iframe>'
);
const result = new Context([
[['#fixture > article', 'section', 'iframe'], ['h1']]
]);
assert.equal(result.frames[0].node.id, 'target');
});
it('skips frames excluded with shadow DOM selectors', () => {
createNestedShadowDom(
fixture,
'<article id="shadowHost"></article>',
'<div><section id="shadowHost"></section></div>',
'<iframe id="target" srcdoc="<h1>My iframe page</h1>"></iframe>'
);
const result = new Context({
exclude: [[['#fixture > article', 'section', 'iframe']]]
});
assert.isEmpty(result.frames);
});
it('skips frames in excluded shadow trees', () => {
createNestedShadowDom(
fixture,
'<article id="shadowHost"></article>',
'<div><section id="shadowHost"></section></div>',
'<iframe id="target" srcdoc="<h1>My iframe page</h1>"></iframe>'
);
const result = new Context({
exclude: [[['#fixture > article', 'section']]]
});
assert.isEmpty(result.frames);
});
describe('.page', () => {
it('is true if context includes the document element', function (done) {
iframeReady(
'../mock/frames/context.html',
$id('fixture'),
'target',
() => {
const result = new Context({
exclude: [['#mocha']]
});
assert.lengthOf(result.frames, 1);
assert.isTrue(result.frames[0].page);
},
done
);
});
it("can be false, even if the frame's documentElement is included", function (done) {
iframeReady(
'../mock/frames/context.html',
$id('fixture'),
'target',
() => {
const result = new Context({
include: [['#fixture']]
});
assert.lengthOf(result.frames, 1);
assert.isFalse(result.frames[0].page);
},
done
);
});
});
describe('.focusable', () => {
it('is true if tabindex is 0', function (done) {
iframeReady(
'../mock/frames/context.html',
$id('fixture'),
'target',
function (iframe) {
iframe.tabIndex = '0';
const result = new Context();
assert.lengthOf(result.frames, 1);
assert.isTrue(result.frames[0].focusable);
},
done
);
});
it('is false if the context has a negative tabindex', function (done) {
iframeReady(
'../mock/frames/context.html',
$id('fixture'),
'target',
function (iframe) {
iframe.tabIndex = '-1';
const result = new Context('#fixture');
assert.lengthOf(result.frames, 1);
assert.isFalse(result.frames[0].focusable);
},
done
);
});
it('is false if the parent context is not focusable', function (done) {
iframeReady(
'../mock/frames/context.html',
$id('fixture'),
'target',
() => {
const result = new Context({
include: [['#fixture']],
focusable: false
});
assert.lengthOf(result.frames, 1);
assert.isFalse(result.frames[0].focusable);
},
done
);
});
});
describe('.size', () => {
it('sets width and height of the frame', function (done) {
iframeReady(
'../mock/frames/context.html',
$id('fixture'),
'target',
function (iframe) {
iframe.width = '100';
iframe.height = '200';
const result = new Context('#fixture');
const size = result.frames[0].size;
assert.closeTo(size.width, 105, 10);
assert.closeTo(size.height, 205, 10);
},
done
);
});
it('works with CSS width / height', function (done) {
iframeReady(
'../mock/frames/context.html',
$id('fixture'),
'target',
function (iframe) {
iframe.setAttribute('style', 'width: 100px; height: 200px');
const result = new Context('#fixture');
const size = result.frames[0].size;
assert.closeTo(size.width, 105, 10);
assert.closeTo(size.height, 205, 10);
},
done
);
});
});
it('combines includes', function (done) {
fixture.innerHTML = '<div id="outer"></div>';
iframeReady(
'../mock/frames/context.html',
$id('outer'),
'target',
() => {
const result = new Context([
['#target', '#foo'],
['#target', '#bar']
]);
assert.lengthOf(result.frames, 1);
assert.deepEqual(result.frames[0].node, $id('target'));
assert.deepEqual(result.frames[0].include, [['#foo'], ['#bar']]);
assert.deepEqual(result.frames[0].exclude, []);
},
done
);
});
it('does not include the same frame twice', function (done) {
fixture.innerHTML = '<div id="outer"></div>';
iframeReady(
'../mock/frames/context.html',
$id('outer'),
'target',
() => {
const result = new Context([$id('target'), $id('target')]);
assert.lengthOf(result.frames, 1);
assert.deepEqual(result.frames[0].node, $id('target'));
},
done
);
});
it('should filter out invisible frames', function (done) {
fixture.innerHTML = '<div id="outer"></div>';
iframeReady(
'../mock/frames/context.html',
$id('outer'),
'target',
() => {
const frame = $id('target');
frame.setAttribute('hidden', 'hidden');
const result = new Context([$id('target')]);
assert.deepEqual(result.frames, []);
},
done
);
});
it('should throw when frame could not be found', function (done) {
fixture.innerHTML = '<div id="outer"></div>';
iframeReady(
'../mock/frames/context.html',
$id('outer'),
'target',
() => {
assert.throws(function () {
new Context(['#notAFrame', '#foo']);
});
},
done
);
});
});
describe('with labelled fame selectors', () => {
it('accepts a single labelled selector', () => {
fixture.innerHTML = '<div id="foo"></div>';
const result = new Context({
fromFrames: ['#foo']
});
assert.deepEqual(selectors(result.include), ['#foo']);
assert.isEmpty(result.exclude);
});
it('accepts multiple labelled selectors', () => {
fixture.innerHTML = '<div id="foo"></div><div id="bar"></div>';
const result = new Context([
{ fromFrames: ['#foo'] },
{ fromFrames: ['#bar'] }
]);
assert.deepEqual(selectors(result.include), ['#foo', '#bar']);
assert.isEmpty(result.exclude);
});
it('accepts labelled selectors on include & exclude', () => {
fixture.innerHTML = '<div id="foo"></div><div id="bar"></div>';
const result = new Context({
include: [{ fromFrames: ['#foo'] }],
exclude: { fromFrames: ['#bar'] }
});
assert.deepEqual(selectors(result.include), ['#foo']);
assert.deepEqual(selectors(result.exclude), ['#bar']);
});
it('throws when not passed an array', () => {
assert.throws(() => {
new Context({ fromFrames: '#fixture' });
});
});
it('throws when fromShadowDom is on the same object', () => {
assert.throws(() => {
new Context({
fromFrames: ['#fixture'],
fromShadowDom: ['#fixture']
});
});
});
it('throws when labelled frames are nested', () => {
assert.throws(() => {
new Context({
fromFrames: ['#fixture', { fromFrames: ['#fixture'] }]
});
});
});
describe('when the selector has length > 1', () => {
it('sets the frame, rather than include / exclude', () => {
fixture.innerHTML = `<iframe id="foo" srcdoc="
<h1>Hello world</h1> <img>
"></iframe>`;
const result = new Context({
include: { fromFrames: ['#foo', 'h1'] },
exclude: { fromFrames: ['iframe', 'img'] }
});
assert.isEmpty(result.include);
assert.isEmpty(result.exclude);
assert.lengthOf(result.frames, 1);
});
it('creates a context for the frame', () => {
fixture.innerHTML = `<iframe id="foo" srcdoc="
<h1>Hello world</h1> <img>
"></iframe>`;
const result = new Context({
include: { fromFrames: ['#foo', 'h1'] },
exclude: { fromFrames: ['iframe', 'img'] }
});
const frameContext = result.frames[0];
assert.lengthOf(result.frames, 1);
assert.deepEqual(frameContext.include, [['h1']]);
assert.deepEqual(frameContext.exclude, [['img']]);
});
});
});
describe('with labelled shadow DOM selectors', () => {
it('accepts a single labelled selector', () => {
createNestedShadowDom(
fixture,
'<article id="shadowHost"></article>',
'<div><section id="shadowHost"></section></div>',
'<h1 id="foo">Heading</h1>'
);
const result = new Context({
fromShadowDom: ['#fixture > article', 'section', 'h1']
});
assert.deepEqual(selectors(result.include), ['#foo']);
assert.isEmpty(result.exclude);
assert.isEmpty(result.frames);
});
it('accepts multiple labelled selectors', () => {
createNestedShadowDom(
fixture,
'<article id="shadowHost"></article>',
'<div><section id="shadowHost"></section></div>',
'<div id="foo"></div><div id="bar"></div>'
);
const result = new Context([
{ fromShadowDom: ['#fixture > article', 'section', '#foo'] },
{ fromShadowDom: ['#fixture > article', 'section', '#bar'] }
]);
assert.deepEqual(selectors(result.include), ['#foo', '#bar']);
assert.isEmpty(result.exclude);
assert.isEmpty(result.frames);
});
it('accepts labelled selectors on include & exclude', () => {
createNestedShadowDom(
fixture,
'<article id="shadowHost"></article>',
'<h1 id="foo"></h1><h2 id="bar"></h2>'
);
const result = new Context({
include: [{ fromShadowDom: ['#fixture > article', 'h1'] }],
exclude: { fromShadowDom: ['#fixture > article', 'h2'] }
});
assert.deepEqual(selectors(result.include), ['#foo']);
assert.deepEqual(selectors(result.exclude), ['#bar']);
});
it('throws when fromShadowDom does not contain an array', () => {
fixture.innerHTML = '<h1>Hello World</h1>';
assert.throws(() => {
new Context({ fromShadowDom: 'h1' });
});
});
it('throws when containing a labelled frame selector', () => {
createNestedShadowDom(
fixture,
'<article id="shadowHost"></article>',
`<iframe id="foo" srcdoc="
<h1>Hello World</h1>
"></iframe>`
);
assert.throws(() => {
new Context({
fromShadowDom: [
'#fixture > article',
{ fromFrames: ['iframe', 'h1'] }
]
});
});
});
it('throws when containing another labelled shadow dom selector', () => {
createNestedShadowDom(
fixture,
'<article id="shadowHost"></article>',
'<div><section id="shadowHost"></section></div>',
'<h1 id="foo">Heading</h1>'
);
assert.throws(() => {
new Context({
fromShadowDom: [
'#fixture > article',
{ fromShadowDom: ['section', 'h1'] }
]
});
});
});
describe('nested in a frame selector', () => {
it('works for unlabelled frame selectors', () => {
createNestedShadowDom(
fixture,
'<article id="shadowHost"></article>',
`<iframe id="foo" srcdoc="
<h1>Hello World</h1>
"></iframe>`
);
const result = new Context([
[
{
fromShadowDom: ['#fixture > article', 'iframe']
},
['h1']
]
]);
const frameNodes = result.frames.map(({ node }) => node);
assert.deepEqual(selectors(frameNodes), ['#foo']);
});
it('works for labelled frame selectors', () => {
createNestedShadowDom(
fixture,
'<article id="shadowHost"></article>',
`<iframe id="foo" srcdoc="
<h1>Hello World</h1>
"></iframe>`
);
const result = new Context({
fromFrames: [
{
fromShadowDom: ['#fixture > article', 'iframe']
},
['h1']
]
});
const frameNodes = result.frames.map(({ node }) => node);
assert.deepEqual(selectors(frameNodes), ['#foo']);
});
});