dom/tests/mochitest/webcomponents/test_shadow_element.html

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <!--
michael@0 4 https://bugzilla.mozilla.org/show_bug.cgi?id=887538
michael@0 5 -->
michael@0 6 <head>
michael@0 7 <title>Test for HTMLShadowElement</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 </head>
michael@0 11 <body>
michael@0 12 <div id="grabme"></div>
michael@0 13 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=887538">Bug 887538</a>
michael@0 14 <script>
michael@0 15 var host = document.createElement("span");
michael@0 16
michael@0 17 // Create three shadow roots on a single host and make sure that shadow elements
michael@0 18 // are associated with the correct shadow root.
michael@0 19 var firstShadow = host.createShadowRoot();
michael@0 20 firstShadow.innerHTML = '<shadow id="shadowone"></shadow>';
michael@0 21 var secondShadow = host.createShadowRoot();
michael@0 22 secondShadow.innerHTML = '<shadow id="shadowtwo"></shadow>';
michael@0 23 var thirdShadow = host.createShadowRoot();
michael@0 24 thirdShadow.innerHTML = '<shadow id="shadowthree"></shadow>';
michael@0 25
michael@0 26 is(firstShadow.getElementById("shadowone").olderShadowRoot, null, "Shadow element in oldest ShadowRoot should not be associated with a ShadowRoot.");
michael@0 27 is(secondShadow.getElementById("shadowtwo").olderShadowRoot, firstShadow, "Shadow element should be associated with older ShadowRoot.");
michael@0 28 is(thirdShadow.getElementById("shadowthree").olderShadowRoot, secondShadow, "Shadow element should be associated with older ShadowRoot.");
michael@0 29
michael@0 30 // Only the first ShadowRoot in tree order is an insertion point.
michael@0 31 host = document.createElement("span");
michael@0 32 firstShadow = host.createShadowRoot();
michael@0 33 secondShadow = host.createShadowRoot();
michael@0 34 secondShadow.innerHTML = '<shadow id="shadowone"></shadow><shadow id="shadowtwo"></shadow>';
michael@0 35 var shadowElemOne = secondShadow.getElementById("shadowone");
michael@0 36 var shadowElemTwo = secondShadow.getElementById("shadowtwo");
michael@0 37
michael@0 38 is(shadowElemOne.olderShadowRoot, firstShadow, "First <shadow> in tree order should be an insertion point.");
michael@0 39 is(shadowElemTwo.olderShadowRoot, null, "Second <shadow> in tree order should not be an insertion point.");
michael@0 40
michael@0 41 // Remove the first <shadow> element and make sure the second <shadow> element becomes an insertion point.
michael@0 42 secondShadow.removeChild(shadowElemOne);
michael@0 43 is(shadowElemOne.olderShadowRoot, null, "<shadow> element not in a ShadowRoot is not an insertion point.");
michael@0 44 is(shadowElemTwo.olderShadowRoot, firstShadow, "Second <shadow> element should become insertion point after first is removed.");
michael@0 45
michael@0 46 // Insert a <shadow> element before the current shadow insertion point and make sure that it becomes an insertion point.
michael@0 47 secondShadow.insertBefore(shadowElemOne, shadowElemTwo);
michael@0 48 is(shadowElemOne.olderShadowRoot, firstShadow, "<shadow> element inserted as first in tree order should become an insertion point.");
michael@0 49 is(shadowElemTwo.olderShadowRoot, null, "<shadow> element should no longer be an insertion point it another is inserted before.");
michael@0 50
michael@0 51 // <shadow> element in fallback content is not an insertion point.
michael@0 52 host = document.createElement("span");
michael@0 53 firstShadow = host.createShadowRoot();
michael@0 54 secondShadow = host.createShadowRoot();
michael@0 55 secondShadow.innerHTML = '<content><shadow id="shadowone"></shadow></content><shadow id="shadowtwo"></shadow>';
michael@0 56 shadowElemOne = secondShadow.getElementById("shadowone");
michael@0 57 shadowElemTwo = secondShadow.getElementById("shadowtwo");
michael@0 58
michael@0 59 is(shadowElemOne.olderShadowRoot, null, "<shadow> element in fallback content is not an insertion point.");
michael@0 60 is(shadowElemTwo.olderShadowRoot, null, "<shadow> element preceeded by another <shadow> element is not an insertion point.");
michael@0 61
michael@0 62 // <shadow> element that is descendant of shadow element is not an insertion point.
michael@0 63 host = document.createElement("span");
michael@0 64 firstShadow = host.createShadowRoot();
michael@0 65 secondShadow = host.createShadowRoot();
michael@0 66 secondShadow.innerHTML = '<shadow><shadow id="shadowone"></shadow></shadow>';
michael@0 67 shadowElemOne = secondShadow.getElementById("shadowone");
michael@0 68 is(shadowElemOne.olderShadowRoot, null, "<shadow> element that is descendant of shadow element is not an insertion point.");
michael@0 69
michael@0 70 // Check projection of <content> elements through <shadow> elements.
michael@0 71 host = document.createElement("span");
michael@0 72 firstShadow = host.createShadowRoot();
michael@0 73 secondShadow = host.createShadowRoot();
michael@0 74 firstShadow.innerHTML = '<content id="firstcontent"></content>';
michael@0 75 secondShadow.innerHTML = '<shadow><span id="one"></span><content id="secondcontent"></content><span id="four"></span></shadow>';
michael@0 76 host.innerHTML = '<span id="two"></span><span id="three"></span>';
michael@0 77 var firstContent = firstShadow.getElementById("firstcontent");
michael@0 78 var secondContent = secondShadow.getElementById("secondcontent");
michael@0 79 var firstDistNodes = firstContent.getDistributedNodes();
michael@0 80 var secondDistNodes = secondContent.getDistributedNodes();
michael@0 81
michael@0 82 is(secondDistNodes.length, 2, "There should be two distributed nodes from the host.");
michael@0 83 ok(secondDistNodes[0].id == "two" &&
michael@0 84 secondDistNodes[1].id == "three", "Nodes projected from host should preserve order.");
michael@0 85
michael@0 86 is(firstDistNodes.length, 4, "There should be four distributed nodes, two from the first shadow, two from the second shadow.");
michael@0 87 ok(firstDistNodes[0].id == "one" &&
michael@0 88 firstDistNodes[1].id == "two" &&
michael@0 89 firstDistNodes[2].id == "three" &&
michael@0 90 firstDistNodes[3].id == "four", "Reprojection through shadow should preserve node order.");
michael@0 91
michael@0 92 // Remove a node from the host and make sure that it is removed from all insertion points.
michael@0 93 host.removeChild(host.firstChild);
michael@0 94 firstDistNodes = firstContent.getDistributedNodes();
michael@0 95 secondDistNodes = secondContent.getDistributedNodes();
michael@0 96
michael@0 97 is(secondDistNodes.length, 1, "There should be one distriubted node remaining after removing node from host.");
michael@0 98 ok(secondDistNodes[0].id == "three", "Span with id=two should have been removed from content element.");
michael@0 99 is(firstDistNodes.length, 3, "There should be three distributed nodes remaining after removing node from host.");
michael@0 100 ok(firstDistNodes[0].id == "one" &&
michael@0 101 firstDistNodes[1].id == "three" &&
michael@0 102 firstDistNodes[2].id == "four", "Reprojection through shadow should preserve node order.");
michael@0 103
michael@0 104 // Check projection of <shadow> elements to <content> elements.
michael@0 105 host = document.createElement("span");
michael@0 106 firstShadow = host.createShadowRoot();
michael@0 107 secondShadow = host.createShadowRoot();
michael@0 108 secondShadow.innerHTML = '<span id="firstspan"><shadow></shadow></span>';
michael@0 109 thirdShadow = secondShadow.getElementById("firstspan").createShadowRoot();
michael@0 110 thirdShadow.innerHTML = '<content id="firstcontent"></content>';
michael@0 111 firstContent = thirdShadow.getElementById("firstcontent");
michael@0 112 var shadowChild = document.createElement("span");
michael@0 113 firstShadow.appendChild(shadowChild);
michael@0 114
michael@0 115 is(firstContent.getDistributedNodes()[0], shadowChild, "Elements in shadow insertioin point should be projected into content insertion points.");
michael@0 116
michael@0 117 // Remove child of ShadowRoot and check that projected node is removed from insertion point.
michael@0 118 firstShadow.removeChild(firstShadow.firstChild);
michael@0 119
michael@0 120 is(firstContent.getDistributedNodes().length, 0, "Reprojected element was removed from ShadowRoot, thus it should be removed from content insertion point.");
michael@0 121
michael@0 122 // Check deeply nested projection of <shadow> elements.
michael@0 123 host = document.createElement("span");
michael@0 124 firstShadow = host.createShadowRoot();
michael@0 125 firstShadow.innerHTML = '<content></content>';
michael@0 126 secondShadow = host.createShadowRoot();
michael@0 127 secondShadow.innerHTML = '<shadow><content></content></shadow>';
michael@0 128 thirdShadow = host.createShadowRoot();
michael@0 129 thirdShadow.innerHTML = '<span id="firstspan"><shadow><content></content></shadow></span>';
michael@0 130 var fourthShadow = thirdShadow.getElementById("firstspan").createShadowRoot();
michael@0 131 fourthShadow.innerHTML = '<content id="firstcontent"></content>';
michael@0 132 firstContent = fourthShadow.getElementById("firstcontent");
michael@0 133 host.innerHTML = '<span></span>';
michael@0 134
michael@0 135 is(firstContent.getDistributedNodes()[0], host.firstChild, "Child of host should be projected to insertion point.");
michael@0 136
michael@0 137 // Remove node and make sure that it is removed from distributed nodes.
michael@0 138 host.removeChild(host.firstChild);
michael@0 139
michael@0 140 is(firstContent.getDistributedNodes().length, 0, "Node removed from host should be removed from insertion point.");
michael@0 141
michael@0 142 // Check projection of fallback content through <shadow> elements.
michael@0 143 host = document.createElement("span");
michael@0 144 firstShadow = host.createShadowRoot();
michael@0 145 firstShadow.innerHTML = '<content><span id="firstspan"></span></content>';
michael@0 146 secondShadow = host.createShadowRoot();
michael@0 147 secondShadow.innerHTML = '<span id="secondspan"><shadow id="firstshadow"></shadow></span>';
michael@0 148 firstShadowElem = secondShadow.getElementById("firstshadow");
michael@0 149 thirdShadow = secondShadow.getElementById("secondspan").createShadowRoot();
michael@0 150 thirdShadow.innerHTML = '<content id="firstcontent"></content>';
michael@0 151 firstContent = thirdShadow.getElementById("firstcontent");
michael@0 152
michael@0 153 is(firstContent.getDistributedNodes().length, 1, "There should be one node distributed from fallback content.");
michael@0 154 is(firstContent.getDistributedNodes()[0], firstShadow.getElementById("firstspan"), "Fallback content should be distributed.");
michael@0 155
michael@0 156 // Add some content to the host (causing the fallback content to be dropped) and make sure distribution nodes are updated.
michael@0 157
michael@0 158 var newElem = document.createElement("div");
michael@0 159 firstShadowElem.appendChild(newElem);
michael@0 160
michael@0 161 is(firstContent.getDistributedNodes().length, 1, "There should be one node distributed from the host.");
michael@0 162 is(firstContent.getDistributedNodes()[0], newElem, "Distributed node should be from host, not fallback content.");
michael@0 163
michael@0 164 // Remove the distribution node and check that fallback content is used.
michael@0 165 firstShadowElem.removeChild(newElem);
michael@0 166
michael@0 167 is(firstContent.getDistributedNodes().length, 1, "There should be one node distributed from fallback content.");
michael@0 168 is(firstContent.getDistributedNodes()[0], firstShadow.getElementById("firstspan"), "Fallback content should be distributed after removing node from host.");
michael@0 169
michael@0 170 </script>
michael@0 171 </body>
michael@0 172 </html>
michael@0 173

mercurial