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
|
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="variant" content="?middle">
<meta name="variant" content="?secondary">
<title>Testing default action of `mousedown` of middle button and `mouseup` of middle/secondary button</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<style>
span {
white-space: nowrap;
}
</style>
</head>
<body>
<div contenteditable></div>
<script>
"use strict";
const button = location.search.substr(1);
function getButtonType(actions) {
return button == "middle" ? actions.ButtonType.MIDDLE : actions.ButtonType.RIGHT;
}
var editor = document.querySelector("div[contenteditable]");
var span1, span2, link;
var selection = getSelection();
function preventDefault(event) {
event.preventDefault();
}
editor.addEventListener("paste", preventDefault, {capture: true});
document.addEventListener("contextmenu", preventDefault, {capture: true});
function resetEditor() {
editor.innerHTML =
'<span id="span1">first span.</span><br><span id="span2">second span.</span><br><a id="link" href="#top">link.</a>';
span1 = document.getElementById("span1");
span2 = document.getElementById("span2");
link = document.getElementById("link");
}
promise_test(async () => {
resetEditor();
editor.blur();
let actions = new test_driver.Actions();
await actions
.pointerMove(0, 0)
.pointerMove(0, 0, {origin: span1})
.pointerDown({button: getButtonType(actions)})
.pointerUp({button: getButtonType(actions)})
.send();
assert_equals(document.activeElement, editor,
"The clicked editor should get focus");
assert_true(selection.isCollapsed,
`Selection should be collapsed after ${button} button click`);
assert_equals(selection.focusNode, span1.firstChild,
`Selection should be collapsed in the first <span> element which was clicked by ${button} button`);
}, `${button} click should set focus to clicked editable element and collapse selection around the clicked point`);
promise_test(async () => {
resetEditor();
editor.focus();
selection.collapse(span1.firstChild, 2);
let actions = new test_driver.Actions();
await actions
.pointerMove(0, 0)
.pointerMove(0, 0, {origin: span2})
.pointerDown({button: getButtonType(actions)})
.pointerUp({button: getButtonType(actions)})
.send();
assert_equals(document.activeElement, editor,
"The clicked editor should keep having focus");
assert_true(selection.isCollapsed,
`Selection should be collapsed after ${button} button click`);
assert_equals(selection.focusNode, span2.firstChild,
`Selection should be collapsed in the second <span> element which was clicked by ${button} button`);
}, `${button} click should move caret in an editable element`);
promise_test(async () => {
resetEditor();
editor.focus();
selection.collapse(span1.firstChild, 2);
addEventListener("mousedown", preventDefault);
let actions = new test_driver.Actions();
await actions
.pointerMove(0, 0)
.pointerMove(0, 0, {origin: span2})
.pointerDown({button: getButtonType(actions)})
.pointerUp({button: getButtonType(actions)})
.send();
removeEventListener("mousedown", preventDefault);
assert_equals(selection.focusNode, span1.firstChild,
"Selection should keep collapsed selection in the first <span> element");
assert_equals(selection.focusOffset, 2,
"Selection should keep collapsed selection at 2 of the first <span> element");
}, `${button} click shouldn't move caret in an editable element if the default of mousedown event is prevented`);
promise_test(async () => {
resetEditor();
editor.focus();
selection.collapse(span1.firstChild, 2);
addEventListener("pointerdown", preventDefault);
let actions = new test_driver.Actions();
await actions
.pointerMove(0, 0)
.pointerMove(0, 0, {origin: span2})
.pointerDown({button: getButtonType(actions)})
.pointerUp({button: getButtonType(actions)})
.send();
removeEventListener("pointerdown", preventDefault);
assert_equals(selection.focusNode, span1.firstChild,
"Selection should keep collapsed selection in the first <span> element");
assert_equals(selection.focusOffset, 2,
"Selection should keep collapsed selection at 2 of the first <span> element");
}, `${button} click shouldn't move caret in an editable element if the default of pointerdown event is prevented`);
promise_test(async () => {
resetEditor();
editor.focus();
selection.collapse(span1.firstChild, 2);
let contextmenuFired = false;
function onContextMenu() {
contextmenuFired = true;
}
document.addEventListener("contextmenu", onContextMenu, {capture: true});
let actions = new test_driver.Actions();
await actions
.pointerMove(0, 0)
.pointerMove(0, 0, {origin: span2})
.keyDown("\uE008")
.pointerDown({button: getButtonType(actions)})
.pointerUp({button: getButtonType(actions)})
.keyUp("\uE008")
.send();
document.removeEventListener("contextmenu", onContextMenu, {capture: true});
if (button != "secondary" || contextmenuFired) {
assert_equals(selection.anchorNode, span1.firstChild,
"Selection#anchorNode should keep in the first <span> element");
assert_equals(selection.anchorOffset, 2,
"Selection#anchorNode should keep at 2 of the first <span> element");
assert_equals(selection.focusNode, span2.firstChild,
`Selection#focusNode should be in the second <span> element which was clicked by ${button} button`);
} else {
// Special case for Firefox. Firefox users can forcibly open context menu
// with pressing shift key. In this case, users may want the secondary
// button click to work as without Shift key press.
assert_true(selection.isCollapsed,
`Selection should be collapsed after ${button} button click because contextmenu was not opened with Shift key`);
assert_equals(selection.focusNode, span2.firstChild,
`Selection should be collapsed in the second <span> element which was clicked by ${
button
} button because contextmenu was not opened with Shift key`);
}
}, `Shift + ${button} click should extend the selection`);
promise_test(async () => {
resetEditor();
editor.focus();
selection.collapse(span1.firstChild, 2);
let actions = new test_driver.Actions();
await actions
.pointerMove(0, 0)
.pointerMove(0, 0, {origin: link})
.keyDown("\uE008")
.pointerDown({button: getButtonType(actions)})
.pointerUp({button: getButtonType(actions)})
.keyUp("\uE008")
.send();
assert_equals(selection.focusNode, link.firstChild,
`Selection#focusNode should be in the <a href> element which was clicked by ${button} button`);
assert_true(selection.isCollapsed,
"Selection#isCollapsed should be true");
}, `Shift + ${button} click in a link shouldn't extend the selection`);
promise_test(async () => {
resetEditor();
editor.focus();
selection.collapse(span1.firstChild, 2);
editor.addEventListener("pointerdown", () => {
assert_true(selection.isCollapsed,
"Selection shouldn't be modified before pointerdown event");
assert_equals(selection.focusNode, span1.firstChild,
"Selection should stay in the first <span> element when pointerdown event is fired (focusNode)");
assert_equals(selection.focusOffset, 2,
"Selection should stay in the first <span> element when pointerdown event is fired (focusOffset)");
}, {once: true});
editor.addEventListener("mousedown", () => {
assert_true(selection.isCollapsed,
"Selection shouldn't be modified before mousedown event");
assert_equals(selection.focusNode, span1.firstChild,
"Selection should stay in the first <span> element when mousedown event is fired (focusNode)");
assert_equals(selection.focusOffset, 2,
"Selection should stay in the first <span> element when mousedown event is fired (focusOffset)");
}, {once: true});
editor.addEventListener("pointerup", () => {
assert_true(selection.isCollapsed,
"Selection should be collapsed before pointerup event");
assert_equals(selection.focusNode, span2.firstChild,
`selection should be collapsed in the second <span> element which was clicked by ${button} button before pointerup event`);
}, {once: true});
let focusOffsetAtMouseUp;
editor.addEventListener("mouseup", () => {
assert_true(selection.isCollapsed,
"Selection should be collapsed before mouseup event");
assert_equals(selection.focusNode, span2.firstChild,
`Selection should be collapsed in the second <span> element which was clicked by ${button} button before mouseup event`);
focusOffsetAtMouseUp = selection.focusOffset;
}, {once: true});
let actions = new test_driver.Actions();
await actions
.pointerMove(0, 0)
.pointerMove(0, 0, {origin: span2})
.pointerDown({button: getButtonType(actions)})
.pointerMove(0, 0, {origin: span1})
.pointerUp({button: getButtonType(actions)})
.send();
assert_true(selection.isCollapsed,
`Selection shouldn't be extended by pointer moves during pressing ${button} button`);
assert_equals(selection.focusNode, span2.firstChild,
"Selection#focusNode should stay in the second <span> element");
assert_equals(selection.focusOffset, focusOffsetAtMouseUp,
"Selection#focusOffset should stay in the second <span> element");
}, `${button} mouse button down should move caret, but its button up shouldn't move caret`);
</script>
</body>
</html>
|