File: test_replace_text.html

package info (click to toggle)
firefox 147.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,484 kB
  • sloc: cpp: 7,607,246; javascript: 6,533,185; ansic: 3,775,227; python: 1,415,393; xml: 634,561; asm: 438,951; java: 186,241; sh: 62,752; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (405 lines) | stat: -rw-r--r-- 12,946 bytes parent folder | download | duplicates (3)
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<!DOCTYPE html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1149826
-->
<html>
<head>
<title>Test for replaceText</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>

<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1149826">Mozilla Bug 1149826</a><br>
<input type="text"><br>
<textarea></textarea>
<div contenteditable></div>

<script>
const gDOMWindowUtils = _getDOMWindowUtils(window);
const Ci = SpecialPowers.Ci;
const IS_WIN = navigator.platform.indexOf("Win") == 0;

async function testReplaceText(INPUT_TESTS, TEXTAREA_TESTS, CONTENTEDITABLE_TESTS, aPreventSetSelection) {
  await SimpleTest.promiseFocus();

  const flags = aPreventSetSelection ?
    Ci.nsIDOMWindowUtils.CONTENT_COMMAND_FLAG_PREVENT_SET_SELECTION :
    0;

  const input = document.querySelector("input");
  input.focus();
  await new Promise(resolve => SimpleTest.executeSoon(resolve));

  info("for <input>");

  for (const TEST of INPUT_TESTS) {
    input.value = TEST.before.value;
    input.selectionStart = TEST.before.start;
    input.selectionEnd = TEST.before.end;
    await new Promise(resolve => SimpleTest.executeSoon(resolve));

    input.addEventListener("beforeinput", e => {
      is(e.inputType, "insertReplacementText",
         "inputType in input must be insertReplacementText by replaceText");
      is(input.selectionStart, TEST.replace.start,
         "Before inputReplacementText, start offset should be changed to the replace start");
      is(input.selectionEnd, TEST.replace.start + TEST.replace.src.length,
         "Before inputReplacementText, end offset should be changed to the replace end");
    }, { once: true } );

    const promiseAfterOnInput =
      new Promise(resolve => input.addEventListener("input", e => {
        is(e.inputType, "insertReplacementText",
           "inputType must be insertReplacementText by replaceText");
        resolve();
      }, { once: true } ));
    gDOMWindowUtils.sendContentCommandEvent(
      "replaceText",
      null,
      TEST.replace.value,
      TEST.replace.start,
      TEST.replace.src,
      flags
    );
    await promiseAfterOnInput

    is(input.value, TEST.after.value,
       "replaceText in input replaces inner text");
    is(input.selectionStart, TEST.after.start,
       "replaceText in input sets expected selection start");
    is(input.selectionEnd, TEST.after.end,
       "replaceText in input sets expected selection end");
  }

  const textarea = document.querySelector("textarea");
  textarea.focus();
  await new Promise(resolve => SimpleTest.executeSoon(resolve));

  info("for <textarea>");

  for (const TEST of TEXTAREA_TESTS) {
    textarea.value = TEST.before.value;
    textarea.selectionStart = TEST.before.start;
    textarea.selectionEnd = TEST.before.end;

    textarea.addEventListener("beforeinput", e => {
      is(e.inputType, "insertReplacementText",
         "inputType must be insertReplacementText by replaceText");
      is(textarea.selectionStart, TEST.replace.start,
         "Before inputReplacementText, start offset be changed to the replace start");
      is(textarea.selectionEnd, TEST.replace.start + TEST.replace.src.length,
         "Before inputReplacementText, end offset be changed to the replace end");
    }, { once: true } );

    const promiseAfterOnTextarea =
      new Promise(resolve => textarea.addEventListener("input", e => {
        is(e.inputType, "insertReplacementText",
           "inputType must be insertReplacementText by replaceText");
        resolve();
      }, { once: true } ));
    gDOMWindowUtils.sendContentCommandEvent(
      "replaceText",
      null,
      TEST.replace.value,
      TEST.replace.start,
      TEST.replace.src,
      flags
    );
    await promiseAfterOnTextarea

    is(textarea.value, TEST.after.value,
      "replaceText in textarea replaces inner text");
    is(textarea.selectionStart, TEST.after.start,
       "replaceText in textarea sets expected selection start");
    is(textarea.selectionEnd, TEST.after.end,
       "replaceText in textarea sets expected selection end");
  }

  const editingHost = document.querySelector("div[contenteditable]");
  editingHost.focus();
  await new Promise(resolve => SimpleTest.executeSoon(resolve));

  info("for contenteditable");

  for (const TEST of CONTENTEDITABLE_TESTS) {
    editingHost.innerHTML = TEST.before.value;
    window.getSelection().setBaseAndExtent(
        // eslint-disable-next-line no-eval
        eval(TEST.before.focusNode),
        TEST.before.focusOffset,
        // eslint-disable-next-line no-eval
        eval(TEST.before.focusNode),
        TEST.before.focusOffset
    );

    editingHost.addEventListener("beforeinput", e => {
      const selection = window.getSelection();
      is(e.inputType, "insertReplacementText",
         "inputType must be insertReplacementText by replaceText");
      // eslint-disable-next-line no-eval
      is(selection.anchorNode, eval(TEST.replace.textNode),
         "Before inputReplacementText, focus node is the Text containing replacing text");
      is(selection.anchorOffset, TEST.replace.start,
         "Before inputReplacementText, focus offset is start of the replace start");
      // eslint-disable-next-line no-eval
      is(selection.focusNode, eval(TEST.replace.textNode),
         "Before inputReplacementText, focus node is the Text containing replacing text");
      is(selection.focusOffset, TEST.replace.start + TEST.replace.src.length,
         "Before inputReplacementText, focus offset is start of the replace start");
    }, { once: true } );

    const promiseAfterEditingHost =
      new Promise(resolve => editingHost.addEventListener("input", e => {
        is(e.inputType, "insertReplacementText",
           "inputType must be insertReplacementText by replaceText");
        resolve();
      }, { once: true } ));
    gDOMWindowUtils.sendContentCommandEvent(
      "replaceText",
      null,
      TEST.replace.value,
      TEST.replace.start,
      TEST.replace.src,
      flags
    );
    await promiseAfterEditingHost

    is(editingHost.textContent, TEST.after.value,
       "replaceText in contenteditable replaces inner text");
    const selection = window.getSelection();
    // eslint-disable-next-line no-eval
    is(selection.focusNode, eval(TEST.after.focusNode),
       "replaceText in contenteditable sets expected focusNode");
    is(selection.focusOffset, TEST.after.focusOffset,
       "replaceText in contenteditable sets expected focusOffset");
  }
}

add_task(async function testReplaceTextWithoutPreventSetSelection() {
  const INPUT_TESTS = [
  { before: {
      value: "foo", start: 3, end: 3
    },
    replace: {
      src: "o", value: "bar", start: 1
    },
    after: {
      value: "fbaro", start: 4, end: 4
    }
  },
  { before: {
      value: "foo ", start: 4, end: 4
    },
    replace: {
      src: "oo", value: "bar", start: 1
    },
    after: {
      value: "fbar ", start: 4, end: 4
    }
  }];

  const TEXTAREA_TESTS = [
  { before: {
      value: "foo", start: 3, end: 3
    },
    replace: {
      src: "o", value: "bar", start: 1
    },
    after: {
      value: "fbaro", start: 4, end: 4
    }
  },
  { before: {
      value: "foo ", start: 4, end: 4
    },
    replace: {
      src: "oo", value: "bar", start: 1
    },
    after: {
      value: "fbar ", start: 4, end: 4
    }
  }];

  const CONTENTEDITABLE_TESTS = [
  { before: {
      value: "foo", focusNode: "editingHost.firstChild", focusOffset: 3
    },
    replace: {
      src: "o", value: "bar", start: 1, textNode: "editingHost.firstChild",
    },
    after: {
      value: "fbaro", focusNode: "editingHost.firstChild", focusOffset: 4, isCollapsed: true
    },
  },
  { before: {
      value: "foo foo", focusNode: "editingHost.firstChild", focusOffset: 4
    },
    replace: {
      src: "oo", value: "bar", start: 1, textNode: "editingHost.firstChild",
    },
    after: {
      value: "fbar foo", focusNode: "editingHost.firstChild", focusOffset: 4, isCollapsed: true
    },
  }];

  await testReplaceText(INPUT_TESTS, TEXTAREA_TESTS, CONTENTEDITABLE_TESTS, false);
});

add_task(async function testReplaceTextWithPreventSetSelection() {
  const INPUT_TESTS = [
  { before: {
      value: "foo", start: 3, end: 3
    },
    replace: {
      src: "o", value: "bar", start: 1
    },
    after: {
      value: "fbaro", start: 5, end: 5
    }
  },
  { before: {
      value: "foo ", start: 4, end: 4
    },
    replace: {
      src: "oo", value: "bar", start: 1
    },
    after: {
      value: "fbar ", start: 5, end: 5
    }
  }];

  const TEXTAREA_TESTS = [
  { before: {
      value: "foo", start: 3, end: 3
    },
    replace: {
      src: "o", value: "bar", start: 1
    },
    after: {
      value: "fbaro", start: 5, end: 5
    }
  },
  { before: {
      value: "foo ", start: 4, end: 4
    },
    replace: {
      src: "oo", value: "bar", start: 1
    },
    after: {
      value: "fbar ", start: 5, end: 5
    }
  }];

  const CONTENTEDITABLE_TESTS = [
  { before: {
      value: "foo", focusNode: "editingHost.firstChild", focusOffset: 3
    },
    replace: {
      src: "o", value: "bar", start: 1, textNode: "editingHost.firstChild",
    },
    after: {
      value: "fbaro", focusNode: "editingHost.firstChild", focusOffset: 5, isCollapsed: true
    },
  },
  { before: {
      value: "foo foo", focusNode: "editingHost.firstChild", focusOffset: 4
    },
    replace: {
      src: "oo", value: "bar", start: 1, textNode: "editingHost.firstChild",
    },
    after: {
      value: "fbar foo", focusNode: "editingHost.firstChild", focusOffset: 5, isCollapsed: true
    },
  }];

  await testReplaceText(INPUT_TESTS, TEXTAREA_TESTS, CONTENTEDITABLE_TESTS, true);
});

add_task(async function testReplaceTextWithCompositionText() {
  await SimpleTest.promiseFocus();

  // Don't replace text during composition
  const input = document.querySelector("input");
  input.value = "";
  input.focus();
  await new Promise(resolve => SimpleTest.executeSoon(resolve));

  let promise =
    new Promise(resolve => input.addEventListener("compositionupdate", resolve, { once: true }));
  synthesizeCompositionChange(
    { "composition":
      { "string": "foo",
        "clauses":
        [
          { "length": 3, "attr": COMPOSITION_ATTR_RAW_CLAUSE }
        ]
      },
    });
  await promise;

  input.addEventListener("input", e => {
    isnot(e.inputType, "insertReplacementText",
          "Don't fire insertReplacementText input event by replaceText");
  }, { once: true } );
  gDOMWindowUtils.sendContentCommandEvent("replaceText", null, "bar", 1, "o");
  await new Promise(resolve => SimpleTest.executeSoon(resolve));

  promise = new Promise(resolve => input.addEventListener("compositionend", resolve, { once: true }));
  synthesizeComposition({type: "compositioncommitasis", key: {key: "KEY_Enter"}});
  await promise;

  is(input.value, "foo",
     "replaceText doesn't replace inner text when having composition");
  is(input.selectionStart, 3, "replaceText sets caret position to next of replaced text");
  is(input.selectionStart, input.selectionEnd, "replaceText sets that selection is collapsed");
});

add_task(async function testReplaceTextBeforeCallingPreventDefault() {
  await SimpleTest.promiseFocus();

  // Call preventDefault on beforeinput
  const input = document.querySelector("input");
  input.value = "foo";
  input.focus();
  await new Promise(resolve => SimpleTest.executeSoon(resolve));

  input.selectionStart = 1
  input.selectionEnd = 2;

  const promise = new Promise(resolve => input.addEventListener("beforeinput", e => {
    e.preventDefault();
    resolve();
  }, { once: true }));
  gDOMWindowUtils.sendContentCommandEvent("replaceText", null, "bar", 1, "o");
  await promise;

  is(input.value, "foo",
     "replaceText doesn't replace inner text of <input> since preventDefault is called");
  is(input.selectionStart, 1, "selectionStart isn't changed since preventDefault is called");
  is(input.selectionEnd, 2, "selectionEnd isn't changed since preventDefault is called");
});

add_task(async function testReplaceTextWithoutMatch() {
  await SimpleTest.promiseFocus();

  const input = document.querySelector("input");
  input.value = "foo";
  input.focus();
  await new Promise(resolve => SimpleTest.executeSoon(resolve));

  input.selectionStart = 1
  input.selectionEnd = 2;

  gDOMWindowUtils.sendContentCommandEvent("replaceText", null, "bar", 1, "a");
  await new Promise(resolve => SimpleTest.executeSoon(resolve));

  is(input.value, "foo",
     "replaceText doesn't replace inner text of <input> due to not matched");
  is(input.selectionStart, 1, "selectionStart isn't changed due to failed");
  is(input.selectionEnd, 2, "selectionEnd isn't changed due to failed");
});
</script>
</body>
</html>