summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/selection/contenteditable/initial-selection-on-focus.tentative.html
blob: fcaf70d877f9b20baed20730df99cb60946e974b (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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
<!doctype html>
<meta charset=utf-8>
<meta name="variant" content="?div">
<meta name="variant" content="?span">
<title>initial selection on focus of contenteditable</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>var testsJsLibraryOnly = true</script>
<script src="../../editing/include/tests.js"></script>
<body>
<script>
"use strict";

(function() {
  const editingHostTagName = document.location.search.substr(1);
  const editor = document.createElement(editingHostTagName);
  editor.style.minHeight = "1em";
  editor.setAttribute("contenteditable", "");
  document.body.insertBefore(editor, document.body.firstChild);
  editor.focus();
  editor.getBoundingClientRect();

  const tests = [
    { description: "empty editor should set focus to start of it",
      content: "{}",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should set selection to start of the text node",
      content: "[]abc",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should set selection to before the <br> node",
      content: "{}<br>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should set selection to before the first <br> node",
      content: "{}<br><br>",
      canTestInInlineEditingHost: true,
    },

    { description: "editor should set selection to start of the text node in the <p> node",
      content: "<p>[]abc</p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should set selection to start of the first visible character in the text node in the <p> node",
      content: "<p> []abc</p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should set selection to start of the text node in the <p> node because of preformatted white-space",
      content: "<p style=\"white-space: pre\">[] abc</p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should set selection to start of the text node in the <p> node because of preformatted line break",
      content: "<p style=\"white-space: pre\">[]\nabc</p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should set selection to before the <br> node in the <p> node",
      content: "<p>{}<br></p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should set selection to before the first <br> node in the <p> node",
      content: "<p>{}<br><br></p>",
      canTestInInlineEditingHost: false,
    },

    { description: "editor should set selection to start of the text node in the <span> node",
      content: "<span>[]abc</span>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should set selection to before the <br> node in the <span> node",
      content: "<span>{}<br></span>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should set selection to before the first <br> node in the <span> node",
      content: "<span>{}<br><br></span>",
      canTestInInlineEditingHost: true,
    },

    { description: "editor should set selection to before the empty <span> node",
      content: "{}<span></span>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should set selection to before the empty <b> node",
      content: "{}<b></b>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should set selection to before the empty <i> node",
      content: "{}<i></i>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should set selection to before the empty <u> node",
      content: "{}<u></u>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should set selection to before the empty <s> node",
      content: "{}<s></s>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should set selection to before the empty <code> node",
      content: "{}<code></code>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should set selection to before the empty <a> node",
      content: "{}<a href=\"foo.html\"></a>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should set selection to before the empty <foobar> node",
      content: "{}<foobar></foobar>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should set selection to before the <input> node",
      content: "{}<input>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should set selection to before the <img> node",
      content: "{}<img alt=\"foo\">",
      canTestInInlineEditingHost: true,
    },

    { description: "editor should set selection to start of the text node in the second <span> node",
      content: "<span></span><span>[]abc</span>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should set selection to before the <br> node in the second <span> node",
      content: "<span></span><span>{}<br></span>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should set selection to start of the text node in the first <span> node #1",
      content: "<span>[]abc</span><span>abc</span>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should set selection to start of the text node in the first <span> node #2",
      content: "<span>[]abc</span><span><br></span>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should set selection to before the <br> node in the first <span> node #1",
      content: "<span>{}<br></span><span><br></span>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should set selection to before the <br> node in the first <span> node #2",
      content: "<span>{}<br></span><span>abc</span>",
      canTestInInlineEditingHost: true,
    },

    { description: "editor should set selection to start of the text node in the second <span> node since the text node in the first <span> node is only whitespaces",
      content: "<span> </span><span>[]abc</span>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should set selection to before the <br> node in the second <span> node since the text node in the first <span> node is only whitespaces",
      content: "<span> </span><span>{}<br></span>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should set selection to start of the text node in the second <span> node even if there is a whitespace only text node before the first <span> node",
      content: " <span></span><span>[]abc</span>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should set selection to before the <br> node in the second <span> node even if there is a whitespace only text node before the first <span> node",
      content: " <span></span><span>{}<br></span>",
      canTestInInlineEditingHost: true,
    },

    { description: "editor should set selection to start of the text node in the second <p> node following the empty <p> node",
      content: "<p></p><p>[]abc</p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should set selection to start of the text node in the second <p> node following another <p> node containing only a comment node",
      content: "<p><!-- comment --></p><p>[]abc</p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should set selection to before the <br> node in the second <p> node",
      content: "<p></p><p>{}<br></p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should set selection to start of the text node in the first <p> node #1",
      content: "<p>[]abc</p><p>abc</p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should set selection to start of the text node in the first <p> node #2",
      content: "<p>[]abc</p><p><br></p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should set selection to before the <br> node in the first <p> node #1",
      content: "<p>{}<br></p><p><br></p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should set selection to before the <br> node in the first <p> node #2",
      content: "<p>{}<br></p><p>abc</p>",
      canTestInInlineEditingHost: false,
    },

    { description: "editor should set selection to start of the text node in the second <p> node since the text node in the first <p> node is only whitespaces",
      content: "<p> </p><p>[]abc</p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should set selection to start of the text node in the first <p> node whose white-spaces are preformatted",
      content: "<p style=\"white-space: pre\">[] </p><p>abc</p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should set selection to start of the text node in the first <p> node whose line breaks are preformatted",
      content: "<p style=\"white-space: pre\">[]\n</p><p>abc</p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should set selection to before the <br> node in the second <p> node since the text node in the first <p> node is only whitespaces",
      content: "<p> </p><p>{}<br></p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should set selection to start of the text node in the second <p> node even if there is a whitespace only text node before the first <p> node",
      content: " <p></p><p>[]abc</p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should set selection to before the <br> node in the second <p> node even if there is a whitespace only text node before the first <p> node",
      content: " <p></p><p>{}<br></p>",
      canTestInInlineEditingHost: false,
    },

    { description: "editor should set selection to start of the text node in the <span> node in the second <p> node",
      content: "<p><span></span></p><p><span>[]abc</span></p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should set selection to before the <br> node in the <span> node in the second <p> node",
      content: "<p><span></span></p><p><span>{}<br></span></p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should set selection to start of the text node in the <span> node in the first <p> node #1",
      content: "<p><span>[]abc</span></p><p><span>abc</span></p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should set selection to start of the text node in the <span> node in the first <p> node #2",
      content: "<p><span>[]abc</span></p><p><span><br></span></p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should set selection to before the <br> node in the <span> node in the first <p> node #1",
      content: "<p><span>{}<br></span></p><p><span><br></span></p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should set selection to before the <br> node in the <span> node in the first <p> node #2",
      content: "<p><span>{}<br></span></p><p><span>abc</span></p>",
      canTestInInlineEditingHost: false,
    },

    { description: "editor should collapse selection before the non-editable <span> node",
      content: "{}<span contenteditable=\"false\"></span>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should collapse selection before the non-editable <span> node even if it has a text node",
      content: "{}<span contenteditable=\"false\">abc</span>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should collapse selection before the non-editable <span> node even if it has a <br> node",
      content: "{}<span contenteditable=\"false\"><br></span>",
      canTestInInlineEditingHost: true,
    },

    { description: "editor should collapse selection before the non-editable empty <span> node followed by a text node",
      content: "{}<span contenteditable=\"false\"></span><span>abc</span>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should collapse selection before the non-editable <span> node having a text node and followed by another text node",
      content: "{}<span contenteditable=\"false\">abc</span><span>def</span>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should collapse selection before the non-editable <span> node having a <br> node and followed by a text node",
      content: "{}<span contenteditable=\"false\"><br></span><span>abc</span>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should collapse selection before the non-editable empty <span> node followed by a <br> node",
      content: "{}<span contenteditable=\"false\"></span><span><br></span>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should collapse selection before the non-editable <span> node having text node and followed by a <br> node",
      content: "{}<span contenteditable=\"false\">abc</span><span><br></span>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should collapse selection before the non-editable <span> node having a <br> node and followed by another <br> node",
      content: "{}<span contenteditable=\"false\"><br></span><span><br></span>",
      canTestInInlineEditingHost: true,
    },

    { description: "editor should collapse selection before the non-editable empty <p> node followed by a text node",
      content: "{}<p contenteditable=\"false\"></p><p>abc</p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should collapse selection before the non-editable <p> node having a text node and followed by another text node",
      content: "{}<p contenteditable=\"false\">abc</p><p>def</p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should collapse selection before the non-editable <p> node having a <br> node and followed by a text node",
      content: "{}<p contenteditable=\"false\"><br></p><p>abc</p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should collapse selection before the non-editable empty <p> node followed by a <br> node",
      content: "{}<p contenteditable=\"false\"></p><p><br></p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should collapse selection before the non-editable <p> node having text node and followed by a <br> node",
      content: "{}<p contenteditable=\"false\">abc</p><p><br></p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should collapse selection before the non-editable <p> node having a <br> node and followed by another <br> node",
      content: "{}<p contenteditable=\"false\"><br></p><p><br></p>",
      canTestInInlineEditingHost: false,
    },

    { description: "editor should collapse selection to start of itself when there is only empty inline elements before the non-editable node before first editable text node",
      content: "{}<span></span><span contenteditable=\"false\"></span><span>abc</span>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should collapse selection to start of itself when there is only empty inline elements before the non-editable node having a text node before first editable text node",
      content: "{}<span></span><span contenteditable=\"false\">abc</span><span>def</span>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should collapse selection to start of itself when there is only empty inline elements before the non-editable node having a <br> node before first editable text node",
      content: "{}<span></span><span contenteditable=\"false\"><br></span><span>abc</span>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should collapse selection to start of itself when there is only empty inline elements before the non-editable node before first editable <br> node",
      content: "{}<span></span><span contenteditable=\"false\"></span><span><br></span>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should collapse selection to start of itself when there is only empty inline elements before the non-editable node having a text node before first editable <br> node",
      content: "{}<span></span><span contenteditable=\"false\">abc</span><span><br></span>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should collapse selection to start of itself when there is only empty inline elements before the non-editable node having a <br> node before first editable <br> node",
      content: "{}<span></span><span contenteditable=\"false\"><br></span><span><br></span>",
      canTestInInlineEditingHost: true,
    },

    { description: "editor should collapse selection to start of the first dive element when there is only empty inline elements before the non-editable node before first editable text node",
      content: "<div>{}<span></span><span contenteditable=\"false\"></span><span>abc</span></div>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should collapse selection to start of the first dive element when there is only empty inline elements before the non-editable node having a text node before first editable text node",
      content: "<div>{}<span></span><span contenteditable=\"false\">abc</span><span>def</span></div>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should collapse selection to start of the first dive element when there is only empty inline elements before the non-editable node having a <br> node before first editable text node",
      content: "<div>{}<span></span><span contenteditable=\"false\"><br></span><span>abc</span></div>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should collapse selection to start of the first dive element when there is only empty inline elements before the non-editable node before first editable <br> node",
      content: "<div>{}<span></span><span contenteditable=\"false\"></span><span><br></span></div>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should collapse selection to start of the first dive element when there is only empty inline elements before the non-editable node having a text node before first editable <br> node",
      content: "<div>{}<span></span><span contenteditable=\"false\">abc</span><span><br></span></div>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should collapse selection to start of the first dive element when there is only empty inline elements before the non-editable node having a <br> node before first editable <br> node",
      content: "<div>{}<span></span><span contenteditable=\"false\"><br></span><span><br></span></div>",
      canTestInInlineEditingHost: false,
    },

    { description: "editor should collapse selection to the first editable text node in the first <span> node even if followed by a non-editable node",
      content: "<span>[]abc</span><span contenteditable=\"false\"></span>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should collapse selection to the first editable text node in the first <span> node even if followed by a non-editable node having another text node",
      content: "<span>[]abc</span><span contenteditable=\"false\">def</span>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should collapse selection to the first editable text node in the first <span> node even if followed by a non-editable node having a <br> node",
      content: "<span>[]abc</span><span contenteditable=\"false\"><br></span>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should collapse selection to the first editable <br> node in the first <span> node even if followed by a non-editable node",
      content: "<span>{}<br></span><span contenteditable=\"false\"></span>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should collapse selection to the first editable <br> node in the first <span> node even if followed by a non-editable node having a text node",
      content: "<span>{}<br></span><span contenteditable=\"false\">abc</span>",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should collapse selection to the first editable <br> node in the first <span> node even if followed by a non-editable node having a <br> node",
      content: "<span>{}<br></span><span contenteditable=\"false\"><br></span>",
      canTestInInlineEditingHost: true,
    },

    { description: "editor should collapse selection to the first editable text node in the first <p> node even if followed by a non-editable node",
      content: "<p>[]abc</p><p contenteditable=\"false\"></p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should collapse selection to the first editable text node in the first <p> node even if followed by a non-editable node having another text node",
      content: "<p>[]abc</p><p contenteditable=\"false\">def</p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should collapse selection to the first editable text node in the first <p> node even if followed by a non-editable node having a <br> node",
      content: "<p>[]abc</p><p contenteditable=\"false\"><br></p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should collapse selection to the first editable <br> node in the first <p> node even if followed by a non-editable node",
      content: "<p>{}<br></p><p contenteditable=\"false\"></p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should collapse selection to the first editable <br> node in the first <p> node even if followed by a non-editable node having a text node",
      content: "<p>{}<br></p><p contenteditable=\"false\">abc</p>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should collapse selection to the first editable <br> node in the first <p> node even if followed by a non-editable node having a <br> node",
      content: "<p>{}<br></p><p contenteditable=\"false\"><br></p>",
      canTestInInlineEditingHost: false,
    },

    { description: "editor should collapse selection to start of itself if first content is an input element",
      content: "{}<input>abc",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should collapse selection to start of itself if first content is an hr element",
      content: "{}<hr>abc",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should collapse selection to start of itself if first content is an textarea element",
      content: "{}<textarea>abc</textarea>def",
      canTestInInlineEditingHost: true,
    },
    { description: "editor should collapse selection to the input element",
      content: "<div>{}<input>abc</div>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should collapse selection to the hr element",
      content: "<div>{}<hr>abc</div>",
      canTestInInlineEditingHost: false,
    },
    { description: "editor should collapse selection to the textarea element",
      content: "<div>{}<textarea>abc</textarea>def</div>",
      canTestInInlineEditingHost: false,
    },
  ];

  const isInlineEditingHost = editingHostTagName == "span";

  const selection = document.getSelection();
  for (const testData of tests) {
    if (isInlineEditingHost && !testData.canTestInInlineEditingHost) {
      continue;
    }
    test(function() {
      editor.blur();
      selection.removeAllRanges();
      editor.innerHTML = testData.content.replace(/[{}\[\]]/g, "");
      editor.focus();
      editor.getBoundingClientRect();

      assert_equals(selection.rangeCount, 1, "Only one caret should be in the editor");
      if (selection.rangeCount) {
        addBrackets(selection.getRangeAt(0));
        assert_equals(editor.innerHTML, testData.content);
      }
    }, testData.description);
  }

  test(function() {
    // Check if selection is initialized after temporarily blurred.
    editor.innerHTML =
      `<${editingHostTagName}>abc</${editingHostTagName}><${editingHostTagName}>def</${editingHostTagName}>`;
    editor.focus();
    // Move selection to the second paragraph.
    selection.collapse(editor.firstChild.nextSibling.firstChild);
    // Reset focus.
    editor.blur();
    editor.focus();
    // Then, selection should still be in the second paragraph.
    assert_equals(selection.rangeCount, 1, "Only one caret should be in the editor");
    if (selection.rangeCount) {
      addBrackets(selection.getRangeAt(0));
      assert_equals(
        editor.innerHTML,
        `<${editingHostTagName}>abc</${editingHostTagName}><${editingHostTagName}>[]def</${editingHostTagName}>`
      );
    }
  }, "editor shouldn't reset selection when it gets focus again");
})();
</script>