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>HTMLSelectMenuElement 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>
<selectmenu id="selectmenu1" required>
<option>one</option>
<option>two</option>
<option>three</option>
<option>four</option>
</selectmenu>
<form>
<selectmenu id="selectmenu2" required>
</selectmenu>
</form>
<script>
test(() => {
let selectMenu = document.createElement('selectmenu');
assert_true(selectMenu.willValidate, "A selectmenu element is a submittable element that is a candidate for constraint validation.");
let option = document.createElement('option');
selectMenu.appendChild(option);
assert_true(selectMenu.checkValidity(), "Always valid when the selectmenu isn't a required value.");
selectMenu.required = true;
assert_equals(selectMenu.value, "");
assert_false(selectMenu.checkValidity(), "A selected placeholder option should invalidate the selectmenu.");
let emptyOption = document.createElement('option');
selectMenu.appendChild(emptyOption);
assert_false(selectMenu.checkValidity(), "A selected placeholder option should invalidate the selectmenu even if there are multiple options.");
emptyOption.selected = true;
assert_true(selectMenu.checkValidity(), "An empty non-placeholder option should be a valid choice.");
let filledOption = document.createElement('option');
filledOption.value = "test";
selectMenu.appendChild(filledOption);
filledOption.selected = true;
assert_equals(selectMenu.value, "test", "The non-empty value should be set.");
assert_true(selectMenu.checkValidity(), "A non-empty non-placeholder option should be a valid choice.");
selectMenu.removeChild(option);
selectMenu.appendChild(emptyOption);
emptyOption.selected = true;
assert_equals(selectMenu.value, "", "The empty value should be set.");
assert_true(selectMenu.checkValidity(), "Only the first option can be seen as a placeholder.");
selectMenu.removeChild(filledOption);
assert_false(selectMenu.checkValidity(), "A selected placeholder option should invalidate the selectmenu.");
emptyOption.value = "test2";
assert_equals(selectMenu.value, "test2");
assert_true(selectMenu.checkValidity(), "A non-empty option value should be a valid choice.");
emptyOption.removeAttribute("value");
assert_equals(selectMenu.value, "");
assert_false(selectMenu.checkValidity());
emptyOption.innerText = "test";
assert_equals(selectMenu.value, "test");
assert_true(selectMenu.checkValidity(), "A non-empty option should be a valid choice.");
const selectMenu1 = document.getElementById('selectmenu1');
assert_equals(selectMenu1.value, "one");
assert_true(selectMenu1.checkValidity(), "A selectmenu with non-empty placeholder option should be valid.");
}, "Validation for placeholder option");
test(() => {
const selectMenu2 = document.getElementById('selectmenu2');
assert_equals(selectMenu2.value, "");
assert_false(selectMenu2.checkValidity());
let form = document.querySelector('form');
let invalidControl = form.querySelector('selectmenu:invalid');
assert_equals(selectMenu2, 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 selectmenu");
</script>
|