has-slotted-query-selector.html (3385B)
1 <!doctype html> 2 <meta charset="utf-8" /> 3 <title>:has-slotted and query-selector</title> 4 <link rel="help" href="https://github.com/w3c/csswg-drafts/pull/10586" /> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <div id="ancestor"><template shadowrootmode="open"><div id="host"><template shadowrootmode="open"><style> 8 slot { 9 color: rgb(0, 0, 0); 10 } 11 :has-slotted { 12 color: rgb(0, 255, 0); 13 } 14 </style><slot></slot></template><slot></slot></div></template></div> 15 <script> 16 const host = ancestor.shadowRoot.querySelector('#host'); 17 for(const container of [host, ancestor]) { 18 host.replaceChildren(document.createElement('slot')); 19 ancestor.replaceChildren(); 20 const slot = host.shadowRoot.querySelector("slot"); 21 22 test(function () { 23 assert_equals(slot.assignedNodes({flatten: true}).length, 0, "slot should have 0 nodes"); 24 assert_false(slot.matches(":has-slotted"), "slot element should not match :has-slotted"); 25 assert_equals(getComputedStyle(slot).color, "rgb(0, 0, 0)", "slot element should not match :has-slotted"); 26 assert_equals(host.shadowRoot.querySelectorAll(":has-slotted").length, 0, "slot element should not match :has-slotted"); 27 }, ":has-slotted does not match or querySelector when there are no assigned nodes" + (container == ancestor ? " (flattened)" : "")); 28 29 test(function () { 30 container.append(document.createElement("div")); 31 assert_equals(slot.assignedNodes({flatten: true}).length, 1, "slot should have 1 node"); 32 assert_true(slot.matches(":has-slotted"), "slot element should match :has-slotted with 1 child node"); 33 assert_equals(getComputedStyle(slot).color, "rgb(0, 255, 0)", "slot element should match :has-slotted with 1 child node"); 34 assert_equals(host.shadowRoot.querySelectorAll(":has-slotted").length, 1, "slot element should match :has-slotted with 1 child node"); 35 }, ":has-slotted must match/querySelector when an element is assigned" + (container == ancestor ? " (flattened)" : "")); 36 37 test(function () { 38 container.replaceChildren(); 39 assert_equals(slot.assignedNodes({flatten: true}).length, 0, "slot should have 0 nodes"); 40 assert_false(slot.matches(":has-slotted"), "slot element should not match :has-slotted after empty"); 41 assert_equals(getComputedStyle(slot).color, "rgb(0, 0, 0)", "slot element should not match :has-slotted after empty"); 42 assert_equals(host.shadowRoot.querySelectorAll(":has-slotted").length, 0, "slot element should not match :has-slotted after empty"); 43 }, ":has-slotted does not when child nodes are replaced" + (container == ancestor ? " (flattened)" : "")); 44 45 test(function () { 46 container.append(document.createTextNode("")); 47 assert_equals(slot.assignedNodes({flatten: true}).length, 1, "slot should have 1 node"); 48 assert_true(slot.matches(":has-slotted"), "slot element should match :has-slotted with 1 text node"); 49 assert_equals(getComputedStyle(slot).color, "rgb(0, 255, 0)", "slot element should match :has-slotted with 1 text node"); 50 assert_equals(host.shadowRoot.querySelectorAll(":has-slotted").length, 1, "slot element should match :has-slotted with 1 text node"); 51 }, ":has-slotted must match/querySelector when a text node is assigned" + (container == ancestor ? " (flattened)" : "")); 52 } 53 </script>