summaryrefslogtreecommitdiffstats
path: root/dom/html/test/forms/test_validation.html
blob: ee0a93a992e675a6404005a2c3bc50bbda9160ff (plain)
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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=345624
-->
<head>
  <title>Test for Bug 345624</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <style>
    input, textarea, fieldset, button, select, keygen, output, object { background-color: rgb(0,0,0) !important; }
    :valid   { background-color: rgb(0,255,0) !important; }
    :invalid { background-color: rgb(255,0,0) !important; }
  </style>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=345624">Mozilla Bug 345624</a>
<p id="display"></p>
<div id="content" style="display: none">
  <fieldset id='f'></fieldset>
  <input id='i' oninvalid="invalidEventHandler(event);">
  <button id='b' oninvalid="invalidEventHandler(event);"></button>
  <select id='s' oninvalid="invalidEventHandler(event);"></select>
  <textarea id='t' oninvalid="invalidEventHandler(event);"></textarea>
  <output id='o' oninvalid="invalidEventHandler(event);"></output>
  <keygen id='k'></keygen>
  <object id='obj'></object>
</div>
<pre id="test">
<script type="application/javascript">

/** Test for Bug 345624 **/

var gInvalid = false;

function invalidEventHandler(aEvent)
{
  function checkInvalidEvent(aEvent)
  {
    is(aEvent.type, "invalid", "Invalid event type should be invalid");
    ok(!aEvent.bubbles, "Invalid event should not bubble");
    ok(aEvent.cancelable, "Invalid event should be cancelable");
  }

  checkInvalidEvent(aEvent);

  gInvalid = true;
}

function checkConstraintValidationAPIExist(element)
{
  ok('willValidate' in element, "willValidate is not available in the DOM");
  ok('validationMessage' in element, "validationMessage is not available in the DOM");
  ok('validity' in element, "validity is not available in the DOM");

  if ('validity' in element) {
    validity = element.validity;
    ok('valueMissing' in validity, "validity.valueMissing is not available in the DOM");
    ok('typeMismatch' in validity, "validity.typeMismatch is not available in the DOM");
    ok('badInput' in validity, "validity.badInput is not available in the DOM");
    ok('patternMismatch' in validity, "validity.patternMismatch is not available in the DOM");
    ok('tooLong' in validity, "validity.tooLong is not available in the DOM");
    ok('rangeUnderflow' in validity, "validity.rangeUnderflow is not available in the DOM");
    ok('rangeOverflow' in validity, "validity.rangeOverflow is not available in the DOM");
    ok('stepMismatch' in validity, "validity.stepMismatch is not available in the DOM");
    ok('customError' in validity, "validity.customError is not available in the DOM");
    ok('valid' in validity, "validity.valid is not available in the DOM");
  }
}

function checkConstraintValidationAPIDefaultValues(element)
{
  // Not checking willValidate because the default value depends of the element

  is(element.validationMessage, "", "validationMessage default value should be empty string");

  ok(!element.validity.valueMissing, "The element should not suffer from a constraint validation");
  ok(!element.validity.typeMismatch, "The element should not suffer from a constraint validation");
  ok(!element.validity.badInput, "The element should not suffer from a constraint validation");
  ok(!element.validity.patternMismatch, "The element should not suffer from a constraint validation");
  ok(!element.validity.tooLong, "The element should not suffer from a constraint validation");
  ok(!element.validity.rangeUnderflow, "The element should not suffer from a constraint validation");
  ok(!element.validity.rangeOverflow, "The element should not suffer from a constraint validation");
  ok(!element.validity.stepMismatch, "The element should not suffer from a constraint validation");
  ok(!element.validity.customError, "The element should not suffer from a constraint validation");
  ok(element.validity.valid, "The element should be valid by default");

  ok(element.checkValidity(), "The element should be valid by default");
}

function checkDefaultPseudoClass()
{
  is(window.getComputedStyle(document.getElementById('f'), null)
       .getPropertyValue('background-color'), "rgb(0, 255, 0)",
     ":valid should apply");

  is(window.getComputedStyle(document.getElementById('o'), null)
       .getPropertyValue('background-color'), "rgb(0, 255, 0)",
     ":valid should apply");

  is(window.getComputedStyle(document.getElementById('obj'), null)
       .getPropertyValue('background-color'), "rgb(0, 0, 0)",
     "Nor :valid and :invalid should apply");

  todo_is(window.getComputedStyle(document.getElementById('k'), null)
       .getPropertyValue('background-color'), "rgb(0, 0, 0)",
     "Nor :valid and :invalid should apply");

  is(window.getComputedStyle(document.getElementById('s'), null)
       .getPropertyValue('background-color'), "rgb(0, 255, 0)",
     ":valid pseudo-class should apply");

  is(window.getComputedStyle(document.getElementById('i'), null)
       .getPropertyValue('background-color'), "rgb(0, 255, 0)",
     ":valid pseudo-class should apply");

  is(window.getComputedStyle(document.getElementById('t'), null)
       .getPropertyValue('background-color'), "rgb(0, 255, 0)",
     ":valid pseudo-class should apply");

  is(window.getComputedStyle(document.getElementById('b'), null)
       .getPropertyValue('background-color'), "rgb(0, 255, 0)",
     ":valid pseudo-class should apply");
}

function checkSpecificWillValidate()
{
  // fieldset, output, object, keygen (TODO) and select elements
  ok(!document.getElementById('f').willValidate, "Fielset element should be barred from constraint validation");
  ok(!document.getElementById('obj').willValidate, "Object element should be barred from constraint validation");
  todo(!document.getElementById('k').willValidate, "Keygen element should be barred from constraint validation");
  ok(document.getElementById('o').willValidate, "Output element should not be barred from constraint validation");
  ok(document.getElementById('s').willValidate, "Select element should not be barred from constraint validation");

  // input element
  i = document.getElementById('i');
  i.type = "hidden";
  ok(!i.willValidate, "Hidden state input should be barred from constraint validation");
  is(window.getComputedStyle(i, null).getPropertyValue('background-color'),
     "rgb(0, 0, 0)", "Nor :valid and :invalid should apply");
  i.type = "reset";
  ok(!i.willValidate, "Reset button state input should be barred from constraint validation");
  is(window.getComputedStyle(i, null).getPropertyValue('background-color'),
     "rgb(0, 0, 0)", "Nor :valid and :invalid should apply");
  i.type = "button";
  ok(!i.willValidate, "Button state input should be barred from constraint validation");
  is(window.getComputedStyle(i, null).getPropertyValue('background-color'),
     "rgb(0, 0, 0)", "Nor :valid and :invalid should apply");
  i.type = "image";
  ok(i.willValidate, "Image state input should not be barred from constraint validation");
  is(window.getComputedStyle(i, null).getPropertyValue('background-color'),
     "rgb(0, 255, 0)", ":valid and :invalid should apply");
  i.type = "submit";
  ok(i.willValidate, "Submit state input should not be barred from constraint validation");
  is(window.getComputedStyle(i, null).getPropertyValue('background-color'),
     "rgb(0, 255, 0)", ":valid and :invalid should apply");
  i.type = "number";
  ok(i.willValidate, "Number state input should not be barred from constraint validation");
  is(window.getComputedStyle(i, null).getPropertyValue('background-color'),
     "rgb(0, 255, 0)", ":valid pseudo-class should apply");
  i.type = "";
  i.readOnly = 'true';
  ok(!i.willValidate, "Readonly input should be barred from constraint validation");
  is(window.getComputedStyle(i, null).getPropertyValue('background-color'),
     "rgb(0, 0, 0)", "Nor :valid and :invalid should apply");
  i.removeAttribute('readOnly');
  ok(i.willValidate, "Default input element should not be barred from constraint validation");
  is(window.getComputedStyle(i, null).getPropertyValue('background-color'),
     "rgb(0, 255, 0)", ":valid pseudo-class should apply");

  // button element
  b = document.getElementById('b');
  b.type = "reset";
  ok(!b.willValidate, "Reset state button should be barred from constraint validation");
  is(window.getComputedStyle(b, null).getPropertyValue('background-color'),
     "rgb(0, 0, 0)", "Nor :valid and :invalid should apply");
  b.type = "button";
  ok(!b.willValidate, "Button state button should be barred from constraint validation");
  is(window.getComputedStyle(b, null).getPropertyValue('background-color'),
     "rgb(0, 0, 0)", "Nor :valid and :invalid should apply");
  b.type = "submit";
  ok(b.willValidate, "Submit state button should not be barred from constraint validation");
  is(window.getComputedStyle(b, null).getPropertyValue('background-color'),
     "rgb(0, 255, 0)", ":valid and :invalid should apply");
  b.type = "";
  ok(b.willValidate, "Default button element should not be barred from constraint validation");
  is(window.getComputedStyle(b, null).getPropertyValue('background-color'),
     "rgb(0, 255, 0)", ":valid pseudo-class should apply");

  // textarea element
  t = document.getElementById('t');
  t.readOnly = true;
  ok(!t.willValidate, "Readonly textarea should be barred from constraint validation");
  is(window.getComputedStyle(t, null).getPropertyValue('background-color'),
     "rgb(0, 0, 0)", "Nor :valid and :invalid should apply");
  t.removeAttribute('readOnly');
  ok(t.willValidate, "Default textarea element should not be barred from constraint validation");
  is(window.getComputedStyle(t, null).getPropertyValue('background-color'),
     "rgb(0, 255, 0)", ":valid pseudo-class should apply");

  // TODO: PROGRESS
  // TODO: METER
}

function checkCommonWillValidate(element)
{
  // Not checking the default value because it has been checked previously.

  // Not checking output elements because they can't be disabled.
  if (element.tagName != 'OUTPUT') {
    element.disabled = true;
    ok(!element.willValidate, "Disabled element should be barred from constraint validation");

    is(window.getComputedStyle(element, null).getPropertyValue('background-color'),
       "rgb(0, 0, 0)", "Nor :valid and :invalid should apply");

    element.removeAttribute('disabled');
  }

  // TODO: If an element has a datalist element ancestor, it is barred from constraint validation.
}

function checkCustomError(element, isBarred)
{
  element.setCustomValidity("message");
  if (!isBarred) {
    is(element.validationMessage, "message",
       "When the element has a custom validity message, validation message should return it");
  } else {
    is(element.validationMessage, "",
       "An element barred from constraint validation can't have a validation message");
  }
  ok(element.validity.customError, "The element should suffer from a custom error");
  ok(!element.validity.valid, "The element should not be valid with a custom error");

  if (element.tagName == "FIELDSET") {
    is(window.getComputedStyle(element, null).getPropertyValue('background-color'),
       isBarred ? "rgb(0, 255, 0)" : "rgb(255, 0, 0)",
       ":invalid pseudo-classs should apply" + element.tagName);
  }
  else {
    is(window.getComputedStyle(element, null).getPropertyValue('background-color'),
       isBarred ? "rgb(0, 0, 0)" : "rgb(255, 0, 0)",
       ":invalid pseudo-classs should apply" + element.tagName);
  }

  element.setCustomValidity("");
  is(element.validationMessage, "", "The element should not have a validation message when reseted");
  ok(!element.validity.customError, "The element should not suffer anymore from a custom error");
  ok(element.validity.valid, "The element should now be valid");

  is(window.getComputedStyle(element, null).getPropertyValue('background-color'),
     isBarred && element.tagName != "FIELDSET" ? "rgb(0, 0, 0)" : "rgb(0, 255, 0)",
     ":valid pseudo-classs should apply");
}

function checkCheckValidity(element)
{
  element.setCustomValidity("message");
  ok(!element.checkValidity(), "checkValidity() should return false when the element is not valid");

  ok(gInvalid, "Invalid event should have been handled");

  gInvalid = false;
  element.setCustomValidity("");

  ok(element.checkValidity(), "Element should be valid");
  ok(!gInvalid, "Invalid event should not have been handled");
}

function checkValidityStateObjectAliveWithoutElement(element)
{
  // We are creating a temporary element and getting it's ValidityState object.
  // Then, we make sure it is removed by the garbage collector and we check the
  // ValidityState default values (it should not crash).

  var v = document.createElement(element).validity;
  SpecialPowers.gc();

  ok(!v.valueMissing,
    "When the element is not alive, it shouldn't suffer from constraint validation");
  ok(!v.typeMismatch,
    "When the element is not alive, it shouldn't suffer from constraint validation");
  ok(!v.badInput,
    "When the element is not alive, it shouldn't suffer from constraint validation");
  ok(!v.patternMismatch,
    "When the element is not alive, it shouldn't suffer from constraint validation");
  ok(!v.tooLong,
    "When the element is not alive, it shouldn't suffer from constraint validation");
  ok(!v.rangeUnderflow,
    "When the element is not alive, it shouldn't suffer from constraint validation");
  ok(!v.rangeOverflow,
    "When the element is not alive, it shouldn't suffer from constraint validation");
  ok(!v.stepMismatch,
    "When the element is not alive, it shouldn't suffer from constraint validation");
  ok(!v.customError,
    "When the element is not alive, it shouldn't suffer from constraint validation");
  ok(v.valid, "When the element is not alive, it should be valid");
}

checkConstraintValidationAPIExist(document.getElementById('f'));
checkConstraintValidationAPIExist(document.getElementById('i'));
checkConstraintValidationAPIExist(document.getElementById('b'));
checkConstraintValidationAPIExist(document.getElementById('s'));
checkConstraintValidationAPIExist(document.getElementById('t'));
checkConstraintValidationAPIExist(document.getElementById('k'));
checkConstraintValidationAPIExist(document.getElementById('o'));
checkConstraintValidationAPIExist(document.getElementById('obj'));

checkConstraintValidationAPIDefaultValues(document.getElementById('f'));
checkConstraintValidationAPIDefaultValues(document.getElementById('i'));
checkConstraintValidationAPIDefaultValues(document.getElementById('b'));
checkConstraintValidationAPIDefaultValues(document.getElementById('s'));
checkConstraintValidationAPIDefaultValues(document.getElementById('t'));
checkConstraintValidationAPIDefaultValues(document.getElementById('k'));
checkConstraintValidationAPIDefaultValues(document.getElementById('o'));
checkConstraintValidationAPIDefaultValues(document.getElementById('obj'));

checkDefaultPseudoClass();

checkSpecificWillValidate();

// Not checking button, fieldset, object and keygen
// because they are always barred from constraint validation.
checkCommonWillValidate(document.getElementById('i'));
checkCommonWillValidate(document.getElementById('s'));
checkCommonWillValidate(document.getElementById('t'));
checkCommonWillValidate(document.getElementById('o'));

/* TODO: add "keygen" element */
checkCustomError(document.getElementById('i'), false);
checkCustomError(document.getElementById('s'), false);
checkCustomError(document.getElementById('t'), false);
checkCustomError(document.getElementById('o'), false);
checkCustomError(document.getElementById('b'), false);
checkCustomError(document.getElementById('f'), true);
checkCustomError(document.getElementById('obj'), true);

// Not checking button, fieldset, object and keygen
// because they are always barred from constraint validation.
checkCheckValidity(document.getElementById('i'));
checkCheckValidity(document.getElementById('s'));
checkCheckValidity(document.getElementById('t'));
checkCheckValidity(document.getElementById('o'));

/* TODO: add "keygen" element */
checkValidityStateObjectAliveWithoutElement("fieldset");
checkValidityStateObjectAliveWithoutElement("input");
checkValidityStateObjectAliveWithoutElement("button");
checkValidityStateObjectAliveWithoutElement("select");
checkValidityStateObjectAliveWithoutElement("textarea");
checkValidityStateObjectAliveWithoutElement("output");
checkValidityStateObjectAliveWithoutElement("object");

</script>
</pre>
</body>
</html>