tor-browser

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

testcommon.js (1594B)


      1 'use strict';
      2 
      3 // Creates a <div> element, appends it to the document body and
      4 // removes the created element during test cleanup.
      5 function createDiv(test, doc) {
      6  return createElement(test, 'div', doc);
      7 }
      8 
      9 // Creates an element with the given |tagName|, appends it to the document body
     10 // and removes the created element during test cleanup.
     11 // If |tagName| is null or undefined, creates a <div> element.
     12 function createElement(test, tagName, doc) {
     13  if (!doc) {
     14    doc = document;
     15  }
     16  var element = doc.createElement(tagName || 'div');
     17  doc.body.appendChild(element);
     18  test.add_cleanup(function() {
     19    element.remove();
     20  });
     21  return element;
     22 }
     23 
     24 // Convert px unit value to a Number
     25 function pxToNum(str) {
     26  return Number(String(str).match(/^(-?[\d.]+)px$/)[1]);
     27 }
     28 
     29 // Cubic bezier with control points (0, 0), (x1, y1), (x2, y2), and (1, 1).
     30 function cubicBezier(x1, y1, x2, y2) {
     31  function xForT(t) {
     32    var omt = 1-t;
     33    return 3 * omt * omt * t * x1 + 3 * omt * t * t * x2 + t * t * t;
     34  }
     35 
     36  function yForT(t) {
     37    var omt = 1-t;
     38    return 3 * omt * omt * t * y1 + 3 * omt * t * t * y2 + t * t * t;
     39  }
     40 
     41  function tForX(x) {
     42    // Binary subdivision.
     43    var mint = 0, maxt = 1;
     44    for (var i = 0; i < 30; ++i) {
     45      var guesst = (mint + maxt) / 2;
     46      var guessx = xForT(guesst);
     47      if (x < guessx) {
     48        maxt = guesst;
     49      } else {
     50        mint = guesst;
     51      }
     52    }
     53    return (mint + maxt) / 2;
     54  }
     55 
     56  return function bezierClosure(x) {
     57    if (x == 0) {
     58      return 0;
     59    }
     60    if (x == 1) {
     61      return 1;
     62    }
     63    return yForT(tForX(x));
     64  }
     65 }