summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/custom-elements/form-associated/ElementInternals-validation.html
blob: 8114a5dbd8763db1fa390f1a93bc0d4c8017fba8 (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
<!DOCTYPE html>
<title>Form validation features of ElementInternals, and :valid :invalid pseudo classes</title>
<body>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="container"></div>
<script>
class MyControl extends HTMLElement {
  static get formAssociated() { return true; }

  constructor() {
    super();
    this.internals_ = this.attachInternals();
  }
  get i() { return this.internals_; }
}
customElements.define('my-control', MyControl);

class NotFormAssociatedElement extends HTMLElement {
  constructor() {
    super();
    this.internals_ = this.attachInternals();
  }
  get i() { return this.internals_; }
}
customElements.define('not-form-associated-element', NotFormAssociatedElement);

test(() => {
  const control = new MyControl();
  assert_true(control.i.willValidate, 'default value is true');

  const datalist = document.createElement('datalist');
  datalist.appendChild(control);
  assert_false(control.i.willValidate, 'false in DATALIST');
  datalist.removeChild(control);
  assert_true(control.i.willValidate, 'remove from DATALIST');

  const fieldset = document.createElement('fieldset');
  fieldset.disabled = true;
  fieldset.appendChild(control);
  assert_false(control.i.willValidate, 'append to disabled FIELDSET');
  fieldset.disabled = false;
  assert_true(control.i.willValidate, 'FIELDSET becomes enabled');
  fieldset.disabled = true;
  assert_false(control.i.willValidate, 'FIELDSET becomes disabled');
  fieldset.removeChild(control);
  assert_true(control.i.willValidate, 'remove from disabled FIELDSET');

  control.setAttribute('disabled', '');
  assert_false(control.i.willValidate, 'with disabled attribute');
  control.removeAttribute('disabled');
  assert_true(control.i.willValidate, 'without disabled attribute');

  control.setAttribute('readonly', '');
  assert_false(control.i.willValidate, 'with readonly attribute');
  control.removeAttribute('readonly');
  assert_true(control.i.willValidate, 'without readonly attribute');
}, 'willValidate');

test(() => {
  const container = document.getElementById("container");
  container.innerHTML = '<will-be-defined></will-be-defined>' +
                        '<will-be-defined disabled></will-be-defined>' +
                        '<will-be-defined readonly></will-be-defined>' +
                        '<datalist><will-be-defined></will-be-defined></datalist>' +
                        '<fieldset disabled><will-be-defined></will-be-defined></fieldset>';

  class WillBeDefined extends HTMLElement {
    static get formAssociated() { return true; }

    constructor() {
      super();
      this.internals_ = this.attachInternals();
    }
    get i() { return this.internals_; }
  }
  customElements.define('will-be-defined', WillBeDefined);
  customElements.upgrade(container);

  const controls = document.querySelectorAll('will-be-defined');
  assert_true(controls[0].i.willValidate, 'default value');
  assert_false(controls[1].i.willValidate, 'with disabled attribute');
  assert_false(controls[2].i.willValidate, 'with readOnly attribute');
  assert_false(controls[3].i.willValidate, 'in datalist');
  assert_false(controls[4].i.willValidate, 'in disabled fieldset');
}, 'willValidate after upgrade');

test(t => {
  const control = document.createElement('will-be-defined-2');

  customElements.define('will-be-defined-2', class extends HTMLElement {
    static get formAssociated() { return true; }
  });

  container.append(control);
  t.add_cleanup(() => { container.innerHTML = ''; });

  const i = control.attachInternals();
  assert_true(i.willValidate);
}, 'willValidate after upgrade (document.createElement)');

test(() => {
  const element = new NotFormAssociatedElement();
  assert_throws_dom('NotSupportedError', () => element.i.willValidate);
}, "willValidate should throw NotSupportedError if the target element is not a form-associated custom element");

test(() => {
  const element = new NotFormAssociatedElement();
  assert_throws_dom('NotSupportedError', () => element.i.validity);
}, "validity should throw NotSupportedError if the target element is not a form-associated custom element");

test(() => {
  const element = new NotFormAssociatedElement();
  assert_throws_dom('NotSupportedError', () => element.i.validationMessage);
}, "validationMessage should throw NotSupportedError if the target element is not a form-associated custom element");

test(() => {
  const element = new NotFormAssociatedElement();
  assert_throws_dom('NotSupportedError', () => element.i.setValidity());
}, "setValidity() should throw NotSupportedError if the target element is not a form-associated custom element");

test(() => {
  const control = document.createElement('my-control');
  const validity = control.i.validity;
  assert_false(validity.valueMissing, 'default valueMissing');
  assert_false(validity.typeMismatch, 'default typeMismatch');
  assert_false(validity.patternMismatch, 'default patternMismatch');
  assert_false(validity.tooLong, 'default tooLong');
  assert_false(validity.tooShort, 'default tooShort');
  assert_false(validity.rangeUnderflow, 'default rangeUnderflow');
  assert_false(validity.rangeOverflow, 'default rangeOverflow');
  assert_false(validity.stepMismatch, 'default stepMismatch');
  assert_false(validity.badInput, 'default badInput');
  assert_false(validity.customError, 'default customError');
  assert_true(validity.valid, 'default valid');

  control.i.setValidity({valueMissing: true}, 'valueMissing message');
  assert_true(validity.valueMissing);
  assert_false(validity.valid);
  assert_equals(control.i.validationMessage, 'valueMissing message');

  control.i.setValidity({typeMismatch: true}, 'typeMismatch message');
  assert_true(validity.typeMismatch);
  assert_false(validity.valueMissing);
  assert_false(validity.valid);
  assert_equals(control.i.validationMessage, 'typeMismatch message');

  control.i.setValidity({patternMismatch: true}, 'patternMismatch message');
  assert_true(validity.patternMismatch);
  assert_false(validity.valid);
  assert_equals(control.i.validationMessage, 'patternMismatch message');

  control.i.setValidity({tooLong: true}, 'tooLong message');
  assert_true(validity.tooLong);
  assert_false(validity.valid);
  assert_equals(control.i.validationMessage, 'tooLong message');

  control.i.setValidity({tooShort: true}, 'tooShort message');
  assert_true(validity.tooShort);
  assert_false(validity.valid);
  assert_equals(control.i.validationMessage, 'tooShort message');

  control.i.setValidity({rangeUnderflow: true}, 'rangeUnderflow message');
  assert_true(validity.rangeUnderflow);
  assert_false(validity.valid);
  assert_equals(control.i.validationMessage, 'rangeUnderflow message');

  control.i.setValidity({rangeOverflow: true}, 'rangeOverflow message');
  assert_true(validity.rangeOverflow);
  assert_false(validity.valid);
  assert_equals(control.i.validationMessage, 'rangeOverflow message');

  control.i.setValidity({stepMismatch: true}, 'stepMismatch message');
  assert_true(validity.stepMismatch);
  assert_false(validity.valid);
  assert_equals(control.i.validationMessage, 'stepMismatch message');

  control.i.setValidity({badInput: true}, 'badInput message');
  assert_true(validity.badInput);
  assert_false(validity.valid);
  assert_equals(control.i.validationMessage, 'badInput message');

  control.i.setValidity({customError: true}, 'customError message');
  assert_true(validity.customError, 'customError should be true.');
  assert_false(validity.valid);
  assert_equals(control.i.validationMessage, 'customError message');

  // Set multiple flags
  control.i.setValidity({badInput: true, customError: true}, 'multiple errors');
  assert_true(validity.badInput);
  assert_true(validity.customError);
  assert_false(validity.valid);
  assert_equals(control.i.validationMessage, 'multiple errors');

  // Clear flags
  control.i.setValidity({}, 'unnecessary message');
  assert_false(validity.badInput);
  assert_false(validity.customError);
  assert_true(validity.valid);
  assert_equals(control.i.validationMessage, '');

  assert_throws_js(TypeError, () => { control.i.setValidity({valueMissing: true}); },
      'setValidity() requires the second argument if the first argument contains true');
}, 'validity and setValidity()');

test(() => {
  document.body.insertAdjacentHTML('afterbegin', '<my-control><light-child></my-control>');
  let control = document.body.firstChild;
  const flags = {valueMissing: true};
  const m = 'non-empty message';

  assert_throws_dom('NotFoundError', () => {
    control.i.setValidity(flags, m, document.body);
  }, 'Not a descendant');

  assert_throws_dom('NotFoundError', () => {
    control.i.setValidity(flags, m, control);
  }, 'Self');

  let notHTMLElement = document.createElementNS('some-random-namespace', 'foo');
  control.appendChild(notHTMLElement);
  assert_throws_js(TypeError, () => {
    control.i.setValidity(flags, m, notHTMLElement);
  }, 'Not a HTMLElement');
  notHTMLElement.remove();

  // A descendant
  control.i.setValidity(flags, m, control.querySelector('light-child'));

  // An element in the direct shadow tree
  let shadow1 = control.attachShadow({mode: 'open'});
  let shadowChild = document.createElement('div');
  shadow1.appendChild(shadowChild);
  control.i.setValidity(flags, m, shadowChild);

  // An element in an nested shadow trees
  let shadow2 = shadowChild.attachShadow({mode: 'closed'});
  let nestedShadowChild = document.createElement('span');
  shadow2.appendChild(nestedShadowChild);
  control.i.setValidity(flags, m, nestedShadowChild);
}, '"anchor" argument of setValidity()');

test(() => {
  const element = new NotFormAssociatedElement();
  assert_throws_dom('NotSupportedError', () => element.i.checkValidity());
}, "checkValidity() should throw NotSupportedError if the target element is not a form-associated custom element");

test(() => {
  const control = document.createElement('my-control');
  let invalidCount = 0;
  control.addEventListener('invalid', e => {
    assert_equals(e.target, control);
    assert_true(e.cancelable);
    ++invalidCount;
  });

  assert_true(control.i.checkValidity(), 'default state');
  assert_equals(invalidCount, 0);

  control.i.setValidity({customError:true}, 'foo');
  assert_false(control.i.checkValidity());
  assert_equals(invalidCount, 1);
}, 'checkValidity()');

test(() => {
  const element = new NotFormAssociatedElement();
  assert_throws_dom('NotSupportedError', () => element.i.reportValidity());
}, "reportValidity() should throw NotSupportedError if the target element is not a form-associated custom element");

test(() => {
  const control = document.createElement('my-control');
  document.body.appendChild(control);
  control.tabIndex = 0;
  let invalidCount = 0;
  control.addEventListener('invalid', e => {
    assert_equals(e.target, control);
    assert_true(e.cancelable);
    ++invalidCount;
  });

  assert_true(control.i.reportValidity(), 'default state');
  assert_equals(invalidCount, 0);

  control.i.setValidity({customError:true}, 'foo');
  assert_false(control.i.reportValidity());
  assert_equals(invalidCount, 1);

  control.blur();
  control.addEventListener('invalid', e => e.preventDefault());
  assert_false(control.i.reportValidity());
}, 'reportValidity()');

test(() => {
  const container = document.getElementById('container');
  container.innerHTML = '<form><input type=submit><my-control>';
  const form = container.querySelector('form');
  const control = container.querySelector('my-control');
  control.tabIndex = 0;
  let invalidCount = 0;
  control.addEventListener('invalid', e => { ++invalidCount; });

  assert_true(control.i.checkValidity());
  assert_true(form.checkValidity());
  control.i.setValidity({valueMissing: true}, 'Please fill out this field');
  assert_false(form.checkValidity());
  assert_equals(invalidCount, 1);

  assert_false(form.reportValidity());
  assert_equals(invalidCount, 2);

  container.querySelector('input[type=submit]').click();
  assert_equals(invalidCount, 3);
}, 'Custom control affects validation at the owner form');

function isValidAccordingToCSS(element, comment) {
  assert_true(element.matches(':valid'), comment ? (comment + ' - :valid') : undefined);
  assert_false(element.matches(':invalid'), comment ? (comment + ' - :invalid') : undefined);
}

function isInvalidAccordingToCSS(element, comment) {
  assert_false(element.matches(':valid'), comment ? (comment + ' - :valid') : undefined);
  assert_true(element.matches(':invalid'), comment ? (comment + ' - :invalid') : undefined);
}

test(() => {
  const container = document.getElementById('container');
  container.innerHTML = '<form><fieldset><my-control>';
  const form = container.querySelector('form');
  const fieldset = container.querySelector('fieldset');
  const control = container.querySelector('my-control');

  isValidAccordingToCSS(control);
  isValidAccordingToCSS(form);
  isValidAccordingToCSS(fieldset);

  control.i.setValidity({typeMismatch: true}, 'Invalid format');
  isInvalidAccordingToCSS(control);
  isInvalidAccordingToCSS(form);
  isInvalidAccordingToCSS(fieldset);

  control.remove();
  isInvalidAccordingToCSS(control);
  isValidAccordingToCSS(form);
  isValidAccordingToCSS(fieldset);

  fieldset.appendChild(control);
  isInvalidAccordingToCSS(form);
  isInvalidAccordingToCSS(fieldset);

  control.i.setValidity({});
  isValidAccordingToCSS(control);
  isValidAccordingToCSS(form);
  isValidAccordingToCSS(fieldset);
}, 'Custom control affects :valid :invalid for FORM and FIELDSET');
</script>
</body>