summaryrefslogtreecommitdiffstats
path: root/layout/base/tests/selection-utils.js
blob: ed902e1cf1cfd177f0933f5f04b3e3a262be53ef (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
// -*- Mode: Javascript; tab-width: 2; indent-tabs-mode: nil; js-indent-level: 2 -*-
function clearSelection(w)
{
  var sel = (w ? w : window).getSelection();
  sel.removeAllRanges();
}

function getNode(e, index) { 
  if (!(typeof index==='number')) return index;
  if (index >= 0) return e.childNodes[index];
  while (++index != 0) e = e.parentNode;
  return e;
}

function dragSelectPointsWithData()
{
  var event = arguments[0];
  var e = arguments[1];
  var x1 = arguments[2];
  var y1 = arguments[3];
  var x2 = arguments[4];
  var y2 = arguments[5];
  dir = x2 > x1 ? 1 : -1;
  event['type'] = "mousedown";
  synthesizeMouse(e, x1, y1, event);
  event['type'] = "mousemove";
  synthesizeMouse(e, x1 + dir, y1, event);
  for (var i = 6; i < arguments.length; ++i)
    synthesizeMouse(e, arguments[i], y1, event);
  synthesizeMouse(e, x2 - dir, y2, event);
  event['type'] = "mouseup";
  synthesizeMouse(e, x2, y2, event);
}

function dragSelectPoints()
{
  var args = Array.prototype.slice.call(arguments);
  dragSelectPointsWithData.apply(this, [{}].concat(args));
}

function dragSelect()
{
  var args = Array.prototype.slice.call(arguments);
  var e = args.shift();
  var x1 = args.shift();
  var x2 = args.shift();
  dragSelectPointsWithData.apply(this, [{},e,x1,5,x2,5].concat(args));
}

function accelDragSelect()
{
  var args = Array.prototype.slice.call(arguments);
  var e = args.shift();
  var x1 = args.shift();
  var x2 = args.shift();
  var y = args.length != 0 ? args.shift() : 5;
  dragSelectPointsWithData.apply(this, [{accelKey: true},e,x1,y,x2,y].concat(args));
}

function shiftClick(e, x, y)
{
  function pos(p) { return (typeof p === "undefined") ? 5 : p }
  synthesizeMouse(e, pos(x), pos(y), { shiftKey: true });
}

function keyRepeat(key,data,repeat)
{
  while (repeat-- > 0)
    synthesizeKey(key, data);
}

function keyRight(data)
{
  var repeat = arguments.length > 1 ? arguments[1] : 1;
  keyRepeat("VK_RIGHT", data, repeat);
}

function keyLeft(data)
{
  var repeat = arguments.length > 1 ? arguments[1] : 1;
  keyRepeat("VK_LEFT", data, repeat);
}

function shiftAccelClick(e, x, y)
{
  function pos(p) { return (typeof p === "undefined") ? 5 : p }
  synthesizeMouse(e, pos(x), pos(y), { shiftKey: true, accelKey: true });
}

function accelClick(e, x, y)
{
  function pos(p) { return (typeof p === "undefined") ? 5 : p }
  synthesizeMouse(e, pos(x), pos(y), { accelKey: true });
}

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

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

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

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

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

function checkRange(i, expected, e) {
  var sel = window.getSelection();
  if (i >= sel.rangeCount) return;
  var r = sel.getRangeAt(i);
  is(r.startContainer, getNode(e, expected[0]), e.id + ": range["+i+"].startContainer");
  is(r.startOffset, expected[1], e.id + ": range["+i+"].startOffset");
  is(r.endContainer, getNode(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);
  }
}