test_nsIEditor_clearUndoRedo.html (3564B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>Test for nsIEditor.clearUndoRedo()</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 (function test_clearing_undo_history() { 45 setValue(editableElement, ""); 46 is( 47 editor.canUndo, 48 false, 49 `Editor for ${selector} shouldn't have undo transaction at start` 50 ); 51 synthesizeKey("a"); 52 is( 53 getValue(editableElement), 54 "a", 55 `Editor for ${selector} should've handled typing "a"` 56 ); 57 is( 58 editor.canUndo, 59 true, 60 `Editor for ${selector} should have undo transaction for the inserted text` 61 ); 62 editor.clearUndoRedo(); 63 is( 64 editor.canUndo, 65 false, 66 `Editor for ${selector} shouldn't have undo transaction after calling nsIEditor.clearUndoRedo()` 67 ); 68 document.execCommand("undo"); 69 is( 70 getValue(editableElement), 71 "a", 72 `Editor for ${selector} should do noting for document.execCommand("undo")` 73 ); 74 })(); 75 76 (function test_clearing_redo_history() { 77 setValue(editableElement, ""); 78 is( 79 editor.canRedo, 80 false, 81 `Editor for ${selector} shouldn't have redo transaction at start` 82 ); 83 synthesizeKey("b"); 84 is( 85 getValue(editableElement), 86 "b", 87 `Editor for ${selector} should've handled typing "b"` 88 ); 89 is( 90 editor.canRedo, 91 false, 92 `Editor for ${selector} shouldn't have redo transaction after inserting text` 93 ); 94 document.execCommand("undo"); 95 is( 96 getValue(editableElement), 97 "", 98 `Editor for ${selector} should've handled the typing "b" after undoing` 99 ); 100 is( 101 editor.canRedo, 102 true, 103 `Editor for ${selector} should have redo transaction of inserting text` 104 ); 105 editor.clearUndoRedo(); 106 is( 107 editor.canRedo, 108 false, 109 `Editor for ${selector} shouldn't have redo transaction after calling nsIEditor.clearUndoRedo()` 110 ); 111 document.execCommand("redo"); 112 is( 113 getValue(editableElement), 114 "", 115 `Editor for ${selector} should do noting for document.execCommand("redo")` 116 ); 117 })(); 118 } 119 SimpleTest.finish(); 120 }); 121 122 </script> 123 </pre> 124 </body> 125 </html>