OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <script src="../../../resources/testharness.js"></script> |
| 3 <script src="../../../resources/testharnessreport.js"></script> |
| 4 <body> |
| 5 <script> |
| 6 // Creates an iframe and calls f after the iframe's onload event. |
| 7 function withIframe(f, opt_document) { |
| 8 var doc = opt_document || document; |
| 9 var iframe = doc.createElement('iframe'); |
| 10 iframe.src = 'data:text/html,'; |
| 11 iframe.onload = function () { f(iframe); }; |
| 12 doc.body.appendChild(iframe); |
| 13 } |
| 14 |
| 15 function testRegistrationContextIsNotShared(contentWindowA, documentA, |
| 16 contentWindowB, documentB) { |
| 17 // Test that element x-u registered in document A is not activated when |
| 18 // x-u is parsed in document B |
| 19 |
| 20 var protoU = Object.create(contentWindowA.HTMLElement.prototype); |
| 21 protoU.createdCallback = function () { |
| 22 assert_unreached('creating an x-u in a different context should ' + |
| 23 'not invoke a callback in this context'); |
| 24 }; |
| 25 documentA.register('x-u', {prototype: protoU}); |
| 26 documentB.body.innerHTML = '<x-u></x-u>'; |
| 27 // if protoU.createdCallback is not invoked; this passed |
| 28 |
| 29 // Test that registering two different custom elements with the same |
| 30 // tag name in each document doesn't lead to any crossed wires |
| 31 |
| 32 var invocations = []; |
| 33 function created(name) { |
| 34 return function () { |
| 35 invocations.push('created ' + name + ' in ' + this.dataset.doc); |
| 36 }; |
| 37 } |
| 38 |
| 39 var protoAV = Object.create(contentWindowA.HTMLElement.prototype); |
| 40 protoAV.createdCallback = created('document A\'s element V'); |
| 41 documentA.register('x-v', {prototype: protoAV}); |
| 42 |
| 43 var protoBV = Object.create(contentWindowB.HTMLElement.prototype); |
| 44 protoBV.createdCallback = created('document B\'s element V'); |
| 45 documentB.register('x-v', {prototype: protoBV}); |
| 46 |
| 47 documentB.body.innerHTML = '<x-v data-doc="document B"></x-v>'; |
| 48 var div = documentA.createElement('div'); |
| 49 div.innerHTML = '<x-v data-doc="document A"></x-v>'; |
| 50 |
| 51 assert_array_equals( |
| 52 invocations, |
| 53 ['created document B\'s element V in document B', |
| 54 'created document A\'s element V in document A'], |
| 55 'should have invoked the created callbacks in reverse creation order'); |
| 56 |
| 57 assert_equals( |
| 58 Object.getPrototypeOf(div.firstChild), |
| 59 protoAV, |
| 60 'the prototype of element V in document A should be the prototype ' + |
| 61 'registered in document A'); |
| 62 |
| 63 assert_equals( |
| 64 Object.getPrototypeOf(documentB.body.firstChild), |
| 65 protoBV, |
| 66 'the prototype of element V in document B should be the prototype ' + |
| 67 'registered in document B'); |
| 68 |
| 69 // Similarly, test that registering two different custom elements with |
| 70 // the same tag name doesn't mix up prototypes. These do not have |
| 71 // any callbacks, to try to tickle lazy wrapping. |
| 72 |
| 73 var protoAW = Object.create(contentWindowA.HTMLElement.prototype); |
| 74 documentA.register('x-w', {prototype: protoAW}); |
| 75 |
| 76 var protoBW = Object.create(contentWindowB.HTMLElement.prototype); |
| 77 documentB.register('x-w', {prototype: protoBW}); |
| 78 |
| 79 var elementA = documentA.createElement('x-w'); |
| 80 var elementB = documentB.createElement('x-w'); |
| 81 |
| 82 assert_equals( |
| 83 Object.getPrototypeOf(elementB), protoBW, |
| 84 'the prototype of element W in document B should be the prototype ' + |
| 85 'registered in document B'); |
| 86 |
| 87 assert_equals( |
| 88 Object.getPrototypeOf(elementA), protoAW, |
| 89 'the prototype of element W in document A should be the prototype ' + |
| 90 'registered in document A'); |
| 91 } |
| 92 |
| 93 (function () { |
| 94 |
| 95 var t = async_test('registration context should not be shared with an ' + |
| 96 'iframe\'s document'); |
| 97 |
| 98 withIframe(t.step_func(function (frameA) { |
| 99 withIframe(t.step_func(function (frameB) { |
| 100 var documentA = frameA.contentDocument; |
| 101 var documentB = frameB.contentDocument; |
| 102 testRegistrationContextIsNotShared( |
| 103 frameA.contentWindow, frameA.contentDocument, |
| 104 frameB.contentWindow, frameB.contentDocument); |
| 105 frameA.remove(); |
| 106 frameB.remove(); |
| 107 t.done(); |
| 108 }), frameA.contentDocument); |
| 109 })); |
| 110 |
| 111 })(); |
| 112 |
| 113 function testSharedRegistrationContext(contentWindow, documentA, documentB) { |
| 114 var documentAUpgradeCandidate = documentA.createElement('x-u'); |
| 115 documentAUpgradeCandidate.dataset.name = 'document A upgrade candidate'; |
| 116 |
| 117 var documentBUpgradeCandidate = documentB.createElement('x-u'); |
| 118 documentBUpgradeCandidate.dataset.name = 'document B upgrade candidate'; |
| 119 |
| 120 var invocations = []; |
| 121 function created() { |
| 122 invocations.push('created ' + this.dataset.name + ' with prototype ' + |
| 123 'tagged ' + this.prototypeTag); |
| 124 } |
| 125 |
| 126 var protoU = Object.create(contentWindow.HTMLElement.prototype); |
| 127 protoU.prototypeTag = 'U'; |
| 128 protoU.createdCallback = created; |
| 129 documentB.register('x-u', {prototype: protoU}); |
| 130 |
| 131 assert_array_equals( |
| 132 invocations, |
| 133 ['created document B upgrade candidate with prototype tagged U', |
| 134 'created document A upgrade candidate with prototype tagged U'], |
| 135 'the created callback should have been called for both elements' + |
| 136 'in reverse creation order'); |
| 137 |
| 138 var protoV = Object.create(contentWindow.HTMLElement.prototype); |
| 139 protoV.prototypeTag = 'V'; |
| 140 protoV.createdCallback = created; |
| 141 documentA.register('x-v', {prototype: protoV}); |
| 142 |
| 143 invocations = []; |
| 144 var div = documentB.createElement('div'); |
| 145 div.innerHTML = '<x-v data-name="document B element V"></x-v>'; |
| 146 assert_array_equals( |
| 147 invocations, |
| 148 ['created document B element V with prototype tagged V'], |
| 149 'the created callback should have been called for the x-v element'); |
| 150 } |
| 151 |
| 152 (function () { |
| 153 |
| 154 var t = async_test('registration context is shared with ' + |
| 155 'DOMImplementation.createDocument'); |
| 156 |
| 157 withIframe(t.step_func(function (frame) { |
| 158 var documentA = frame.contentDocument; |
| 159 var documentB = documentA.implementation.createHTMLDocument(); |
| 160 testSharedRegistrationContext(frame.contentWindow, documentA, documentB); |
| 161 frame.remove(); |
| 162 t.done(); |
| 163 })); |
| 164 |
| 165 })(); |
| 166 |
| 167 (function () { |
| 168 |
| 169 var t = async_test('registration context is shared with the template document');
|
| 170 |
| 171 withIframe(t.step_func(function (frame) { |
| 172 var documentA = frame.contentDocument; |
| 173 documentA.body.innerHTML = '<template>foo</template>'; |
| 174 var documentB = documentA.body.firstChild.content.ownerDocument; |
| 175 testSharedRegistrationContext(frame.contentWindow, documentA, documentB); |
| 176 frame.remove(); |
| 177 t.done(); |
| 178 })); |
| 179 |
| 180 })(); |
| 181 |
| 182 </script> |
OLD | NEW |