tor-browser

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

elementsFromPoint.js (1670B)


      1 function nodeToString(node) {
      2  var str = '';
      3  if (node.nodeType == Node.ELEMENT_NODE) {
      4    str += node.nodeName;
      5    if (node.id)
      6      str += '#' + node.id;
      7    else if (node.class)
      8      str += '.' + node.class;
      9  } else if (node.nodeType == Node.TEXT_NODE) {
     10    str += '\'' + node.data + '\'';
     11  } else if (node.nodeType == Node.DOCUMENT_NODE) {
     12    str += '#document';
     13  }
     14  return str;
     15 }
     16 
     17 function nodeListToString(nodes) {
     18  var nodeString = '';
     19 
     20  for (var i = 0; i < nodes.length; i++) {
     21    var str = nodeToString(nodes[i]);
     22    if (!str)
     23      continue;
     24    nodeString += str;
     25    if (i + 1 < nodes.length)
     26      nodeString += ', ';
     27  }
     28  return nodeString;
     29 }
     30 
     31 function assertElementsFromPoint(doc, x, y, expected) {
     32  var query = doc + '.elementsFromPoint(' + x + ',' + y + ')';
     33  var sequence = eval(query);
     34  assert_equals(nodeListToString(sequence), nodeListToString(expected), query);
     35 }
     36 
     37 function checkElementsFromPointFourCorners(doc, element, expectedTopLeft, expectedTopRight, expectedBottomLeft, expectedBottomRight) {
     38  var rect = eval(doc + '.getElementById(\'' + element + '\')').getBoundingClientRect();
     39  var topLeft = {x: rect.left + 1, y: rect.top + 1};
     40  var topRight = {x: rect.right - 1, y: rect.top + 1};
     41  var bottomLeft = {x: rect.left + 1, y: rect.bottom - 1};
     42  var bottomRight = {x: rect.right - 1, y: rect.bottom - 1};
     43 
     44  assertElementsFromPoint(doc, topLeft.x, topLeft.y, expectedTopLeft);
     45  assertElementsFromPoint(doc, topRight.x, topRight.y, expectedTopRight);
     46  assertElementsFromPoint(doc, bottomLeft.x, bottomLeft.y, expectedBottomLeft);
     47  assertElementsFromPoint(doc, bottomRight.x, bottomRight.y, expectedBottomRight);
     48 }