tor-browser

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

browser_inspector_menu-03-paste-items.js (5070B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 http://creativecommons.org/publicdomain/zero/1.0/ */
      3 "use strict";
      4 
      5 // Test that different paste items work in the context menu
      6 
      7 const TEST_URL = URL_ROOT + "doc_inspector_menu.html";
      8 const PASTE_ADJACENT_HTML_DATA = [
      9  {
     10    desc: "As First Child",
     11    clipboardData: "2",
     12    menuId: "node-menu-pastefirstchild",
     13  },
     14  {
     15    desc: "As Last Child",
     16    clipboardData: "4",
     17    menuId: "node-menu-pastelastchild",
     18  },
     19  {
     20    desc: "Before",
     21    clipboardData: "1",
     22    menuId: "node-menu-pastebefore",
     23  },
     24  {
     25    desc: "After",
     26    clipboardData: "<span>5</span>",
     27    menuId: "node-menu-pasteafter",
     28  },
     29 ];
     30 
     31 var clipboard = require("resource://devtools/shared/platform/clipboard.js");
     32 registerCleanupFunction(() => {
     33  clipboard = null;
     34 });
     35 
     36 add_task(async function () {
     37  const { inspector } = await openInspectorForURL(TEST_URL);
     38 
     39  await testPasteOuterHTMLMenu();
     40  await testPasteInnerHTMLMenu();
     41  await testPasteAdjacentHTMLMenu();
     42 
     43  async function testPasteOuterHTMLMenu() {
     44    info("Testing that 'Paste Outer HTML' menu item works.");
     45 
     46    await SimpleTest.promiseClipboardChange(
     47      "this was pasted (outerHTML)",
     48      () => {
     49        clipboard.copyString("this was pasted (outerHTML)");
     50      }
     51    );
     52 
     53    const outerHTMLSelector = "#paste-area h1";
     54 
     55    const nodeFront = await getNodeFront(outerHTMLSelector, inspector);
     56    await selectNode(nodeFront, inspector);
     57 
     58    const allMenuItems = openContextMenuAndGetAllItems(inspector, {
     59      target: getContainerForNodeFront(nodeFront, inspector).tagLine,
     60    });
     61 
     62    const onNodeReselected = inspector.markup.once("reselectedonremoved");
     63    allMenuItems.find(item => item.id === "node-menu-pasteouterhtml").click();
     64 
     65    info("Waiting for inspector selection to update");
     66    await onNodeReselected;
     67 
     68    const outerHTML = await getContentPageElementProperty("body", "outerHTML");
     69    ok(
     70      outerHTML.includes(clipboard.getText()),
     71      "Clipboard content was pasted into the node's outer HTML."
     72    );
     73    ok(
     74      !(await hasMatchingElementInContentPage(outerHTMLSelector)),
     75      "The original node was removed."
     76    );
     77  }
     78 
     79  async function testPasteInnerHTMLMenu() {
     80    info("Testing that 'Paste Inner HTML' menu item works.");
     81 
     82    await SimpleTest.promiseClipboardChange(
     83      "this was pasted (innerHTML)",
     84      () => {
     85        clipboard.copyString("this was pasted (innerHTML)");
     86      }
     87    );
     88    const innerHTMLSelector = "#paste-area .inner";
     89    const getInnerHTML = () =>
     90      getContentPageElementProperty(innerHTMLSelector, "innerHTML");
     91    const origInnerHTML = await getInnerHTML();
     92 
     93    const nodeFront = await getNodeFront(innerHTMLSelector, inspector);
     94    await selectNode(nodeFront, inspector);
     95 
     96    const allMenuItems = openContextMenuAndGetAllItems(inspector, {
     97      target: getContainerForNodeFront(nodeFront, inspector).tagLine,
     98    });
     99 
    100    const onMutation = inspector.once("markupmutation");
    101    allMenuItems.find(item => item.id === "node-menu-pasteinnerhtml").click();
    102    info("Waiting for mutation to occur");
    103    await onMutation;
    104 
    105    Assert.strictEqual(
    106      await getInnerHTML(),
    107      clipboard.getText(),
    108      "Clipboard content was pasted into the node's inner HTML."
    109    );
    110    ok(
    111      await hasMatchingElementInContentPage(innerHTMLSelector),
    112      "The original node has been preserved."
    113    );
    114    await undoChange(inspector);
    115    Assert.strictEqual(
    116      await getInnerHTML(),
    117      origInnerHTML,
    118      "Previous innerHTML has been restored after undo"
    119    );
    120  }
    121 
    122  async function testPasteAdjacentHTMLMenu() {
    123    const refSelector = "#paste-area .adjacent .ref";
    124    const adjacentNodeSelector = "#paste-area .adjacent";
    125    const nodeFront = await getNodeFront(refSelector, inspector);
    126    await selectNode(nodeFront, inspector);
    127    const markupTagLine = getContainerForNodeFront(
    128      nodeFront,
    129      inspector
    130    ).tagLine;
    131 
    132    for (const { clipboardData, menuId } of PASTE_ADJACENT_HTML_DATA) {
    133      const allMenuItems = openContextMenuAndGetAllItems(inspector, {
    134        target: markupTagLine,
    135      });
    136      info(`Testing ${menuId} for ${clipboardData}`);
    137 
    138      await SimpleTest.promiseClipboardChange(clipboardData, () => {
    139        clipboard.copyString(clipboardData);
    140      });
    141 
    142      const onMutation = inspector.once("markupmutation");
    143      allMenuItems.find(item => item.id === menuId).click();
    144      info("Waiting for mutation to occur");
    145      await onMutation;
    146    }
    147 
    148    let html = await getContentPageElementProperty(
    149      adjacentNodeSelector,
    150      "innerHTML"
    151    );
    152    Assert.strictEqual(
    153      html.trim(),
    154      '1<span class="ref">234</span><span>5</span>',
    155      "The Paste as Last Child / as First Child / Before / After worked as " +
    156        "expected"
    157    );
    158    await undoChange(inspector);
    159 
    160    html = await getContentPageElementProperty(
    161      adjacentNodeSelector,
    162      "innerHTML"
    163    );
    164    Assert.strictEqual(
    165      html.trim(),
    166      '1<span class="ref">234</span>',
    167      "Undo works for paste adjacent HTML"
    168    );
    169  }
    170 });