tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

selection-utils.js (4305B)


      1 // -*- Mode: Javascript; tab-width: 2; indent-tabs-mode: nil; js-indent-level: 2 -*-
      2 // vim: set ts=2 sw=2 et tw=78:
      3 function clearSelection(w) {
      4  var sel = (w ? w : window).getSelection();
      5  sel.removeAllRanges();
      6 }
      7 
      8 function getNode(e, index) {
      9  if (!(typeof index === "number")) {
     10    return index;
     11  }
     12  if (index >= 0) {
     13    return e.childNodes[index];
     14  }
     15  while (++index != 0) {
     16    e = e.parentNode;
     17  }
     18  return e;
     19 }
     20 
     21 function dragSelectPointsWithData() {
     22  var event = arguments[0];
     23  var e = arguments[1];
     24  var x1 = arguments[2];
     25  var y1 = arguments[3];
     26  var x2 = arguments[4];
     27  var y2 = arguments[5];
     28  dir = x2 > x1 ? 1 : -1;
     29  event.type = "mousedown";
     30  synthesizeMouse(e, x1, y1, event);
     31  event.type = "mousemove";
     32  synthesizeMouse(e, x1 + dir, y1, event);
     33  for (var i = 6; i < arguments.length; ++i) {
     34    synthesizeMouse(e, arguments[i], y1, event);
     35  }
     36  synthesizeMouse(e, x2 - dir, y2, event);
     37  event.type = "mouseup";
     38  synthesizeMouse(e, x2, y2, event);
     39 }
     40 
     41 function dragSelectPoints() {
     42  var args = Array.prototype.slice.call(arguments);
     43  dragSelectPointsWithData.apply(this, [{}].concat(args));
     44 }
     45 
     46 function dragSelect() {
     47  var args = Array.prototype.slice.call(arguments);
     48  var e = args.shift();
     49  var x1 = args.shift();
     50  var x2 = args.shift();
     51  dragSelectPointsWithData.apply(this, [{}, e, x1, 5, x2, 5].concat(args));
     52 }
     53 
     54 function accelDragSelect() {
     55  var args = Array.prototype.slice.call(arguments);
     56  var e = args.shift();
     57  var x1 = args.shift();
     58  var x2 = args.shift();
     59  var y = args.length != 0 ? args.shift() : 5;
     60  dragSelectPointsWithData.apply(
     61    this,
     62    [{ accelKey: true }, e, x1, y, x2, y].concat(args)
     63  );
     64 }
     65 
     66 function shiftClick(e, x, y) {
     67  function pos(p) {
     68    return typeof p === "undefined" ? 5 : p;
     69  }
     70  synthesizeMouse(e, pos(x), pos(y), { shiftKey: true });
     71 }
     72 
     73 function keyRepeat(key, data, repeat) {
     74  while (repeat-- > 0) {
     75    synthesizeKey(key, data);
     76  }
     77 }
     78 
     79 function keyRight(data) {
     80  var repeat = arguments.length > 1 ? arguments[1] : 1;
     81  keyRepeat("VK_RIGHT", data, repeat);
     82 }
     83 
     84 function keyLeft(data) {
     85  var repeat = arguments.length > 1 ? arguments[1] : 1;
     86  keyRepeat("VK_LEFT", data, repeat);
     87 }
     88 
     89 function shiftAccelClick(e, x, y) {
     90  function pos(p) {
     91    return typeof p === "undefined" ? 5 : p;
     92  }
     93  synthesizeMouse(e, pos(x), pos(y), { shiftKey: true, accelKey: true });
     94 }
     95 
     96 function accelClick(e, x, y) {
     97  function pos(p) {
     98    return typeof p === "undefined" ? 5 : p;
     99  }
    100  synthesizeMouse(e, pos(x), pos(y), { accelKey: true });
    101 }
    102 
    103 function addChildRanges(arr, e) {
    104  var sel = window.getSelection();
    105  for (i = 0; i < arr.length; ++i) {
    106    var data = arr[i];
    107    var r = new Range();
    108    r.setStart(getNode(e, data[0]), data[1]);
    109    r.setEnd(getNode(e, data[2]), data[3]);
    110    sel.addRange(r);
    111  }
    112 }
    113 
    114 function checkText(text, e) {
    115  var sel = window.getSelection();
    116  is(sel.toString(), text, e.id + ": selected text");
    117 }
    118 
    119 function checkRangeText(text, index) {
    120  var r = window.getSelection().getRangeAt(index);
    121  is(r.toString(), text, e.id + ": range[" + index + "].toString()");
    122 }
    123 
    124 function checkRangeCount(n, e) {
    125  var sel = window.getSelection();
    126  is(sel.rangeCount, n, e.id + ": Selection range count");
    127 }
    128 
    129 function checkRangePoints(i, expected, e) {
    130  var sel = window.getSelection();
    131  if (i >= sel.rangeCount) {
    132    return;
    133  }
    134  var r = sel.getRangeAt(i);
    135  is(r.startContainer, expected[0], e.id + ": range.startContainer");
    136  is(r.startOffset, expected[1], e.id + ": range.startOffset");
    137  is(r.endContainer, expected[2], e.id + ": range.endContainer");
    138  is(r.endOffset, expected[3], e.id + ": range.endOffset");
    139 }
    140 
    141 function checkRange(i, expected, e) {
    142  var sel = window.getSelection();
    143  if (i >= sel.rangeCount) {
    144    return;
    145  }
    146  var r = sel.getRangeAt(i);
    147  is(
    148    r.startContainer,
    149    getNode(e, expected[0]),
    150    e.id + ": range[" + i + "].startContainer"
    151  );
    152  is(r.startOffset, expected[1], e.id + ": range[" + i + "].startOffset");
    153  is(
    154    r.endContainer,
    155    getNode(e, expected[2]),
    156    e.id + ": range[" + i + "].endContainer"
    157  );
    158  is(r.endOffset, expected[3], e.id + ": range[" + i + "].endOffset");
    159 }
    160 
    161 function checkRanges(arr, e) {
    162  checkRangeCount(arr.length, e);
    163  for (i = 0; i < arr.length; ++i) {
    164    var expected = arr[i];
    165    checkRange(i, expected, e);
    166  }
    167 }