dom/tests/mochitest/webcomponents/test_content_element.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial