-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathButton.test.js
1215 lines (1066 loc) · 47.2 KB
/
Button.test.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
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
import { assert } from '@esm-bundle/chai';
import '../../loaders/theme.js';
import Button from '../../components/Button.js';
import { axTree, iterateMeaningfulAXNodes } from '../plugins/axTree.js';
import { html, makeFromConstructor, makeFromString, makeFromTagName, sendKeydown, sendKeypress, sendKeyup } from '../utils.js';
beforeEach(() => document.body.replaceChildren());
describe('mdw-button', () => {
it('can be created with document.createElement', () => {
const element = makeFromTagName('mdw-button');
assert.equal(element.tagName.toLowerCase(), 'mdw-button');
});
it('can be created with new ()', () => {
const element = makeFromConstructor(Button);
assert.equal(element.tagName.toLowerCase(), 'mdw-button');
});
it('can be created with fragment', () => {
const element = makeFromString('<mdw-button></mdw-button>');
assert.equal(element.tagName.toLowerCase(), 'mdw-button');
});
describe('layout', () => {
it('should have inline-* display', () => {
const element = html`<mdw-button></mdw-button>`;
assert.isTrue(window.getComputedStyle(element).display.startsWith('inline'));
});
});
describe('[value]', () => {
it('should have blank defaultValue', () => {
/** @type {InstanceType<Button>} */
const element = html`<mdw-button></mdw-button>`;
assert.isFalse(element.hasAttribute('value'));
assert.equal(element.defaultValue, '');
});
it('should be configurable on creation', () => {
/** @type {InstanceType<Button>} */
const element = html`<mdw-button value=80></mdw-button>`;
assert.equal(element.getAttribute('value'), '80');
assert.equal(element.defaultValue, '80');
assert.equal(element.value, '80');
});
it('should be configurable over property', () => {
/** @type {InstanceType<Button>} */
const element = html`<mdw-button></mdw-button>`;
element.defaultValue = '80';
assert.equal(element.defaultValue, '80');
assert.equal(element.value, '80');
});
it('should be configurable over attribute', () => {
/** @type {InstanceType<Button>} */
const element = html`<mdw-button></mdw-button>`;
element.setAttribute('value', '80');
assert.equal(element.defaultValue, '80');
assert.equal(element.value, '80');
});
});
describe('aria', () => {
it('returns button role', async () => {
const element = html`<mdw-button></mdw-button>`;
const results = await axTree({ selector: element.tagName });
const [{ role }] = iterateMeaningfulAXNodes(results);
assert.equal(role, 'button');
});
it('supports aria-label', async () => {
const element = html`<mdw-button aria-label=foo></mdw-button>`;
const results = await axTree({ selector: element.tagName });
const [{ name }] = iterateMeaningfulAXNodes(results);
assert.equal(name, 'foo');
});
it('labels based on slot', async () => {
const element = html`<mdw-button>foo</mdw-button>`;
const results = await axTree({ selector: element.tagName });
const [{ name }] = iterateMeaningfulAXNodes(results);
assert.equal(name, 'foo');
});
it('labels based on defaultValue when empty', async () => {
const element = html`<mdw-button value=foo></mdw-icon-button>`;
const results = await axTree({ selector: element.tagName });
const [{ role, name }] = iterateMeaningfulAXNodes(results);
assert.equal(role, 'button');
assert.equal(name, 'foo');
});
it('labels based on slot despite defaultValue', async () => {
const element = html`<mdw-button value=foo>bar</mdw-icon-button>`;
const results = await axTree({ selector: element.tagName });
const [{ role, name }] = iterateMeaningfulAXNodes(results);
assert.equal(role, 'button');
assert.equal(name, 'bar');
});
it('returns link role with [href]', async () => {
const element = html`<mdw-button href=#></mdw-button>`;
const results = await axTree({ selector: element.tagName });
const [{ role }] = iterateMeaningfulAXNodes(results);
assert.equal(role, 'link');
});
it('supports aria-label with [href]', async () => {
const element = html`<mdw-button href=# aria-label=foo></mdw-button>`;
const results = await axTree({ selector: element.tagName });
const [{ role, name }] = iterateMeaningfulAXNodes(results);
assert.equal(role, 'link');
assert.equal(name, 'foo');
});
it('labels based on slot with [href]', async () => {
const element = html`<mdw-button href=#>foo</mdw-button>`;
const results = await axTree({ selector: element.tagName });
const [{ role, name }] = iterateMeaningfulAXNodes(results);
assert.equal(role, 'link');
assert.equal(name, 'foo');
});
});
/* eslint-disable camelcase */
/** @see https://wpt.live/html/semantics/forms/the-button-element/ */
describe('wpt - <button>', () => {
/** @see https://wpt.live/html/semantics/forms/the-button-element/active-onblur.html */
describe('active-onblur', () => {
it('Buttons should clear :active when the user tabs away from them while holding spacebar.', async () => {
/** @type {InstanceType<Button>} */
const b1 = html`<mdw-button>button one</mdw-button>`;
/** @type {InstanceType<Button>} */
const b2 = html`<mdw-button>button two</mdw-button>`;
b1.focus();
// Hold spacebar down
await sendKeydown(' ');
assert.equal(b1.pressedState, true, 'Buttons should be :active while the spacebar is pressed down.');
// Press tab
await sendKeypress('Tab');
assert.equal(b1.pressedState, false, 'Buttons should not be :active after tab is used to change focus.');
assert.equal(b2.focusedState, true);
// Release spacebar
await sendKeyup(' ');
});
});
/** @see https://wpt.live/html/semantics/forms/the-button-element/button-activate-keyup-prevented.html */
describe('button-activate-keyup-prevented', () => {
it('Button activation submits on keyup, but not if keydown is defaultPrevented', async () => {
/** @type {InstanceType<Button>} */
const button = html`<mdw-button type=submit>The button</mdw-button>`;
button.focus();
assert.equal(document.activeElement, button, 'Button should be focused');
// assert.isTrue(button.focusedState, 'Button show focus state');
const clickPromise = new Promise((resolve) => {
button.addEventListener('click', resolve, { once: true });
});
await sendKeypress(' ');
await clickPromise;
assert.isTrue(true, 'Button should have activated');
document.addEventListener('keydown', (e) => {
e.preventDefault();
});
button.addEventListener('click', () => assert.fail('button got incorrectly activated'));
await sendKeypress(' ');
await new Promise((resolve) => setTimeout(resolve, 0));
assert.isTrue(true, 'Button should not have activated');
});
});
/** @see https://wpt.live/html/semantics/forms/the-button-element/button-activate.html */
describe('button-activate', () => {
it('button activation behaviour submits form', (done) => {
const form = html`
<form>
<mdw-button type=submit>Submit</mdw-button>
</form>
`;
const button = /** @type {InstanceType<Button>} */ (form.firstElementChild);
form.addEventListener('submit', (e) => {
e.preventDefault();
done();
});
button.click();
});
});
/** @see https://wpt.live/html/semantics/forms/the-button-element/button-checkvalidity.html */
it('checkvalidity', () => {
const form = html`
<form method="post"
enctype="application/x-www-form-urlencoded"
action="">
<p><mdw-button type=submit>button</mdw-button></p>
</form>
`;
const p = (form.firstElementChild);
const button = /** @type {InstanceType<Button>} */ (p.firstElementChild);
try {
assert.equal(button.checkValidity(), true, 'calling of checkValidity method is failed.');
} catch {
assert.fail('autofocus attribute is not exist.'); // [sic]
}
});
/** @see https://wpt.live/html/semantics/forms/the-button-element/button-click-submits.html */
describe('button-click-submits', () => {
it('clicking a button with .click() should trigger a submit (form connected)', (done) => {
const form = html`<form><mdw-button type=submit>Submit</mdw-button></form>`;
const button = /** @type {InstanceType<Button>} */ (form.firstElementChild);
form.addEventListener('submit', (ev) => {
ev.preventDefault();
assert.equal(ev.target, form);
done();
});
button.click();
});
it('clicking a button with .click() should not trigger a submit (form disconnected)', (done) => {
const form = makeFromString('<form><mdw-button type=submit>Submit</mdw-button></form>', false);
const button = /** @type {InstanceType<Button>} */ (form.firstElementChild);
form.addEventListener('submit', (ev) => {
ev.preventDefault();
assert.fail('Form should not be submitted');
});
button.click();
setTimeout(done, 500);
});
it('clicking a button by dispatching an event should trigger a submit (form connected)', (done) => {
const form = html`<form><mdw-button type=submit>Submit</mdw-button></form>`;
const button = /** @type {InstanceType<Button>} */ (form.firstElementChild);
form.addEventListener('submit', (ev) => {
ev.preventDefault();
assert.equal(ev.target, form);
done();
});
const e = new MouseEvent('click');
button.dispatchEvent(e);
});
it('clicking a button by dispatching an event should not trigger a submit (form disconnected)', (done) => {
const form = makeFromString('<form><mdw-button type=submit>Submit</mdw-button></form>', false);
const button = /** @type {InstanceType<Button>} */ (form.firstElementChild);
form.addEventListener('submit', (ev) => {
ev.preventDefault();
assert.fail('Form should not be submitted');
});
const e = new MouseEvent('click');
button.dispatchEvent(e);
setTimeout(done, 500);
});
it('clicking a button that cancels the event should not trigger a submit', (done) => {
const form = makeFromString('<form><mdw-button type=submit>Submit</mdw-button></form>', false);
const button = /** @type {InstanceType<Button>} */ (form.firstElementChild);
// [sic] seems like a bad test because not part of DOM anyway
form.addEventListener('submit', (ev) => {
ev.preventDefault();
assert.fail('Form should not be submitted');
});
button.addEventListener('click', ((ev) => {
ev.preventDefault();
setTimeout(done, 500);
}));
button.click();
});
// Custom test
it('clicking a button that cancels the event should not trigger a submit (connected)', (done) => {
const form = html`<form><mdw-button type=submit>Submit</mdw-button></form>`;
const button = /** @type {InstanceType<Button>} */ (form.firstElementChild);
form.addEventListener('submit', (ev) => {
ev.preventDefault();
assert.fail('Form should not be submitted');
});
button.addEventListener('click', ((ev) => {
ev.preventDefault();
setTimeout(done, 500);
}));
button.click();
});
it('clicking a disabled button (via disabled attribute) should not trigger submit', (done) => {
const form = html`<form><mdw-button type=submit disabled>Submit</mdw-button></form>`;
const button = /** @type {InstanceType<Button>} */ (form.firstElementChild);
form.addEventListener('submit', ((ev) => {
ev.preventDefault();
assert.fail('Form should not be submitted');
}));
button.click();
setTimeout(done, 500);
});
it('clicking a disabled button (via ancestor fieldset) should not trigger submit', (done) => {
const form = html`<form><fieldset disabled><mdw-button type=submit>hello</mdw-button></fieldset></form>`;
const fieldset = (form.firstElementChild);
const button = /** @type {InstanceType<Button>} */ (fieldset.firstElementChild);
form.addEventListener('submit', (ev) => {
ev.preventDefault();
assert.fail('Form should not be submitted');
});
button.click();
setTimeout(done, 500);
});
it("clicking a button inside a disabled fieldset's legend *should* trigger submit", (done) => {
const form = html`
<form>
<fieldset disabled>
<legend>
<mdw-button type=submit>hello</mdw-button>
</legend>
</fieldset>
</form>
`;
/** @type {InstanceType<Button>} */
const button = form.querySelector('mdw-button');
form.addEventListener('submit', (ev) => {
ev.preventDefault();
assert.equal(ev.target, form);
done();
});
button.click();
});
it('clicking the child of a button with .click() should trigger a submit', (done) => {
const form = html`<form><mdw-button type=submit><span></span></mdw-button></form>`;
const span = form.querySelector('span');
form.addEventListener('submit', (ev) => {
ev.preventDefault();
assert.equal(ev.target, form);
done();
});
span.click();
});
it('clicking the child of a button by dispatching a non-bubbling event should not trigger submit', (done) => {
const form = document.createElement('form');
const button = document.createElement('mdw-button');
const span = document.createElement('span');
button.appendChild(span);
form.appendChild(button);
document.body.appendChild(form);
form.addEventListener('submit', (ev) => {
ev.preventDefault();
assert.fail('Form should not be submitted');
});
span.addEventListener('click', (ev) => {
ev.preventDefault();
setTimeout(done, 500);
});
const e = new MouseEvent('click', { bubbles: false });
span.dispatchEvent(e);
});
});
/** @see https://wpt.live/html/semantics/forms/the-button-element/button-events.html */
describe('button-events', () => {
/** @type {HTMLFormElement} */
let fm1;
/** @type {InstanceType<Button>} */
let btn;
/** @type {InstanceType<Button>} */
let menu_btn;
beforeEach(() => {
fm1 = html`
<form name="fm1" style="display:none">
<mdw-button type=submit>BUTTON</mdw-button>
<mdw-button type="menu" menu="menu">MENU BUTTON</mdw-button>
</form>
`;
[btn, menu_btn] = /** @type {HTMLCollectionOf<InstanceType<Button>>} */ (fm1.children);
});
it('The submit event must be fired when click a button in submit status', (done) => {
fm1.addEventListener('submit', (evt) => {
evt.preventDefault();
assert.isTrue(evt.isTrusted, 'The isTrusted attribute of the submit event should be true.');
assert.isTrue(evt.bubbles, 'The bubbles attribute of the submit event should be true.');
assert.isTrue(evt.cancelable, 'The cancelable attribute of the submit event should be true.');
assert.isTrue(evt instanceof Event, 'The submit event is an instance of Event interface.');
done();
});
btn.type = 'submit';
assert.equal(btn.type, 'submit', "The button type should be 'submit'.");
btn.click();
});
it('The reset event must be fired when click a button in reset status', (done) => {
fm1.addEventListener('reset', (evt) => {
assert.isTrue(evt.isTrusted, 'The isTrusted attribute of the reset event should be true.');
assert.isTrue(evt.bubbles, 'The bubbles attribute of the reset event should be true.');
assert.isTrue(evt.cancelable, 'The cancelable attribute of the reset event should be true.');
assert.isTrue(evt instanceof Event, 'The reset event is an instance of Event interface.');
done();
});
btn.type = 'reset';
assert.equal(btn.type, 'reset', "The button type should be 'reset'.");
btn.click();
});
it("type=button shouldn't trigger submit or reset events", (done) => {
btn.type = 'button';
assert.equal(btn.type, 'button', "The button type should be 'button'.");
fm1.addEventListener('submit', () => {
assert.fail("type=button shouldn't trigger submission.");
});
fm1.addEventListener('reset', () => {
assert.fail("type=button shouldn't reset the form.");
});
btn.click();
done();
});
it('Switching from type=button to type=submit should submit the form', (done) => {
btn.type = 'button';
btn.addEventListener('click', () => { btn.type = 'submit'; });
fm1.addEventListener('submit', (evt) => {
evt.preventDefault();
assert.equal(btn.type, 'submit', "The button type should be 'submit'.");
done();
});
btn.click();
});
it('Switching from type=button to type=reset should reset the form', (done) => {
btn.type = 'button';
btn.addEventListener('click', () => { btn.type = 'reset'; });
fm1.addEventListener('reset', (evt) => {
evt.preventDefault();
assert.equal(btn.type, 'reset', "The button type should be 'reset'.");
done();
});
btn.click();
});
it('Innermost button should submit its form', (done) => {
btn.type = 'submit';
btn.innerHTML = '';
const fm2 = document.createElement('form');
const btn2 = /** @type {InstanceType<Button>} */ (document.createElement('mdw-button'));
btn2.type = 'submit';
fm2.appendChild(btn2);
btn.appendChild(fm2);
assert.isTrue(fm1.contains(fm2), 'Should have nested forms');
/** @param {Event} evt */
function submitListener(evt) {
evt.preventDefault();
assert.equal(evt.target, fm2, 'Innermost form should have got the submit event');
}
window.addEventListener('submit', submitListener, true);
btn2.click();
window.removeEventListener('submit', submitListener, true);
done();
});
it('Innermost button should reset its form', (done) => {
btn.type = 'reset';
btn.innerHTML = '';
const fm2 = document.createElement('form');
const btn2 = document.createElement('button');
btn2.type = 'reset';
fm2.appendChild(btn2);
btn.appendChild(fm2);
assert.isTrue(fm1.contains(fm2), 'Should have nested forms');
/** @param {Event} evt */
function resetListener(evt) {
evt.currentTarget.removeEventListener(evt.type, resetListener, true);
evt.preventDefault();
assert.equal(evt.target, fm2, 'Innermost form should have got the reset event');
done();
}
window.addEventListener('reset', resetListener, true);
btn2.click();
});
it('Anchor inside a button should be prevent button activation', (done) => {
btn.type = 'submit';
btn.innerHTML = '';
const a = document.createElement('a');
a.href = '#';
btn.appendChild(a);
fm1.addEventListener('submit', () => {
assert.fail("type=button shouldn't trigger submission.");
});
a.click();
done();
});
it('input type=submit inside a button should be prevent button activation', (done) => {
btn.type = 'submit';
btn.innerHTML = '';
const fm2 = document.createElement('form');
const btn2 = document.createElement('input');
btn2.type = 'submit';
fm2.appendChild(btn2);
btn.appendChild(fm2);
assert.isTrue(fm1.contains(fm2), 'Should have nested forms');
/** @param {Event} evt */
function submitListener(evt) {
evt.preventDefault();
assert.equal(evt.target, fm2, 'Innermost form should have got the submit event');
}
window.addEventListener('submit', submitListener, true);
btn2.click();
window.removeEventListener('submit', submitListener, true);
done();
});
});
/** @see https://wpt.live/html/semantics/forms/the-button-element/button-labels.html */
it('button-labels', () => {
const form = html`
<form method="post"
enctype="application/x-www-form-urlencoded"
action=""
id="input_form">
<p><label>Full name:<label>(name)<mdw-button id='button_id1'>button1</mdw-button><small>Format: First Last</small></label></label></p>
<p><label>Age: <mdw-button id='button_id2'>button2</mdw-button></label></p>
</form>
`;
/** @type {NodeListOf<InstanceType<Button>>} */
const [button1, button2] = form.querySelectorAll('mdw-button');
assert.isTrue(button1.labels instanceof NodeList, 'button1.labels is NodeList');
assert.equal(button1.labels.length, 2, 'button1.labels.length');
assert.isTrue(button2.labels instanceof NodeList, 'button2.labels is NodeList');
assert.equal(button2.labels.length, 1, 'button2.labels.length');
});
/** @see https://wpt.live/html/semantics/forms/the-button-element/button-menu-historical.html */
describe('button-menu-historical', () => {
it('button.menu, the potentially-reflecting IDL attribute, does not exist', () => {
/** @type {InstanceType<Button>} */
const button = html`
<mdw-button id="b" type="menu" menu="m">button</mdw-button>
<menu id="m"></menu>
`;
assert.isFalse('menu' in button, 'The menu property must not exist on the button');
// @ts-ignore intentional
assert.equal(button.menu, undefined, 'The value of the menu property on the button must be undefined');
});
it('button.type reflects properly', () => {
/** @type {InstanceType<Button>} */
const button = html`
<mdw-button id="b" type="menu" menu="m">button</mdw-button>
<menu id="m"></menu>
`;
// <mdw-button> defaults to 'button', not 'submit'
assert.equal(button.type, 'button', 'The type property must reflect as its invalid value default of submit');
});
});
/** @see https://wpt.live/html/semantics/forms/the-button-element/button-setcustomvalidity.html */
describe('button-setcustomvalidity', () => {
it('button setCustomValidity is correct', () => {
/** @type {InstanceType<Button>} */
const elem = html`<mdw-button></mdw-button>`;
assert.isFalse(elem.validity.customError);
elem.setCustomValidity('custom error');
assert.isTrue(elem.validity.customError);
});
});
/** @see https://wpt.live/html/semantics/forms/the-button-element/button-submit-children.html */
describe('button-submit-children', () => {
it('This test will pass if a form navigation successfully occurs when'
+ ' clicking a child element of a <button type=submit> element with a'
+ ' onclick event handler which prevents the default form submission'
+ ' and manually calls form.submit() instead.', (done) => {
const frame1 = html`
<iframe name=frame1 id=frame1></iframe>
<form id=form1 target=frame1 action="about:blank">
<mdw-button id=submitbutton type=submit>
<div id=buttonchilddiv>
button child div text
</div>
</mdw-button>
</form>
`;
frame1.addEventListener('load', () => done());
const submitButton = document.getElementById('submitbutton');
submitButton.addEventListener('click', (event) => {
event.preventDefault();
const form = /** @type {HTMLFormElement} */ (document.getElementById('form1'));
form.submit();
});
const buttonChildDiv = document.getElementById('buttonchilddiv');
buttonChildDiv.click();
});
});
/** @see https://wpt.live/html/semantics/forms/the-button-element/button-submit-remove-children-jssubmit.html */
describe('button-submit-remove-children-jssubmit', () => {
it('This test will pass if a form navigation successfully occurs when'
+ ' clicking a child element of a <button type=submit> element with a'
+ " onclick event handler which removes the button's child and then"
+ 'calls form.submit().', (done) => {
const frame1 = html`
<iframe name=frame1 id=frame1></iframe>
<form id=form1 target=frame1 action="about:blank">
<mdw-button id=submitbutton type=submit>
<span id=outerchild>
<span id=innerchild>submit</span>
</span>
</mdw-button>
</form>
`;
frame1.addEventListener('load', () => done());
const submitButton = document.getElementById('submitbutton');
submitButton.addEventListener('click', (event) => {
document.getElementById('outerchild').remove();
// @ts-ignore skip cast
document.getElementById('form1').submit();
});
document.getElementById('innerchild').click();
});
});
/** @see https://wpt.live/html/semantics/forms/the-button-element/button-submit-remove-children.html */
describe('button-submit-remove-children', () => {
it('This test will pass if a form navigation successfully occurs when'
+ ' clicking a child element of a <button type=submit> element with a'
+ ' onclick event handler which removes the button\'s child.', (done) => {
const frame1 = html`
<iframe name=frame1 id=frame1></iframe>
<form id=form1 target=frame1 action="about:blank">
<mdw-button id=submitbutton type=submit>
<span id=outerchild>
<span id=innerchild>submit</span>
</span>
</mdw-button>
</form>
`;
frame1.addEventListener('load', () => done());
const submitButton = document.getElementById('submitbutton');
submitButton.addEventListener('click', (event) => {
document.getElementById('outerchild').remove();
});
document.getElementById('innerchild').click();
});
});
/** @see https://wpt.live/html/semantics/forms/the-button-element/button-submit-remove-jssubmit.html */
describe('button-submit-remove-jssubmit', () => {
it('This test will pass if a form navigation successfully occurs when'
+ ' clicking a <button type=submit> element with a onclick event'
+ ' handler which removes the button and then calls form.submit().', function (done) {
/** @type {HTMLIFrameElement} */
const frame = html`
<iframe name="frame" id="frame"></iframe>
<form id="form" target="frame" action="does_not_exist.html">
<input id="input" name="name" value="foo">
<mdw-button id="submitbutton" type="submit">Submit</mdw-button>
</form>
`;
frame.addEventListener('load', (() => {
const expected = (new URL('does_not_exist.html?name=bar', window.location.href)).href;
if (frame.contentWindow.location.href !== expected && navigator.userAgent.includes('Firefox')) {
console.log('Skipping test due to Firefox bug.');
// Firefox bug?
this.skip();
} else {
assert.equal(frame.contentWindow.location.href, expected);
}
done();
}));
const form = /** @type {HTMLFormElement} */ (document.getElementById('form'));
const input = /** @type {HTMLInputElement} */ (document.getElementById('input'));
const submitButton = document.getElementById('submitbutton');
submitButton.addEventListener('click', (event) => {
submitButton.remove();
form.submit();
input.value = 'bar';
form.submit();
input.value = 'baz';
});
submitButton.click();
});
});
/** @see https://wpt.live/html/semantics/forms/the-button-element/button-type-enumerated-ascii-case-insensitive.html */
describe('button-type-enumerated-ascii-case-insensitive', () => {
// mdw-button's default is "button" not "submit"
it('keyword reset', () => {
assert.equal(html`<mdw-button type="reset">`.type, 'reset', 'lowercase valid');
assert.equal(html`<mdw-button type="ReSeT">`.type, 'reset', 'mixed case valid');
assert.equal(html`<mdw-button type="reſet">`.type, 'button', 'non-ASCII invalid');
});
it('keyword button', () => {
assert.equal(html`<mdw-button type="button">`.type, 'button', 'lowercase valid');
// vacuous: the invalid value default is currently submit, so even if the UA
// treats this as invalid, the observable behaviour would still be correct
assert.equal(html`<mdw-button type="BuTtoN">`.type, 'button', 'mixed case valid');
// vacuous: the invalid value default is currently submit, so even if the UA
// treats this as valid, the observable behaviour would still be correct
assert.equal(html`<mdw-button type="ßutton">`.type, 'button', 'non-ASCII invalid');
});
});
/** @see https://wpt.live/html/semantics/forms/the-button-element/button-type.html */
describe('button-type', () => {
// mdw-button's default is "button" not "submit"
it("a button's type should be submit by default", () => {
const button =/** @type {InstanceType<Button>} */ (document.createElement('mdw-button'));
assert.equal(button.type, 'button');
});
it("a button's type should stay within the range of valid values", () => {
const button =/** @type {InstanceType<Button>} */ (document.createElement('mdw-button'));
for (const type of ['reset', 'button', 'submit']) {
button.type = type;
assert.equal(button.type, type);
button.type = type.toUpperCase();
assert.equal(button.type, type);
}
button.type = 'reset';
button.type = 'asdfgdsafd';
assert.equal(button.type, 'button');
button.type = 'reset';
button.type = '';
assert.equal(button.type, 'button');
});
});
/** @see https://wpt.live/html/semantics/forms/the-button-element/button-untrusted-key-event.html */
describe('button-untrusted-key-event.html', () => {
beforeEach(() => {
// eslint-disable-next-line no-unused-expressions
html`
<form id="input_form">
<mdw-button name="submitButton" type="submit">Submit</mdw-button>
<mdw-button name="resetButton" type="reset">Reset</mdw-button>
<mdw-button name="buttonButton" type="button">Button</mdw-button>
</form>
`;
const form = document.querySelector('form');
form.addEventListener('submit', (e) => {
e.preventDefault();
assert.fail('form should not be submitted');
});
for (const button of document.querySelectorAll('mdw-button')) {
button.addEventListener('click', (e) => {
assert.fail(`${button.type} button should not be clicked`);
});
}
});
// Create and append button elements
for (const button of document.querySelectorAll('button')) {
it(`Dispatching untrusted keypress events to ${button.type} button should not cause click event`, () => {
// keyCode: Enter
button.dispatchEvent(
new KeyboardEvent('keypress', {
keyCode: 13,
}),
);
// key: Enter
button.dispatchEvent(
new KeyboardEvent('keypress', {
key: 'Enter',
}),
);
// keyCode: Space
button.dispatchEvent(
new KeyboardEvent('keypress', {
keyCode: 32,
}),
);
// key: Space
button.dispatchEvent(
new KeyboardEvent('keypress', {
key: ' ',
}),
);
});
it(`Dispatching untrusted keyup/keydown events to ${button.type} button should not cause click event`, () => {
// keyCode: Enter
button.dispatchEvent(
new KeyboardEvent('keydown', {
keyCode: 13,
}),
);
button.dispatchEvent(
new KeyboardEvent('keyup', {
keyCode: 13,
}),
);
// key: Enter
button.dispatchEvent(
new KeyboardEvent('keydown', {
key: 'Enter',
}),
);
button.dispatchEvent(
new KeyboardEvent('keyup', {
key: 'Enter',
}),
);
// keyCode: Space
button.dispatchEvent(
new KeyboardEvent('keydown', {
keyCode: 32,
}),
);
button.dispatchEvent(
new KeyboardEvent('keyup', {
keyCode: 32,
}),
);
// key: Space
button.dispatchEvent(
new KeyboardEvent('keydown', {
key: ' ',
}),
);
button.dispatchEvent(
new KeyboardEvent('keyup', {
key: ' ',
}),
);
});
}
});
/** @see https://wpt.live/html/semantics/forms/the-button-element/button-validation.html */
describe('button-validation', () => {
// mdw-button's default type is "button" not "submit"
// mdw-button's default willValidate is false (button)
/**
* @param {InstanceType<Button>} element
* @param {string} expectedType
* @param {boolean} willValidate
* @param {string} desc
*/
// eslint-disable-next-line unicorn/consistent-function-scoping
function willValid(element, expectedType, willValidate, desc) {
it(desc, () => {
assert.equal(element.type, expectedType);
assert.equal(element.willValidate, willValidate);
});
}
willValid(html`<mdw-button id=btn1>button</mdw-button>`, 'button', false, 'missing type attribute');
willValid(html`<mdw-button id=btn2 type=submit>button</mdw-button>`, 'submit', true, 'submit type attribute');
willValid(html`<mdw-button id=btn3 type=reset>button</mdw-button>`, 'reset', false, 'reset type attribute');
willValid(html`<mdw-button id=btn4 type=button>button</mdw-button>`, 'button', false, 'button type attribute');
willValid(html`<mdw-button id=btn5 type=menu>button</mdw-button>`, 'button', false, 'historical menu type attribute');
willValid(html`<mdw-button id=btn6 type=foobar>button</mdw-button>`, 'button', false, 'invalid type attribute');
});
/** @see https://wpt.live/html/semantics/forms/the-button-element/button-validationmessage.html */
it('button-validationmessage', () => {
const form = html`
<form method="post"
enctype="application/x-www-form-urlencoded"
action=""
id="input_form">
<p><mdw-button id='button_id' type=submit>button</mdw-button></p>
</form>
`;
/** @type {InstanceType<Button>} */
const button = form.querySelector('mdw-button');
if (typeof (button.validationMessage) === 'string') {
assert.equal(button.validationMessage, '', 'validationMessage attribute is not correct.');
} else {
assert.fail('validationMessage attribute is not exist.');
}
});
/** @see https://wpt.live/html/semantics/forms/the-button-element/button-validity.html */
it('button-validity', () => {
const form = html`
<form method="post"
enctype="application/x-www-form-urlencoded"
action=""
id="input_form">
<p><mdw-button id='button_id' type=submit>button</mdw-button></p>
</form>
`;
/** @type {InstanceType<Button>} */
const button = form.querySelector('mdw-button');
if (typeof (button.validity) === 'object') {
assert.equal(button.validity.valueMissing, false, 'validity attribute is not correct.');
} else {
assert.fail('validity attribute is not exist.');
}
});
/** @see https://wpt.live/html/semantics/forms/the-button-element/button-willvalidate-readonly-attribute.html */
it('button-willvalidate-readonly-attribute', () => {
// mdw-button's default type is "button" not "submit"
// mdw-button's default willValidate is false (button)
/** @type {InstanceType<Button>} */
const implicitSubmitButton = html`<mdw-button id="implicitSubmitButton" readonly>1</mdw-button>`;
/** @type {InstanceType<Button>} */
const explicitSubmitButton = html`<mdw-button id="explicitSubmitButton" readonly type="submit">2</mdw-button>`;
// assert.isTrue(implicitSubmitButton.willValidate);
assert.isTrue(explicitSubmitButton.willValidate);
});
/** @see https://wpt.live/html/semantics/forms/the-button-element/button-willvalidate.html */
it('button-willvalidate', () => {
const form = html`
<form method="post"
enctype="application/x-www-form-urlencoded"
action=""
id="input_form">
<p><mdw-button id='button_id' type=submit>button</mdw-button></p>
</form>
`;
/** @type {InstanceType<Button>} */
const button = form.querySelector('mdw-button');
if (typeof (button.willValidate) === 'boolean') {
assert.equal(button.willValidate, true, 'willValidate attribute is not correct.');
} else {
assert.fail('willValidate attribute is not exist.');
}
});