adopted-callback.html (7678B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>Custom Elements: adoptedCallback</title> 5 <meta name="author" title="Ryosuke Niwa" href="mailto:rniwa@webkit.org"> 6 <meta name="assert" content="adoptedCallback must be enqueued whenever custom element is adopted into a new document"> 7 <link rel="help" href="https://w3c.github.io/webcomponents/spec/custom/#dfn-connected-callback"> 8 <script src="/resources/testharness.js"></script> 9 <script src="/resources/testharnessreport.js"></script> 10 <script src="./resources/custom-elements-helpers.js"></script> 11 </head> 12 <body> 13 <div id="log"></div> 14 <script> 15 16 var calls = []; 17 class MyCustomElement extends HTMLElement { 18 connectedCallback() { calls.push('connected'); } 19 adoptedCallback(oldDocument, newDocument) { calls.push('adopted'); calls.push(oldDocument); calls.push(newDocument); } 20 disconnectedCallback() { calls.push('disconnected'); } 21 } 22 customElements.define('my-custom-element', MyCustomElement); 23 24 test(function () { 25 var instance = document.createElement('my-custom-element'); 26 calls = []; 27 document.body.appendChild(instance); 28 assert_array_equals(calls, ['connected']); 29 }, 'Inserting a custom element into the owner document must not enqueue and invoke adoptedCallback'); 30 31 document_types().forEach(function (entry) { 32 if (entry.isOwner) 33 return; 34 35 var documentName = entry.name; 36 var getDocument = entry.create; 37 38 promise_test(function () { 39 return getDocument().then(function (doc) { 40 var instance = document.createElement('my-custom-element'); 41 calls = []; 42 doc.documentElement.appendChild(instance); 43 assert_array_equals(calls, ['adopted', document, doc, 'connected']); 44 }); 45 }, 'Inserting a custom element into ' + documentName + ' must enqueue and invoke adoptedCallback'); 46 47 promise_test(function () { 48 return getDocument().then(function (doc) { 49 var instance = document.createElement('my-custom-element'); 50 document.body.appendChild(instance); 51 calls = []; 52 doc.documentElement.appendChild(instance); 53 assert_array_equals(calls, ['disconnected', 'adopted', document, doc, 'connected']); 54 }); 55 }, 'Moving a custom element from the owner document into ' + documentName + ' must enqueue and invoke adoptedCallback'); 56 57 promise_test(function () { 58 return getDocument().then(function (doc) { 59 var instance = document.createElement('my-custom-element'); 60 var parent = document.createElement('div'); 61 parent.appendChild(instance); 62 calls = []; 63 doc.documentElement.appendChild(parent); 64 assert_array_equals(calls, ['adopted', document, doc, 'connected']); 65 }); 66 }, 'Inserting an ancestor of custom element into ' + documentName + ' must enqueue and invoke adoptedCallback'); 67 68 promise_test(function () { 69 return getDocument().then(function (doc) { 70 var instance = document.createElement('my-custom-element'); 71 var parent = document.createElement('div'); 72 parent.appendChild(instance); 73 document.body.appendChild(parent); 74 calls = []; 75 doc.documentElement.appendChild(parent); 76 assert_array_equals(calls, ['disconnected', 'adopted', document, doc, 'connected']); 77 }); 78 }, 'Moving an ancestor of custom element from the owner document into ' + documentName + ' must enqueue and invoke adoptedCallback'); 79 80 promise_test(function () { 81 return getDocument().then(function (doc) { 82 var instance = document.createElement('my-custom-element'); 83 var host = doc.createElementNS('http://www.w3.org/1999/xhtml', 'div'); 84 var shadowRoot = host.attachShadow({mode: 'closed'}); 85 doc.documentElement.appendChild(host); 86 87 calls = []; 88 shadowRoot.appendChild(instance); 89 assert_array_equals(calls, ['adopted', document, doc, 'connected']); 90 }); 91 }, 'Inserting a custom element into a shadow tree in ' + documentName + ' must enqueue and invoke adoptedCallback'); 92 93 promise_test(function () { 94 return getDocument().then(function (doc) { 95 var instance = document.createElement('my-custom-element'); 96 var host = document.createElement('div'); 97 var shadowRoot = host.attachShadow({mode: 'closed'}); 98 shadowRoot.appendChild(instance); 99 100 calls = []; 101 doc.documentElement.appendChild(host); 102 assert_array_equals(calls, ['adopted', document, doc, 'connected']); 103 }); 104 }, 'Inserting the shadow host of a custom element into ' + documentName + ' must enqueue and invoke adoptedCallback'); 105 106 promise_test(function () { 107 return getDocument().then(function (doc) { 108 var instance = document.createElement('my-custom-element'); 109 var host = document.createElement('div'); 110 var shadowRoot = host.attachShadow({mode: 'closed'}); 111 shadowRoot.appendChild(instance); 112 document.body.appendChild(host); 113 114 calls = []; 115 doc.documentElement.appendChild(host); 116 assert_array_equals(calls, ['disconnected', 'adopted', document, doc, 'connected']); 117 }); 118 }, 'Moving the shadow host of a custom element from the owner document into ' + documentName + ' must enqueue and invoke adoptedCallback'); 119 120 promise_test(function () { 121 return getDocument().then(function (doc) { 122 const instance = document.createElement('my-custom-element'); 123 const host = document.createElement('div'); 124 const shadowRoot = host.attachShadow({mode: 'closed'}); 125 shadowRoot.appendChild(instance); 126 document.body.appendChild(host); 127 128 calls = []; 129 doc.documentElement.appendChild(shadowRoot); 130 assert_array_equals(calls, ['disconnected', 'adopted', document, doc, 'connected']); 131 }); 132 }, 'Moving the shadow host\'s shadow of a custom element from the owner document into ' + documentName + ' must enqueue and invoke adoptedCallback'); 133 134 promise_test(function () { 135 return getDocument().then(function (doc) { 136 const instance = document.createElement('my-custom-element'); 137 const template = document.createElement('template'); 138 const templateContent = template.content; 139 templateContent.appendChild(instance); 140 document.body.appendChild(template); 141 142 calls = []; 143 doc.documentElement.appendChild(templateContent); 144 if (doc === templateContent.ownerDocument) { 145 assert_array_equals(calls, ['connected']); 146 } else { 147 assert_array_equals(calls, ['adopted', templateContent.ownerDocument, doc, 'connected']); 148 } 149 }); 150 }, 'Moving the <template>\'s content of a custom element from the owner document into ' + documentName + ' must enqueue and invoke adoptedCallback'); 151 152 promise_test(function () { 153 return getDocument().then(function (doc) { 154 var instance = document.createElement('my-custom-element'); 155 var host = doc.createElementNS('http://www.w3.org/1999/xhtml', 'div'); 156 var shadowRoot = host.attachShadow({mode: 'closed'}); 157 158 calls = []; 159 shadowRoot.appendChild(instance); 160 assert_array_equals(calls, ['adopted', document, doc]); 161 }); 162 }, 'Inserting a custom element into a detached shadow tree that belongs to ' + documentName + ' must enqueue and invoke adoptedCallback'); 163 }); 164 165 </script> 166 </body> 167 </html>