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
|
<!DOCTYPE html>
<title>HTMLselectElement Test: value and selectedOption</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<select id="select0"></select>
<select id="select1">
<option>one</option>
<option>two</option>
<div>I'm a div with no part attr</div>
<option id="select1-option3">three</option>
<option>four</option>
</select>
<select id="select2">
<div behavior="option">one</div>
<div behavior="option">two</div>
<div>I'm a div with no part attr</div>
<div behavior="option">three</div>
<div behavior="option">four</div>
</select>
<select id="select3">
<div>I'm a div with no part attr</div>
<option id="select3-child1">one</option>
<option id="select3-child2">two</option>
<option id="select3-child3">three</option>
</select>
<select id="select4">
<button>
<selectedcontent id="select4-custom-selected-value">Default custom selected-value text</selectedcontent>
</button>
<option id="select4-option1">one</option>
<option id="select4-option2">two</option>
</select>
<select id="select5">
<button>
<selectedcontent id="select5-custom-selected-value">Default custom selected-value text</selectedcontent>
</button>
<div>
<option id="select5-option1">one</option>
<option id="select5-option2">two</option>
</div>
</select>
<select id="select6">
<option id="select6-option1">one</option>
<option id="select6-option2" selected>two</option>
<option id="select6-option3">three</option>
</select>
<select id="select7">
<option id="select7-option1">one</option>
<option id="select7-option2" selected value="test">two</option>
<option>three</option>
</select>
<style>
select, ::picker(select) {
appearance: base-select;
}
</style>
<script>
test(() => {
const select0 = document.getElementById("select0");
assert_equals(select0.value, "");
assert_equals(select0.selectedOptions.length, 0);
select0.value = "something";
assert_equals(select0.value, "", "If there is no matching option, select should be cleared");
assert_equals(select0.selectedOptions.length, 0);
}, "Test that HTMLselect with no options has empty string for value and null for selectedOption");
test(() => {
const select1 = document.getElementById("select1");
assert_equals(select1.value, "one", "value should start with the text of the first option part");
select1.value = "three";
assert_equals(select1.value, "three", "value can be set to the text of an option part");
assert_equals(select1.selectedOptions[0], document.getElementById("select1-option3"));
select1.value = "I'm a div with no part attr";
assert_equals(select1.value, "", "If there is no matching option select should be cleared");
assert_equals(select1.selectedOptions.length, 0);
}, "Test value and selectedOption with HTMLOptionElement element option parts");
test(() => {
const select1 = document.getElementById("select1");
select1.value = "one";
assert_equals(select1.value, "one");
select1.value = null;
assert_equals(select1.value, "");
assert_equals(select1.selectedOptions.length, 0);
}, "Test value and selectedOption when value is null");
test(() => {
const select1 = document.getElementById("select1");
select1.value = "one";
assert_equals(select1.value, "one");
select1.value = undefined;
assert_equals(select1.value, "");
assert_equals(select1.selectedOptions.length, 0);
}, "Test value and selectedOption when value is undefined");
test(() => {
const select2 = document.getElementById("select2");
assert_equals(select2.value, "", "Non-HTMLOptionElements shouldn't be treated as option parts");
assert_equals(select2.selectedOptions.length, 0);
select2.value = "three";
assert_equals(select2.value, "", "value can't be set when there are no option parts'");
assert_equals(select2.selectedOptions.length, 0);
}, "Test value with non-HTMLOptionElement elements labeled as parts");
test(() => {
const select3 = document.getElementById("select3");
assert_equals(select3.value, "one", "value should start with the text of the first option part");
assert_equals(select3.selectedOptions[0], document.getElementById("select3-child1"));
document.getElementById("select3-child3").remove();
assert_equals(select3.value, "one", "Removing a non-selected option should not change the value");
assert_equals(select3.selectedOptions[0], document.getElementById("select3-child1"));
document.getElementById("select3-child1").remove();
assert_equals(select3.value, "two", "When the selected option is removed, the new first option should become selected");
assert_equals(select3.selectedOptions[0], document.getElementById("select3-child2"));
document.getElementById("select3-child2").remove();
assert_equals(select3.value, "", "When all options are removed, value should be the empty string");
assert_equals(select3.selectedOptions.length, 0);
}, "Test that value and selectedOption are updated when options are removed");
test(() => {
const select4 = document.getElementById("select4");
let customSelectedValuePart = document.getElementById("select4-custom-selected-value");
assert_equals(select4.value, "one", "value should start with the text of the first option part");
assert_equals(select4.selectedOptions[0], document.getElementById("select4-option1"));
assert_equals(customSelectedValuePart.innerText, "one", "Custom selected value part should be set to initial value of select");
select4.value = "two";
assert_equals(customSelectedValuePart.innerText, "two", "Custom selected value part should be updated when value of select changes");
assert_equals(select4.selectedOptions[0], document.getElementById("select4-option2"));
}, "Test that slotted-in selected-value part is updated to value of select");
test(() => {
const select5 = document.getElementById("select5");
let customSelectedValuePart = document.getElementById("select5-custom-selected-value");
assert_equals(select5.value, "one", "value should start with the text of the first option part");
assert_equals(select5.selectedOptions[0], document.getElementById("select5-option1"));
assert_equals(customSelectedValuePart.innerText, "one", "Custom selected value part should be set to initial value of select");
select5.value = "two";
assert_equals(customSelectedValuePart.innerText, "two", "Custom selected value part should be updated when value of select changes");
assert_equals(select5.selectedOptions[0], document.getElementById("select5-option2"));
}, "Test that option parts in a slotted-in listbox are reflected in the value property");
test(() => {
let select = document.createElement('select');
assert_equals(select.value, "");
let option = document.createElement('option');
option.innerText = "one";
select.appendChild(option);
assert_equals(select.value, "one");
assert_equals(select.selectedOptions[0], option);
let newOption = document.createElement('option');
newOption.innerText = 'two';
select.appendChild(newOption);
select.value = "two";
assert_equals(select.value, "two");
assert_equals(select.selectedOptions[0], newOption);
select.value = "one";
assert_equals(select.value, "one");
assert_equals(select.selectedOptions[0], option);
}, "Test that value and selectedOption are correctly updated");
test(() => {
const select = document.getElementById("select6");
let selectOption1 = document.getElementById("select6-option1");
assert_equals(select.value, "two");
assert_equals(select.selectedOptions[0], document.getElementById("select6-option2"));
assert_false(selectOption1.selected);
selectOption1.selected = true;
assert_equals(select.value, "one");
assert_equals(select.selectedOptions[0], selectOption1);
let newOption = document.createElement("option");
newOption.innerText = "four";
newOption.selected = true;
select.appendChild(newOption);
assert_equals(select.value, "four");
assert_equals(select.selectedOptions[0], newOption);
assert_false(selectOption1.selected);
select.value = "three";
assert_equals(select.selectedOptions[0], document.getElementById("select6-option3"));
assert_false(newOption.selected);
}, "Test that HTMLOption.selected updates select.value and select.selectedOptions");
test(() => {
const select = document.getElementById("select7");
let selectOption1 = document.getElementById("select7-option1");
assert_equals(select.value, "test");
assert_equals(select.selectedOptions[0], document.getElementById("select7-option2"));
assert_false(selectOption1.selected);
selectOption1.selected = true;
assert_equals(select.value, "one");
assert_equals(select.selectedOptions[0], selectOption1);
selectOption1.value = "new test";
assert_equals(select.value, "new test");
assert_equals(select.selectedOptions[0], selectOption1);
selectOption1.removeAttribute("value");
assert_equals(select.value, "one");
assert_equals(select.selectedOptions[0], selectOption1);
selectOption1.value = "";
assert_equals(select.value, "");
assert_equals(select.selectedOptions[0], selectOption1);
}, "Test that HTMLOption.value updates select.value");
</script>
|