File: Element-innerHTML.html

package info (click to toggle)
firefox 147.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • 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 (144 lines) | stat: -rw-r--r-- 6,669 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
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
<!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>
<div id="host">
    <template shadowrootmode="open" shadowrootclonable="true" shadowrootcustomelementregistry>
        <new-element></new-element>
    </template>
</div>
<body>
<script>

function createConnectedShadowTree(test, customElementRegistry) {
    const host = document.createElement('div');
    const shadowRoot = host.attachShadow({mode: 'closed', customElementRegistry});
    document.body.appendChild(host);
    test.add_cleanup(() => host.remove());
    return shadowRoot;
}

class GlobalSomeElement extends HTMLElement { };
customElements.define('some-element', GlobalSomeElement);

class GlobalOtherElement extends HTMLElement { };
customElements.define('other-element', GlobalOtherElement);

class WrongNewElement extends HTMLElement{ };
customElements.define('new-element', WrongNewElement);

test((test) => {
    const registry = new CustomElementRegistry;

    class ScopedSomeElement extends HTMLElement { };
    registry.define('some-element', ScopedSomeElement);

    class ScopedOtherElement extends HTMLElement { };
    registry.define('other-element', ScopedOtherElement);

    const someElement = document.createElement('some-element', {customElementRegistry: registry});
    assert_true(someElement instanceof ScopedSomeElement);
    someElement.innerHTML = '<other-element></other-element>';
    assert_true(someElement.querySelector('other-element') instanceof ScopedOtherElement);
}, 'innerHTML on a disconnected element should use the scoped registry it was created with');

test((test) => {
    const registry = new CustomElementRegistry;

    class ScopedSomeElement extends HTMLElement { };
    registry.define('some-element', ScopedSomeElement);

    class ScopedOtherElement extends HTMLElement { };
    registry.define('other-element', ScopedOtherElement);

    const someElement = document.createElement('some-element', {customElementRegistry: registry});
    assert_true(someElement instanceof ScopedSomeElement);
    someElement.innerHTML = `
        <other-element id="foo">
            <other-element id="bar">
                <div id="baz">
                    <other-element id="nested-ce"></other-element>
                </div>
            </other-element>
        </other-element>
        <div id="bux"></div>
        <another-element></another-element>`;

    assert_true(someElement.querySelector('#foo') instanceof ScopedOtherElement);
    assert_true(someElement.querySelector('#bar') instanceof ScopedOtherElement);
    assert_true(someElement.querySelector('#nested-ce') instanceof ScopedOtherElement);

    assert_true(someElement.querySelector('#foo').customElementRegistry === registry);
    assert_true(someElement.querySelector('#bar').customElementRegistry === registry);
    assert_true(someElement.querySelector('#baz').customElementRegistry === registry);
    assert_true(someElement.querySelector('#nested-ce').customElementRegistry === registry);
    assert_true(someElement.querySelector('#bux').customElementRegistry === registry);
    assert_true(someElement.querySelector('another-element').customElementRegistry === registry);
}, 'nested descendants in innerHTML on a disconnected element should use the scoped registry the element was created with');

test((test) => {
    const registry = new CustomElementRegistry;

    class ScopedSomeElement extends HTMLElement { };
    registry.define('some-element', ScopedSomeElement);

    const div = document.createElement('div', {customElementRegistry: registry});
    div.innerHTML = '<span></span>';
    assert_equals(div.querySelector('span').customElementRegistry, registry);
}, 'innerHTML on a disconnected element should use the scoped registry it was created with when parsing a simple HTML');

test((test) => {
    const registry1 = new CustomElementRegistry;
    const registry2 = new CustomElementRegistry;

    class ScopedSomeElement extends HTMLElement { };
    registry1.define('some-element', ScopedSomeElement);

    class ScopedOtherElement1 extends HTMLElement { };
    registry1.define('other-element', ScopedOtherElement1);
    class ScopedOtherElement2 extends HTMLElement { };
    registry2.define('other-element', ScopedOtherElement2);

    const shadowRoot1 = createConnectedShadowTree(test, registry1);
    const shadowRoot2 = createConnectedShadowTree(test, registry2);
    const someElement = document.createElement('some-element', {customElementRegistry: registry1});
    someElement.innerHTML = '<other-element></other-element>';
    assert_true(someElement.querySelector('other-element') instanceof ScopedOtherElement1);
    shadowRoot2.appendChild(someElement);
    someElement.innerHTML = '<other-element></other-element>';
    assert_true(someElement.querySelector('other-element') instanceof ScopedOtherElement1);
    someElement.remove();
    someElement.innerHTML = '<other-element></other-element>';
    assert_true(someElement.querySelector('other-element') instanceof ScopedOtherElement1);
}, 'innerHTML on an inserted element should continue to use the scoped registry it was created with');

test((test) => {
    const shadowRoot = host.cloneNode(true).shadowRoot;
    const container_element = shadowRoot.lastElementChild;
    assert_equals(container_element.customElementRegistry, null);
    assert_false(container_element instanceof WrongNewElement);

    container_element.innerHTML = '<new-element><new-element></new-element></new-element>';
    const outer_element = container_element.querySelector('new-element');
    const inner_element = outer_element.querySelector('new-element');
    assert_equals(outer_element.customElementRegistry, null);
    assert_false(outer_element instanceof WrongNewElement);
    assert_equals(inner_element.customElementRegistry, null);
    assert_false(inner_element instanceof WrongNewElement);
}, 'nested descendants in innerHTML should use the null registry when the container element has null registry');

test((test) => {
    const shadowRoot = host.cloneNode(true).shadowRoot;
    shadowRoot.firstElementChild.insertAdjacentHTML('afterend', '<new-element></new-element>');
    const container_element = shadowRoot.lastElementChild;
    assert_equals(container_element.customElementRegistry, null);
    assert_false(container_element instanceof WrongNewElement);
}, 'insertAdjacentHTML should use the element\'s registry even when the registry is null');

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