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
|
<!doctype html>
<meta charset=utf-8>
<title>focus move tests caused by a call of Range.setEnd(), Range.setEndAfter() and Range.setEndBefore()</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<body>
<div style="height: 3000px;">Spacer to check whether or not page was scrolled down to focused editor</div>
<p id="staticBefore">static text</p>
<div id="editor" contenteditable><p>content of editor</p></div>
<div id="outerEditor" contenteditable
><p>content of outer editor</p><div id="staticInEditor" contenteditable="false"
><p>static content of outer editor</p><div id="innerEditor" contenteditable
><p>content of inner editor</p></div></div></div>
<p id="staticAfter">static text</p>
<p><a id="anchor" href="about:blank">anchor</a></p>
<script>
"use strict";
var staticBefore = {
element: document.getElementById("staticBefore"),
textNode: document.getElementById("staticBefore").firstChild,
textLength: document.getElementById("staticBefore").firstChild.length
};
var editor = {
element: document.getElementById("editor"),
textNode: document.getElementById("editor").firstChild.firstChild,
textLength: document.getElementById("editor").firstChild.firstChild.length
};
var outerEditor = {
element: document.getElementById("outerEditor"),
textNode: document.getElementById("outerEditor").firstChild.firstChild,
textLength: document.getElementById("outerEditor").firstChild.firstChild.length
};
var staticInEditor = {
element: document.getElementById("staticInEditor"),
textNode: document.getElementById("staticInEditor").firstChild,
textLength: document.getElementById("staticInEditor").firstChild.length
};
var innerEditor = {
element: document.getElementById("innerEditor"),
textNode: document.getElementById("innerEditor").firstChild.firstChild,
textLength: document.getElementById("innerEditor").firstChild.firstChild.length
};
var staticAfter = {
element: document.getElementById("staticAfter"),
textNode: document.getElementById("staticAfter").firstChild,
textLength: document.getElementById("staticAfter").firstChild.length
};
var anchor = {
element: document.getElementById("anchor"),
textNode: document.getElementById("anchor").firstChild,
textLength: document.getElementById("anchor").firstChild.length
};
function resetFocusAndSelectionRange(aFocus)
{
document.getSelection().removeAllRanges();
if (document.activeElement) {
document.activeElement.blur();
}
if (aFocus) {
aFocus.element.focus();
document.getSelection().collapse(aFocus.textNode, 0);
} else {
document.getSelection().collapse(staticBefore.textNode, 0);
}
document.documentElement.scrollTop = 0;
}
function setEnd(aNode, aOffset)
{
document.getSelection().getRangeAt(0).setEnd(aNode, aOffset);
}
function setEndBefore(aNode, aOffset)
{
document.getSelection().getRangeAt(0).setEndBefore(aNode);
}
function setEndAfter(aNode, aOffset)
{
document.getSelection().getRangeAt(0).setEndAfter(aNode);
}
// Range.setEnd*() should work same as collapse if specified end position is before its start.
[{ func: setEnd, doingDescription: "Range.setEnd()" },
{ func: setEndBefore, doingDescription: "Range.setEndBefore()" }].forEach((aTest, aIndex, aArray)=>{
test(function() {
resetFocusAndSelectionRange(editor);
document.getSelection().selectAllChildren(editor.textNode);
aTest.func(staticBefore.textNode, 0);
assert_equals(document.activeElement, editor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'editor' after " + aTest.doingDescription + " with start of the first text node of 'staticBefore' (before the selection) when active element is 'editor'");
test(function() {
resetFocusAndSelectionRange(outerEditor);
document.getSelection().selectAllChildren(outerEditor.textNode);
aTest.func(editor.textNode, 0);
assert_equals(document.activeElement, editor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'editor' after " + aTest.doingDescription + " with start of the first text node of 'editor' (before the selection) when active element is 'outerEditor'");
test(function() {
resetFocusAndSelectionRange();
document.getSelection().selectAllChildren(staticInEditor.textNode);
aTest.func(outerEditor.textNode, 0);
assert_equals(document.activeElement, outerEditor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'outerEditor' after " + aTest.doingDescription + " with start of the first text node of 'outerEditor' (before the selection) when active element is the <body>");
test(function() {
resetFocusAndSelectionRange(innerEditor);
document.getSelection().selectAllChildren(innerEditor.textNode);
aTest.func(outerEditor.textNode, 0);
assert_equals(document.activeElement, outerEditor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'outerEditor' after " + aTest.doingDescription + " with start of the first text node of 'outerEditor' (before the selection) when active element is 'innerEditor'");
test(function() {
resetFocusAndSelectionRange(anchor);
document.getSelection().selectAllChildren(anchor.textNode);
aTest.func(staticAfter.textNode, 0);
assert_equals(document.activeElement, anchor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'anchor' after " + aTest.doingDescription + " with start of the first text node of 'staticAfter' (before the selection) when active element is 'anchor'");
test(function() {
resetFocusAndSelectionRange();
document.getSelection().collapse(staticBefore.textNode, staticBefore.textLength);
aTest.func(staticBefore.textNode, 0);
assert_equals(document.activeElement, document.body);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be the <body> after " + aTest.doingDescription + " with start of the first text node of 'staticBefore' (before the collapsed selection) when active element is the <body>");
test(function() {
resetFocusAndSelectionRange(editor);
document.getSelection().collapse(editor.textNode, editor.textLength);
aTest.func(staticBefore.textNode, 0);
assert_equals(document.activeElement, editor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'editor' after " + aTest.doingDescription + " with start of the first text node of 'staticBefore' (before the collapsed selection) when active element is 'editor'");
test(function() {
resetFocusAndSelectionRange(outerEditor);
document.getSelection().collapse(outerEditor.textNode, outerEditor.textLength);
aTest.func(editor.textNode, 0);
assert_equals(document.activeElement, editor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'editor' after " + aTest.doingDescription + " with start of the first text node of 'editor' (before the collapsed selection) when active element is 'outerEditor'");
test(function() {
resetFocusAndSelectionRange();
document.getSelection().collapse(staticInEditor.textNode, staticInEditor.textLength);
aTest.func(outerEditor.textNode, 0);
assert_equals(document.activeElement, outerEditor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'outerEditor' after " + aTest.doingDescription + " with start of the first text node of 'outerEditor' (before the collapsed selection) when active element is the <body> and selection is in 'staticInEditor'");
test(function() {
resetFocusAndSelectionRange(innerEditor);
document.getSelection().collapse(innerEditor.textNode, innerEditor.textLength);
aTest.func(outerEditor.textNode, 0);
assert_equals(document.activeElement, outerEditor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'outerEditor' after " + aTest.doingDescription + " with start of the first text node of 'outerEditor' (before the collapsed selection) when active element is 'innerEditor'");
test(function() {
resetFocusAndSelectionRange(anchor);
document.getSelection().collapse(anchor.textNode, anchor.textLength);
aTest.func(staticAfter.textNode, 0);
assert_equals(document.activeElement, anchor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'anchor' after " + aTest.doingDescription + " with start of the first text node of 'staticAfter' (before the collapsed selection) when active element is 'anchor'");
test(function() {
resetFocusAndSelectionRange();
document.getSelection().collapse(anchor.textNode, anchor.textLength);
aTest.func(anchor.textNode, 0);
assert_equals(document.activeElement, document.body);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be the <body> after " + aTest.doingDescription + " with start of the first text node of 'anchor' (before the collapsed selection) when active element is the <body>");
test(function() {
resetFocusAndSelectionRange();
document.getSelection().setBaseAndExtent(staticBefore.textNode, staticBefore.textLength,
editor.textNode, editor.textLength);
aTest.func(staticBefore.textNode, 0);
assert_equals(document.activeElement, document.body);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be the <body> after " + aTest.doingDescription + " with start of the first text node of 'staticBefore' (before the selection, between end of the first text node of 'staticBefore' and end of the first text node of 'editor') when active element is the <body>");
test(function() {
resetFocusAndSelectionRange();
document.getSelection().setBaseAndExtent(editor.textNode, editor.textLength,
outerEditor.textNode, outerEditor.textLength);
aTest.func(editor.textNode, 0);
assert_equals(document.activeElement, editor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'editor' after " + aTest.doingDescription + " with start of the first text node of 'editor' (before the selection, between end of the first text node of 'editor' and end of the first text node of 'outerEditor') when active element is the <body>");
test(function() {
resetFocusAndSelectionRange(outerEditor);
document.getSelection().setBaseAndExtent(outerEditor.textNode, outerEditor.textLength,
innerEditor.textNode, innerEditor.textLength);
aTest.func(outerEditor.textNode, 0);
assert_equals(document.activeElement, outerEditor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'outerEditor' after " + aTest.doingDescription + " with start of the first text node of 'outerEditor' (before the selection, between end of the first text node of 'outerEditor' and end of the first text node of 'innerEditor') when active element is 'outerEditor'");
test(function() {
resetFocusAndSelectionRange();
document.getSelection().setBaseAndExtent(staticInEditor.textNode, staticInEditor.textLength,
innerEditor.textNode, innerEditor.textLength);
aTest.func(staticInEditor.textNode, 0);
assert_equals(document.activeElement, document.body);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be the <body> after " + aTest.doingDescription + " with start of the first text node of 'staticInEditor' (before the selection, between end of the first text node of 'staticInEditor' and end of the first text node of 'innerEditor') when active element is the <body>");
});
test(function() {
resetFocusAndSelectionRange();
document.getSelection().selectAllChildren(staticAfter.textNode);
setEndAfter(innerEditor.textNode);
assert_equals(document.activeElement, innerEditor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'innerEditor' after Selection.setEndAfter() with the first text node of 'innerEditor' (before the selection) when active element is the <body> and selection is in 'staticAfter'");
test(function() {
resetFocusAndSelectionRange(innerEditor);
document.getSelection().selectAllChildren(innerEditor.textNode);
setEndAfter(staticInEditor.textNode);
assert_equals(document.activeElement, innerEditor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'innerEditor' after Selection.setEndAfter() with the first text node of 'staticInEditor' (before the selection) when active element is 'innerEditor'");
test(function() {
resetFocusAndSelectionRange();
document.getSelection().selectAllChildren(staticInEditor.textNode);
setEndAfter(outerEditor.textNode);
assert_equals(document.activeElement, outerEditor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'outerEditor' after Selection.setEndAfter() with the first text node of 'outerEditor' (before the selection) when active element is the <body> and selection is in 'staticInEditor'");
test(function() {
resetFocusAndSelectionRange(innerEditor);
document.getSelection().selectAllChildren(innerEditor.textNode);
setEndAfter(outerEditor.textNode);
assert_equals(document.activeElement, outerEditor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'outerEditor' after Selection.setEndAfter() with the first text node of 'outerEditor' (before the selection) when active element is 'innerEditor'");
test(function() {
resetFocusAndSelectionRange(outerEditor);
document.getSelection().selectAllChildren(outerEditor.textNode);
setEndAfter(editor.textNode);
assert_equals(document.activeElement, editor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'editor' after Selection.setEndAfter() with the first text node of 'editor' (before the selection) when active element is 'outerEditor'");
test(function() {
resetFocusAndSelectionRange(editor);
document.getSelection().selectAllChildren(editor.textNode);
setEndAfter(staticBefore.textNode);
assert_equals(document.activeElement, editor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'editor' after Selection.setEndAfter() with the first text node of 'staticBefore' (before the selection) when active element is 'editor'");
test(function() {
resetFocusAndSelectionRange(anchor);
document.getSelection().selectAllChildren(anchor.textNode);
setEndAfter(staticBefore.textNode);
assert_equals(document.activeElement, anchor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'anchor' after Selection.setEndAfter() with the first text node of 'staticBefore' (before the selection) when active element is 'anchor'");
// Range.setEnd*() should blur focused editing host when it expands selection to outside of it.
[{ func: setEnd, doingDescription: "Range.setEnd()" },
{ func: setEndAfter, doingDescription: "Range.setEndAfter()" },
{ func: setEndBefore, doingDescription: "Range.setEndBefore()" }].forEach((aTest, aIndex, aArray)=>{
test(function() {
resetFocusAndSelectionRange(editor);
document.getSelection().collapse(editor.textNode, 0);
aTest.func(outerEditor.textNode, 0);
assert_equals(document.activeElement, editor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'editor' after " + aTest.doingDescription + " with start of the first text node of 'outerEditor' (after end of the collapsed selection) when active element is 'editor'");
test(function() {
resetFocusAndSelectionRange(outerEditor);
document.getSelection().collapse(outerEditor.textNode, 0);
aTest.func(staticInEditor.textNode, 0);
assert_equals(document.activeElement, outerEditor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'outerEditor' after " + aTest.doingDescription + " with start of the first text node of 'outerEditor' (after end of the collapsed selection) when active element is 'outerEditor'");
test(function() {
resetFocusAndSelectionRange(outerEditor);
document.getSelection().collapse(outerEditor.textNode, 0);
aTest.func(innerEditor.textNode, 0);
assert_equals(document.activeElement, outerEditor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'outerEditor' after " + aTest.doingDescription + " with start of the first text node of 'innerEditor' (after end of the collapsed selection) when active element is 'outerEditor'");
test(function() {
resetFocusAndSelectionRange(outerEditor);
document.getSelection().collapse(outerEditor.textNode, 0);
aTest.func(staticAfter.textNode, 0);
assert_equals(document.activeElement, outerEditor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'outerEditor' after " + aTest.doingDescription + " with start of the first text node of 'staticAfter' (after end of the collapsed selection) when active element is 'outerEditor'");
test(function() {
resetFocusAndSelectionRange(editor);
document.getSelection().selectAllChildren(editor.textNode);
aTest.func(outerEditor.textNode, 0);
assert_equals(document.activeElement, editor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'editor' after " + aTest.doingDescription + " with start of the first text node of 'outerEditor' (after end of the selection) when active element is 'editor'");
test(function() {
resetFocusAndSelectionRange(outerEditor);
document.getSelection().selectAllChildren(outerEditor.textNode);
aTest.func(staticInEditor.textNode, 0);
assert_equals(document.activeElement, outerEditor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'outerEditor' after " + aTest.doingDescription + " with start of the first text node of 'outerEditor' (after end of the selection) when active element is 'outerEditor'");
test(function() {
resetFocusAndSelectionRange(outerEditor);
document.getSelection().selectAllChildren(outerEditor.textNode);
aTest.func(innerEditor.textNode, 0);
assert_equals(document.activeElement, outerEditor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'outerEditor' after " + aTest.doingDescription + " with start of the first text node of 'innerEditor' (after end of the selection) when active element is 'outerEditor'");
test(function() {
resetFocusAndSelectionRange(outerEditor);
document.getSelection().selectAllChildren(outerEditor.textNode);
aTest.func(staticAfter.textNode, 0);
assert_equals(document.activeElement, outerEditor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'outerEditor' after " + aTest.doingDescription + " with start of the first text node of 'staticAfter' (after end of the selection) when active element is 'outerEditor'");
});
// Range.setEnd*() should move focus to an editing host when the range is shrunken into it.
[{ func: setEnd, doingDescription: "Range.setEnd()" },
{ func: setEndAfter, doingDescription: "Range.setEndAfter()" }].forEach((aTest, aIndex, aArray)=>{
test(function() {
resetFocusAndSelectionRange();
document.getSelection().setBaseAndExtent(editor.textNode, 0,
outerEditor.textNode, outerEditor.textLength);
aTest.func(editor.textNode, editor.textLength);
assert_equals(document.activeElement, editor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'editor' after " + aTest.doingDescription + " with end of the first text node of 'editor' (shrunken into 'editor') when active element is the <body>");
test(function() {
resetFocusAndSelectionRange(outerEditor);
document.getSelection().setBaseAndExtent(outerEditor.textNode, 0,
staticInEditor.textNode, staticInEditor.textLength);
aTest.func(outerEditor.textNode, outerEditor.textLength);
assert_equals(document.activeElement, outerEditor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'outerEditor' after " + aTest.doingDescription + " with end of the first text node of 'outerEditor' (shrunken into 'outerEditor') when active element is 'outerEditor' and selection end is in 'staticInEditor'");
test(function() {
resetFocusAndSelectionRange(outerEditor);
document.getSelection().setBaseAndExtent(outerEditor.textNode, 0,
innerEditor.textNode, innerEditor.textLength);
aTest.func(outerEditor.textNode, outerEditor.textLength);
assert_equals(document.activeElement, outerEditor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'outerEditor' after " + aTest.doingDescription + " with end of the first text node of 'outerEditor' (shrunken into 'outerEditor') when active element is 'outerEditor' and selection end is in 'innerEditor'");
test(function() {
resetFocusAndSelectionRange();
document.getSelection().setBaseAndExtent(innerEditor.textNode, 0,
staticAfter.textNode, staticAfter.textLength);
aTest.func(innerEditor.textNode, innerEditor.textLength);
assert_equals(document.activeElement, innerEditor.element);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be 'innerEditor' after " + aTest.doingDescription + " with end of the first text node of 'innerEditor' (shrunken into 'innerEditor') when active element is 'innerEditor' and selection end is in 'staticAfter'");
test(function() {
resetFocusAndSelectionRange();
document.getSelection().setBaseAndExtent(staticInEditor.textNode, 0,
innerEditor.textNode, innerEditor.textLength);
aTest.func(staticInEditor.textNode, staticInEditor.textLength);
assert_equals(document.activeElement, document.body);
assert_equals(document.documentElement.scrollTop, 0);
}, "Active element should be the <body> after " + aTest.doingDescription + " with end of the first text node of 'staticInEditor' (shrunken into 'staticInEditor') when active element is the <body>");
});
</script>
|