Bug 1874684 - Part 17: Fix uninitialised variable warnings from clang-tidy. r=allstarschh
[gecko.git] / editor / libeditor / tests / test_nsIEditor_clearUndoRedo.html
blob695826c0d81b10b8541ece7bd0606180fdf675ce
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>
15 SimpleTest.waitForExplicitFinish();
16 SimpleTest.waitForFocus(() => {
17 function isTextEditor(aElement) {
18 return aElement.tagName.toLowerCase() == "input" ||
19 aElement.tagName.toLowerCase() == "textarea";
21 function getEditor(aElement) {
22 if (isTextEditor(aElement)) {
23 return SpecialPowers.wrap(aElement).editor;
25 return SpecialPowers.wrap(window).docShell.editingSession?.getEditorForWindow(window);
27 function setValue(aElement, aValue) {
28 if (isTextEditor(aElement)) {
29 aElement.value = aValue;
30 return;
32 aElement.innerHTML = aValue;
34 function getValue(aElement) {
35 if (isTextEditor(aElement)) {
36 return aElement.value;
38 return aElement.innerHTML.replace(/<br>/g, "");
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`
51 synthesizeKey("a");
52 is(
53 getValue(editableElement),
54 "a",
55 `Editor for ${selector} should've handled typing "a"`
57 is(
58 editor.canUndo,
59 true,
60 `Editor for ${selector} should have undo transaction for the inserted text`
62 editor.clearUndoRedo();
63 is(
64 editor.canUndo,
65 false,
66 `Editor for ${selector} shouldn't have undo transaction after calling nsIEditor.clearUndoRedo()`
68 document.execCommand("undo");
69 is(
70 getValue(editableElement),
71 "a",
72 `Editor for ${selector} should do noting for document.execCommand("undo")`
74 })();
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`
83 synthesizeKey("b");
84 is(
85 getValue(editableElement),
86 "b",
87 `Editor for ${selector} should've handled typing "b"`
89 is(
90 editor.canRedo,
91 false,
92 `Editor for ${selector} shouldn't have redo transaction after inserting text`
94 document.execCommand("undo");
95 is(
96 getValue(editableElement),
97 "",
98 `Editor for ${selector} should've handled the typing "b" after undoing`
101 editor.canRedo,
102 true,
103 `Editor for ${selector} should have redo transaction of inserting text`
105 editor.clearUndoRedo();
107 editor.canRedo,
108 false,
109 `Editor for ${selector} shouldn't have redo transaction after calling nsIEditor.clearUndoRedo()`
111 document.execCommand("redo");
113 getValue(editableElement),
115 `Editor for ${selector} should do noting for document.execCommand("redo")`
117 })();
119 SimpleTest.finish();
122 </script>
123 </pre>
124 </body>
125 </html>