-
Notifications
You must be signed in to change notification settings - Fork 54
/
ElementInternals-labels.html
50 lines (45 loc) · 1.81 KB
/
ElementInternals-labels.html
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
<!DOCTYPE html>
<title>labels attribute of ElementInternals, and label association</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);
const container = document.querySelector('#container');
test(() => {
container.innerHTML = '<label><span><my-control></my-control></span></label>';
let label = container.querySelector('label');
let control = container.querySelector('my-control');
assert_equals(label.control, control);
assert_true(control.i.labels instanceof NodeList);
assert_array_equals(control.i.labels, [label]);
container.innerHTML = '<label for="mc"></label><form><my-control id="mc"></my-control></form>';
label = container.querySelector('label');
control = container.querySelector('my-control');
assert_equals(label.control, control);
assert_equals(label.form, control.i.form);
assert_array_equals(control.i.labels, [label]);
container.innerHTML = '<label for="mc"></label><label for="mc"><my-control id="mc">';
const labels = container.querySelectorAll('label');
control = container.querySelector('my-control');
assert_array_equals(control.i.labels, labels);
}, 'LABEL association');
test(() => {
container.innerHTML = '<label for="mc"></label><form><my-control id="mc"></my-control></form>';
const control = container.querySelector('my-control');
let clickCount = 0;
control.addEventListener('click', e => { ++clickCount; });
container.querySelector('label').click();
assert_equals(clickCount, 1);
}, 'LABEL click');
</script>
</body>