tor-browser

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

box-comparison.js (4945B)


      1 function spaceBetween(childBox, parentBox) {
      2    return {
      3        left: childBox.left - parentBox.left,
      4        right: parentBox.right - childBox.right,
      5        top: childBox.top - parentBox.top,
      6        bottom: parentBox.bottom - childBox.bottom
      7    };
      8 }
      9 
     10 function measureSpaceAround(id) {
     11    var mrow = document.getElementById(id);
     12    var mrowBox = mrow.getBoundingClientRect();
     13    var parentBox = mrow.parentNode.getBoundingClientRect();
     14    var childBox = mrow.firstElementChild.getBoundingClientRect();
     15    return spaceBetween(childBox, parentBox);
     16 }
     17 
     18 function compareSpaceWithAndWithoutStyle(tag, style, parentStyle, direction) {
     19    if (!FragmentHelper.isValidChildOfMrow(tag) ||
     20        FragmentHelper.isEmpty(tag))
     21        throw `Invalid argument: ${tag}`;
     22 
     23    if (!direction)
     24      direction = "ltr";
     25    document.body.insertAdjacentHTML("beforeend", `<div style="position: absolute;">\
     26 <div style="display: inline-block"><math display="block"><mrow dir="${direction}">${MathMLFragments[tag]}</mrow></math></div>\
     27 <div style="display: inline-block"><math display="block"><mrow dir="${direction}">${MathMLFragments[tag]}</mrow></math></div>\
     28 </div>`);
     29    var div = document.body.lastElementChild;
     30 
     31    var styleDiv = div.firstElementChild;
     32    var styleMath = styleDiv.firstElementChild;
     33    var styleParent = styleMath.firstElementChild;
     34    if (parentStyle)
     35        styleParent.setAttribute("style", parentStyle);
     36    var styleElement = FragmentHelper.element(styleMath);
     37    styleElement.setAttribute("style", style);
     38    var styleChild = FragmentHelper.forceNonEmptyElement(styleElement);
     39    FragmentHelper.forceNonEmptyDescendants(styleElement);
     40    var styleMathBox = styleMath.getBoundingClientRect();
     41    var styleElementBox = styleElement.getBoundingClientRect();
     42    var styleChildBox = styleChild.getBoundingClientRect();
     43    var styleSpace = spaceBetween(styleChildBox, styleMathBox);
     44 
     45    var noStyleDiv = div.lastElementChild;
     46    var noStyleMath = noStyleDiv.firstElementChild;
     47    var noStyleElement = FragmentHelper.element(noStyleMath);
     48    var noStyleChild = FragmentHelper.forceNonEmptyElement(noStyleElement);
     49    FragmentHelper.forceNonEmptyDescendants(noStyleElement);
     50    var noStyleMathBox = noStyleMath.getBoundingClientRect();
     51    var noStyleElementBox = noStyleElement.getBoundingClientRect();
     52    var noStyleChildBox = noStyleChild.getBoundingClientRect();
     53    var noStyleSpace = spaceBetween(noStyleChildBox, noStyleMathBox);
     54 
     55    var preferredWidthDelta =
     56        styleDiv.getBoundingClientRect().width -
     57        noStyleDiv.getBoundingClientRect().width;
     58 
     59    div.style = "display: none;"; // Hide the div after measurement.
     60 
     61    return {
     62        preferred_width_delta: preferredWidthDelta,
     63        left_delta: styleSpace.left - noStyleSpace.left,
     64        right_delta: styleSpace.right - noStyleSpace.right,
     65        top_delta: styleSpace.top - noStyleSpace.top,
     66        bottom_delta: styleSpace.bottom - noStyleSpace.bottom,
     67        element_width_delta: styleElementBox.width - noStyleElementBox.width,
     68        element_height_delta: styleElementBox.height - noStyleElementBox.height
     69    };
     70 }
     71 
     72 function compareSizeWithAndWithoutStyle(tag, style) {
     73    if (!FragmentHelper.isValidChildOfMrow(tag))
     74        throw `Invalid argument: ${tag}`;
     75 
     76    // FIXME <mrow> only needed as workaround for https://bugzilla.mozilla.org/show_bug.cgi?id=1658135
     77    document.body.insertAdjacentHTML("beforeend", `<div style="position: absolute;">\
     78 <div style="display: inline-block"><math><mrow>${MathMLFragments[tag]}</mrow></math></div>\
     79 <div style="display: inline-block"><math><mrow>${MathMLFragments[tag]}</mrow></math></div>\
     80 </div>`);
     81    var div = document.body.lastElementChild;
     82 
     83    var styleDiv = div.firstElementChild;
     84    var styleParent = styleDiv.firstElementChild.firstElementChild;
     85    var styleElement = FragmentHelper.element(styleParent);
     86    styleElement.setAttribute("style", style);
     87    var styleParentBox = styleParent.getBoundingClientRect();
     88    var styleElementBox = styleElement.getBoundingClientRect();
     89 
     90    var noStyleDiv = div.lastElementChild;
     91    var noStyleParent = noStyleDiv.firstElementChild.firstElementChild;
     92    var noStyleElement = FragmentHelper.element(noStyleParent);
     93    var noStyleParentBox = noStyleParent.getBoundingClientRect();
     94    var noStyleElementBox = noStyleElement.getBoundingClientRect();
     95 
     96    var preferredWidthDelta =
     97        styleDiv.getBoundingClientRect().width -
     98        noStyleDiv.getBoundingClientRect().width;
     99 
    100    div.style = "display: none;"; // Hide the div after measurement.
    101 
    102    return {
    103        preferred_width_delta: preferredWidthDelta,
    104        width_delta: styleParentBox.width - noStyleParentBox.width,
    105        height_delta: styleParentBox.height - noStyleParentBox.height,
    106        element_width_delta: styleElementBox.width - noStyleElementBox.width,
    107        element_height_delta: styleElementBox.height - noStyleElementBox.height
    108    };
    109 };