summaryrefslogtreecommitdiffstats
path: root/dom/base/test/test_user_select.html
blob: c2b1175974f7def4b689dc6e06ec019edb470fb5 (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
<!DOCTYPE>
<html>
<head>
<title>-moz-user-select selection tests</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" />

<style type="text/css">
@font-face {
  font-family: Ahem;
  src: url("Ahem.ttf");
}
body { font-family: Ahem; font-size: 20px; }
s, .non-selectable { -moz-user-select: none; }
n { display: none; }
a { position:absolute; bottom: 0; right:0; }
.text { -moz-user-select: text; }
</style>

</head>
<body>

<div id="test1">aaaaaaa<s>bbbbbbbb</s>ccccccc</div>
<div id="test2"><s>aaaaaaa</s>bbbbbbbbccccccc</div>
<div id="test3">aaaaaaabbbbbbbb<s>ccccccc</s></div>
<div id="test4">aaaaaaa<x><s>bbbbbbbb</s></x>ccccccc</div>
<div id="test5"><x><s>aaaaaaa</s></x>bbbbbbbbccccccc</div>
<div id="test6">aaaaaaabbbbbbbb<x><s>ccccccc</s></x></div>
<div id="test7">aaaaaaa<x><s><n>bbbb</n>bbbb</s></x>ccccccc</div>
<div id="test8"><x><s>aa<n>aaa</n>aa</s></x>bbbbbbbbccccccc</div>
<div id="test9">aaaaaaabbbbbbbb<x><s>cc<n>ccccc</n></s></x></div>
<div id="testA">aaaaaaa<n>bbb<s>bbbbb</s></n>ccccccc</div>
<div id="testB"><n><s>aaaa</s>aaa</n>bbbbbbbbccccccc</div>
<div id="testC">aaaaaaabbbbbbbb<n>cc<s>c</s>cccc</n></div>
<div id="testE">aaa<s id="testEc1">aaaa<a class="text">bbbb</a>dd<a>cccc</a>ddddddd</s>eeee</div>
<div id="testF">aaaa
<div class="non-selectable">x</div>
<div class="non-selectable">x</div>
<div class="non-selectable">x</div>
bbbb</div>
<div id="testG" style="white-space:pre">aaaa
<div class="non-selectable">x</div>
<div class="non-selectable">x</div>
<div class="non-selectable">x</div>
bbbb</div>
<div id="testH" style="white-space:pre">aaaa
<div class="non-selectable">x</div><input>
bbbbbbb</div>

<iframe id="testD" src="data:text/html,<body>aaaa<span style='-moz-user-select:none'>bbbb</span>cccc"></iframe>

<pre id="test">
<script class="testbody" type="text/javascript">

function test()
{
  function clear(w)
  {
    var sel = (w ? w : window).getSelection();
    sel.removeAllRanges();
  }
  function doneTest(e)
  {
    // We hide the elements we're done with so that later tests
    // are inside the rather narrow iframe mochitest gives us.
    // It matters for synthesizeMouse event tests.
    e.style.display = 'none';
    e.offsetHeight;
  }

  function dragSelect(e, x1, x2, x3)
  {
    dir = x2 > x1 ? 1 : -1;
    synthesizeMouse(e, x1, 5, { type: "mousedown" });
    synthesizeMouse(e, x1 + dir, 5, { type: "mousemove" });
    if (x3)
      synthesizeMouse(e, x3, 5, { type: "mousemove" });
    synthesizeMouse(e, x2 - dir, 5, { type: "mousemove" });
    synthesizeMouse(e, x2, 5, { type: "mouseup" });
  }

  function shiftClick(e, x)
  {
    synthesizeMouse(e, x, 5, { type: "mousedown", shiftKey: true });
    synthesizeMouse(e, x, 5, { type: "mouseup", shiftKey: true });
  }

  function init(arr, e)
  {
    clear();
    var sel = window.getSelection();
    for (i = 0; i < arr.length; ++i) {
      var data = arr[i];
      var r = new Range()
      r.setStart(node(e, data[0]), data[1]);
      r.setEnd(node(e, data[2]), data[3]);
      sel.addRange(r);
    }
  }

  function NL(s) { return s.replace(/(\r\n|\n\r|\r)/g, '\n'); }

  function checkText(text, e)
  {
    var sel = window.getSelection();
    is(NL(sel.toString()), text, e.id + ": selected text")
  }

  function checkRangeText(text, index)
  {
    var r = window.getSelection().getRangeAt(index);
    is(NL(r.toString()), text, e.id + ": range["+index+"].toString()")
  }

  function node(e, arg)
  {
    if (typeof arg == "number")
      return arg == -1 ? e : e.childNodes[arg];
    return arg;
  }

  function checkRangeCount(n, e)
  {
    var sel = window.getSelection();
    is(sel.rangeCount, n, e.id + ": Selection range count");
  }

  function checkRange(i, expected, e) {
    var sel = window.getSelection();
    var r = sel.getRangeAt(i);
    is(r.startContainer, node(e, expected[0]), e.id + ": range["+i+"].startContainer");
    is(r.startOffset, expected[1], e.id + ": range["+i+"].startOffset");
    is(r.endContainer, node(e, expected[2]), e.id + ": range["+i+"].endContainer");
    is(r.endOffset, expected[3], e.id + ": range["+i+"].endOffset");
  }

  function checkRanges(arr, e)
  {
    checkRangeCount(arr.length, e);
    for (i = 0; i < arr.length; ++i) {
      var expected = arr[i];
      checkRange(i, expected, e);
    }
  }

  // ======================================================
  // ================== dragSelect tests ==================
  // ======================================================

  var e = document.getElementById('test1');
  dragSelect(e, 20, 340);
  checkText('aaaaaacc', e);
  checkRanges([[0,1,-1,1], [2,0,2,2]], e);

  clear();
  dragSelect(e, 20, 260, 120);
  checkText('aaaaa', e);
  checkRanges([[0,1,0,6]], e);
  doneTest(e);

  clear();
  e = document.getElementById('test2');
  dragSelect(e, 20, 340);
  checkText('', e);
  checkRanges([], e);

  clear();
  dragSelect(e, 340, 20, 140);
  checkText('bbbbbbbbcc', e);
  checkRanges([[1,0,1,10]], e);
  // #test2 is used again below

  clear();
  e = document.getElementById('test3');
  dragSelect(e, 20, 340, 295);
  checkText('aaaaaabbbbbbbb', e);
  checkRanges([[0,1,0,15]], e);
  // #test3 is used again below

  clear();
  e = document.getElementById('test4');
  dragSelect(e, 20, 340);
  checkText('aaaaaacc', e);
  checkRanges([[0,1,1,0], [2,0,2,2]], e);
  doneTest(e);

  clear();
  e = document.getElementById('test5');
  dragSelect(e, 340, 20, 140);
  checkText('bbbbbbbbcc', e);
  checkRanges([[1,0,1,10]], e);
  doneTest(e);

  clear();
  e = document.getElementById('test6');
  dragSelect(e, 20, 340, 295);
  checkText('aaaaaabbbbbbbb', e);
  checkRanges([[0,1,0,15]], e);
  doneTest(e);

  clear();
  e = document.getElementById('test7');
  dragSelect(e, 20, 340);
  checkText('aaaaaacccccc', e);
  checkRanges([[0,1,1,0], [2,0,2,6]], e);
  doneTest(e);

  clear();
  e = document.getElementById('test8');
  dragSelect(e, 340, 20, 140);
  checkText('bbbbbccccc', e);
  checkRanges([[1,3,1,13]], e);
  doneTest(e);

  clear();
  e = document.getElementById('test9');
  dragSelect(e, 20, 340, 295);
  checkText('aaaaaabbbbbbbb', e);
  checkRanges([[0,1,0,15]], e);
  doneTest(e);

  clear();
  e = document.getElementById('testA');
  dragSelect(e, 20, 340);
  checkText('aaaaaaccccccc', e);
  checkRanges([[0,1,2,7]], e);
  checkRangeText('aaaaaabbbbbbbbccccccc', 0);
  doneTest(e);

  clear();
  e = document.getElementById('testB');
  dragSelect(e, 340, 20, 140);
  checkText('bbbbbbbccccccc', e);
  checkRanges([[1,1,1,15]], e);
  doneTest(e);

  clear();
  e = document.getElementById('testE');
  dragSelect(e, 20, 360, 295);
  checkText('aa\nbbbb\nee', e);
  checkRangeCount(3, e);
  checkRange(0, [0,1,-1,1], e);
  checkRange(1, [1,0,-1,2], e.children[0]);
  checkRange(2, [2,0,2,2], e);
  doneTest(e);

  // ======================================================
  // ================== shift+click tests =================
  // ======================================================

  // test extending a selection that starts in a -moz-user-select:none node
  clear();
  e = document.getElementById('test2');
  init([[0,0,0,1]], e);
  checkRangeText('aaaaaaa', 0);
  checkText('', e);
  shiftClick(e, 340);
  checkRangeText('bbbbbbbbcc', 0);
  checkText('bbbbbbbbcc', e);
  checkRanges([[-1,1,1,10]], e);
  doneTest(e);

  // test extending a selection that end in a -moz-user-select:none node
  clear();
  e = document.getElementById('test3');
  init([[1,0,1,1]], e);
  checkRangeText('ccccccc', 0);
  checkText('', e);
  shiftClick(e, 20);
  checkRangeText('aaaaaabbbbbbbb', 0);
  checkText('aaaaaabbbbbbbb', e);
  checkRanges([[0,1,-1,1]], e);
  doneTest(e);

  clear();
  e = document.getElementById('testF');
  synthesizeMouse(e, 1, 1, {});
  synthesizeMouse(e, 400, 100, { shiftKey: true });
  checkText("aaaa bbbb", e);
  checkRanges([[0,0,-1,1],[6,0,6,5]], e);
  doneTest(e);

  clear();
  e = document.getElementById('testG');
  synthesizeMouse(e, 1, 1, {});
  synthesizeMouse(e, 400, 180, { shiftKey: true });
  checkText("aaaa bbbb", e); // XXX this doesn't seem right - bug 1247799
  checkRanges([[0,0,-1,1],[2,0,-1,3],[4,0,-1,5],[6,0,6,5]], e);
  doneTest(e);

  clear();
  e = document.getElementById('testH');
  synthesizeMouse(e, 1, 1, {});
  synthesizeMouse(e, 30, 90, { shiftKey: true });
  synthesizeMouse(e, 50, 90, { shiftKey: true });
  synthesizeMouse(e, 70, 90, { shiftKey: true });
  checkText("aaaa bbb", e);
  checkRanges([[0,0,-1,1],[-1,2,3,4]], e);

  doneTest(e);
  // ======================================================
  // ==================== Script tests ====================
  // ======================================================

  clear();
  e = document.getElementById('testD');
  clear(e.contentWindow);
  sel = e.contentWindow.getSelection();
  sel.selectAllChildren(e.contentDocument.body);
  is(window.getSelection().rangeCount, 0, "testD: no selection in outer window");
  is(sel.toString(), 'aaaacccc', "testD: scripted selection");
  is(sel.rangeCount, 1, "testD: scripted selection isn't filtered");
  is(sel.getRangeAt(0).toString(), 'aaaabbbbcccc', "testD: scripted selection isn't filtered");

  // ======================================================
  // ================== Kbd command tests =================
  // ======================================================

  clear();
  e = document.getElementById('testD');
  clear(e.contentWindow);
  e.contentWindow.focus();
  synthesizeKey("a", { accelKey:true }, e.contentWindow);
  sel = e.contentWindow.getSelection();
  is(window.getSelection().rangeCount, 0, "testD: no selection in outer window");
  is(sel.toString(), 'aaaacccc', "testD: kbd selection");
  is(sel.rangeCount, 2, "testD: kbd selection is filtered");
  is(sel.getRangeAt(0).toString(), 'aaaa', "testD: kbd selection is filtered");
  is(sel.getRangeAt(1).toString(), 'cccc', "testD: kbd selection is filtered");
  doneTest(e);

  clear();
  SimpleTest.finish();
}
window.onload = function() { setTimeout(test, 0); };
SimpleTest.waitForExplicitFinish();
</script>
</pre>
</body>
</html>