summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/custom-elements/scoped-registry/ShadowRoot-innerHTML-upgrade.tentative.html
blob: e21c9dd03326d68976bffc99483d04edd1bb8145 (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
<!DOCTYPE html>
<meta name="author" title="Xiaocheng Hu" href="mailto:xiaochengh@chromium.org">
<meta name="assert" content="Custom element constructors can re-enter with different definitions">
<link rel="help" href="https://wicg.github.io/webcomponents/proposals/Scoped-Custom-Element-Registries">
<link rel="help" href="https://github.com/WICG/webcomponents/issues/969">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<div id="testdiv"></div>

<script>
class TestAutonomous extends HTMLElement {};
class TestCustomizedBuiltIn extends HTMLParagraphElement {};

function attachShadowForTest(t, registry) {
  const host = document.createElement('div');
  const shadow = host.attachShadow({mode: 'open', registry});
  document.body.appendChild(host);
  t.add_cleanup(() => host.remove());
  return shadow;
}

test(t => {
  const registry = new CustomElementRegistry;
  registry.define('test-element', TestAutonomous);

  const shadow = attachShadowForTest(t, registry);
  shadow.innerHTML = '<test-element></test-element>';
  assert_true(shadow.firstChild instanceof TestAutonomous, 'target tree scope');

  // Verify that it doesn't pollute other tree scopes.
  const shadow2 = attachShadowForTest(t);
  shadow2.innerHTML = '<test-element></test-element>';
  assert_false(shadow2.firstChild instanceof TestAutonomous, 'tree scope without registry');

  const shadow3 = attachShadowForTest(t, new CustomElementRegistry);
  shadow3.innerHTML = '<test-element></test-element>';
  assert_false(shadow3.firstChild instanceof TestAutonomous, 'tree scope with different registry');

  t.add_cleanup(() => testdiv.firstChild.remove());
  testdiv.innerHTML = '<test-element></test-element>';
  assert_false(testdiv.firstChild instanceof TestAutonomous, 'main document');
}, 'Upgrade into autonomous custom element when inserted via innerHTML');

test(t => {
  const registry = new CustomElementRegistry;
  const shadow = attachShadowForTest(t, registry);
  shadow.innerHTML = '<test-element></test-element>';

  const shadow2 = attachShadowForTest(t);
  shadow2.innerHTML = '<test-element></test-element>';

  const shadow3 = attachShadowForTest(t, new CustomElementRegistry);
  shadow3.innerHTML = '<test-element></test-element>';

  t.add_cleanup(() => testdiv.firstChild.remove());
  testdiv.innerHTML = '<test-element></test-element>';

  registry.define('test-element', TestAutonomous);

  // Elements in the target tree scope should be upgraded.
  assert_true(shadow.firstChild instanceof TestAutonomous, 'target tree scope');

  // Verify that it doesn't pollute other tree scopes.
  assert_false(shadow2.firstChild instanceof TestAutonomous, 'tree scope without registry');
  assert_false(shadow3.firstChild instanceof TestAutonomous, 'tree scope with different registry');
  assert_false(testdiv.firstChild instanceof TestAutonomous, 'main document');
}, 'Upgrade into autonomous custom element when definition is added');

test(t => {
  const registry = new CustomElementRegistry;
  registry.define('test-element', TestCustomizedBuiltIn, {extends: 'p'});

  const shadow = attachShadowForTest(t, registry);
  shadow.innerHTML = '<p is="test-element"></p>';
  assert_true(shadow.firstChild instanceof TestCustomizedBuiltIn, 'target tree scope');

  // Verify that it doesn't pollute other tree scopes.
  const shadow2 = attachShadowForTest(t);
  shadow2.innerHTML = '<p is="test-element"></p>';
  assert_false(shadow2.firstChild instanceof TestCustomizedBuiltIn, 'tree scope without registry');

  const shadow3 = attachShadowForTest(t, new CustomElementRegistry);
  shadow3.innerHTML = '<p is="test-element"></p>';
  assert_false(shadow3.firstChild instanceof TestCustomizedBuiltIn, 'tree scope with different registry');

  t.add_cleanup(() => testdiv.firstChild.remove());
  testdiv.innerHTML = '<p is="test-element"></p>';
  assert_false(testdiv.firstChild instanceof TestCustomizedBuiltIn, 'main document');
}, 'Upgrade into customized built-in element when inserted via innerHTML');

test(t => {
  const registry = new CustomElementRegistry;
  const shadow = attachShadowForTest(t, registry);
  shadow.innerHTML = '<p is="test-element"></p>';

  const shadow2 = attachShadowForTest(t);
  shadow2.innerHTML = '<p is="test-element"></p>';

  const shadow3 = attachShadowForTest(t, new CustomElementRegistry);
  shadow3.innerHTML = '<p is="test-element"></p>';

  t.add_cleanup(() => testdiv.firstChild.remove());
  testdiv.innerHTML = '<p is="test-element"></p>';

  registry.define('test-element', TestCustomizedBuiltIn, {extends: 'p'});

  // Elements in the target tree scope should be upgraded.
  assert_true(shadow.firstChild instanceof TestCustomizedBuiltIn, 'target tree scope');

  // Verify that it doesn't pollute other tree scopes.
  assert_false(shadow2.firstChild instanceof TestCustomizedBuiltIn, 'tree scope without registry');
  assert_false(shadow3.firstChild instanceof TestCustomizedBuiltIn, 'tree scope with different registry');
  assert_false(testdiv.firstChild instanceof TestCustomizedBuiltIn, 'main document');
}, 'Upgrade into customized built-in element when definition is added');
</script>