template-content-hierarcy.html (3641B)
1 <!DOCTYPE html> 2 <meta name="author" title="Takayoshi Kochi" href="mailto:kochi@chromium.org"> 3 <script src="/resources/testharness.js"></script> 4 <script src="/resources/testharnessreport.js"></script> 5 <div id=log></div> 6 <div id="parent"> 7 <template id="tmpl"><span>Happy Templating!</span></template> 8 </div> 9 <script> 10 test(() => { 11 var parent = document.getElementById('parent'); 12 var tmpl = document.getElementById('tmpl'); 13 14 assert_equals(tmpl.innerHTML, '<span>Happy Templating!</span>'); 15 var span = tmpl.content.querySelector('span'); 16 17 // Hierarchy checks at various combinations. 18 assert_throws_dom('HierarchyRequestError', () => { 19 tmpl.content.appendChild(parent); 20 }, 'Template content should throw if any of ancestor is being appended.'); 21 assert_throws_dom('HierarchyRequestError', () => { 22 tmpl.content.appendChild(tmpl); 23 }, 'Template content should throw if its host is being appended.'); 24 assert_throws_dom('HierarchyRequestError', () => { 25 span.appendChild(parent); 26 }, 'Template content child should throw if any of ancestor is being appended.'); 27 assert_throws_dom('HierarchyRequestError', () => { 28 span.appendChild(tmpl); 29 }, 'Template content child should throw template\'s host is being appended.'); 30 }, "Template content should throw when its ancestor is being appended."); 31 32 test(() => { 33 var parent = document.getElementById('parent'); 34 var tmpl = document.getElementById('tmpl'); 35 36 assert_equals(tmpl.innerHTML, '<span>Happy Templating!</span>'); 37 var span = tmpl.content.querySelector('span'); 38 39 var tmpl_doc = tmpl.content.ownerDocument; 40 assert_equals(tmpl.ownerDocument, document); 41 assert_not_equals(tmpl_doc, document); 42 43 var new_doc = document.implementation.createHTMLDocument(); 44 assert_not_equals(new_doc, document); 45 assert_not_equals(new_doc, tmpl_doc); 46 47 // Try moving tmpl.content to new_doc and check the results. 48 const tmplContentNodeDocument = tmpl.content.ownerDocument; 49 const tmplContentAdoptResult = new_doc.adoptNode(tmpl.content); 50 assert_equals(tmpl.content, tmplContentAdoptResult); 51 assert_equals(tmpl.ownerDocument, document); 52 assert_equals(tmpl.content.ownerDocument, tmplContentNodeDocument); 53 54 // Hierarchy checks at various combinations. 55 assert_throws_dom('HierarchyRequestError', () => { 56 tmpl.content.appendChild(parent); 57 }, 'Template content should throw if any of ancestor is being appended.'); 58 assert_throws_dom('HierarchyRequestError', () => { 59 tmpl.content.appendChild(tmpl); 60 }, 'Template content should throw if its host is being appended.'); 61 assert_throws_dom('HierarchyRequestError', () => { 62 span.appendChild(parent); 63 }, 'Template content child should throw if any of ancestor is being appended.'); 64 assert_throws_dom('HierarchyRequestError', () => { 65 span.appendChild(tmpl); 66 }, 'Template content child should throw template\'s host is being appended.'); 67 68 // Sanity check: template.content before and after move. 69 var tmpl_content_reference = tmpl.content; 70 assert_equals(tmpl.content.firstChild, span, 71 '<span> should be kept until it is removed, even after ' + 72 'adopted to another document.'); 73 new_doc.body.appendChild(tmpl.content); 74 assert_equals(tmpl.content.firstChild, null, 75 '<span> should be removed from template content.'); 76 assert_equals(tmpl_content_reference, tmpl.content, 77 'template.content should be identical before and after ' + 78 'moving its children.'); 79 }, 'Template content should throw exception when its ancestor in ' + 80 'a different document but connected via host is being append.'); 81 </script>