tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_markup_shadowdom_clickreveal.js (3813B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Test that the corresponding non-slotted node container gets selected when clicking on
      7 // the reveal link for a slotted node.
      8 
      9 const TEST_URL = `data:text/html;charset=utf-8,
     10  <test-component>
     11    <div slot="slot1" id="el1">slot1-1</div>
     12    <div slot="slot1" id="el2">slot1-2</div>
     13  </test-component>
     14 
     15  <script>
     16    'use strict';
     17    customElements.define('test-component', class extends HTMLElement {
     18      constructor() {
     19        super();
     20        let shadowRoot = this.attachShadow({mode: 'open'});
     21        shadowRoot.innerHTML = '<slot name="slot1"></slot>';
     22      }
     23    });
     24  </script>`;
     25 
     26 // Test reveal link with mouse navigation
     27 add_task(async function () {
     28  const checkWithMouse = checkRevealLink.bind(null, clickOnRevealLink);
     29  await testRevealLink(checkWithMouse, checkWithMouse);
     30 });
     31 
     32 // Test reveal link with keyboard navigation (Enter and Spacebar keys)
     33 add_task(async function () {
     34  const checkWithEnter = checkRevealLink.bind(
     35    null,
     36    keydownOnRevealLink.bind(null, "KEY_Enter")
     37  );
     38  const checkWithSpacebar = checkRevealLink.bind(
     39    null,
     40    keydownOnRevealLink.bind(null, " ")
     41  );
     42 
     43  await testRevealLink(checkWithEnter, checkWithSpacebar);
     44 });
     45 
     46 async function testRevealLink(revealFnFirst, revealFnSecond) {
     47  const { inspector } = await openInspectorForURL(TEST_URL);
     48  const { markup } = inspector;
     49 
     50  info("Find and expand the test-component shadow DOM host.");
     51  const hostFront = await getNodeFront("test-component", inspector);
     52  const hostContainer = markup.getContainer(hostFront);
     53  await expandContainer(inspector, hostContainer);
     54 
     55  info("Expand the shadow root");
     56  const shadowRootContainer = hostContainer.getChildContainers()[0];
     57  await expandContainer(inspector, shadowRootContainer);
     58 
     59  info("Expand the slot");
     60  const slotContainer = shadowRootContainer.getChildContainers()[0];
     61  await expandContainer(inspector, slotContainer);
     62 
     63  const slotChildContainers = slotContainer.getChildContainers();
     64  is(slotChildContainers.length, 2, "Expecting 2 slotted children");
     65 
     66  await revealFnFirst(inspector, slotChildContainers[0].node);
     67  is(inspector.selection.nodeFront.id, "el1", "The right node was selected");
     68  is(hostContainer.getChildContainers()[1].node, inspector.selection.nodeFront);
     69 
     70  await revealFnSecond(inspector, slotChildContainers[1].node);
     71  is(inspector.selection.nodeFront.id, "el2", "The right node was selected");
     72  is(hostContainer.getChildContainers()[2].node, inspector.selection.nodeFront);
     73 }
     74 
     75 async function checkRevealLink(actionFn, inspector, node) {
     76  const slottedContainer = inspector.markup.getContainer(node, true);
     77  info("Select the slotted container for the element");
     78  await selectNode(node, inspector, "no-reason", true);
     79  ok(inspector.selection.isSlotted(), "The selection is the slotted version");
     80  ok(
     81    inspector.markup.getSelectedContainer().isSlotted(),
     82    "The selected container is slotted"
     83  );
     84 
     85  const link = slottedContainer.elt.querySelector(".reveal-link");
     86  is(
     87    link.getAttribute("role"),
     88    "link",
     89    "Reveal link has the role=link attribute"
     90  );
     91 
     92  info("Click on the reveal link and wait for the new node to be selected");
     93  await actionFn(inspector, slottedContainer);
     94  const selectedFront = inspector.selection.nodeFront;
     95  is(selectedFront, node, "The same node front is still selected");
     96  ok(
     97    !inspector.selection.isSlotted(),
     98    "The selection is not the slotted version"
     99  );
    100  // wait until the selected container isn't the one we had before.
    101  await waitFor(
    102    () => inspector.markup.getSelectedContainer() !== slottedContainer
    103  );
    104  ok(
    105    !inspector.markup.getSelectedContainer().isSlotted(),
    106    "The selected container is not slotted"
    107  );
    108 }