summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/custom-elements/Document-createElement.html
blob: 7772f36e20d9bde9fd1c8408037ba9f1f4274e60 (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
<!DOCTYPE html>
<html>
<head>
<title>Custom Elements: document.createElement should create an element with synchronous custom elements flag set</title>
<meta name="author" title="Ryosuke Niwa" href="mailto:rniwa@webkit.org">
<meta name="assert" content="document.createElement should create an element with synchronous custom elements flag set">
<link rel="help" content="https://dom.spec.whatwg.org/#dom-document-createelement">
<link rel="help" content="https://dom.spec.whatwg.org/#concept-create-element">
<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(function () {
    class MyCustomElement extends HTMLElement {};

    assert_true(document.createElement('my-custom-element') instanceof HTMLElement);
    assert_false(document.createElement('my-custom-element') instanceof MyCustomElement);

    customElements.define('my-custom-element', MyCustomElement);
    var instance = document.createElement('my-custom-element');
    assert_true(instance instanceof MyCustomElement);
    assert_equals(instance.localName, 'my-custom-element');
    assert_equals(instance.namespaceURI, 'http://www.w3.org/1999/xhtml', 'A custom element HTML must use HTML namespace');

}, 'document.createElement must create an instance of custom elements');

function assert_reports(expected, testFunction, message) {
    var uncaughtError = null;
    window.onerror = function (message, url, lineNumber, columnNumber, error) { uncaughtError = error; return true; }
    testFunction();
    if (typeof(expected) == 'string')
        assert_equals(uncaughtError, expected, message);
    else if (expected && 'name' in expected)
        assert_equals(uncaughtError.name, expected.name, message);
    else
      assert_equals(uncaughtError, expected, message);
    window.onerror = null;
}

function assert_not_reports(testFunction, message) {
    assert_reports(null, testFunction, message);
}

test(function () {
    class ObjectCustomElement extends HTMLElement {
        constructor()
        {
            return {foo: 'bar'};
        }
    };
    customElements.define('object-custom-element', ObjectCustomElement);

    var instance = new ObjectCustomElement;
    assert_true(instance instanceof Object);
    assert_equals(instance.foo, 'bar');

    var instance;
    assert_reports({name: 'TypeError'}, function () { instance = document.createElement('object-custom-element'); });
    assert_equals(instance.localName, 'object-custom-element');
    assert_true(instance instanceof HTMLUnknownElement);
}, 'document.createElement must report a TypeError when the result of Construct is not a DOM node');

test(function () {
    class TextCustomElement extends HTMLElement {
        constructor()
        {
            return document.createTextNode('hello');
        }
    };
    customElements.define('text-custom-element', TextCustomElement);
    assert_true(new TextCustomElement instanceof Text);
    var instance;
    assert_reports({name: 'TypeError'}, function () { instance = document.createElement('text-custom-element'); });
    assert_equals(instance.localName, 'text-custom-element');
    assert_true(instance instanceof HTMLUnknownElement);
}, 'document.createElement must report a TypeError when the result of Construct is a TextNode');

test(function () {
    class ElementWithAttribute extends HTMLElement {
        constructor()
        {
            super();
            this.setAttribute('id', 'foo');
        }
    };
    customElements.define('element-with-attribute', ElementWithAttribute);
    assert_true(new ElementWithAttribute instanceof ElementWithAttribute);
    var instance;
    assert_reports({name: 'NotSupportedError'}, function () { instance = document.createElement('element-with-attribute'); });
    assert_equals(instance.localName, 'element-with-attribute');
    assert_true(instance instanceof HTMLUnknownElement);
}, 'document.createElement must report a NotSupportedError when attribute is added by setAttribute during construction');

test(function () {
    class ElementWithAttrNode extends HTMLElement {
        constructor()
        {
            super();
            this.attributes.setNamedItem(document.createAttribute('title'));
        }
    };
    customElements.define('element-with-attr-node', ElementWithAttrNode);
    assert_true(new ElementWithAttrNode instanceof ElementWithAttrNode);
    var instance;
    assert_reports({name: 'NotSupportedError'}, function () { instance = document.createElement('element-with-attr-node'); });
    assert_equals(instance.localName, 'element-with-attr-node');
    assert_true(instance instanceof HTMLUnknownElement);
}, 'document.createElement must report a NotSupportedError when attribute is added by attributes.setNamedItem during construction');

test(function () {
    class ElementWithNoAttributes extends HTMLElement {
        constructor()
        {
            super();
            this.attributes.setNamedItem(document.createAttribute('title'));
            this.removeAttribute('title');
        }
    };
    customElements.define('element-with-no-attiributes', ElementWithNoAttributes);
    assert_true(new ElementWithNoAttributes instanceof ElementWithNoAttributes);
    var instance;
    assert_not_reports(function () { instance = document.createElement('element-with-no-attiributes'); });
    assert_true(instance instanceof ElementWithNoAttributes);
}, 'document.createElement must not report a NotSupportedError when attribute is added and removed during construction');

test(function () {
    class ElementWithChildText extends HTMLElement {
        constructor()
        {
            super();
            this.appendChild(document.createTextNode('hello'));
        }
    };
    customElements.define('element-with-child-text', ElementWithChildText);
    assert_true(new ElementWithChildText instanceof ElementWithChildText);
    var instance;
    assert_reports({name: 'NotSupportedError'}, function () { instance = document.createElement('element-with-child-text'); });
    assert_equals(instance.localName, 'element-with-child-text');
    assert_true(instance instanceof HTMLUnknownElement);
}, 'document.createElement must report a NotSupportedError when a Text child is added during construction');

test(function () {
    class ElementWithChildComment extends HTMLElement {
        constructor()
        {
            super();
            this.appendChild(document.createComment('hello'));
        }
    };
    customElements.define('element-with-child-comment', ElementWithChildComment);
    assert_true(new ElementWithChildComment instanceof ElementWithChildComment);
    var instance;
    assert_reports({name: 'NotSupportedError'}, function () { instance = document.createElement('element-with-child-comment'); });
    assert_equals(instance.localName, 'element-with-child-comment');
    assert_true(instance instanceof HTMLUnknownElement);
}, 'document.createElement must report a NotSupportedError when a Comment child is added during construction');

test(function () {
    class ElementWithChildElement extends HTMLElement {
        constructor()
        {
            super();
            this.appendChild(document.createElement('div'));
        }
    };
    customElements.define('element-with-child-element', ElementWithChildElement);
    assert_true(new ElementWithChildElement instanceof ElementWithChildElement);
    var instance;
    assert_reports({name: 'NotSupportedError'}, function () { instance = document.createElement('element-with-child-element'); });
    assert_equals(instance.localName, 'element-with-child-element');
    assert_true(instance instanceof HTMLUnknownElement);
}, 'document.createElement must report a NotSupportedError when an element child is added during construction');

test(function () {
    class ElementWithNoChildElements extends HTMLElement {
        constructor()
        {
            super();
            this.appendChild(document.createElement('div'));
            this.removeChild(this.firstChild);
        }
    };
    customElements.define('element-with-no-child-elements', ElementWithNoChildElements);
    var instance;
    assert_not_reports(function () { instance = document.createElement('element-with-no-child-elements'); });
    assert_true(instance instanceof ElementWithNoChildElements);
}, 'document.createElement must not report a NotSupportedError when an element child is added and removed during construction');

test(function () {
    class ElementWithParent extends HTMLElement {
        constructor()
        {
            super();
            document.createElement('div').appendChild(this);
        }
    };
    customElements.define('element-with-parent', ElementWithParent);
    assert_true(new ElementWithParent instanceof ElementWithParent);
    var instance;
    assert_reports({name: 'NotSupportedError'}, function () { instance = document.createElement('element-with-parent'); });
    assert_equals(instance.localName, 'element-with-parent');
    assert_true(instance instanceof HTMLUnknownElement);
}, 'document.createElement must report a NotSupportedError when the element gets inserted into another element during construction');

test(function () {
    class ElementWithNoParent extends HTMLElement {
        constructor()
        {
            super();
            document.createElement('div').appendChild(this);
            this.parentNode.removeChild(this);
        }
    };
    customElements.define('element-with-no-parent', ElementWithNoParent);
    var instance;
    assert_not_reports(function () { instance = document.createElement('element-with-no-parent'); });
    assert_true(instance instanceof ElementWithNoParent);
}, 'document.createElement must not report a NotSupportedError when the element is inserted and removed from another element during construction');

document_types().forEach(function (entry, testNumber) {
    if (entry.isOwner)
        return;

    var getDocument = entry.create;
    var documentName = entry.name;

    promise_test(function () {
        return getDocument().then(function (doc) {
            class ElementWithAdoptCall extends HTMLElement {
                constructor()
                {
                    super();
                    doc.adoptNode(this);
                }
            };
            var name = 'element-with-adopt-call-' + testNumber;
            customElements.define(name, ElementWithAdoptCall);
            assert_true(new ElementWithAdoptCall instanceof ElementWithAdoptCall);
            var instance;
            assert_reports({name: 'NotSupportedError'}, function () { instance = document.createElement(name); });
            assert_equals(instance.localName, name);
            assert_true(instance instanceof HTMLUnknownElement);
        });
    }, `document.createElement must report a NotSupportedError when the element is adopted into a ${documentName} during construction`);

    promise_test(function () {
        return getDocument().then(function (doc) {
            class ElementInsertedIntoAnotherDocument extends HTMLElement {
                constructor()
                {
                    super();
                    doc.documentElement.appendChild(this);
                }
            };
            var name = 'element-inserted-into-another-document-' + testNumber;
            customElements.define(name, ElementInsertedIntoAnotherDocument);
            assert_true(new ElementInsertedIntoAnotherDocument instanceof ElementInsertedIntoAnotherDocument);
            var instance;
            assert_reports({name: 'NotSupportedError'}, function () { instance = document.createElement(name); });
            assert_equals(instance.localName, name);
            assert_true(instance instanceof HTMLUnknownElement);
        });
    }, `document.createElement must report a NotSupportedError when the element is inserted into a ${documentName} during construction`);

    promise_test(function () {
        return getDocument().then(function (doc) {
            class ElementThatGetAdoptedBack extends HTMLElement {
                constructor()
                {
                    super();
                    doc.adoptNode(this);
                    document.adoptNode(this);
                }
            };
            var name = 'element-that-get-adopted-back' + testNumber;
            customElements.define(name, ElementThatGetAdoptedBack);
            var instance;
            assert_not_reports(function () { instance = document.createElement(name); });
            assert_true(instance instanceof ElementThatGetAdoptedBack);
        });
    }, `document.createElement must not report a NotSupportedError when the element is adopted back from a ${documentName} during construction`);
});

test(function () {
    class DivCustomElement extends HTMLElement {
        constructor()
        {
            super();
            return document.createElement('div');
        }
    };
    customElements.define('div-custom-element', DivCustomElement);
    assert_true(new DivCustomElement instanceof HTMLDivElement);
    var instance;
    assert_reports({name: 'NotSupportedError'}, function () { instance = document.createElement('div-custom-element'); });
    assert_equals(instance.localName, 'div-custom-element');
    assert_true(instance instanceof HTMLUnknownElement);
}, 'document.createElement must report a NotSupportedError when the local name of the element does not match that of the custom element');

test(function () {
    var exceptionToThrow = {name: 'exception thrown by a custom constructor'};
    class ThrowCustomElement extends HTMLElement {
        constructor()
        {
            super();
            if (exceptionToThrow)
                throw exceptionToThrow;
        }
    };
    customElements.define('throw-custom-element', ThrowCustomElement);

    assert_throws_exactly(exceptionToThrow, function () { new ThrowCustomElement; });
    var instance;
    assert_reports(exceptionToThrow, function () { instance = document.createElement('throw-custom-element'); });
    assert_equals(instance.localName, 'throw-custom-element');
    assert_true(instance instanceof HTMLUnknownElement);

    exceptionToThrow = false;
    var instance = document.createElement('throw-custom-element');
    assert_true(instance instanceof ThrowCustomElement);
    assert_equals(instance.localName, 'throw-custom-element');

}, 'document.createElement must report an exception thrown by a custom element constructor');

test(() => {
  class MyElement extends HTMLElement {
      constructor() {
          super();
          this.foo = true;
      }
  }
  customElements.define("my-element", MyElement);

  const instance = document.createElement('my-element', undefined);
  assert_true(instance.foo);
}, 'document.createElement with undefined options value should be upgraded.');
</script>
</body>
</html>