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