Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / dom / custom / registration-context-isolation.html
blobd82bb8fcb275a1cb27f1e5418c42907567708410
1 <!DOCTYPE html>
2 <script src="../../../resources/testharness.js"></script>
3 <script src="../../../resources/testharnessreport.js"></script>
4 <script src="test-harness-utils.js"></script>
5 <body>
6 <script>
7 function TestRegistrationContextIsolation(windowA, documentA,
8 windowB, documentB) {
9 this.windowA = windowA;
10 this.documentA = documentA;
11 this.windowB = windowB;
12 this.documentB = documentB;
15 TestRegistrationContextIsolation.prototype.
16 testRegistrationContextIsNotShared = function () {
17 this.testRegisterInAInstantiateInB_shouldNotActivateDefinition();
18 this.testRegisterSameName_definitionsShouldNotConflict();
19 this.testRegisterSameName_lazyWrappingShouldNotSharePrototypes();
22 TestRegistrationContextIsolation.prototype.
23 testRegisterInAInstantiateInB_shouldNotActivateDefinition = function () {
25 // Test that element x-u registered in document A is not activated when
26 // x-u is parsed in document B
28 var protoU = Object.create(this.windowA.HTMLElement.prototype);
29 protoU.createdCallback = function () {
30 assert_unreached('creating an x-u in a different context should ' +
31 'not invoke a callback in this context');
33 this.documentA.registerElement('x-u', {prototype: protoU});
35 var container = this.documentB.createElement('div');
36 container.innerHTML = '<x-u></x-u>';
37 // if protoU.createdCallback is not invoked; this passed
40 TestRegistrationContextIsolation.prototype.
41 testRegisterSameName_definitionsShouldNotConflict = function () {
42 // Test that registering two different custom elements with the same
43 // tag name in each document doesn't lead to any crossed wires
45 var invocations = [];
46 function created(name) {
47 return function () {
48 invocations.push('created ' + name + ' in ' + this.dataset.doc);
52 var protoAV = Object.create(this.windowA.HTMLElement.prototype);
53 protoAV.createdCallback = created('document A\'s element V');
54 this.documentA.registerElement('x-v', {prototype: protoAV});
56 var protoBV = Object.create(this.windowB.HTMLElement.prototype);
57 protoBV.createdCallback = created('document B\'s element V');
58 this.documentB.registerElement('x-v', {prototype: protoBV});
60 var containerB = this.documentB.createElement('div');
61 containerB.innerHTML = '<x-v data-doc="document B"></x-v>';
62 var containerA = this.documentA.createElement('div');
63 containerA.innerHTML = '<x-v data-doc="document A"></x-v>';
65 assert_array_equals(
66 invocations,
67 ['created document B\'s element V in document B',
68 'created document A\'s element V in document A'],
69 'should have invoked the created callbacks in reverse creation order');
71 assert_equals(
72 Object.getPrototypeOf(containerA.firstChild),
73 protoAV,
74 'the prototype of element V in document A should be the prototype ' +
75 'registered in document A');
77 assert_equals(
78 Object.getPrototypeOf(containerB.firstChild),
79 protoBV,
80 'the prototype of element V in document B should be the prototype ' +
81 'registered in document B');
84 TestRegistrationContextIsolation.prototype.
85 testRegisterSameName_lazyWrappingShouldNotSharePrototypes = function () {
86 // Registering two different custom elements with the same tag
87 // name should not mix up prototypes. These do not have any
88 // callbacks, to try to tickle lazy wrapping.
90 var protoAW = Object.create(this.windowA.HTMLElement.prototype);
91 this.documentA.registerElement('x-w', {prototype: protoAW});
93 var protoBW = Object.create(this.windowB.HTMLElement.prototype);
94 protoBW.createdCallback = function () {};
95 this.documentB.registerElement('x-w', {prototype: protoBW});
97 var elementA = this.documentA.createElement('x-w');
98 var elementB = this.documentB.createElement('x-w');
100 assert_equals(
101 Object.getPrototypeOf(elementB), protoBW,
102 'the prototype of element W in document B should be the prototype ' +
103 'registered in document B');
105 assert_equals(
106 Object.getPrototypeOf(elementA), protoAW,
107 'the prototype of element W in document A should be the prototype ' +
108 'registered in document A');
111 (function () {
113 var t = async_test('registration context should not be shared with an ' +
114 'iframe\'s document');
116 withFrame(t.step_func(function (frameA) {
117 withFrame(t.step_func(function (frameB) {
118 var documentA = frameA.contentDocument;
119 var documentB = frameB.contentDocument;
120 var tester = new TestRegistrationContextIsolation(
121 frameA.contentWindow, frameA.contentDocument,
122 frameB.contentWindow, frameB.contentDocument);
123 tester.testRegistrationContextIsNotShared();
124 frameA.remove();
125 frameB.remove();
126 t.done();
127 }));
128 }));
130 })();
132 (function () {
134 var t = async_test('registration context should not be shared with the ' +
135 'template document');
137 withFrame(t.step_func(function (frame) {
138 var documentA = frame.contentDocument;
139 documentA.body.innerHTML = '<template>foo</template>';
140 var documentB = documentA.body.firstChild.content.ownerDocument;
141 var tester = new TestRegistrationContextIsolation(
142 frame.contentWindow, documentA,
143 frame.contentWindow, documentB);
144 tester.testRegistrationContextIsNotShared();
145 frame.remove();
146 t.done();
147 }));
149 })();
151 (function () {
153 var t = async_test('registration context should not be created by ' +
154 'DOMImplementation-created documents');
156 withFrame(t.step_func(function (frame) {
157 // Test transitively sloughing off a registration context through
158 // multiple createDocument/createHTMLDocument steps.
160 var documentA = frame.contentDocument;
162 // This document is not HTML, XHTML; it will not process custom elements.
163 var documentB = documentA.implementation.createDocument(null, '');
164 assert_throws(
165 'NOT_SUPPORTED_ERR',
166 function() { documentB.registerElement('x-a'); });
168 // This document will not process custom elements because there is
169 // nothing to inherit from B.
170 var documentC = documentB.implementation.createHTMLDocument();
171 assert_throws(
172 'NOT_SUPPORTED_ERR',
173 function() { documentC.registerElement('x-b'); });
175 // Nor this one.
176 var documentD = documentC.implementation.createDocument(
177 'http://www.w3.org/1999/xhtml', 'html');
178 assert_throws(
179 'NOT_SUPPORTED_ERR',
180 function() { documentD.registerElement('x-c'); });
182 frame.remove();
183 t.done();
184 }));
186 })();
188 </script>