dom/tests/mochitest/webcomponents/test_template.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <!--
michael@0 4 https://bugzilla.mozilla.org/show_bug.cgi?id=818976
michael@0 5 -->
michael@0 6 <head>
michael@0 7 <title>Test for template element</title>
michael@0 8 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
michael@0 10 <script>
michael@0 11 function shouldNotCall() {
michael@0 12 ok(false, "Template contents should be inert.");
michael@0 13 }
michael@0 14 </script>
michael@0 15 <template>
michael@0 16 <script>
michael@0 17 shouldNotCall();
michael@0 18 </script>
michael@0 19 </template>
michael@0 20 </head>
michael@0 21 <body>
michael@0 22 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=818976">Bug 818976</a>
michael@0 23 <template id="grabme"><div id="insidetemplate"></div></template>
michael@0 24 <template id="justtemplate"></template>
michael@0 25 <template id="first">Hi<template>Bye</template></template>
michael@0 26 <div><template id="second"><span></span></template></div>
michael@0 27 <template id="cloneme"><span>I want a clone</span><span>me too</span></template>
michael@0 28 <template id="cycleone"></template>
michael@0 29 <template id="cycletwo"><template></template></template>
michael@0 30 <template id="cyclethree"></template>
michael@0 31 <template id="cyclefour"><template></template></template>
michael@0 32 <template id="appendtome"></template>
michael@0 33 <template id="insertinme"></template>
michael@0 34 <template>
michael@0 35 <script>
michael@0 36 shouldNotCall();
michael@0 37 </script>
michael@0 38 </template>
michael@0 39 <div id="fillme"></div>
michael@0 40 <script>
michael@0 41 var templateEl = document.getElementById("grabme");
michael@0 42 ok(templateEl, "template element should be in document.");
michael@0 43 is(window.getComputedStyle(templateEl).display, "none", "Template element should not be visible.");
michael@0 44 ok(!document.getElementById("insidetemplate"), "Template content should not be in document.");
michael@0 45 is(templateEl.childNodes.length, 0, "Template element should have no children.");
michael@0 46 is(templateEl.content.childNodes.length, 1, "Template content should have 1 child <div>.");
michael@0 47
michael@0 48 // Make sure that template is owned by different document.
michael@0 49 ok(templateEl.content.ownerDocument != templateEl.ownerDocument, "Template should be in a different document because the current document has a browsing context.");
michael@0 50 var otherTemplateEl = document.getElementById("first");
michael@0 51 is(templateEl.content.ownerDocument, otherTemplateEl.content.ownerDocument, "Template contents within the same document should be owned by the same template contents owner.");
michael@0 52
michael@0 53 var htmlDoc = document.implementation.createHTMLDocument();
michael@0 54 var otherDocTemplateEl = htmlDoc.createElement("template");
michael@0 55 isnot(otherDocTemplateEl.content.ownerDocument, htmlDoc, "Template content owner should be a new document.");
michael@0 56
michael@0 57 var templateOwnerDoc = otherDocTemplateEl.content.ownerDocument;
michael@0 58 var docCreatedTemplateEl = templateOwnerDoc.createElement("template");
michael@0 59 is(docCreatedTemplateEl.content.ownerDocument, templateOwnerDoc, "Template content owner of template elements created by a template document should be the template document.");
michael@0 60
michael@0 61 // Tests for XMLSerializer
michael@0 62 templateEl = document.getElementById("justtemplate");
michael@0 63 var serializer = new XMLSerializer();
michael@0 64 is(serializer.serializeToString(templateEl), '<template xmlns="http://www.w3.org/1999/xhtml" id="justtemplate"></template>', "XMLSerializer should serialize template element.");
michael@0 65
michael@0 66 templateEl = document.getElementById("first");
michael@0 67 is(serializer.serializeToString(templateEl), '<template xmlns="http://www.w3.org/1999/xhtml" id="first">Hi<template>Bye</template></template>', "XMLSerializer should serialize template content.");
michael@0 68
michael@0 69 // Tests for innerHTML.
michael@0 70 is(templateEl.innerHTML, 'Hi<template>Bye</template>', "innerHTML should serialize content.");
michael@0 71 // Tests for outerHTML, not specified but should do something reasonable.
michael@0 72 is(templateEl.outerHTML, '<template id="first">Hi<template>Bye</template></template>', "outerHTML should serialize content.");
michael@0 73
michael@0 74 templateEl.innerHTML = "Hello";
michael@0 75 is(templateEl.innerHTML, "Hello", "innerHTML of template should be set to 'Hello'");
michael@0 76 is(templateEl.childNodes.length, 0, "Template element should have no children.");
michael@0 77 is(templateEl.content.childNodes.length, 1, "Template content should have 'Hello' as child.");
michael@0 78
michael@0 79 // Test for innerHTML on parent of template element.
michael@0 80 var templateParent = document.getElementById("second").parentNode;
michael@0 81 is(templateParent.innerHTML, '<template id="second"><span></span></template>', "InnerHTML on parent of template element should serialize template and template content.");
michael@0 82
michael@0 83 templateEl.innerHTML = '<template id="inner">Hello</template>';
michael@0 84 ok(templateEl.content.childNodes[0] instanceof HTMLTemplateElement, "Template content should have <template> as child.");
michael@0 85 is(templateEl.content.childNodes[0].childNodes.length, 0, "Parsed temlate element should have no children.");
michael@0 86 is(templateEl.content.childNodes[0].content.childNodes.length, 1, "Parsed temlate element should have 'Hello' in content.");
michael@0 87
michael@0 88 // Test cloning.
michael@0 89 templateEl = document.getElementById("cloneme");
michael@0 90 var nonDeepClone = templateEl.cloneNode(false);
michael@0 91 is(nonDeepClone.childNodes.length, 0, "There should be no children on the clone.");
michael@0 92 is(nonDeepClone.content.childNodes.length, 0, "Content should not be cloned.");
michael@0 93 var deepClone = templateEl.cloneNode(true);
michael@0 94 is(deepClone.childNodes.length, 0, "There should be no children on the clone.");
michael@0 95 is(deepClone.content.childNodes.length, 2, "The content should be cloned.");
michael@0 96
michael@0 97 // Append content into a node.
michael@0 98 var parentEl = document.getElementById("fillme");
michael@0 99 parentEl.appendChild(templateEl.content);
michael@0 100 is(parentEl.childNodes.length, 2, "Parent should be appended with cloned content.");
michael@0 101
michael@0 102 // Test exceptions thrown for cycles.
michael@0 103 templateEl = document.getElementById("cycleone");
michael@0 104 try {
michael@0 105 templateEl.content.appendChild(templateEl);
michael@0 106 ok(false, "Exception should be thrown when creating cycles in template content.");
michael@0 107 } catch (ex) {
michael@0 108 ok(true, "Exception should be thrown when creating cycles in template content.");
michael@0 109 }
michael@0 110
michael@0 111 templateEl = document.getElementById("cycletwo");
michael@0 112 try {
michael@0 113 // Append template to template content within the template content.
michael@0 114 templateEl.content.childNodes[0].content.appendChild(templateEl);
michael@0 115 ok(false, "Exception should be thrown when creating cycles in template content.");
michael@0 116 } catch (ex) {
michael@0 117 ok(true, "Exception should be thrown when creating cycles in template content.");
michael@0 118 }
michael@0 119
michael@0 120 templateEl = document.getElementById("cyclethree");
michael@0 121 try {
michael@0 122 templateEl.appendChild(templateEl);
michael@0 123 ok(false, "Exception should be thrown when creating cycles in hierarchy.");
michael@0 124 } catch (ex) {
michael@0 125 ok(true, "Exception should be thrown when creating cycles in hierarchy.");
michael@0 126 }
michael@0 127
michael@0 128 templateEl = document.getElementById("cyclefour");
michael@0 129 try {
michael@0 130 templateEl.content.childNodes[0].appendChild(templateEl);
michael@0 131 ok(false, "Exception should be thrown when creating cycles in hierarchy.");
michael@0 132 } catch (ex) {
michael@0 133 ok(true, "Exception should be thrown when creating cycles in hierarchy.");
michael@0 134 }
michael@0 135
michael@0 136 templateEl = document.getElementById("insertinme");
michael@0 137 var sentinel = document.createElement("div");
michael@0 138 try {
michael@0 139 templateEl.content.appendChild(sentinel);
michael@0 140 templateEl.content.insertBefore(templateEl, sentinel);
michael@0 141 ok(false, "Exception should be thrown when creating cycles in hierarchy.");
michael@0 142 } catch (ex) {
michael@0 143 ok(true, "Exception should be thrown when creating cycles in hierarchy.");
michael@0 144 }
michael@0 145
michael@0 146 // Appending normal stuff into content should work.
michael@0 147 templateEl = document.getElementById("appendtome");
michael@0 148 templateEl.content.appendChild(document.createElement("div"));
michael@0 149 is(templateEl.content.childNodes.length, 1, "Template should have div element appended as child");
michael@0 150
michael@0 151 </script>
michael@0 152 </body>
michael@0 153 </html>

mercurial