summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/mozilla/tests/editor/delete-space-after-double-click-selection.html
blob: 6c065cbc12ca5f495d69dddb68ff42e24f23ae5c (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="timeout" content="long">
  <title>Test for Bug 1783641</title>
  <script src=/resources/testharness.js></script>
  <script src=/resources/testharnessreport.js></script>
  <script src="/resources/testdriver.js"></script>
  <script src="/resources/testdriver-actions.js"></script>
  <style>
    .testStyle {
      font-family: 'Courier New', Courier, monospace;
      font-size: 12px;
      padding: 0px;
      width: 200px;
    }
  </style>
</head>
<body>
  <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1783641">Mozilla Bug 1783641</a><br />
  <span class="testStyle" id="placeholder"></span>
  <input class="testStyle" type="text" />
  <div class="testStyle" contenteditable></div>
  <textarea class="testStyle"></textarea>
  <script>

    promise_test(async t => {
      await new Promise(resolve => { window.onload = resolve; });

      await SpecialPowers.pushPrefEnv({
        set: [
          ["editor.word_select.delete_space_after_doubleclick_selection", true],
          ["layout.word_select.eat_space_to_next_word", false]
        ]
      });
    }, "Test setup");
    const placeHolder = document.getElementById("placeholder");
    const deleteKey = "\uE017";

    function waitForRender() {
      return new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
    };

    for (const selector of ["input", "div[contenteditable]", "textarea"]) {
      const editableElement = document.querySelector(selector);
      editableElement.focus();

      /**
       * Helper functions to set or get the value and the current selection of `editableElement`,
       * regardless of its actual type.
       */
      const setValue = (aValue) => {
        editableElement.tagName.toLowerCase() == "div"
          ? editableElement.innerHTML = aValue
          : editableElement.value = aValue;
      }
      const getValue = () => {
        return editableElement.tagName.toLowerCase() == "div"
          ? editableElement.innerHTML
          : editableElement.value;
      };
      const getSelection = () => {
        return editableElement.tagName.toLowerCase() == "div"
          ? document.getSelection().toString()
          : editableElement.value.substring(
            editableElement.selectionStart,
            editableElement.selectionEnd
          );
      };

      /**
       * Places a double click in `editableElement` at exactly the end of `aPlaceHolderText` and press delete.
       * `aPlaceholderText` therefore should contain the same text as the value of the `editableElement`
       * up to the point where the doubleclick should happen.
       *
       * If `aSelectionValue` is defined, the selection created by the double click is compared to `aSelectionValue`.
       */
      const doubleClickAndDelete = async (aPlaceHolderText, aSelectionValue = undefined) => {
        placeHolder.innerHTML = aPlaceHolderText;
        editableElement.focus();
        await waitForRender();
        const absInputPos = editableElement.getBoundingClientRect();
        selectionOffset = {
          x: placeHolder.getBoundingClientRect().width,
          y: Math.floor(placeHolder.getBoundingClientRect().height / 2)
        };
        await (new test_driver.Actions()
          // for some reason this still doesn't work:
          // .pointerMove(Math.floor(selectionOffset.x), Math.floor(selectionOffset.y), { origin: editableElement })
          // but this does:
          .pointerMove(
            Math.floor(absInputPos.x + selectionOffset.x),
            Math.floor(absInputPos.y + selectionOffset.y),
            { origin: "viewport" }
          )
          .pointerDown()
          .pointerUp()
          .pointerDown()
          .pointerUp())
          .send()
        await waitForRender();
        if (aSelectionValue !== undefined) {
          assert_equals(getSelection(), aSelectionValue, "Wrong selection value!");
        }
        return test_driver.send_keys(editableElement, deleteKey);
      };
      if (editableElement.tagName.toLowerCase() == "div") {
        promise_test(async t => {
          setValue("<p>abc def<span></span></p>");
          await doubleClickAndDelete("abc de", "def");
          await waitForRender();
          assert_equals(
            getValue(),
            "<p>abc</p>",
            "The <span> at the end of the string must be removed, as well as the whitespace in between words.");
        }, `${editableElement.tagName}: An empty span at the end of the selection should be considered end of selection!`);
      }
      promise_test(async t => {
        setValue("one two");
        await doubleClickAndDelete("on", "one");
        await waitForRender();
        assert_equals(
          getValue(),
          "two",
          "The whitespace between words must be removed when a word at the beginning is selected and deleted!"
        );
      }, `${editableElement.tagName}: Remove word at the beginning of string should remove the whitespace in between.`);

      promise_test(async t => {
        setValue("one two");
        await doubleClickAndDelete("one tw", "two");
        await waitForRender();
        assert_equals(
          getValue(),
          "one",
          "The whitespace between words must be removed when a word is selected at the end of the string and deleted!"
        );
      }, `${editableElement.tagName}: Remove word at the end of a string should remove the whitespace in between.`);

      promise_test(async t => {
        setValue("one two three");
        await doubleClickAndDelete("one tw", "two");
        await waitForRender();
        assert_equals(
          getValue(),
          "one three",
          "One whitespace between words must be removed when a word is selected and deleted!"
        );
        await waitForRender();
        if (editableElement.tagName.toLowerCase() == "div") {
          document.getSelection().setBaseAndExtent(
            editableElement.firstChild,
            0,
            editableElement.firstChild,
            3
          );
        }
        else {
          editableElement.setSelectionRange(0, 3);
        }
        await test_driver.send_keys(editableElement, deleteKey);
        // div[contenteditable] returns '&nbsp;three' here.
        assert_equals(
          getValue().replace(/&nbsp;/g, " "),
          " three",
          "The whitespace must not be removed when selecting a word without doubleclicking it!"
        );

      }, `${editableElement.tagName}: Remove word in the middle of a string should remove one whitespace ` +
      "only if selection is created by double click.");

      promise_test(async t => {
        setValue("one  two three");
        await doubleClickAndDelete("one  tw", "two");
        await waitForRender();
        assert_equals(
          getValue(),
          "one  three",
          "One whitespace between words must be removed when a word is selected and deleted!"
        );
      }, `${editableElement.tagName}: Only one whitespace character should be removed when there are multiple.`);

      promise_test(async t => {
        setValue("one  two");
        await doubleClickAndDelete("one  tw", "two");
        await waitForRender();
        assert_equals(
          getValue(),
          "one ",
          "One whitespace character between words must be removed when a word is selected and deleted!"
        );
      }, `${editableElement.tagName}: Only one whitespace character should be removed when ` +
      "there are multiple whitespaces and the deleted range is the end of the string.");

      promise_test(async t => {
        setValue("one two, three");
        await doubleClickAndDelete("one tw", "two");
        await waitForRender();
        assert_equals(
          getValue(),
          "one, three",
          "The whitespace in front of the selected word must be removed when punctuation follows selection!"
        );
      }, `${editableElement.tagName}: Removing a word before punctuation should remove the whitespace.`);

      promise_test(async t => {
        setValue("one, two");
        await doubleClickAndDelete("one, tw", "two");
        await waitForRender();
        assert_equals(
          getValue(),
          "one,",
          "The whitespace in front of the selected word must be removed!"
        );
      }, `${editableElement.tagName}: Remove a word after punctuation should remove the whitespace.`);

      promise_test(async t => {
        setValue("one\u00A0two, three"); // adds a &nbsp;
        await doubleClickAndDelete("one tw", "two");
        await waitForRender();
        assert_equals(
          getValue(),
          "one, three",
          "The whitespace between words must be removed when a word is selected and deleted!"
        );
      }, `${editableElement.tagName}: Removing a word between a &nbsp; and punctuation should remove the nbsp character.`);

      if (editableElement.tagName.toLowerCase() == "div") {
        promise_test(async t => {
          setValue("one two<br>");
          await doubleClickAndDelete("one tw", "two");
          await waitForRender();
          assert_equals(
            getValue(),
            "one<br>",
            "The line break must be preserved!"
          );
        }, `${editableElement.tagName}: Removing a word in front of a line break should preserve the line break.`);
      }
      if (editableElement.tagName.toLowerCase() == "textarea") {
        promise_test(async t => {
          setValue("one two\n");
          await doubleClickAndDelete("one tw", "two");
          await waitForRender();
          assert_equals(
            getValue(),
            "one\n",
            "The line break must be preserved!"
          );
        }, `${editableElement.tagName}: RRemoving a word in front of a line break should preserve the line break.`);
      }
      promise_test(async t => {
        setValue("one two");
        await doubleClickAndDelete("on", "one");
        await waitForRender();
        assert_equals(
          getValue(),
          "two",
          "The whitespace between words must be removed when a word at the beginning is selected and deleted!"
        );
        document.execCommand("undo", false, null);
        assert_equals(
          getValue(),
          "one two",
          "Undo action must restore the original state!"
        );
        document.execCommand("redo", false, null);
        assert_equals(
          getValue(),
          "two",
          "Redo action must remove the word and whitespace again!"
        );
      }, `${editableElement.tagName}: Undo and Redo actions should take the removed whitespace into account.`);
    }
  </script>
</body>
</html>