File: initial_selection_on_focus.html

package info (click to toggle)
firefox-esr 68.10.0esr-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,143,932 kB
  • sloc: cpp: 5,227,879; javascript: 4,315,531; ansic: 2,467,042; python: 794,975; java: 349,993; asm: 232,034; xml: 228,320; sh: 82,008; lisp: 41,202; makefile: 22,347; perl: 15,555; objc: 5,277; cs: 4,725; yacc: 1,778; ada: 1,681; pascal: 1,673; lex: 1,417; exp: 527; php: 436; ruby: 225; awk: 162; sed: 53; csh: 44
file content (360 lines) | stat: -rw-r--r-- 19,200 bytes parent folder | download | duplicates (12)
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
<!doctype html>
<meta charset=utf-8>
<title>initial selection on focus of contenteditable</title>
<!-- if you move this file into cross-browser's directly, you should include
     editing/include/tests.js for using addBrackets() and get rid of it from
     this file. -->
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<body>
<p id="staticText">out of editor</p>
<div id="editor" contenteditable style="min-height: 1em;"></div>
<script>
"use strict";

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

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

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

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

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

    { 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>",
    },
    { 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>",
    },
    { 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>",
    },
    { 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>",
    },

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

    { 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>",
    },
    { 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>",
    },
    { 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>",
    },
    { 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>",
    },

    { 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>",
    },
    { 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>",
    },
    { 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>",
    },
    { 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>",
    },
    { 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>",
    },
    { 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>",
    },

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

    { description: "editor should set focus to before the non-editable empty <span> node followed by a text node",
      content: "{}<span contenteditable=\"false\"></span><span>abc</span>",
    },
    { description: "editor should set focus to 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>",
    },
    { description: "editor should set focus to 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>",
    },
    { description: "editor should set focus to before the non-editable empty <span> node followed by a <br> node",
      content: "{}<span contenteditable=\"false\"></span><span><br></span>",
    },
    { description: "editor should set focus to before the non-editable <span> node having text node and followed by a <br> node",
      content: "{}<span contenteditable=\"false\">abc</span><span><br></span>",
    },
    { description: "editor should set focus to 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>",
    },

    { description: "editor should set focus to before the non-editable empty <p> node followed by a text node",
      content: "{}<p contenteditable=\"false\"></p><p>abc</p>",
    },
    { description: "editor should set focus to 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>",
    },
    { description: "editor should set focus to 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>",
    },
    { description: "editor should set focus to before the non-editable empty <p> node followed by a <br> node",
      content: "{}<p contenteditable=\"false\"></p><p><br></p>",
    },
    { description: "editor should set focus to before the non-editable <p> node having text node and followed by a <br> node",
      content: "{}<p contenteditable=\"false\">abc</p><p><br></p>",
    },
    { description: "editor should set focus to 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>",
    },

    { description: "editor should set focus to start of it if there is non-editable node before first editable text node",
      content: "{}<span></span><span contenteditable=\"false\"></span><span>abc</span>",
    },
    { description: "editor should set focus to start of it if there is non-editable node having a text node before first editable text node",
      content: "{}<span></span><span contenteditable=\"false\">abc</span><span>def</span>",
    },
    { description: "editor should set focus to start of it if there is non-editable node having a <br> node before first editable text node",
      content: "{}<span></span><span contenteditable=\"false\"><br></span><span>abc</span>",
    },
    { description: "editor should set focus to start of it if there is non-editable node before first editable <br> node",
      content: "{}<span></span><span contenteditable=\"false\"></span><span><br></span>",
    },
    { description: "editor should set focus to start of it if there is non-editable node having a text node before first editable <br> node",
      content: "{}<span></span><span contenteditable=\"false\">abc</span><span><br></span>",
    },
    { description: "editor should set focus to start of it if there is non-editable node having a <br> node before first editable <br> node",
      content: "{}<span></span><span contenteditable=\"false\"><br></span><span><br></span>",
    },

    { description: "editor should set focus 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>",
    },
    { description: "editor should set focus 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>",
    },
    { description: "editor should set focus 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>",
    },
    { description: "editor should set focus 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>",
    },
    { description: "editor should set focus 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>",
    },
    { description: "editor should set focus 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>",
    },

    { description: "editor should set focus 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>",
    },
    { description: "editor should set focus 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>",
    },
    { description: "editor should set focus 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>",
    },
    { description: "editor should set focus 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>",
    },
    { description: "editor should set focus 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>",
    },
    { description: "editor should set focus 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>",
    },
  ];

  // This function is copied from editing/include/tests.js
  function addBrackets(range) {
    // Handle the collapsed case specially, to avoid confusingly getting the
    // markers backwards in some cases
    if (range.startContainer.nodeType == Node.TEXT_NODE ||
        range.startContainer.nodeType == Node.COMMENT_NODE) {
      if (range.collapsed) {
        range.startContainer.insertData(range.startOffset, "[]");
      } else {
        range.startContainer.insertData(range.startOffset, "[");
      }
    } else {
      var marker = range.collapsed ? "{}" : "{";
      if (range.startOffset != range.startContainer.childNodes.length &&
          range.startContainer.childNodes[range.startOffset].nodeType == Node.TEXT_NODE) {
        range.startContainer.childNodes[range.startOffset].insertData(0, marker);
      } else if (range.startOffset != 0 &&
                 range.startContainer.childNodes[range.startOffset - 1].nodeType == Node.TEXT_NODE) {
        range.startContainer.childNodes[range.startOffset - 1].appendData(marker);
      } else {
        // Seems to serialize as I'd want even for tables . . . IE doesn't
        // allow undefined to be passed as the second argument (it throws
        // an exception), so we have to explicitly check the number of
        // children and pass null.
        range.startContainer.insertBefore(document.createTextNode(marker),
          range.startContainer.childNodes.length == range.startOffset ?
            null : range.startContainer.childNodes[range.startOffset]);
      }
    }
    if (range.collapsed) {
      return;
    }
    if (range.endContainer.nodeType == Node.TEXT_NODE ||
        range.endContainer.nodeType == Node.COMMENT_NODE) {
      range.endContainer.insertData(range.endOffset, "]");
    } else {
      if (range.endOffset != range.endContainer.childNodes.length &&
          range.endContainer.childNodes[range.endOffset].nodeType == Node.TEXT_NODE) {
        range.endContainer.childNodes[range.endOffset].insertData(0, "}");
      } else if (range.endOffset != 0 &&
                 range.endContainer.childNodes[range.endOffset - 1].nodeType == Node.TEXT_NODE) {
        range.endContainer.childNodes[range.endOffset - 1].appendData("}");
      } else {
        range.endContainer.insertBefore(document.createTextNode("}"),
          range.endContainer.childNodes.length == range.endOffset ?
            null : range.endContainer.childNodes[range.endOffset]);
      }
    }
  }

  var editor = document.getElementById("editor");
  var textInP = document.getElementById("staticText").firstChild;
  var selection = document.getSelection();
  for (var i = 0; i < tests.length; i++) {
    test(function() {
      // Select outside the editor.
      editor.blur();
      selection.collapse(textInP);

      // Initialize the editor content.
      editor.innerHTML = tests[i].content.replace(/[{}\[\]]/g, "");

      // Make the editor focused.
      editor.focus();

      assert_equals(selection.rangeCount, 1);
      if (selection.rangeCount) {
        addBrackets(selection.getRangeAt(0));
        assert_equals(editor.innerHTML, tests[i].content);
      }
    }, tests[i].description);
  }

  test(function() {
    // Check if selection is initialized after temporarily blurred.
    editor.innerHTML = "<p>abc</p><p>def</p>";
    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);
    if (selection.rangeCount) {
      addBrackets(selection.getRangeAt(0));
      assert_equals(editor.innerHTML, "<p>abc</p><p>[]def</p>");
    }
  }, "editor shouldn't reset selection when it gets focus again");
})();
</script>