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
|
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title>Selector: pseudo-classes (:valid, :invalid)</title>
<link rel="author" title="Denis Ah-Kang" href="mailto:denis@w3.org" id=link1>
<link rel=help href="https://html.spec.whatwg.org/multipage/#pseudo-classes" id=link2>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="utils.js"></script>
<style>
#styleTests form, #styleTests fieldset, #failExample { background-color:red; }
#styleTests > :valid, #validExample { background-color:green; }
#styleTests > :invalid, #invalidExample { background-color:lime; }
</style>
</head>
<body>
<div id="log"></div>
<div id='simpleConstraints'>
<input type=text id=text1 value="foobar" required>
<input type=text id=text2 required>
</div>
<div id='FormSelection'>
<form id=form1>
<input type=text id=text3 value="foobar" required>
</form>
<form id=form2>
<input type=text id=text4 required>
</form>
</div>
<div id='FieldSetSelection'>
<fieldset id=fieldset1>
<input type=text id=text5 value="foobar" required>
</fieldset>
<fieldset id=fieldset2>
<input type=text id=text6 required>
</fieldset>
</div>
<div id='patternConstraints'>
<input type=text id=text7 value="AAA" pattern="[0-9][A-Z]{3}">
<input type=text id=text8 value="0AAA" pattern="[0-9][A-Z]{3}">
</div>
<div id='numberConstraints'>
<input type=number id=number1 value=0 min=1>
<input type=number id=number2 value=1 min=1>
</div>
<div id='styleTests'>
<form>
</form>
<form>
<input type=text min=8 value=4>
</form>
<form>
<input type=number min=8 value=4>
</form>
<fieldset>
</fieldset>
<fieldset>
<input type=text min=8 value=4>
</fieldset>
<fieldset>
<input type=number min=8 value=4>
</fieldset>
<div id='validExample'></div>
<div id='invalidExample'></div>
<div id='failExample'></div>
</div>
<script>
testSelectorIdsMatch("#simpleConstraints :valid", ["text1"], "':valid' matches elements that satisfy their constraints");
testSelectorIdsMatch("#FormSelection :valid", ["form1", "text3"], "':valid' matches form elements that are not the form owner of any elements that themselves are candidates for constraint validation but do not satisfy their constraints");
testSelectorIdsMatch("#FieldSetSelection :valid", ["fieldset1", "text5"], "':valid' matches fieldset elements that have no descendant elements that themselves are candidates for constraint validation but do not satisfy their constraints");
testSelectorIdsMatch("#patternConstraints :valid", [ "text8" ], "':valid' matches elements that satisfy their pattern constraints");
testSelectorIdsMatch("#numberConstraints :valid", [ "number2" ], "':valid' matches elements that satisfy their number constraints");
testSelectorIdsMatch("#simpleConstraints :invalid", ["text2"], "':invalid' matches elements that do not satisfy their simple text constraints");
testSelectorIdsMatch("#FormSelection :invalid", ["form2", "text4"], "':invalid' matches form elements that are the form owner of one or more elements that themselves are candidates for constraint validation but do not satisfy their constraints");
testSelectorIdsMatch("#FieldSetSelection :invalid", ["fieldset2", "text6"], "':invalid' matches fieldset elements that have of one or more descendant elements that themselves are candidates for constraint validation but do not satisfy their constraints");
testSelectorIdsMatch("#patternConstraints :invalid", ["text7"], "':invalid' matches elements that do not satisfy their pattern constraints");
testSelectorIdsMatch("#numberConstraints :invalid", ["number1"], "':invalid' matches elements that do not satisfy their number constraints");
document.getElementById("text7").value="0BBB";
testSelectorIdsMatch("#patternConstraints :valid", [ "text7", "text8" ], "':valid' matches new elements that satisfy their constraints");
testSelectorIdsMatch("#patternConstraints :invalid", [], "':invalid' doesn't match new elements that satisfy their constraints");
document.getElementById("text8").value="BBB";
testSelectorIdsMatch("#patternConstraints :valid", ["text7"], "':valid' doesn't match new elements that do not satisfy their constraints");
testSelectorIdsMatch("#patternConstraints :invalid", ["text8"], "':invalid' matches new elements that do not satisfy their constraints");
function getBGColor(elem) {
return getComputedStyle(elem).backgroundColor;
}
function testStyles(type) {
var elems = document.querySelectorAll("#styleTests " + type),
empty = elems[0],
valid = elems[1],
invalid = elems[2],
validInput = valid.querySelector("input"),
invalidInput = invalid.querySelector("input"),
expectedValidBGColor = getBGColor(document.getElementById("validExample")),
expectedInvalidBGColor = getBGColor(document.getElementById("invalidExample")),
expectedFailBGColor = getBGColor(document.getElementById("failExample"));
test(function() {
assert_equals(getBGColor(empty), expectedValidBGColor, "wrong background-color");
}, 'empty ' + type + ' correctly styled on page-load');
test(function() {
assert_equals(getBGColor(valid), expectedValidBGColor, "wrong background-color");
}, 'valid ' + type + ' correctly styled on page-load');
test(function() {
assert_equals(getBGColor(invalid), expectedInvalidBGColor, "wrong background-color");
}, 'invalid ' + type + ' correctly styled on page-load');
test(function() {
empty.appendChild(validInput.cloneNode());
assert_equals(getBGColor(empty), expectedValidBGColor, "wrong background-color");
}, 'programmatically adding valid to empty ' + type + ' results in correct style');
test(function() {
empty.appendChild(invalidInput.cloneNode());
assert_equals(getBGColor(empty), expectedInvalidBGColor, "wrong background-color");
}, 'programmatically adding invalid to empty ' + type + ' results in correct style');
validInput.type = "number";
invalidInput.type = "text";
test(function() {
assert_equals(getBGColor(valid), expectedInvalidBGColor, "wrong background-color");
}, 'programmatically-invalidated ' + type + ' correctly styled');
test(function() {
assert_equals(getBGColor(invalid), expectedValidBGColor, "wrong background-color");
}, 'programmatically-validated ' + type + ' correctly styled');
}
test(testStyles.bind(undefined, "form"), ":valid/:invalid styling for <form>");
test(testStyles.bind(undefined, "fieldset"), ":valid/:invalid styling for <fieldset>");
</script>
</body>
</html>
|