connected-callbacks.html (3752B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>Custom Elements: connectedCallback</title> 5 <meta name="author" title="Ryosuke Niwa" href="mailto:rniwa@webkit.org"> 6 <meta name="assert" content="connectedCallback must be enqueued whenever custom element is inserted into a 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', this); } 19 disconnectedCallback() { calls.push('disconnected', this); } 20 } 21 customElements.define('my-custom-element', MyCustomElement); 22 23 document_types().forEach(function (entry) { 24 var documentName = entry.name; 25 var getDocument = entry.create; 26 27 promise_test(function () { 28 return getDocument().then(function (doc) { 29 var instance = document.createElement('my-custom-element'); 30 calls = []; 31 doc.documentElement.appendChild(instance); 32 assert_array_equals(calls, ['connected', instance]); 33 }); 34 }, 'Inserting a custom element into ' + documentName + ' must enqueue and invoke connectedCallback'); 35 36 promise_test(function () { 37 return getDocument().then(function (doc) { 38 var instance = document.createElement('my-custom-element'); 39 var parent = document.createElement('div'); 40 parent.appendChild(instance); 41 calls = []; 42 doc.documentElement.appendChild(parent); 43 assert_array_equals(calls, ['connected', instance]); 44 }); 45 }, 'Inserting an ancestor of custom element into ' + documentName + ' must enqueue and invoke connectedCallback'); 46 47 promise_test(function () { 48 return getDocument().then(function (doc) { 49 var instance = document.createElement('my-custom-element'); 50 var host = doc.createElementNS('http://www.w3.org/1999/xhtml', 'div'); 51 var shadowRoot = host.attachShadow({mode: 'closed'}); 52 doc.documentElement.appendChild(host); 53 54 calls = []; 55 shadowRoot.appendChild(instance); 56 assert_array_equals(calls, ['connected', instance]); 57 }); 58 }, 'Inserting a custom element into a shadow tree in ' + documentName + ' must enqueue and invoke connectedCallback'); 59 60 promise_test(function () { 61 return getDocument().then(function (doc) { 62 var instance = document.createElement('my-custom-element'); 63 var host = doc.createElementNS('http://www.w3.org/1999/xhtml', 'div'); 64 var shadowRoot = host.attachShadow({mode: 'closed'}); 65 shadowRoot.appendChild(instance); 66 67 calls = []; 68 doc.documentElement.appendChild(host); 69 assert_array_equals(calls, ['connected', instance]); 70 }); 71 }, 'Inserting the shadow host of a custom element into ' + documentName + ' must enqueue and invoke connectedCallback'); 72 73 promise_test(function () { 74 return getDocument().then(function (doc) { 75 var instance = document.createElement('my-custom-element'); 76 var host = doc.createElementNS('http://www.w3.org/1999/xhtml', 'div'); 77 var shadowRoot = host.attachShadow({mode: 'closed'}); 78 79 calls = []; 80 shadowRoot.appendChild(instance); 81 assert_array_equals(calls, []); 82 }); 83 }, 'Inserting a custom element into a detached shadow tree that belongs to ' + documentName + ' must not enqueue and invoke connectedCallback'); 84 }); 85 86 </script> 87 </body> 88 </html>