dom/tests/mochitest/webcomponents/test_nested_content_element.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/tests/mochitest/webcomponents/test_nested_content_element.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,127 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=806506
     1.8 +-->
     1.9 +<head>
    1.10 +  <title>Test for HTMLContent element</title>
    1.11 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    1.12 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.13 +</head>
    1.14 +<body>
    1.15 +<div id="grabme"></div>
    1.16 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=806506">Bug 806506</a>
    1.17 +<script>
    1.18 +
    1.19 +/**
    1.20 + * Constructs a node with a nested ShadowRoot with the following structure:
    1.21 + * <span> - - - - - - - - - - <ShadowRoot>
    1.22 + *   <span>                     <span> - - - - - - - - - - <ShadowRoot>
    1.23 + *    id=one                      id=four                    <span>
    1.24 + *    data-color=red              data-color=orange            id=eleven
    1.25 + *   <span>                       <span>                     <content>
    1.26 + *    id=two                        id=five                    id=twelve
    1.27 + *    data-color=blue               data-color=purple          select=secondSelect
    1.28 + *   <span>                       <content>                  <span>
    1.29 + *    id=three                      id=six                     id=thirteen
    1.30 + *    data-color=green              select=firstSelect
    1.31 + *                                  <span>
    1.32 + *                                    id=seven
    1.33 + *                                  <content>
    1.34 + *                                    id=eight
    1.35 + *                                  <span>
    1.36 + *                                    id=nine
    1.37 + *                                <span>
    1.38 + *                                  id=ten
    1.39 + *                                  data-color=grey
    1.40 + */
    1.41 +function constructTree(firstSelect, secondSelect) {
    1.42 +  var rootSpan = document.createElement("span");
    1.43 +  rootSpan.innerHTML = '<span id="one" data-color="red"></span><span id="two" data-color="blue"></span><span id="three" data-color="green"></span>';
    1.44 +  var firstShadow = rootSpan.createShadowRoot();
    1.45 +  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>';
    1.46 +  var secondShadow = firstShadow.firstChild.createShadowRoot();
    1.47 +  secondShadow.innerHTML = '<span id="eleven"></span><content id="twelve" select="' + secondSelect + '"></content><span id="thirteen"></span>';
    1.48 +  return rootSpan;
    1.49 +}
    1.50 +
    1.51 +// Create a tree with content that matches on everything and check node distribution.
    1.52 +var allSpan = constructTree("*", "*");
    1.53 +var firstContent = allSpan.shadowRoot.getElementById("six");
    1.54 +var firstDistNodes = firstContent.getDistributedNodes();
    1.55 +is(firstDistNodes.length, 3, "Universal selector should match all nodes.");
    1.56 +// Check the order of the distributed nodes.
    1.57 +is(firstDistNodes.item(0).id, "one", "First distributed node should have id of 'one'");
    1.58 +is(firstDistNodes.item(1).id, "two", "Second distributed node should have id of 'two'");
    1.59 +is(firstDistNodes.item(2).id, "three", "Third distributed node should have id of 'three'");
    1.60 +var secondContent = allSpan.shadowRoot.firstChild.shadowRoot.getElementById("twelve");
    1.61 +var secondDistNodes = secondContent.getDistributedNodes();
    1.62 +is(secondDistNodes.length, 5, "Universial selector should match all nodes including those distributed into content.");
    1.63 +// Check the order of the distribute nodes.
    1.64 +is(secondDistNodes.item(0).id, "five", "First distributed node should have id of 'five'");
    1.65 +is(secondDistNodes.item(1).id, "one", "Second distributed (reprojected) node should have id of 'one'");
    1.66 +is(secondDistNodes.item(2).id, "two", "Third distributed (reprojected) node should have id of 'two'");
    1.67 +is(secondDistNodes.item(3).id, "three", "Fourth distributed (reprojected) node should have id of 'three'");
    1.68 +is(secondDistNodes.item(4).id, "ten", "Fifth distributed node should have id of 'ten'");
    1.69 +
    1.70 +// Append an element after id=two and make sure that it is inserted into the corrent
    1.71 +// position in the insertion points.
    1.72 +var additionalSpan = document.createElement("span");
    1.73 +additionalSpan.id = "additional";
    1.74 +
    1.75 +// Insert the additional span in the third position, before the span with id=three.
    1.76 +allSpan.insertBefore(additionalSpan, allSpan.childNodes.item(2));
    1.77 +firstDistNodes = firstContent.getDistributedNodes();
    1.78 +secondDistNodes = secondContent.getDistributedNodes();
    1.79 +is(firstDistNodes.length, 4, "First insertion point should match one more node.");
    1.80 +is(firstDistNodes.item(2).id, "additional", "Additional span should have been inserted into the third position of the first insertion point.");
    1.81 +
    1.82 +is(secondDistNodes.length, 6, "Second insertion point should match one more node.");
    1.83 +is(secondDistNodes.item(3).id, "additional", "Additional span should have been inserted into the fourth position of the second insertion point.");
    1.84 +
    1.85 +function nodeListDoesNotContain(nodeList, element) {
    1.86 +  for (var i = 0; i < nodeList.length; i++) {
    1.87 +    if (nodeList[i] == element) {
    1.88 +      return false;
    1.89 +    }
    1.90 +  }
    1.91 +  return true;
    1.92 +}
    1.93 +
    1.94 +// Remove the span with id=one and check that it is removed from all insertion points.
    1.95 +allSpan = constructTree("*", "*");
    1.96 +var spanOne = allSpan.firstChild;
    1.97 +allSpan.removeChild(spanOne);
    1.98 +firstContent = allSpan.shadowRoot.getElementById("six");
    1.99 +ok(nodeListDoesNotContain(firstContent.getDistributedNodes(), spanOne), "Child removed from host should not appear in insertion point node list.");
   1.100 +secondContent = allSpan.shadowRoot.firstChild.shadowRoot.getElementById("twelve");
   1.101 +ok(nodeListDoesNotContain(secondContent.getDistributedNodes(), spanOne), "Child removed from host should not appear in nested insertion point node list.");
   1.102 +
   1.103 +// Make sure <content> in fallback content is inactive.
   1.104 +// First insertion point will not match anything and will use fallback content.
   1.105 +allSpan = constructTree("#nomatch", "*");
   1.106 +var fallbackInsertionPoint = allSpan.shadowRoot.getElementById("eight");
   1.107 +is(fallbackInsertionPoint.getDistributedNodes().length, 0, "Insertion points in default content should be inactive.");
   1.108 +
   1.109 +// Insertion points with non-universal selectors.
   1.110 +allSpan = constructTree("span[data-color=blue]", "*");
   1.111 +firstContent = allSpan.shadowRoot.getElementById("six");
   1.112 +is(firstContent.getDistributedNodes().length, 1, "Insertion point selector should only match one node.");
   1.113 +is(firstContent.getDistributedNodes()[0].dataset.color, "blue", "Projected node should match selector.");
   1.114 +secondContent = allSpan.shadowRoot.firstChild.shadowRoot.getElementById("twelve");
   1.115 +is(secondContent.getDistributedNodes().length, 3, "Second insertion point should match two children and one reprojected node.");
   1.116 +is(secondContent.getDistributedNodes()[1].dataset.color, "blue", "Projected node should match selector.");
   1.117 +
   1.118 +allSpan = constructTree("span[data-color=blue]", "span[data-color=blue]");
   1.119 +firstContent = allSpan.shadowRoot.getElementById("six");
   1.120 +is(firstContent.getDistributedNodes().length, 1, "Insertion point selector should only match one node.");
   1.121 +is(firstContent.getDistributedNodes()[0].dataset.color, "blue", "Projected node should match selector.");
   1.122 +secondContent = allSpan.shadowRoot.firstChild.shadowRoot.getElementById("twelve");
   1.123 +is(secondContent.getDistributedNodes().length, 1, "Insertion point should only match reprojected node.");
   1.124 +is(secondContent.getDistributedNodes()[0].dataset.color, "blue", "Projected node should match selector.");
   1.125 +
   1.126 +// Make sure that dynamically appended default content will get distributed.
   1.127 +</script>
   1.128 +</body>
   1.129 +</html>
   1.130 +

mercurial