summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/custom-elements/upgrading/upgrading-enqueue-reactions.html
blob: 8238eee624afee25f19356b4de244713b5047038 (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
<!DOCTYPE html>
<html>
<head>
<title>Custom Elements: Upgrading custom elements should enqueue attributeChanged and connected callbacks</title>
<meta name="author" title="Ryosuke Niwa" href="mailto:rniwa@webkit.org">
<meta name="assert" content="Upgrading custom elements should enqueue attributeChanged and connected callbacksml">
<meta name="help" content="https://html.spec.whatwg.org/#upgrades">
<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>
setup({allow_uncaught_exception:true});

test_with_window(function (contentWindow) {
    const contentDocument = contentWindow.document;
    contentDocument.write('<test-element id="some" title="This is a test">');

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

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

    assert_equals(log.length, 3);
    assert_constructor_log_entry(log[0], undefinedElement);
    assert_attribute_log_entry(log[1], {name: 'id', oldValue: null, newValue: 'some', namespace: null});
    assert_attribute_log_entry(log[2], {name: 'title', oldValue: null, newValue: 'This is a test', namespace: null});
}, 'Upgrading a custom element must enqueue attributeChangedCallback on each attribute');

test_with_window(function (contentWindow) {
    const contentDocument = contentWindow.document;
    contentDocument.write('<test-element id="some" title="This is a test" class="foo">');

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

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

    assert_equals(log.length, 3);
    assert_constructor_log_entry(log[0], undefinedElement);
    assert_attribute_log_entry(log[1], {name: 'id', oldValue: null, newValue: 'some', namespace: null});
    assert_attribute_log_entry(log[2], {name: 'class', oldValue: null, newValue: 'foo', namespace: null});
}, 'Upgrading a custom element not must enqueue attributeChangedCallback on unobserved attributes');

test_with_window(function (contentWindow) {
    const contentDocument = contentWindow.document;
    contentDocument.write('<test-element id="some" title="This is a test" class="foo">');

    const undefinedElement = contentDocument.querySelector('test-element');
    assert_equals(Object.getPrototypeOf(undefinedElement), 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));
        }
    }
    contentWindow.customElements.define('test-element', TestElement);
    assert_equals(Object.getPrototypeOf(undefinedElement), TestElement.prototype);

    assert_equals(log.length, 2);
    assert_constructor_log_entry(log[0], undefinedElement);
    assert_connected_log_entry(log[1], undefinedElement);
}, 'Upgrading a custom element must enqueue connectedCallback if the element in the document');

test_with_window(function (contentWindow) {
    const contentDocument = contentWindow.document;
    contentDocument.write('<test-element id="some" title="This is a test" class="foo">');

    const undefinedElement = contentDocument.querySelector('test-element');
    assert_equals(Object.getPrototypeOf(undefinedElement), 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 ['class', 'id']; }
    }
    contentWindow.customElements.define('test-element', TestElement);
    assert_equals(Object.getPrototypeOf(undefinedElement), TestElement.prototype);

    assert_equals(log.length, 4);
    assert_constructor_log_entry(log[0], undefinedElement);
    assert_attribute_log_entry(log[1], {name: 'id', oldValue: null, newValue: 'some', namespace: null});
    assert_attribute_log_entry(log[2], {name: 'class', oldValue: null, newValue: 'foo', namespace: null});
    assert_connected_log_entry(log[3], undefinedElement);
}, 'Upgrading a custom element must enqueue attributeChangedCallback before connectedCallback');

test_with_window(function (contentWindow) {
    const contentDocument = contentWindow.document;
    contentDocument.write('<test-element id="some" title="This is a test" class="foo">');

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

    let log = [];
    class TestElement extends contentWindow.HTMLElement {
        constructor() {
            super();
            log.push(create_constructor_log(this));
            throw 'Exception thrown as a part of test';
        }
        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 ['class', 'id']; }
    }
    contentWindow.customElements.define('test-element', TestElement);
    assert_equals(Object.getPrototypeOf(undefinedElement), TestElement.prototype);

    assert_equals(log.length, 1);
    assert_constructor_log_entry(log[0], undefinedElement);
}, 'Upgrading a custom element must not invoke attributeChangedCallback and connectedCallback when the element failed to upgrade');

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