1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<script src="../include/editor-test-utils.js"></script>
<iframe srcdoc=""></iframe>
<script>
"use strict";
const iframe = document.querySelector("iframe");
promise_test(async () => {
await new Promise(resolve => {
addEventListener("load", resolve, {once: true});
});
}, "Waiting for load...");
/**
* This test does NOT test whether the edit result is valid or invalid.
* This test just tests whether "undo" and "redo" restores previous state
* and additional "undo" and "redo" does not run unexpectedly.
*
* description: Set string to explain what's testing.
* editorInnerHTML: Set initial innerHTML value of editor.
* init: Set a function object if you need to test complicated cases, e.g.,
* testing with empty text node.
* run: Set a function object which run something modifying the editor (or
* does nothing).
* expectedUndoResult: Set an expected innerHTML result as string or array
* of the string. If this is not specified, it's compared
* with editorInnerHTML value.
* cleanUp: Set a function object if you need to clean something up after the
* test.
*/
const tests = [
{
description: "insertParagraph at start of a paragraph",
editorInnerHTML: "<p>[]abcdef</p>",
run: (win, doc, editingHost) => {
doc.execCommand("insertParagraph");
},
},
{
description: "insertParagraph at middle of a paragraph",
editorInnerHTML: "<p>abc[]def</p>",
run: (win, doc, editingHost) => {
doc.execCommand("insertParagraph");
},
},
{
description: "insertParagraph at end of a paragraph",
editorInnerHTML: "<p>abcdef[]</p>",
run: (win, doc, editingHost) => {
doc.execCommand("insertParagraph");
},
},
{
description: "insertParagraph at start of a listitem",
editorInnerHTML: "<ul><li>[]abcdef</li></ul>",
run: (win, doc, editingHost) => {
doc.execCommand("insertParagraph");
},
},
{
description: "insertParagraph at middle of a listitem",
editorInnerHTML: "<ul><li>abc[]def</li></ul>",
run: (win, doc, editingHost) => {
doc.execCommand("insertParagraph");
},
},
{
description: "insertParagraph at end of a listitem",
editorInnerHTML: "<ul><li>abcdef[]</li></ul>",
run: (win, doc, editingHost) => {
doc.execCommand("insertParagraph");
},
},
{
description: "insertLineBreak at start of a paragraph",
editorInnerHTML: "<p>[]abcdef</p>",
run: (win, doc, editingHost) => {
doc.execCommand("insertLineBreak");
},
},
{
description: "insertLineBreak at middle of a paragraph",
editorInnerHTML: "<p>abc[]def</p>",
run: (win, doc, editingHost) => {
doc.execCommand("insertLineBreak");
},
},
{
description: "insertLineBreak at end of a paragraph",
editorInnerHTML: "<p>abcdef[]</p>",
run: (win, doc, editingHost) => {
doc.execCommand("insertLineBreak");
},
},
{
description: "insertLineBreak at start of a listitem",
editorInnerHTML: "<ul><li>[]abcdef</li></ul>",
run: (win, doc, editingHost) => {
doc.execCommand("insertLineBreak");
},
},
{
description: "insertLineBreak at middle of a listitem",
editorInnerHTML: "<ul><li>abc[]def</li></ul>",
run: (win, doc, editingHost) => {
doc.execCommand("insertLineBreak");
},
},
{
description: "insertLineBreak at end of a listitem",
editorInnerHTML: "<ul><li>abcdef[]</li></ul>",
run: (win, doc, editingHost) => {
doc.execCommand("insertLineBreak");
},
},
{
description: "delete at start of second paragraph",
editorInnerHTML: "<p>abc</p><p>[]def</p>",
run: (win, doc, editingHost) => {
doc.execCommand("delete");
}
},
{
description: "forwarddelete at end of first paragraph",
editorInnerHTML: "<p>abc[]</p><p>def</p>",
run: (win, doc, editingHost) => {
doc.execCommand("forwarddelete");
}
},
{
description: "delete at start of second paragraph starting with an emoji",
editorInnerHTML: "<p>abc\uD83D\uDC49</p><p>[]\uD83D\uDC48def</p>",
run: (win, doc, editingHost) => {
doc.execCommand("delete");
}
},
{
description: "forwarddelete at end of first paragraph ending with an emoji",
editorInnerHTML: "<p>abc\uD83D\uDC49[]</p><p>\uD83D\uDC48def</p>",
run: (win, doc, editingHost) => {
doc.execCommand("forwarddelete");
}
},
];
for (const curTest of tests) {
promise_test(async t => {
await new Promise(resolve => {
iframe.addEventListener("load", resolve, {once: true});
iframe.srcdoc = "<html><body><div contenteditable></div></body></html>";
});
const contentDocument = iframe.contentDocument;
const contentWindow = iframe.contentWindow;
contentWindow.focus();
const editingHost = contentDocument.querySelector("div[contenteditable]");
const utils = new EditorTestUtils(editingHost, window);
utils.setupEditingHost(curTest.editorInnerHTML);
contentDocument.documentElement.scrollHeight; // flush pending things
if (typeof curTest.init == "function") {
await curTest.init(contentWindow, contentDocument, editingHost);
}
const initialValue = editingHost.innerHTML;
await curTest.run(contentWindow, contentDocument, editingHost);
const newValue = editingHost.innerHTML;
test(t2 => {
const ret = contentDocument.execCommand("undo");
if (curTest.expectedUndoResult !== undefined) {
if (typeof curTest.expectedUndoResult == "string") {
assert_equals(
editingHost.innerHTML,
curTest.expectedUndoResult,
`${t2.name}: should restore the innerHTML value`
);
} else {
assert_in_array(
editingHost.innerHTML,
curTest.expectedUndoResult,
`${t2.name}: should restore one of the innerHTML values`
);
}
} else {
assert_equals(
editingHost.innerHTML,
initialValue,
`${t2.name}: should restore the initial innerHTML value`
);
}
assert_true(ret, `${t2.name}: execCommand("undo") should return true`);
}, `${t.name} - first undo`);
test(t3 => {
const ret = contentDocument.execCommand("redo");
assert_equals(
editingHost.innerHTML,
newValue,
`${t3.name}: should restore the modified innerHTML value`
);
assert_true(ret, `${t3.name}: execCommand("redo") should return true`);
}, `${curTest.description} - first redo`);
test(t4 => {
const ret = contentDocument.execCommand("redo");
assert_equals(
editingHost.innerHTML,
newValue,
`${t4.name}: should not modify the modified innerHTML value`
);
assert_false(ret, `${t4.name}: execCommand("redo") should return false`);
}, `${curTest.description} - second redo`);
if (typeof curTest.cleanUp == "function") {
await curTest.cleanUp(contentWindow, contentDocument, editingHost);
}
iframe.srcdoc = "";
contentDocument.documentElement.scrollHeight; // flush pending things
await new Promise(resolve =>
requestAnimationFrame(
() => requestAnimationFrame(resolve)
)
);
}, curTest.description);
}
</script>
|