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
|
<!DOCTYPE html>
<html lang="en">
<title>HTMLSelectListElement Test: validity</title>
<link rel="author" title="Ionel Popescu" href="mailto:iopopesc@microsoft.com">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<selectlist id="selectlist1" required>
<option>one</option>
<option>two</option>
<option>three</option>
<option>four</option>
</selectlist>
<form>
<selectlist id="selectlist2" required>
</selectlist>
</form>
<script>
test(() => {
let selectList = document.createElement('selectlist');
assert_true(selectList.willValidate, "A selectlist element is a submittable element that is a candidate for constraint validation.");
let option = document.createElement('option');
selectList.appendChild(option);
assert_true(selectList.checkValidity(), "Always valid when the selectlist isn't a required value.");
selectList.required = true;
assert_equals(selectList.value, "");
assert_false(selectList.checkValidity(), "A selected placeholder option should invalidate the selectlist.");
let emptyOption = document.createElement('option');
selectList.appendChild(emptyOption);
assert_false(selectList.checkValidity(), "A selected placeholder option should invalidate the selectlist even if there are multiple options.");
emptyOption.selected = true;
assert_true(selectList.checkValidity(), "An empty non-placeholder option should be a valid choice.");
let filledOption = document.createElement('option');
filledOption.value = "test";
selectList.appendChild(filledOption);
filledOption.selected = true;
assert_equals(selectList.value, "test", "The non-empty value should be set.");
assert_true(selectList.checkValidity(), "A non-empty non-placeholder option should be a valid choice.");
selectList.removeChild(option);
selectList.appendChild(emptyOption);
emptyOption.selected = true;
assert_equals(selectList.value, "", "The empty value should be set.");
assert_true(selectList.checkValidity(), "Only the first option can be seen as a placeholder.");
selectList.removeChild(filledOption);
assert_false(selectList.checkValidity(), "A selected placeholder option should invalidate the selectlist.");
emptyOption.value = "test2";
assert_equals(selectList.value, "test2");
assert_true(selectList.checkValidity(), "A non-empty option value should be a valid choice.");
emptyOption.removeAttribute("value");
assert_equals(selectList.value, "");
assert_false(selectList.checkValidity());
emptyOption.innerText = "test";
assert_equals(selectList.value, "test");
assert_true(selectList.checkValidity(), "A non-empty option should be a valid choice.");
const selectList1 = document.getElementById('selectlist1');
assert_equals(selectList1.value, "one");
assert_true(selectList1.checkValidity(), "A selectlist with non-empty placeholder option should be valid.");
}, "Validation for placeholder option");
test(() => {
const selectList2 = document.getElementById('selectlist2');
assert_equals(selectList2.value, "");
assert_false(selectList2.checkValidity());
let form = document.querySelector('form');
let invalidControl = form.querySelector('selectlist:invalid');
assert_equals(selectList2, invalidControl);
let didDispatchInvalid = false;
invalidControl.addEventListener('invalid', e => { didDispatchInvalid = true; });
let didDispatchSubmit = false;
form.addEventListener('submit', event => { event.preventDefault(); didDispatchSubmit = true; });
form.requestSubmit();
assert_true(didDispatchInvalid);
assert_false(didDispatchSubmit);
}, "Check form not submitted for invalid selectlist");
</script>
|