templates-copy-document-owner.html (4653B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>HTML Templates: ownerDocument of cloned template content is set to template content owner</title> 5 <meta name="author" title="Sergey G. Grekhov" href="mailto:sgrekhov@unipro.ru"> 6 <meta name="author" title="Aleksei Yu. Semenov" href="a.semenov@unipro.ru"> 7 <meta name="assert" content="ownerDocument of cloned template content is set to template content owner"> 8 <link rel="help" href="http://www.w3.org/TR/2013/WD-html-templates-20130214/#node-clone-additions"> 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 function checkOwnerDocument(node, doc) { 18 if ((node !== null) && (node !== undefined)) { 19 assert_equals(node.ownerDocument, doc, 20 'Wrong ownerDocument of the template copy\'s node ' + node.nodeName); 21 for (var i = 0; i < node.childNodes.length; i++) { 22 if (node.childNodes[i].nodeType === Node.ELEMENT_NODE) { 23 checkOwnerDocument(node.childNodes[i], doc); 24 if (node.childNodes[i].nodeName === 'TEMPLATE') { 25 checkOwnerDocument(node.childNodes[i].content, doc); 26 } 27 } 28 } 29 } 30 } 31 32 33 test(function () { 34 var doc = newHTMLDocument(); 35 doc.body.innerHTML = '<template id="tmpl1">' + 36 '<div id="div1">This is div inside template</div>' + 37 '<div id="div2">This is another div inside template</div>' + 38 '</template>'; 39 40 var template = doc.querySelector('#tmpl1'); 41 var copy = template.cloneNode(true); 42 43 assert_equals(copy.content.childNodes.length, 2, 44 'Wrong number of template content\'s copy child nodes'); 45 checkOwnerDocument(copy.content, template.content.ownerDocument); 46 47 }, 'ownerDocument of cloned template content is set to template content owner. ' 48 + 'Test cloning with children'); 49 50 51 52 test(function () { 53 var doc = newHTMLDocument(); 54 doc.body.innerHTML = '<template id="tmpl1">' + 55 '<div id="div1">This is div inside template</div>' + 56 '<div id="div2">This is another div inside template</div>' + 57 '</template>'; 58 59 var template = doc.querySelector('#tmpl1'); 60 var copy = template.cloneNode(false); 61 62 assert_equals(copy.content.childNodes.length, 0, 63 'Wrong number of template content\'s copy child nodes'); 64 checkOwnerDocument(copy.content, template.content.ownerDocument); 65 66 }, 'ownerDocument of cloned template content is set to template content owner. ' 67 + 'Test cloning without children'); 68 69 70 71 test(function () { 72 var doc = newHTMLDocument(); 73 doc.body.innerHTML = '<template id="tmpl1">' + 74 '<div id="div1">This is div inside template</div>' + 75 '<div id="div2">This is another div inside template</div>' + 76 '</template>'; 77 78 var template = doc.querySelector('#tmpl1'); 79 var copy = template.cloneNode(); 80 81 assert_equals(copy.content.childNodes.length, 0, 82 'Wrong number of template content\'s copy child nodes'); 83 checkOwnerDocument(copy.content, template.content.ownerDocument); 84 85 }, 'ownerDocument of cloned template content is set to template content owner. ' 86 + 'Test cloneNode() with no arguments (false by default)'); 87 88 89 90 test(function () { 91 var doc = newHTMLDocument(); 92 doc.body.innerHTML = '<template id="tmpl1">' + 93 '<div id="div1">This is div inside template</div>' + 94 '<div id="div2">This is another div inside template</div>' + 95 '<template id="tmpl2">' + 96 '<div id="div3">This is div inside nested template</div>' + 97 '<div id="div4">This is another div inside nested template</div>' + 98 '</template>' + 99 '</template>'; 100 101 var template = doc.querySelector('#tmpl1'); 102 var copy = template.cloneNode(true); 103 104 assert_equals(copy.content.childNodes.length, 3, 105 'Wrong number of template content\'s copy child nodes'); 106 checkOwnerDocument(copy.content, template.content.ownerDocument); 107 108 }, 'ownerDocument of cloned template content is set to template content owner. ' 109 + 'Test cloning nested template'); 110 111 112 113 testInIFrame('../resources/template-contents.html', function(context) { 114 var doc = context.iframes[0].contentDocument; 115 116 var template = doc.body.querySelector('template'); 117 var copy = template.cloneNode(true); 118 119 checkOwnerDocument(copy.content, template.content.ownerDocument); 120 121 }, 'ownerDocument of cloned template content is set to template content owner. ' 122 + 'Test loading HTML document from file'); 123 124 </script> 125 </body> 126 </html>