Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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>
16 /**
17 * Constructs a node with a nested ShadowRoot with the following structure:
18 * <span> - - - - - - - - - - <ShadowRoot>
19 * <span> <span> - - - - - - - - - - <ShadowRoot>
20 * id=one id=four <span>
21 * data-color=red data-color=orange id=eleven
22 * <span> <span> <content>
23 * id=two id=five id=twelve
24 * data-color=blue data-color=purple select=secondSelect
25 * <span> <content> <span>
26 * id=three id=six id=thirteen
27 * data-color=green select=firstSelect
28 * <span>
29 * id=seven
30 * <content>
31 * id=eight
32 * <span>
33 * id=nine
34 * <span>
35 * id=ten
36 * data-color=grey
37 */
38 function constructTree(firstSelect, secondSelect) {
39 var rootSpan = document.createElement("span");
40 rootSpan.innerHTML = '<span id="one" data-color="red"></span><span id="two" data-color="blue"></span><span id="three" data-color="green"></span>';
41 var firstShadow = rootSpan.createShadowRoot();
42 firstShadow.innerHTML = '<span id="four" data-color="orange"><span id="five" data-color="purple"></span><content id="six" select="' + firstSelect + '"><span id="seven"></span><content id="eight"></content><span id="nine"></span></content><span id="ten"></span></span>';
43 var secondShadow = firstShadow.firstChild.createShadowRoot();
44 secondShadow.innerHTML = '<span id="eleven"></span><content id="twelve" select="' + secondSelect + '"></content><span id="thirteen"></span>';
45 return rootSpan;
46 }
48 // Create a tree with content that matches on everything and check node distribution.
49 var allSpan = constructTree("*", "*");
50 var firstContent = allSpan.shadowRoot.getElementById("six");
51 var firstDistNodes = firstContent.getDistributedNodes();
52 is(firstDistNodes.length, 3, "Universal selector should match all nodes.");
53 // Check the order of the distributed nodes.
54 is(firstDistNodes.item(0).id, "one", "First distributed node should have id of 'one'");
55 is(firstDistNodes.item(1).id, "two", "Second distributed node should have id of 'two'");
56 is(firstDistNodes.item(2).id, "three", "Third distributed node should have id of 'three'");
57 var secondContent = allSpan.shadowRoot.firstChild.shadowRoot.getElementById("twelve");
58 var secondDistNodes = secondContent.getDistributedNodes();
59 is(secondDistNodes.length, 5, "Universial selector should match all nodes including those distributed into content.");
60 // Check the order of the distribute nodes.
61 is(secondDistNodes.item(0).id, "five", "First distributed node should have id of 'five'");
62 is(secondDistNodes.item(1).id, "one", "Second distributed (reprojected) node should have id of 'one'");
63 is(secondDistNodes.item(2).id, "two", "Third distributed (reprojected) node should have id of 'two'");
64 is(secondDistNodes.item(3).id, "three", "Fourth distributed (reprojected) node should have id of 'three'");
65 is(secondDistNodes.item(4).id, "ten", "Fifth distributed node should have id of 'ten'");
67 // Append an element after id=two and make sure that it is inserted into the corrent
68 // position in the insertion points.
69 var additionalSpan = document.createElement("span");
70 additionalSpan.id = "additional";
72 // Insert the additional span in the third position, before the span with id=three.
73 allSpan.insertBefore(additionalSpan, allSpan.childNodes.item(2));
74 firstDistNodes = firstContent.getDistributedNodes();
75 secondDistNodes = secondContent.getDistributedNodes();
76 is(firstDistNodes.length, 4, "First insertion point should match one more node.");
77 is(firstDistNodes.item(2).id, "additional", "Additional span should have been inserted into the third position of the first insertion point.");
79 is(secondDistNodes.length, 6, "Second insertion point should match one more node.");
80 is(secondDistNodes.item(3).id, "additional", "Additional span should have been inserted into the fourth position of the second insertion point.");
82 function nodeListDoesNotContain(nodeList, element) {
83 for (var i = 0; i < nodeList.length; i++) {
84 if (nodeList[i] == element) {
85 return false;
86 }
87 }
88 return true;
89 }
91 // Remove the span with id=one and check that it is removed from all insertion points.
92 allSpan = constructTree("*", "*");
93 var spanOne = allSpan.firstChild;
94 allSpan.removeChild(spanOne);
95 firstContent = allSpan.shadowRoot.getElementById("six");
96 ok(nodeListDoesNotContain(firstContent.getDistributedNodes(), spanOne), "Child removed from host should not appear in insertion point node list.");
97 secondContent = allSpan.shadowRoot.firstChild.shadowRoot.getElementById("twelve");
98 ok(nodeListDoesNotContain(secondContent.getDistributedNodes(), spanOne), "Child removed from host should not appear in nested insertion point node list.");
100 // Make sure <content> in fallback content is inactive.
101 // First insertion point will not match anything and will use fallback content.
102 allSpan = constructTree("#nomatch", "*");
103 var fallbackInsertionPoint = allSpan.shadowRoot.getElementById("eight");
104 is(fallbackInsertionPoint.getDistributedNodes().length, 0, "Insertion points in default content should be inactive.");
106 // Insertion points with non-universal selectors.
107 allSpan = constructTree("span[data-color=blue]", "*");
108 firstContent = allSpan.shadowRoot.getElementById("six");
109 is(firstContent.getDistributedNodes().length, 1, "Insertion point selector should only match one node.");
110 is(firstContent.getDistributedNodes()[0].dataset.color, "blue", "Projected node should match selector.");
111 secondContent = allSpan.shadowRoot.firstChild.shadowRoot.getElementById("twelve");
112 is(secondContent.getDistributedNodes().length, 3, "Second insertion point should match two children and one reprojected node.");
113 is(secondContent.getDistributedNodes()[1].dataset.color, "blue", "Projected node should match selector.");
115 allSpan = constructTree("span[data-color=blue]", "span[data-color=blue]");
116 firstContent = allSpan.shadowRoot.getElementById("six");
117 is(firstContent.getDistributedNodes().length, 1, "Insertion point selector should only match one node.");
118 is(firstContent.getDistributedNodes()[0].dataset.color, "blue", "Projected node should match selector.");
119 secondContent = allSpan.shadowRoot.firstChild.shadowRoot.getElementById("twelve");
120 is(secondContent.getDistributedNodes().length, 1, "Insertion point should only match reprojected node.");
121 is(secondContent.getDistributedNodes()[0].dataset.color, "blue", "Projected node should match selector.");
123 // Make sure that dynamically appended default content will get distributed.
124 </script>
125 </body>
126 </html>