summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_paste_redirect_focus_in_paste_event_listener.html
blob: b82938158e9feb5b184a0a2a090056269d9d4115 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Testing handling "paste" command when a "paste" event listener moves focus</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script>
"use strict";

SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(async () => {
  info("Waiting for initializing clipboard...");
  await SimpleTest.promiseClipboardChange(
    "plain text",
    () => SpecialPowers.clipboardCopyString("plain text")
  );

  const transferable =
    SpecialPowers.Cc["@mozilla.org/widget/transferable;1"].createInstance(SpecialPowers.Ci.nsITransferable);
  transferable.init(
    SpecialPowers.wrap(window).docShell.QueryInterface(SpecialPowers.Ci.nsILoadContext)
  );
  const supportString =
    SpecialPowers.Cc["@mozilla.org/supports-string;1"].createInstance(SpecialPowers.Ci.nsISupportsString);
  supportString.data = "plain text";
  transferable.setTransferData("text/plain", supportString);

  function getValue(aElement) {
    if (aElement.tagName.toLowerCase() == "input" ||
        aElement.tagName.toLowerCase() == "textarea") {
      return aElement.value;
    }
    return aElement.textContent;
  }
  function setValue(aElement, aValue) {
    if (aElement.tagName.toLowerCase() == "input" ||
        aElement.tagName.toLowerCase() == "textarea") {
      aElement.value = aValue;
      return;
    }
    aElement.innerHTML = aValue === "" ? "<br>" : aValue;
  }

  for (const command of [
    "cmd_paste",
    "cmd_pasteNoFormatting",
    "cmd_pasteQuote",
    "cmd_pasteTransferable"
  ]) {
    for (const editableSelector of [
      "#src > input",
      "#src > textarea",
      "#src > div[contenteditable]"
    ]) {
      const editableElement = document.querySelector(editableSelector);
      const editableElementDesc = `<${
        editableElement.tagName.toLocaleLowerCase()
      }${editableElement.hasAttribute("contenteditable") ? " contenteditable" : ""}>`;
      (test_from_editableElement_to_input => {
        const input = document.querySelector("#dest > input");
        editableElement.focus();
        editableElement.addEventListener(
          "paste",
          () => input.focus(),
          {once: true}
        );
        SpecialPowers.doCommand(window, command, transferable);
        is(
          getValue(editableElement).replace(/\n/g, ""),
          "",
          `${command}: ${
            editableElementDesc
          } should not have the pasted text because focus is redirected to <input> in a "paste" event listener`
        );
        is(
          input.value.replace("> ", ""),
          "plain text",
          `${command}: new focused <input> (moved from ${
            editableElementDesc
          }) should have the pasted text`
        );
        setValue(editableElement, "");
        input.value = "";
      })();

      (test_from_editableElement_to_contenteditable => {
        const contentEditable = document.querySelector("#dest > div[contenteditable]");
        editableElement.focus();
        editableElement.addEventListener(
          "paste",
          () => contentEditable.focus(),
          {once: true}
        );
        SpecialPowers.doCommand(window, command, transferable);
        is(
          getValue(editableElement).replace(/\n/g, ""),
          "",
          `${command}: ${
            editableElementDesc
          } should not have the pasted text because focus is redirected to <div contenteditable> in a "paste" event listener`
        );
        is(
          contentEditable.textContent.replace(/\n/g, "").replace("> ", ""),
          "plain text",
          `${command}: new focused <div contenteditable> (moved from ${
            editableElementDesc
          }) should have the pasted text`
        );
        setValue(editableElement, "");
        contentEditable.innerHTML = "<br>";
      })();

      (test_from_editableElement_to_non_editable => {
        const button = document.querySelector("#dest > button");
        editableElement.focus();
        editableElement.addEventListener(
          "paste",
          () => button.focus(),
          {once: true}
        );
        SpecialPowers.doCommand(window, command, transferable);
        is(
          getValue(editableElement).replace(/\n/g, ""),
          "",
          `${command}: ${
            editableElementDesc
          } should not have the pasted text because focus is redirected to <button> in a "paste" event listener`
        );
        setValue(editableElement, "");
      })();
    }
  }

  SimpleTest.finish();
});
</script>
</head>
<body>
<div id="src"><input><textarea></textarea><div contenteditable><br></div></div>
<div id="dest"><input><div contenteditable><br></div><button>button</button></div>
</body>
</html>