summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/contextMenu/browser_contextmenu_contenteditable.js
blob: ccb0be8d951f7a7e389108e30dced78688e285fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

let contextMenu;

const example_base =
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  "http://example.com/browser/browser/base/content/test/contextMenu/";
const chrome_base =
  "chrome://mochitests/content/browser/browser/base/content/test/contextMenu/";

/* import-globals-from contextmenu_common.js */
Services.scriptloader.loadSubScript(
  chrome_base + "contextmenu_common.js",
  this
);

async function openMenuAndPaste(browser, useFormatting) {
  const kElementToUse = "test-contenteditable-spellcheck-false";
  let oldText = await SpecialPowers.spawn(browser, [kElementToUse], elemID => {
    return content.document.getElementById(elemID).textContent;
  });

  // Open context menu and paste
  await test_contextmenu(
    "#" + kElementToUse,
    [
      "context-undo",
      null, // whether we can undo changes mid-test.
      "context-redo",
      false,
      "---",
      null,
      "context-cut",
      false,
      "context-copy",
      false,
      "context-paste",
      true,
      "context-paste-no-formatting",
      true,
      "context-delete",
      false,
      "context-selectall",
      true,
    ],
    {
      keepMenuOpen: true,
    }
  );
  let popupHidden = BrowserTestUtils.waitForPopupEvent(contextMenu, "hidden");
  let menuID = "context-paste" + (useFormatting ? "" : "-no-formatting");
  contextMenu.activateItem(document.getElementById(menuID));
  await popupHidden;
  await SpecialPowers.spawn(
    browser,
    [kElementToUse, oldText, useFormatting],
    (elemID, textToReset, expectStrong) => {
      let node = content.document.getElementById(elemID);
      Assert.stringContains(
        node.textContent,
        "Bold text",
        "Text should have been pasted"
      );
      if (expectStrong) {
        isnot(
          node.querySelector("strong"),
          null,
          "Should be markup in the text."
        );
      } else {
        is(
          node.querySelector("strong"),
          null,
          "Should be no markup in the text."
        );
      }
      node.textContent = textToReset;
    }
  );
}

add_task(async function test_contenteditable() {
  // Put some HTML on the clipboard:
  const xferable = Cc["@mozilla.org/widget/transferable;1"].createInstance(
    Ci.nsITransferable
  );
  xferable.init(null);
  xferable.addDataFlavor("text/html");
  xferable.setTransferData(
    "text/html",
    PlacesUtils.toISupportsString("<strong>Bold text</strong>")
  );
  xferable.addDataFlavor("text/plain");
  xferable.setTransferData(
    "text/plain",
    PlacesUtils.toISupportsString("Bold text")
  );
  Services.clipboard.setData(
    xferable,
    null,
    Services.clipboard.kGlobalClipboard
  );

  let url = example_base + "subtst_contextmenu.html";
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url,
    },
    async function (browser) {
      await openMenuAndPaste(browser, false);
      await openMenuAndPaste(browser, true);
    }
  );
});