File: Document-createElement.html

package info (click to toggle)
firefox 147.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,324 kB
  • sloc: cpp: 7,607,156; javascript: 6,532,492; ansic: 3,775,158; python: 1,415,368; xml: 634,556; asm: 438,949; java: 186,241; sh: 62,751; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (100 lines) | stat: -rw-r--r-- 4,877 bytes parent folder | download | duplicates (2)
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
<!DOCTYPE html>
<html>
<head>
<meta name="author" title="Ryosuke Niwa" href="mailto:rniwa@webkit.org">
<link rel="help" href="https://github.com/whatwg/html/issues/10854">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<script>
// Keep this ~synchronized with Document-createElementNS
"use strict";

const scopedRegistry = new CustomElementRegistry();
const otherScopedRegistry = new CustomElementRegistry();
class GlobalABElement extends HTMLElement {};
class ScopedABElement extends HTMLElement {};
customElements.define('a-b', GlobalABElement);
scopedRegistry.define('a-b', ScopedABElement);

test(() => {
    assert_true(document.createElement('a-b') instanceof GlobalABElement);
}, 'createElement should use the global registry by default');

test(() => {
    assert_true(document.createElement('a-b', {customElementRegistry: scopedRegistry}) instanceof ScopedABElement);
}, 'createElement should use the specified scoped registry');

test(() => {
    const elements = {
        div: HTMLDivElement,
        form: HTMLFormElement,
        span: HTMLSpanElement,
        table: HTMLTableElement,
        unknown: HTMLUnknownElement,
    };
    for (const localName in elements) {
        const scopedElement = document.createElement(localName, {customElementRegistry: scopedRegistry});
        assert_true(scopedElement instanceof elements[localName], localName);
        assert_equals(scopedElement.customElementRegistry, scopedRegistry);

        const globalExplicitElement = document.createElement(localName, {customElementRegistry: window.customElements});
        assert_true(globalExplicitElement instanceof elements[localName], localName);
        assert_equals(globalExplicitElement.customElementRegistry, window.customElements);

        const globalImplicitElement = document.createElement(localName);
        assert_true(globalImplicitElement instanceof elements[localName], localName);
        assert_equals(globalImplicitElement.customElementRegistry, window.customElements);
    }
}, 'createElement should create a builtin element regardless of a custom element registry specified');

test(() => {
    assert_true(document.createElement('a-b', {customElementRegistry: window.customElements}) instanceof GlobalABElement);
}, 'createElement should use the specified global registry');

test(() => {
    const element = document.createElement('a-b', {customElementRegistry: otherScopedRegistry});
    assert_equals(element.__proto__.constructor.name, 'HTMLElement');
}, 'createElement should create an upgrade candidate when there is no matching definition in the specified registry');

test((t) => {
    assert_equals(document.createElement('div', {customElementRegistry: null}).customElementRegistry, null);
}, `document.createElement should create a builtin element with null registry if customElementRegistry is set to null`);

test((t) => {
    assert_equals(document.createElement('c-d', {customElementRegistry: null}).customElementRegistry, null);
}, `document.createElement should create a custom element candidate with null registry if customElementRegistry is set to null`);

test((t) => {
    assert_equals(document.createElement('a-b', {customElementRegistry: null}).customElementRegistry, null);
}, `document.createElement should create a custom element candidate with null registry if customElementRegistry is set to null even if there is a custom element of the same name in the glboal registry`);

test(() => {
    class CDElement extends HTMLElement { }
    const registry = new CustomElementRegistry;
    const cdElement = document.createElement('c-d', {customElementRegistry: registry});
    assert_false(cdElement instanceof CDElement);
    assert_equals(cdElement.__proto__.constructor.name, 'HTMLElement');
    registry.define('c-d', CDElement);
    assert_false(cdElement instanceof CDElement); // Not yet upgraded since it's disconnected.
    assert_equals(cdElement.__proto__.constructor.name, 'HTMLElement');
    document.body.appendChild(cdElement);
    assert_true(cdElement instanceof CDElement);
}, 'createElement should create an upgrade candidate and the candidate should be upgraded when the element is defined');

test(() => {
    const doc = new Document();
    const scopedElement = doc.createElement("time", {customElementRegistry: scopedRegistry});
    assert_equals(scopedElement.namespaceURI, null);
    assert_equals(scopedElement.customElementRegistry, scopedRegistry);

    const abElement = doc.createElement("a-b", {customElementRegistry: scopedRegistry});
    assert_equals(abElement.namespaceURI, null);
    assert_equals(abElement.customElementRegistry, scopedRegistry);
    assert_false(abElement instanceof ScopedABElement);
}, 'createElement on a non-HTML document should still handle registries correctly');

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