OLD | NEW |
(Empty) | |
| 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 el
ement is adopted into a new document"> |
| 7 <link rel="help" href="https://w3c.github.io/webcomponents/spec/custom/#dfn-conn
ected-callback"> |
| 8 <script src="/resources/testharness.js"></script> |
| 9 <script src="/resources/testharnessreport.js"></script> |
| 10 <script src="./resources/document-types.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.pus
h(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 invo
ke adoptedCallback'); |
| 30 |
| 31 DocumentTypes.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 a ' + 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 a ' + 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 a ' + 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 a ' +
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 a ' + 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 a ' + 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
a ' + documentName + ' must enqueue and invoke adoptedCallback'); |
| 119 |
| 120 promise_test(function () { |
| 121 return getDocument().then(function (doc) { |
| 122 var instance = document.createElement('my-custom-element'); |
| 123 var host = doc.createElementNS('http://www.w3.org/1999/xhtml', 'div'
); |
| 124 var shadowRoot = host.attachShadow({mode: 'closed'}); |
| 125 |
| 126 calls = []; |
| 127 shadowRoot.appendChild(instance); |
| 128 assert_array_equals(calls, ['adopted', document, doc]); |
| 129 }); |
| 130 }, 'Inserting a custom element into a detached shadow tree that belongs to a
' + documentName + ' must enqueue and invoke adoptedCallback'); |
| 131 }); |
| 132 |
| 133 </script> |
| 134 </body> |
| 135 </html> |
OLD | NEW |