tor-browser

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

browser_inspector_picker-useragent-widget.js (2625B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 const TEST_URI = `data:text/html;charset=utf-8,
      8  <!DOCTYPE html>
      9  <video controls></video>`;
     10 
     11 // Test that using the node picker on user agent widgets only selects shadow dom
     12 // elements if `devtools.inspector.showAllAnonymousContent` is true.
     13 // If not, we should only surface the host, in this case, the <video>.
     14 //
     15 // For this test we use a <video controls> tag, which is using shadow dom.
     16 add_task(async function () {
     17  // Run the test for both values for devtools.inspector.showAllAnonymousContent
     18  await runUserAgentWidgetPickerTest({ enableAnonymousContent: false });
     19  await runUserAgentWidgetPickerTest({ enableAnonymousContent: true });
     20 });
     21 
     22 async function runUserAgentWidgetPickerTest({ enableAnonymousContent }) {
     23  await pushPref(
     24    "devtools.inspector.showAllAnonymousContent",
     25    enableAnonymousContent
     26  );
     27  const { inspector, toolbox } = await openInspectorForURL(TEST_URI);
     28 
     29  info("Use the node picker inside the <video> element");
     30  await startPicker(toolbox);
     31  const onPickerStopped = toolbox.nodePicker.once("picker-stopped");
     32  await pickElement(inspector, "video", 5, 5);
     33  await onPickerStopped;
     34 
     35  const selectedNode = inspector.selection.nodeFront;
     36  if (enableAnonymousContent) {
     37    // We do not assert specifically which node was selected, we just want to
     38    // check the node was under the shadow DOM for the <video type=date>
     39    const shadowHost = getShadowHost(selectedNode);
     40    Assert.notStrictEqual(
     41      selectedNode.tagName.toLowerCase(),
     42      "video",
     43      "The selected node is not the <video>"
     44    );
     45    ok(shadowHost, "The selected node is in a shadow root");
     46    is(shadowHost.tagName.toLowerCase(), "video", "The shadowHost is <video>");
     47  } else {
     48    is(
     49      selectedNode.tagName.toLowerCase(),
     50      "video",
     51      "The selected node is the <video>"
     52    );
     53  }
     54 }
     55 
     56 /**
     57 * Retrieve the nodeFront for the shadow host containing the provided nodeFront.
     58 * Returns null if the nodeFront is not in a shadow DOM.
     59 *
     60 * @param {NodeFront} nodeFront
     61 *        The nodeFront for which we want to retrieve the shadow host.
     62 * @return {NodeFront} The nodeFront corresponding to the shadow host, or null
     63 *         if the nodeFront is not in shadow DOM.
     64 */
     65 function getShadowHost(nodeFront) {
     66  let parent = nodeFront;
     67  while (parent) {
     68    if (parent.isShadowHost) {
     69      return parent;
     70    }
     71    parent = parent.parentOrHost();
     72  }
     73  return null;
     74 }