node-document-changes.html (8655B)
1 <!DOCTYPE html> 2 <html> 3 <head> 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='/html/resources/common.js'></script> 12 </head> 13 <body> 14 <div id="log"></div> 15 <script type="text/javascript"> 16 17 test(function() { 18 var doc1 = newHTMLDocument(); 19 var template = doc1.createElement('template'); 20 21 assert_equals(template.ownerDocument, doc1, 'Wrong template node owner document'); 22 assert_not_equals(template.content.ownerDocument, doc1, 23 'Wrong template content owner document'); 24 25 var doc2 = newHTMLDocument(); 26 var template2 = doc2.createElement('template'); 27 doc2.body.appendChild(template); 28 29 assert_equals(template.ownerDocument, template2.ownerDocument, 30 'Template node owner document should be changed'); 31 assert_equals(template.content.ownerDocument, template2.content.ownerDocument, 32 'Template content owner document should be changed'); 33 34 }, 'Changing of template element\'s node document. ' + 35 'Test that ownerDocument of an empty template and its content changes'); 36 37 test(function() { 38 var doc1 = newHTMLDocument(); 39 doc1.body.innerHTML = '<template id="tmpl"><div>Div content</div> And some more text</template>'; 40 41 var template = doc1.querySelector('#tmpl'); 42 43 assert_equals(template.ownerDocument, doc1, 44 'Wrong template node owner document'); 45 assert_not_equals(template.content.ownerDocument, doc1, 46 'Wrong template content owner document'); 47 48 var doc2 = newHTMLDocument(); 49 var template2 = doc2.createElement('template'); 50 doc2.body.appendChild(template); 51 52 assert_equals(template.ownerDocument, template2.ownerDocument, 53 'Template node owner document should be changed'); 54 assert_equals(template.content.ownerDocument, template2.content.ownerDocument, 55 'Template content owner document should be changed'); 56 57 assert_equals(template.content.querySelector('div').ownerDocument, 58 template2.content.ownerDocument, 59 'Template content descendants owner document should be changed'); 60 61 }, 'Changing of template element\'s node document. ' + 62 'Test that ownerDocument of a not empty template and its content changes'); 63 64 test(function() { 65 var doc1 = newHTMLDocument(); 66 doc1.body.innerHTML = '' 67 + '<template id="tmpl"><div>Div content</div> And some more text' 68 + '<template id="tmpl2"><div>Template content</div></template>' 69 + '</template>'; 70 71 var template = doc1.querySelector('#tmpl'); 72 73 assert_equals(template.ownerDocument, doc1, 'Wrong template node owner document'); 74 assert_not_equals(template.content.ownerDocument, doc1, 75 'Wrong template content owner document'); 76 77 var nestedTemplate = template.content.querySelector('#tmpl2'); 78 79 assert_equals(nestedTemplate.ownerDocument, template.content.ownerDocument, 80 'Wrong nested template node owner document'); 81 assert_equals(nestedTemplate.content.ownerDocument, template.content.ownerDocument, 82 'Wrong nested template content owner document'); 83 84 var doc2 = newHTMLDocument(); 85 var template2 = doc2.createElement('template'); 86 doc2.body.appendChild(template); 87 88 assert_equals(template.ownerDocument, template2.ownerDocument, 89 'Template node owner document should be changed'); 90 assert_equals(template.content.ownerDocument, template2.content.ownerDocument, 91 'Template content owner document should be changed'); 92 assert_equals(template.content.querySelector('div').ownerDocument, 93 template2.content.ownerDocument, 94 'Template content descendants owner document should be changed'); 95 96 assert_equals(nestedTemplate.ownerDocument, 97 template2.content.ownerDocument, 98 'Nested template node owner document should be changed'); 99 assert_equals(nestedTemplate.content.ownerDocument, 100 template2.content.ownerDocument, 101 'Nested template content owner document should be changed'); 102 assert_equals(nestedTemplate.content.querySelector('div').ownerDocument, 103 template2.content.ownerDocument, 104 'Owner document of the nested template content descendants should be changed'); 105 106 }, 'Changing of template element\'s node document. ' + 107 'Test that ownerDocument of nested template and its content changes'); 108 109 testInIFrame('../resources/template-contents.html', function(context) { 110 var doc1 = context.iframes[0].contentDocument; 111 112 var template = doc1.body.querySelector('template'); 113 114 var doc2 = newHTMLDocument(); 115 var template2 = doc2.createElement('template'); 116 doc2.body.appendChild(template); 117 118 assert_equals(template.ownerDocument, template2.ownerDocument, 119 'Template node owner document should be changed'); 120 assert_equals(template.content.ownerDocument, 121 template2.content.ownerDocument, 122 'Template content owner document should be changed'); 123 assert_equals(template.content.querySelector('div').ownerDocument, 124 template2.content.ownerDocument, 125 'Template content descendants owner document should be changed'); 126 127 }, 'Changing of template element\'s node document. ' + 128 'Test document loaded from a file'); 129 130 testInIFrame('../resources/template-contents.html', function(context) { 131 var doc1 = context.iframes[0].contentDocument; 132 133 var doc2 = newHTMLDocument(); 134 var template = doc2.createElement('template'); 135 var div = doc2.createElement('div'); 136 template.content.appendChild(div); 137 138 doc1.body.appendChild(template); 139 140 assert_not_equals(template.ownerDocument, doc2, 141 'Template node owner document should be changed'); 142 assert_not_equals(template.content.ownerDocument, doc2, 143 'Template content owner document should be changed'); 144 assert_not_equals(div.ownerDocument, doc2, 145 'Template content descendants owner document should be changed'); 146 147 assert_equals(template.ownerDocument, doc1, 148 'Template node owner document should be changed'); 149 // doc1 has browsing context so it cannot be template.content.ownerDocument 150 assert_not_equals(template.content.ownerDocument, doc1, 151 'Template content owner document should be a new document'); 152 assert_equals(div.ownerDocument, template.content.ownerDocument, 153 'Template content descendants owner document should be ' + 154 'template content document owner'); 155 156 }, 'Changing of template element\'s node document. ' + 157 'Adobt template element into a document that has a browsing context'); 158 159 testInIFrame('../resources/template-contents.html', function(context) { 160 var doc1 = context.iframes[0].contentDocument; 161 162 var template = doc1.querySelector('template'); 163 var div = template.content.querySelector('div'); 164 var templateContentOwner = template.content.ownerDocument; 165 166 var doc2 = document; 167 168 doc2.body.appendChild(template); 169 170 assert_not_equals(template.ownerDocument, doc1, 171 'Template node owner document should be changed'); 172 assert_not_equals(template.content.ownerDocument, templateContentOwner, 173 'Template content owner document should be changed'); 174 assert_not_equals(div.ownerDocument, templateContentOwner, 175 'Template content descendants owner document should be changed'); 176 177 assert_equals(template.ownerDocument, doc2, 178 'Template node owner document should be changed'); 179 // doc2 has browsing context, so it cannot be template.content.ownerDocument 180 assert_not_equals(template.content.ownerDocument, doc2, 181 'Template content owner document should be a new document'); 182 assert_equals(div.ownerDocument, template.content.ownerDocument, 183 'Template content descendants owner document should be ' + 184 'template content document owner'); 185 186 }, 'Changing of template element\'s node document. ' + 187 'Test the case when both old and new owner documents of template element ' + 188 'have browsing context'); 189 190 </script> 191 </body> 192 </html>