test_nsIEditor_undoRedoEnabled.html (2625B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>Test for nsIEditor.undoRedoEnabled</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.undoRedoEnabled, 47 true, 48 `undo/redo in editor for ${selector} should be enabled by default` 49 ); 50 editor.enableUndo(false); 51 is( 52 editor.undoRedoEnabled, 53 false, 54 `undo/redo in editor for ${selector} should be disable after calling enableUndo(false)` 55 ); 56 synthesizeKey("a"); 57 is( 58 getValue(editableElement), 59 "a", 60 `inserting text should be handled by editor for ${selector} even if undo/redo is disabled` 61 ); 62 is( 63 editor.canUndo, 64 false, 65 `undo transaction shouldn't be created by editor for ${selector} when undo/redo is disabled` 66 ); 67 editor.enableUndo(true); 68 is( 69 editor.undoRedoEnabled, 70 true, 71 `undo/redo in editor for ${selector} should be enabled after calling enableUndo(true)` 72 ); 73 synthesizeKey("b"); 74 is( 75 getValue(editableElement), 76 "ab", 77 `inserting text should be handled by editor for ${selector} after enabling undo/redo` 78 ); 79 is( 80 editor.canUndo, 81 true, 82 `undo transaction should be created by editor for ${selector} when undo/redo is enabled again` 83 ); 84 } 85 SimpleTest.finish(); 86 }); 87 88 </script> 89 </pre> 90 </body> 91 </html>