4 <title>HTML Templates: When node's document changes its owner document should be changed</title>
5 <meta name="author" title="Sergey G. Grekhov" href="mailto:sgrekhov@unipro.ru">
6 <meta name="author" title="Aleksei Yu. Semenov" href="mailto:a.semenov@unipro.ru">
7 <meta name="assert" content="When a template element's node document changes, the template element's content DocumentFragment must be adopted into the new node document's template contents owner document">
8 <link rel="help" href="http://www.w3.org/TR/2013/WD-html-templates-20130214/#template-element">
9 <script src="../../../../resources/testharness.js"></script>
10 <script src="../../../../resources/testharnessreport.js"></script>
11 <script src='../testcommon.js'></script>
12 <link rel="stylesheet" href="../../../../resources/testharness.css">
16 <script type="text/javascript">
20 var doc1 = newHTMLDocument();
21 var template = doc1.createElement('template');
23 assert_equals(template.ownerDocument, doc1, 'Wrong template node owner document');
24 assert_equals(template.content.ownerDocument, doc1,
25 'Wrong template content owner document');
27 var doc2 = newHTMLDocument();
28 var template2 = doc2.createElement('template');
29 doc2.body.appendChild(template);
31 assert_equals(template.ownerDocument, template2.ownerDocument,
32 'Template node owner document should be changed');
33 assert_equals(template.content.ownerDocument, template2.content.ownerDocument,
34 'Template content owner document should be changed');
36 }, 'Changing of template element\'s node document. ' +
37 'Test that ownerDocument of an empty template and its content changes');
41 var doc1 = newHTMLDocument();
42 doc1.body.innerHTML = '<template id="tmpl"><div>Div content</div> And some more text</template>';
44 var template = doc1.querySelector('#tmpl');
46 assert_equals(template.ownerDocument, doc1,
47 'Wrong template node owner document');
48 assert_equals(template.content.ownerDocument, doc1,
49 'Wrong template content owner document');
51 var doc2 = newHTMLDocument();
52 var template2 = doc2.createElement('template');
53 doc2.body.appendChild(template);
55 assert_equals(template.ownerDocument, template2.ownerDocument,
56 'Template node owner document should be changed');
57 assert_equals(template.content.ownerDocument, template2.content.ownerDocument,
58 'Template content owner document should be changed');
60 assert_equals(template.content.querySelector('div').ownerDocument,
61 template2.content.ownerDocument,
62 'Template content descendants owner document should be changed');
64 }, 'Changing of template element\'s node document. ' +
65 'Test that ownerDocument of a not empty template and its content changes');
69 var doc1 = newHTMLDocument();
70 doc1.body.innerHTML = ''
71 + '<template id="tmpl"><div>Div content</div> And some more text'
72 + '<template id="tmpl2"><div>Template content</div></template>'
75 var template = doc1.querySelector('#tmpl');
77 assert_equals(template.ownerDocument, doc1, 'Wrong template node owner document');
78 assert_equals(template.content.ownerDocument, doc1,
79 'Wrong template content owner document');
81 var nestedTemplate = template.content.querySelector('#tmpl2');
83 assert_equals(nestedTemplate.ownerDocument, doc1,
84 'Wrong nested template node owner document');
85 assert_equals(nestedTemplate.content.ownerDocument, doc1,
86 'Wrong nested template content owner document');
88 var doc2 = newHTMLDocument();
89 var template2 = doc2.createElement('template');
90 doc2.body.appendChild(template);
92 assert_equals(template.ownerDocument, template2.ownerDocument,
93 'Template node owner document should be changed');
94 assert_equals(template.content.ownerDocument, template2.content.ownerDocument,
95 'Template content owner document should be changed');
96 assert_equals(template.content.querySelector('div').ownerDocument,
97 template2.content.ownerDocument,
98 'Template content descendants owner document should be changed');
100 assert_equals(nestedTemplate.ownerDocument,
101 template2.content.ownerDocument,
102 'Nested template node owner document should be changed');
103 assert_equals(nestedTemplate.content.ownerDocument,
104 template2.content.ownerDocument,
105 'Nested template content owner document should be changed');
106 assert_equals(nestedTemplate.content.querySelector('div').ownerDocument,
107 template2.content.ownerDocument,
108 'Owner document of the nested template content descendants should be changed');
110 }, 'Changing of template element\'s node document. ' +
111 'Test that ownerDocument of nested template and its content changes');
114 testInIFrame('../resources/template-contents.html', function(context) {
115 var doc1 = context.iframes[0].contentDocument;
117 var template = doc1.body.querySelector('template');
119 var doc2 = newHTMLDocument();
120 var template2 = doc2.createElement('template');
121 doc2.body.appendChild(template);
123 assert_equals(template.ownerDocument, template2.ownerDocument,
124 'Template node owner document should be changed');
125 assert_equals(template.content.ownerDocument,
126 template2.content.ownerDocument,
127 'Template content owner document should be changed');
128 assert_equals(template.content.querySelector('div').ownerDocument,
129 template2.content.ownerDocument,
130 'Template content descendants owner document should be changed');
132 }, 'Changing of template element\'s node document. ' +
133 'Test document loaded from a file');
136 testInIFrame('../resources/template-contents.html', function(context) {
137 var doc1 = context.iframes[0].contentDocument;
139 var doc2 = newHTMLDocument();
140 var template = doc2.createElement('template');
141 var div = doc2.createElement('div');
142 template.content.appendChild(div);
144 doc1.body.appendChild(template);
146 assert_not_equals(template.ownerDocument, doc2,
147 'Template node owner document should be changed');
148 assert_not_equals(template.content.ownerDocument, doc2,
149 'Template content owner document should be changed');
150 assert_not_equals(div.ownerDocument, doc2,
151 'Template content descendants owner document should be changed');
153 assert_equals(template.ownerDocument, doc1,
154 'Template node owner document should be changed');
155 // doc1 has browsing context so it cannot be template.content.ownerDocument
156 assert_not_equals(template.content.ownerDocument, doc1,
157 'Template content owner document should be a new document');
158 assert_equals(div.ownerDocument, template.content.ownerDocument,
159 'Template content descendants owner document should be ' +
160 'template content document owner');
162 }, 'Changing of template element\'s node document. ' +
163 'Adobt template element into a document that has a browsing context');
166 testInIFrame('../resources/template-contents.html', function(context) {
167 var doc1 = context.iframes[0].contentDocument;
169 var template = doc1.querySelector('template');
170 var div = template.content.querySelector('div');
171 var templateContentOwner = template.content.ownerDocument;
175 doc2.body.appendChild(template);
178 assert_not_equals(template.ownerDocument, doc1,
179 'Template node owner document should be changed');
180 assert_not_equals(template.content.ownerDocument, templateContentOwner,
181 'Template content owner document should be changed');
182 assert_not_equals(div.ownerDocument, templateContentOwner,
183 'Template content descendants owner document should be changed');
185 assert_equals(template.ownerDocument, doc2,
186 'Template node owner document should be changed');
187 // doc2 has browsing context, so it cannot be template.content.ownerDocument
188 assert_not_equals(template.content.ownerDocument, doc2,
189 'Template content owner document should be a new document');
190 assert_equals(div.ownerDocument, template.content.ownerDocument,
191 'Template content descendants owner document should be ' +
192 'template content document owner');
194 }, 'Changing of template element\'s node document. ' +
195 'Test the case when both old and new owner documents of template element ' +
196 'have browsing context');