tor-browser

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

test_selection_preventDefault.html (5671B)


      1 <!DOCTYPE>
      2 <html>
      3 <head>
      4 <title>selection preventDefault test</title>
      5 <script src="/tests/SimpleTest/SimpleTest.js"></script>
      6 <script src="/tests/SimpleTest/EventUtils.js"></script>
      7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
      8 
      9 <style type="text/css">
     10  #fixedDiv1 {
     11    position: fixed;
     12    right: 0;
     13    overflow: scroll;
     14    width: 200px;
     15    top: 0;
     16  }
     17  input {
     18    font-size: 16px;
     19    height: 16px;
     20    width: 80px;
     21    margin: 0;
     22    padding: 0;
     23    -moz-appearance: none;
     24  }
     25 </style>
     26 
     27 </head>
     28 <body>
     29 <input id="input" type="text" value="iiiiiiiii iiiiiiiii iiiiiiiii">
     30 <div id="fixedDiv1" class="testingDiv">
     31 dddddd dddddd dddddd
     32 </div>
     33 <pre id="test">
     34 <script class="testbody" type="text/javascript">
     35 
     36 var fixedDiv1 = document.getElementById("fixedDiv1");
     37 var input = document.getElementById("input");
     38 
     39 function test()
     40 {
     41  function getSelectionForEditor(aEditorElement)
     42  {
     43    return SpecialPowers.wrap(aEditorElement).editor.selection;
     44  }
     45 
     46  function clear()
     47  {
     48    var sel = window.getSelection();
     49    if (sel.rangeCount > 0)
     50      sel.collapseToEnd();
     51    sel = getSelectionForEditor(input);
     52    if (sel.rangeCount > 0)
     53      sel.collapseToEnd();
     54  }
     55 
     56  const kFalse = 0;
     57  const kTrue  = 1;
     58  const kToDo  = 2;
     59 
     60  function check(aFixedDiv1ShouldBeSelected,
     61                 aInputShouldBeSelected,
     62                 aTestingDescription)
     63  {
     64    function checkCharacter(aSelectedText,
     65                            aShouldBeIncludedCharacter,
     66                            aSouldBeSelected,
     67                            aElementName)
     68    {
     69      var boolvalue = aSouldBeSelected & kTrue;
     70      var f = aSouldBeSelected & kToDo ? todo : ok;
     71      var str = aSelectedText.replace('\n', '\\n');
     72      if (boolvalue) {
     73        f(aSelectedText.includes(aShouldBeIncludedCharacter),
     74          "The contents of " + aElementName +
     75          " aren't selected (" + aTestingDescription +
     76          "): Selected String: \"" + str + "\"");
     77      } else {
     78        f(!aSelectedText.includes(aShouldBeIncludedCharacter),
     79          "The contents of " + aElementName +
     80          " are selected (" + aTestingDescription +
     81          "): Selected String: \"" + str + "\"");
     82      }
     83    }
     84 
     85    var sel = window.getSelection().toString();
     86    checkCharacter(sel, "d", aFixedDiv1ShouldBeSelected, "fixedDiv1");
     87 
     88    // input contents must not be included on the parent
     89    // selection.
     90    checkCharacter(sel, "i",
     91      SpecialPowers.getBoolPref("dom.selection.mimic_chrome_tostring.enabled")
     92      ? aInputShouldBeSelected
     93      : false
     94      , "input (checking on parent)");
     95 
     96    var selInput = getSelectionForEditor(input).toString();
     97    checkCharacter(selInput, "i", aInputShouldBeSelected, "input");
     98  }
     99 
    100  function eventHandler(evt) {
    101    evt.preventDefault();
    102  }
    103 
    104  // prevent default action on mousedown should prevent selection
    105  fixedDiv1.addEventListener("mousedown", eventHandler);
    106  synthesizeMouse(fixedDiv1, 30, 5, { type: "mousedown" });
    107  synthesizeMouse(fixedDiv1, 40, 5, { type: "mousemove" });
    108  synthesizeMouse(fixedDiv1, 40, 5, { type: "mouseup" });
    109  check(kFalse, kFalse, "fixedDiv1-fixedDiv1-mousedown");
    110  clear();
    111 
    112  input.addEventListener("mousedown", eventHandler);
    113  synthesizeMouse(input, 20, 5, { type: "mousedown" });
    114  synthesizeMouse(input, 40, 5, { type: "mousemove" });
    115  synthesizeMouse(input, 40, 5, { type: "mouseup" });
    116  check(kFalse, kFalse, "input-input-mousedown");
    117  clear();
    118 
    119  // clean up mousedown listener
    120  [fixedDiv1, input].forEach(function(element) {
    121     element.removeEventListener("mousedown", eventHandler);
    122  });
    123 
    124  // prevent default action on mouseup should not affect the selection state
    125  fixedDiv1.addEventListener("mouseup", eventHandler);
    126  synthesizeMouse(fixedDiv1, 30, 5, { type: "mousedown" });
    127  synthesizeMouse(fixedDiv1, 40, 5, { type: "mousemove" });
    128  synthesizeMouse(fixedDiv1, 40, 5, { type: "mouseup" });
    129  check(kTrue, kFalse, "fixedDiv1-fixedDiv1-mouseup");
    130  clear();
    131 
    132  input.addEventListener("mouseup", eventHandler);
    133  synthesizeMouse(input, 20, 5, { type: "mousedown" });
    134  synthesizeMouse(input, 40, 5, { type: "mousemove" });
    135  synthesizeMouse(input, 40, 5, { type: "mouseup" });
    136  check(kFalse, kTrue, "input-input-mouseup");
    137  clear();
    138 
    139  [fixedDiv1, input].forEach(function(element) {
    140     element.removeEventListener("mouseup", eventHandler);
    141  });
    142 
    143  // touchmove event should not affect the selection state
    144  synthesizeTouch(fixedDiv1, 30, 5, { type: "touchstart" });
    145  synthesizeTouch(fixedDiv1, 40, 5, { type: "touchmove" });
    146  check(kFalse, kFalse, "fixedDiv1-fixedDiv1-touchmove");
    147  synthesizeTouch(fixedDiv1, 40, 5, { type: "touchend" });
    148  clear();
    149 
    150  synthesizeTouch(input, 20, 5, { type: "touchstart" });
    151  synthesizeTouch(input, 40, 5, { type: "touchmove" });
    152  check(kFalse, kFalse, "input-input-touchmove");
    153  synthesizeTouch(input, 40, 5, { type: "touchend" });
    154  clear();
    155 
    156  fixedDiv1.addEventListener("touchmove", eventHandler);
    157  synthesizeTouch(fixedDiv1, 30, 5, { type: "touchstart" });
    158  synthesizeTouch(fixedDiv1, 40, 5, { type: "touchmove" });
    159  check(kFalse, kFalse, "fixedDiv1-fixedDiv1-touchmove-preventDefault");
    160  synthesizeTouch(fixedDiv1, 40, 5, { type: "touchend" });
    161  clear();
    162 
    163  input.addEventListener("touchmove", eventHandler);
    164  synthesizeTouch(input, 20, 5, { type: "touchstart" });
    165  synthesizeTouch(input, 40, 5, { type: "touchmove" });
    166  check(kFalse, kFalse, "input-input-touchmove-preventDefault");
    167  synthesizeTouch(input, 40, 5, { type: "touchend" });
    168  clear();
    169 
    170  SimpleTest.finish();
    171 }
    172 window.onload = function() { setTimeout(test, 0); };
    173 SimpleTest.waitForExplicitFinish();
    174 </script>
    175 </pre>
    176 </body>
    177 </html>