dom/tests/mochitest/webcomponents/test_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_content_element.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,131 @@
     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 +// Create a ShadowRoot and append some nodes, containing an insertion point with a universal selector.
    1.19 +var shadow = $("grabme").createShadowRoot();
    1.20 +shadow.innerHTML = '<span><content id="point"></content></span>';
    1.21 +
    1.22 +// Get the insertion point from the ShadowRoot and check that child of host is distributed.
    1.23 +// Insertion point should match everything because the selector set is empty.
    1.24 +var insertionPoint = shadow.getElementById("point");
    1.25 +$("grabme").innerHTML = '<div id="distme"></div>';
    1.26 +var distNodes = insertionPoint.getDistributedNodes();
    1.27 +is(distNodes[0], $("distme"), "Child of bound content should be distributed into insertion point with universal selector.");
    1.28 +is(distNodes.length, 1, "Should only have one child distributed into insertion point.");
    1.29 +
    1.30 +// Add another node to bound content and make sure that the node list is static and does not change.
    1.31 +var someSpan = document.createElement("span");
    1.32 +$("grabme").appendChild(someSpan);
    1.33 +is(distNodes.length, 1, "NodeList from getDistributedNodes should be static.");
    1.34 +
    1.35 +// Test content select.
    1.36 +$("grabme").innerHTML = '<div id="first" class="tall"></div><div id="second" class="skinny"></div>';
    1.37 +shadow.innerHTML = '<span><content select=".tall" id="point"></content></span>';
    1.38 +var insertionPoint = shadow.getElementById("point");
    1.39 +distNodes = insertionPoint.getDistributedNodes();
    1.40 +is(distNodes.length, 1, "Insertion point should only match element with the 'tall' class.");
    1.41 +is(distNodes[0], $("first"), "Insertion point should only match element with the 'tall' class.");
    1.42 +
    1.43 +// Get rid of the select attribute and check that the insertion point matches everything.
    1.44 +insertionPoint.removeAttribute("select");
    1.45 +is(insertionPoint.getDistributedNodes().length, 2, "After removing the 'select' attribute, the insertion point should match everything.");
    1.46 +
    1.47 +// Set an invalid selector and make sure that nothing is matched.
    1.48 +insertionPoint.setAttribute("select", "div:first-child");
    1.49 +is(insertionPoint.getDistributedNodes().length, 0, "Invalid selectors should match nothing.");
    1.50 +
    1.51 +// all compound selectors must only be permitted simple selectors.
    1.52 +insertionPoint.setAttribute("select", "div:first-child, span");
    1.53 +is(insertionPoint.getDistributedNodes().length, 0, "Invalid selectors should match nothing.");
    1.54 +
    1.55 +// Test multiple compound selectors.
    1.56 +$("grabme").innerHTML = '<div id="first"></div><span id="second"></span><span data-match-me="pickme" id="third"></span>';
    1.57 +insertionPoint.setAttribute("select", "span[data-match-me=pickme], div");
    1.58 +distNodes = insertionPoint.getDistributedNodes();
    1.59 +is(distNodes.length, 2, "Insertion point selector should only match two nodes.");
    1.60 +is(distNodes[0], $("first"), "First child node should match selector.");
    1.61 +is(distNodes[1], $("third"), "Third child node should match selector.");
    1.62 +
    1.63 +// Test select property
    1.64 +insertionPoint.select = "#second, #third";
    1.65 +distNodes = insertionPoint.getDistributedNodes();
    1.66 +is(distNodes.length, 2, "Insertion point selector (set using property) should only match two nodes.");
    1.67 +is(distNodes[0], $("second"), "First child node should match selector.");
    1.68 +is(distNodes[1], $("third"), "Third child node should match selector.");
    1.69 +is(insertionPoint.select, "#second, #third", "select property should be transparent.");
    1.70 +
    1.71 +// Empty set of selectors should match everything.
    1.72 +insertionPoint.select = "";
    1.73 +is(insertionPoint.getDistributedNodes().length, 3, "Empty set of selectors (set using property) should match everything.");
    1.74 +
    1.75 +// Remove insertion point and make sure that the point does not have any nodes distributed.
    1.76 +$("grabme").innerHTML = '<div></div><span></span>';
    1.77 +insertionPoint.removeAttribute("select");
    1.78 +is(insertionPoint.getDistributedNodes().length, 2, "Insertion point with univeral selector should match two nodes.");
    1.79 +var insertionParent = insertionPoint.parentNode;
    1.80 +insertionParent.removeChild(insertionPoint);
    1.81 +is(insertionPoint.getDistributedNodes().length, 0, "Insertion point should match no nodes after removal.");
    1.82 +insertionParent.appendChild(insertionPoint);
    1.83 +is(insertionPoint.getDistributedNodes().length, 2, "Insertion point should match two nodes after appending.");
    1.84 +
    1.85 +// Test multiple insertion points and check tree order distribution of points.
    1.86 +// Append two divs and three spans into host.
    1.87 +$("grabme").innerHTML = '<div></div><span></span><div></div><span></span><span></span>';
    1.88 +shadow.innerHTML = '<content select="div" id="divpoint"></content><content select="div, span" id="allpoint"></content>';
    1.89 +// Insertion point matching div
    1.90 +var divPoint = shadow.getElementById("divpoint");
    1.91 +// Insertion point matching span and div
    1.92 +var allPoint = shadow.getElementById("allpoint");
    1.93 +
    1.94 +is(divPoint.getDistributedNodes().length, 2, "Two div nodes should be distributed into divPoint.");
    1.95 +is(allPoint.getDistributedNodes().length, 3, "Remaining nodes should be distributed into allPoint.");
    1.96 +
    1.97 +shadow.removeChild(allPoint);
    1.98 +is(divPoint.getDistributedNodes().length, 2, "Number of div distributed into insertion point should not change.");
    1.99 +is(allPoint.getDistributedNodes().length, 0, "Removed insertion point should not have any nodes.");
   1.100 +
   1.101 +shadow.insertBefore(allPoint, divPoint);
   1.102 +is(allPoint.getDistributedNodes().length, 5, "allPoint should have nodes distributed before divPoint.");
   1.103 +is(divPoint.getDistributedNodes().length, 0, "divPoint should have no distributed nodes because they are all distributed to allPoint.");
   1.104 +
   1.105 +// Make sure that fallback content are in the distributed nodes.
   1.106 +$("grabme").innerHTML = '<div id="one"></div><div id="two"></div>';
   1.107 +shadow.innerHTML = '<content select="#nothing" id="point"><span id="fallback"></span></content>';
   1.108 +insertionPoint = shadow.getElementById("point");
   1.109 +is(insertionPoint.getDistributedNodes().length, 1, "There should be one distributed node from fallback content.");
   1.110 +is(insertionPoint.getDistributedNodes()[0].id, "fallback", "Distributed node should be fallback content.");
   1.111 +
   1.112 +$("grabme").innerHTML = '';
   1.113 +shadow.innerHTML = '<content select="div" id="point"><span id="one"></span><span id="two"></span></content>';
   1.114 +insertionPoint = shadow.getElementById("point");
   1.115 +// Make sure that two fallback nodes are distributed into the insertion point.
   1.116 +is(insertionPoint.getDistributedNodes().length, 2, "There should be two distributed nodes from fallback content.");
   1.117 +is(insertionPoint.getDistributedNodes()[0].id, "one", "First distributed node should have an ID of one.");
   1.118 +is(insertionPoint.getDistributedNodes()[1].id, "two", "Second distributed node should have an ID of two.");
   1.119 +
   1.120 +// Append a node that gets matched by the insertion point, thus causing the fallback content to be removed.
   1.121 +var matchingDiv = document.createElement("div");
   1.122 +matchingDiv.id = "three";
   1.123 +$("grabme").appendChild(matchingDiv);
   1.124 +is(insertionPoint.getDistributedNodes().length, 1, "There should be one node distributed from the host.");
   1.125 +is(insertionPoint.getDistributedNodes()[0].id, "three", "Node distriubted from host should have id of three.");
   1.126 +
   1.127 +// Remove the matching node from the host and make sure that the fallback content gets distributed.
   1.128 +$("grabme").removeChild(matchingDiv);
   1.129 +is(insertionPoint.getDistributedNodes().length, 2, "There should be two distributed nodes from fallback content.");
   1.130 +is(insertionPoint.getDistributedNodes()[0].id, "one", "First distributed node should have an ID of one.");
   1.131 +is(insertionPoint.getDistributedNodes()[1].id, "two", "Second distributed node should have an ID of two.");
   1.132 +</script>
   1.133 +</body>
   1.134 +</html>

mercurial