Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
1 <!DOCTYPE HTML>
2 <html>
3 <!--
4 https://bugzilla.mozilla.org/show_bug.cgi?id=806506
5 -->
6 <head>
7 <title>Test for HTMLContent element</title>
8 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
10 </head>
11 <body>
12 <div id="grabme"></div>
13 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=806506">Bug 806506</a>
14 <script>
15 // Create a ShadowRoot and append some nodes, containing an insertion point with a universal selector.
16 var shadow = $("grabme").createShadowRoot();
17 shadow.innerHTML = '<span><content id="point"></content></span>';
19 // Get the insertion point from the ShadowRoot and check that child of host is distributed.
20 // Insertion point should match everything because the selector set is empty.
21 var insertionPoint = shadow.getElementById("point");
22 $("grabme").innerHTML = '<div id="distme"></div>';
23 var distNodes = insertionPoint.getDistributedNodes();
24 is(distNodes[0], $("distme"), "Child of bound content should be distributed into insertion point with universal selector.");
25 is(distNodes.length, 1, "Should only have one child distributed into insertion point.");
27 // Add another node to bound content and make sure that the node list is static and does not change.
28 var someSpan = document.createElement("span");
29 $("grabme").appendChild(someSpan);
30 is(distNodes.length, 1, "NodeList from getDistributedNodes should be static.");
32 // Test content select.
33 $("grabme").innerHTML = '<div id="first" class="tall"></div><div id="second" class="skinny"></div>';
34 shadow.innerHTML = '<span><content select=".tall" id="point"></content></span>';
35 var insertionPoint = shadow.getElementById("point");
36 distNodes = insertionPoint.getDistributedNodes();
37 is(distNodes.length, 1, "Insertion point should only match element with the 'tall' class.");
38 is(distNodes[0], $("first"), "Insertion point should only match element with the 'tall' class.");
40 // Get rid of the select attribute and check that the insertion point matches everything.
41 insertionPoint.removeAttribute("select");
42 is(insertionPoint.getDistributedNodes().length, 2, "After removing the 'select' attribute, the insertion point should match everything.");
44 // Set an invalid selector and make sure that nothing is matched.
45 insertionPoint.setAttribute("select", "div:first-child");
46 is(insertionPoint.getDistributedNodes().length, 0, "Invalid selectors should match nothing.");
48 // all compound selectors must only be permitted simple selectors.
49 insertionPoint.setAttribute("select", "div:first-child, span");
50 is(insertionPoint.getDistributedNodes().length, 0, "Invalid selectors should match nothing.");
52 // Test multiple compound selectors.
53 $("grabme").innerHTML = '<div id="first"></div><span id="second"></span><span data-match-me="pickme" id="third"></span>';
54 insertionPoint.setAttribute("select", "span[data-match-me=pickme], div");
55 distNodes = insertionPoint.getDistributedNodes();
56 is(distNodes.length, 2, "Insertion point selector should only match two nodes.");
57 is(distNodes[0], $("first"), "First child node should match selector.");
58 is(distNodes[1], $("third"), "Third child node should match selector.");
60 // Test select property
61 insertionPoint.select = "#second, #third";
62 distNodes = insertionPoint.getDistributedNodes();
63 is(distNodes.length, 2, "Insertion point selector (set using property) should only match two nodes.");
64 is(distNodes[0], $("second"), "First child node should match selector.");
65 is(distNodes[1], $("third"), "Third child node should match selector.");
66 is(insertionPoint.select, "#second, #third", "select property should be transparent.");
68 // Empty set of selectors should match everything.
69 insertionPoint.select = "";
70 is(insertionPoint.getDistributedNodes().length, 3, "Empty set of selectors (set using property) should match everything.");
72 // Remove insertion point and make sure that the point does not have any nodes distributed.
73 $("grabme").innerHTML = '<div></div><span></span>';
74 insertionPoint.removeAttribute("select");
75 is(insertionPoint.getDistributedNodes().length, 2, "Insertion point with univeral selector should match two nodes.");
76 var insertionParent = insertionPoint.parentNode;
77 insertionParent.removeChild(insertionPoint);
78 is(insertionPoint.getDistributedNodes().length, 0, "Insertion point should match no nodes after removal.");
79 insertionParent.appendChild(insertionPoint);
80 is(insertionPoint.getDistributedNodes().length, 2, "Insertion point should match two nodes after appending.");
82 // Test multiple insertion points and check tree order distribution of points.
83 // Append two divs and three spans into host.
84 $("grabme").innerHTML = '<div></div><span></span><div></div><span></span><span></span>';
85 shadow.innerHTML = '<content select="div" id="divpoint"></content><content select="div, span" id="allpoint"></content>';
86 // Insertion point matching div
87 var divPoint = shadow.getElementById("divpoint");
88 // Insertion point matching span and div
89 var allPoint = shadow.getElementById("allpoint");
91 is(divPoint.getDistributedNodes().length, 2, "Two div nodes should be distributed into divPoint.");
92 is(allPoint.getDistributedNodes().length, 3, "Remaining nodes should be distributed into allPoint.");
94 shadow.removeChild(allPoint);
95 is(divPoint.getDistributedNodes().length, 2, "Number of div distributed into insertion point should not change.");
96 is(allPoint.getDistributedNodes().length, 0, "Removed insertion point should not have any nodes.");
98 shadow.insertBefore(allPoint, divPoint);
99 is(allPoint.getDistributedNodes().length, 5, "allPoint should have nodes distributed before divPoint.");
100 is(divPoint.getDistributedNodes().length, 0, "divPoint should have no distributed nodes because they are all distributed to allPoint.");
102 // Make sure that fallback content are in the distributed nodes.
103 $("grabme").innerHTML = '<div id="one"></div><div id="two"></div>';
104 shadow.innerHTML = '<content select="#nothing" id="point"><span id="fallback"></span></content>';
105 insertionPoint = shadow.getElementById("point");
106 is(insertionPoint.getDistributedNodes().length, 1, "There should be one distributed node from fallback content.");
107 is(insertionPoint.getDistributedNodes()[0].id, "fallback", "Distributed node should be fallback content.");
109 $("grabme").innerHTML = '';
110 shadow.innerHTML = '<content select="div" id="point"><span id="one"></span><span id="two"></span></content>';
111 insertionPoint = shadow.getElementById("point");
112 // Make sure that two fallback nodes are distributed into the insertion point.
113 is(insertionPoint.getDistributedNodes().length, 2, "There should be two distributed nodes from fallback content.");
114 is(insertionPoint.getDistributedNodes()[0].id, "one", "First distributed node should have an ID of one.");
115 is(insertionPoint.getDistributedNodes()[1].id, "two", "Second distributed node should have an ID of two.");
117 // Append a node that gets matched by the insertion point, thus causing the fallback content to be removed.
118 var matchingDiv = document.createElement("div");
119 matchingDiv.id = "three";
120 $("grabme").appendChild(matchingDiv);
121 is(insertionPoint.getDistributedNodes().length, 1, "There should be one node distributed from the host.");
122 is(insertionPoint.getDistributedNodes()[0].id, "three", "Node distriubted from host should have id of three.");
124 // Remove the matching node from the host and make sure that the fallback content gets distributed.
125 $("grabme").removeChild(matchingDiv);
126 is(insertionPoint.getDistributedNodes().length, 2, "There should be two distributed nodes from fallback content.");
127 is(insertionPoint.getDistributedNodes()[0].id, "one", "First distributed node should have an ID of one.");
128 is(insertionPoint.getDistributedNodes()[1].id, "two", "Second distributed node should have an ID of two.");
129 </script>
130 </body>
131 </html>