tor-browser

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

test_nsIEditor_undoAll.html (3004B)


      1 <!DOCTYPE html>
      2 <html>
      3 <head>
      4 <title>Test for nsIEditor.undoAll()</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 </head>
      9 <body>
     10 <p id="display"></p>
     11 <div id="content"><input><textarea></textarea><div contenteditable></div></div>
     12 <pre id="test">
     13 <script>
     14 
     15 SimpleTest.waitForExplicitFinish();
     16 SimpleTest.waitForFocus(() => {
     17  function isTextEditor(aElement) {
     18    return aElement.tagName.toLowerCase() == "input" ||
     19           aElement.tagName.toLowerCase() == "textarea";
     20  }
     21  function getEditor(aElement) {
     22    if (isTextEditor(aElement)) {
     23      return SpecialPowers.wrap(aElement).editor;
     24    }
     25    return SpecialPowers.wrap(window).docShell.editingSession?.getEditorForWindow(window);
     26  }
     27  function setValue(aElement, aValue) {
     28    if (isTextEditor(aElement)) {
     29      aElement.value = aValue;
     30      return;
     31    }
     32    aElement.innerHTML = aValue;
     33  }
     34  function getValue(aElement) {
     35    if (isTextEditor(aElement)) {
     36      return aElement.value;
     37    }
     38    return aElement.innerHTML.replace(/<br>/g, "");
     39  }
     40  for (const selector of ["input", "textarea", "div[contenteditable]"]) {
     41    const editableElement = document.querySelector(selector);
     42    editableElement.focus();
     43    const editor = getEditor(editableElement);
     44    setValue(editableElement, "");
     45    is(
     46      editor.canUndo,
     47      false,
     48      `Editor for ${selector} shouldn't have undo transaction at start`
     49    );
     50    is(
     51      editor.canRedo,
     52      false,
     53      `Editor for ${selector} shouldn't have redo transaction at start`
     54    );
     55 
     56    synthesizeKey("b");
     57    is(
     58      getValue(editableElement),
     59      "b",
     60      `Editor for ${selector} should've handled inserting "b"`
     61    );
     62    is(
     63      editor.canUndo,
     64      true,
     65      `Editor for ${selector} should have undo transaction after inserting "b"`
     66    );
     67    is(
     68      editor.canRedo,
     69      false,
     70      `Editor for ${selector} shouldn't have redo transaction after inserting "b"`
     71    );
     72 
     73    synthesizeKey("KEY_ArrowLeft");
     74    synthesizeKey("a");
     75    is(
     76      getValue(editableElement),
     77      "ab",
     78      `Editor for ${selector} should've handled inserting "a" before "b"`
     79    );
     80    is(
     81      editor.canUndo,
     82      true,
     83      `Editor for ${selector} should have undo transaction after inserting text again`
     84    );
     85    is(
     86      editor.canRedo,
     87      false,
     88      `Editor for ${selector} should have redo transaction after inserting text again`
     89    );
     90 
     91    editor.undoAll();
     92    is(
     93      getValue(editableElement),
     94      "",
     95      `Editor for ${selector} should've undone everything`
     96    );
     97    is(
     98      editor.canUndo,
     99      false,
    100      `Editor for ${selector} shouldn't have undo transactions after undoAll() called`
    101    );
    102    is(
    103      editor.canRedo,
    104      true,
    105      `Editor for ${selector} should have redo transaction after undoAll() called`
    106    );
    107 
    108  }
    109  SimpleTest.finish();
    110 });
    111 
    112 </script>
    113 </pre>
    114 </body>
    115 </html>