summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/custom-elements/custom-element-reaction-queue.html
blob: 246b15a0af36cffa0b64f1d57e4208538b92bdd7 (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
<!DOCTYPE html>
<html>
<head>
<title>Custom Elements: Each element must have its own custom element reaction queue</title>
<meta name="author" title="Ryosuke Niwa" href="mailto:rniwa@webkit.org">
<meta name="assert" content="Each element must have its own custom element reaction queue">
<meta name="help" content="https://html.spec.whatwg.org/multipage/scripting.html#custom-element-reaction-queue">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="./resources/custom-elements-helpers.js"></script>
</head>
<body>
<div id="log"></div>
<script>

test_with_window(function (contentWindow) {
    const contentDocument = contentWindow.document;
    contentDocument.write('<test-element id="first-element">');
    contentDocument.write('<test-element id="second-element">');

    const element1 = contentDocument.getElementById('first-element');
    const element2 = contentDocument.getElementById('second-element');
    assert_equals(Object.getPrototypeOf(element1), contentWindow.HTMLElement.prototype);
    assert_equals(Object.getPrototypeOf(element2), contentWindow.HTMLElement.prototype);

    let log = [];
    class TestElement extends contentWindow.HTMLElement {
        constructor() {
            super();
            log.push(create_constructor_log(this));
        }
        connectedCallback(...args) {
            log.push(create_connected_callback_log(this, ...args));
        }
        attributeChangedCallback(...args) {
            log.push(create_attribute_changed_callback_log(this, ...args));
        }
        static get observedAttributes() { return ['id']; }
    }
    contentWindow.customElements.define('test-element', TestElement);
    assert_equals(Object.getPrototypeOf(element1), TestElement.prototype);
    assert_equals(Object.getPrototypeOf(element2), TestElement.prototype);

    assert_equals(log.length, 6);
    assert_constructor_log_entry(log[0], element1);
    assert_attribute_log_entry(log[1], {name: 'id', oldValue: null, newValue: 'first-element', namespace: null});
    assert_connected_log_entry(log[2], element1);
    assert_constructor_log_entry(log[3], element2);
    assert_attribute_log_entry(log[4], {name: 'id', oldValue: null, newValue: 'second-element', namespace: null});
    assert_connected_log_entry(log[5], element2);
}, 'Upgrading a custom element must invoke attributeChangedCallback and connectedCallback before start upgrading another element');

test_with_window(function (contentWindow) {
    const contentDocument = contentWindow.document;
    contentDocument.write('<test-element>');

    const element = contentDocument.querySelector('test-element');
    assert_equals(Object.getPrototypeOf(element), contentWindow.HTMLElement.prototype);

    let log = [];
    class TestElement extends contentWindow.HTMLElement {
        constructor() {
            super();
            this.id = "foo";
            this.setAttribute('id', 'foo');
            this.removeAttribute('id');
            this.style.fontSize = '10px';
            log.push(create_constructor_log(this));
        }
        connectedCallback(...args) {
            log.push(create_connected_callback_log(this, ...args));
        }
        attributeChangedCallback(...args) {
            log.push(create_attribute_changed_callback_log(this, ...args));
        }
        static get observedAttributes() { return ['id', 'style']; }
    }
    contentWindow.customElements.define('test-element', TestElement);
    assert_equals(Object.getPrototypeOf(element), TestElement.prototype);

    assert_equals(log.length, 2);
    assert_constructor_log_entry(log[0], element);
    assert_connected_log_entry(log[1], element);
}, 'Upgrading a custom element must not invoke attributeChangedCallback for the attribute that is changed during upgrading');

test_with_window(function (contentWindow) {
    const contentDocument = contentWindow.document;
    contentDocument.write('<test-element id="first-element">');
    contentDocument.write('<test-element id="second-element">');

    const element1 = contentDocument.getElementById('first-element');
    const element2 = contentDocument.getElementById('second-element');
    assert_equals(Object.getPrototypeOf(element1), contentWindow.HTMLElement.prototype);
    assert_equals(Object.getPrototypeOf(element2), contentWindow.HTMLElement.prototype);

    let log = [];
    class TestElement extends contentWindow.HTMLElement {
        constructor() {
            super();
            log.push(create_constructor_log(this));
            if (this == element1) {
                element2.setAttribute('title', 'hi');
                element2.removeAttribute('title');
                element2.setAttribute('class', 'foo');
            }
        }
        connectedCallback(...args) {
            log.push(create_connected_callback_log(this, ...args));
        }
        attributeChangedCallback(...args) {
            log.push(create_attribute_changed_callback_log(this, ...args));
        }
        static get observedAttributes() { return ['id', 'class', 'title']; }
    }
    contentWindow.customElements.define('test-element', TestElement);
    assert_equals(Object.getPrototypeOf(element1), TestElement.prototype);
    assert_equals(Object.getPrototypeOf(element2), TestElement.prototype);

    assert_equals(log.length, 7);
    assert_constructor_log_entry(log[0], element1);
    assert_attribute_log_entry(log[1], {name: 'id', oldValue: null, newValue: 'first-element', namespace: null});
    assert_connected_log_entry(log[2], element1);
    assert_constructor_log_entry(log[3], element2);
    assert_attribute_log_entry(log[4], {name: 'id', oldValue: null, newValue: 'second-element', namespace: null});
    assert_attribute_log_entry(log[5], {name: 'class', oldValue: null, newValue: 'foo', namespace: null});
    assert_connected_log_entry(log[6], element2);
}, 'Mutating a undefined custom element while upgrading a custom element must not enqueue or invoke reactions on the mutated element');

test_with_window(function (contentWindow) {
    let log = [];
    let element1;
    let element2;
    class TestElement extends contentWindow.HTMLElement {
        constructor() {
            super();
            log.push(create_constructor_log(this));
        }
        adoptedCallback(...args) {
            log.push(create_adopted_callback_log(this, ...args));
            if (this == element1)
                element3.setAttribute('id', 'foo');
        }
        connectedCallback(...args) {
            log.push(create_connected_callback_log(this, ...args));
        }
        attributeChangedCallback(...args) {
            log.push(create_attribute_changed_callback_log(this, ...args));
        }
        static get observedAttributes() { return ['id', 'class']; }
    }

    contentWindow.customElements.define('test-element', TestElement);

    let contentDocument = contentWindow.document;
    element1 = contentDocument.createElement('test-element');
    element2 = contentDocument.createElement('test-element');
    element3 = contentDocument.createElement('test-element');
    assert_equals(Object.getPrototypeOf(element1), TestElement.prototype);
    assert_equals(Object.getPrototypeOf(element2), TestElement.prototype);
    assert_equals(Object.getPrototypeOf(element3), TestElement.prototype);

    assert_equals(log.length, 3);
    assert_constructor_log_entry(log[0], element1);
    assert_constructor_log_entry(log[1], element2);
    assert_constructor_log_entry(log[2], element3);
    log = [];

    const container = contentDocument.createElement('div');
    container.appendChild(element1);
    container.appendChild(element2);
    container.appendChild(element3);

    const anotherDocument = document.implementation.createHTMLDocument();
    anotherDocument.documentElement.appendChild(container);

    assert_equals(log.length, 7);
    assert_adopted_log_entry(log[0], element1);
    assert_adopted_log_entry(log[1], element3);
    assert_connected_log_entry(log[2], element3);
    assert_attribute_log_entry(log[3], {name: 'id', oldValue: null, newValue: 'foo', namespace: null});
    assert_connected_log_entry(log[4], element1);
    assert_adopted_log_entry(log[5], element2);
    assert_connected_log_entry(log[6], element2);

}, 'Mutating another custom element inside adopted callback must invoke all pending callbacks on the mutated element');


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