test_nsIEditor_canUndo_canRedo.html (4845B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>Test for nsIEditor.canUndo and nsIEditor.canRedo</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 document.execCommand("undo"); 92 is( 93 getValue(editableElement), 94 "b", 95 `Editor for ${selector} should've undone inserting "a"` 96 ); 97 is( 98 editor.canUndo, 99 true, 100 `Editor for ${selector} should have undo transaction for inserting "b" after undoing inserting "a"` 101 ); 102 is( 103 editor.canRedo, 104 true, 105 `Editor for ${selector} should have redo transaction for inserting "b" after undoing inserting "a"` 106 ); 107 108 document.execCommand("undo"); 109 is( 110 getValue(editableElement), 111 "", 112 `Editor for ${selector} should've undone inserting "b"` 113 ); 114 is( 115 editor.canUndo, 116 false, 117 `Editor for ${selector} shouldn't have undo transaction after undoing all things` 118 ); 119 is( 120 editor.canRedo, 121 true, 122 `Editor for ${selector} should have redo transaction after undoing all things` 123 ); 124 125 document.execCommand("redo"); 126 is( 127 getValue(editableElement), 128 "b", 129 `Editor for ${selector} should've redone inserting "b"` 130 ); 131 is( 132 editor.canUndo, 133 true, 134 `Editor for ${selector} should have undo transaction after redoing inserted "a"` 135 ); 136 is( 137 editor.canRedo, 138 true, 139 `Editor for ${selector} should have redo transaction after redoing inserted "a"` 140 ); 141 142 document.execCommand("redo"); 143 is( 144 getValue(editableElement), 145 "ab", 146 `Editor for ${selector} should've redone inserting "b"` 147 ); 148 is( 149 editor.canUndo, 150 true, 151 `Editor for ${selector} should have undo transaction after redoing all things` 152 ); 153 is( 154 editor.canRedo, 155 false, 156 `Editor for ${selector} shouldn't have redo transaction for after redoing all things` 157 ); 158 159 document.execCommand("undo"); 160 synthesizeKey("c"); 161 is( 162 getValue(editableElement), 163 "cb", 164 `Editor for ${selector} should've redone inserting "b"` 165 ); 166 is( 167 editor.canUndo, 168 true, 169 `Editor for ${selector} should have undo transaction after inserting another undoing once` 170 ); 171 is( 172 editor.canRedo, 173 false, 174 `Editor for ${selector} shouldn't have redo transaction after inserting another undoing once` 175 ); 176 } 177 SimpleTest.finish(); 178 }); 179 180 </script> 181 </pre> 182 </body> 183 </html>