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
|
<!doctype html>
<meta charset=utf-8>
<title>Test insertParagraph when selection collapsed in void element</title>
<meta name="timeout" content="long">
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<div contenteditable></div>
<script>
"use strict";
const voidElements = [
"br",
"embed",
"hr",
"img",
"input",
"wbr",
];
// This test tests whether the inserted text is inserted into, when selection
// is collapsed in the void element. The expected results are based on Blink,
// but the results of <hr>, <embed> and <wbr> elements are not consistent with
// the other elements'. Therefore, Blink also does not pass some of the
// following tests.
// FYI: This cannot be tested by editing/run because there is no way to collapse
// selection into a void element with the framework.
const editor = document.querySelector("div[contenteditable]");
for (const container of ["div", "h1", "li"]) {
const openTagOfContainer = (() => {
if (container == "li") {
return "<ol><li>";
}
return `<${container}>`;
})();
const closeTagOfContainer = (() => {
if (container == "li") {
return "</li></ol>";
}
return `</${container}>`;
})();
const closeAndOpenTagsOfSplitPoint = (() => {
if (container == "li") {
return "</li><li>";
}
return `</${container}><${container}>`;
})();
for (const tag of voidElements) {
const visibleTag = tag == "hr" || tag == "img" || tag == "input";
test(() => {
editor.innerHTML = `${openTagOfContainer}<${tag}>${closeTagOfContainer}`;
const element = editor.querySelector(tag);
editor.focus();
const selection = getSelection();
selection.collapse(element, 0);
document.execCommand("insertParagraph");
if (tag == "br") {
if (!visibleTag && container == "h1") {
assert_in_array(
editor.innerHTML,
`${openTagOfContainer}<br>${closeTagOfContainer}<div><br></div>`,
`The paragraph should be inserted before the <${tag}> element`
);
} else {
assert_in_array(
editor.innerHTML,
`${openTagOfContainer}<br>${closeAndOpenTagsOfSplitPoint}<br>${closeTagOfContainer}`,
`The paragraph should be inserted before the <${tag}> element`
);
}
} else if (!visibleTag && container == "h1") {
assert_in_array(
editor.innerHTML,
[
`${openTagOfContainer}<br>${closeTagOfContainer}<div><${tag}></div>`,
`${openTagOfContainer}<br>${closeTagOfContainer}<div><${tag}><br></div>`,
],
`The paragraph should be inserted before the <${tag}> element`
);
} else {
assert_in_array(
editor.innerHTML,
[
`${openTagOfContainer}<br>${closeAndOpenTagsOfSplitPoint}<${tag}>${closeTagOfContainer}`,
`${openTagOfContainer}<br>${closeAndOpenTagsOfSplitPoint}<${tag}><br>${closeTagOfContainer}`,
],
`The paragraph should be inserted before the <${tag}> element`
);
}
}, `Inserting paragraph when selection is collapsed in <${tag}> in <${container}> which is only child`);
test(() => {
editor.innerHTML = `${openTagOfContainer}<${tag}>${closeTagOfContainer}`;
const element = editor.querySelector(tag);
editor.focus();
const selection = getSelection();
selection.collapse(element, 0);
element.getBoundingClientRect();
document.execCommand("insertParagraph");
if (tag == "br") {
if (!visibleTag && container == "h1") {
assert_in_array(
editor.innerHTML,
`${openTagOfContainer}<br>${closeTagOfContainer}<div><br></div>`,
`The paragraph should be inserted before the <${tag}> element`
);
} else {
assert_in_array(
editor.innerHTML,
`${openTagOfContainer}<br>${closeAndOpenTagsOfSplitPoint}<br>${closeTagOfContainer}`,
`The paragraph should be inserted before the <${tag}> element`
);
}
} else if (!visibleTag && container == "h1") {
assert_in_array(
editor.innerHTML,
[
`${openTagOfContainer}<br>${closeTagOfContainer}<div><${tag}></div>`,
`${openTagOfContainer}<br>${closeTagOfContainer}<div><${tag}><br></div>`,
],
`The paragraph should be inserted before the <${tag}> element`
);
} else {
assert_in_array(
editor.innerHTML,
[
`${openTagOfContainer}<br>${closeAndOpenTagsOfSplitPoint}<${tag}>${closeTagOfContainer}`,
`${openTagOfContainer}<br>${closeAndOpenTagsOfSplitPoint}<${tag}><br>${closeTagOfContainer}`,
],
`The paragraph should be inserted before the <${tag}> element`
);
}
}, `Inserting paragraph when selection is collapsed in <${tag}> in <${container}> which is only child (explicitly flushes maybe pending layout)`);
test(() => {
editor.innerHTML = `${openTagOfContainer}abc<${tag}>${closeTagOfContainer}`;
const element = editor.querySelector(tag);
editor.focus();
const selection = getSelection();
selection.collapse(element, 0);
document.execCommand("insertParagraph");
if (tag == "br") {
if (!visibleTag && container == "h1") {
assert_in_array(
editor.innerHTML,
[
`${openTagOfContainer}abc${closeTagOfContainer}<div><br></div>`,
`${openTagOfContainer}abc<br>${closeTagOfContainer}<div><br></div>`,
],
`The paragraph should be split before the <${tag}> element`
);
} else {
assert_in_array(
editor.innerHTML,
[
`${openTagOfContainer}abc${closeAndOpenTagsOfSplitPoint}<br>${closeTagOfContainer}`,
`${openTagOfContainer}abc<br>${closeAndOpenTagsOfSplitPoint}<br>${closeTagOfContainer}`,
],
`The paragraph should be split before the <${tag}> element`
);
}
} else if (!visibleTag && container == "h1") {
assert_in_array(
editor.innerHTML,
[
`${openTagOfContainer}abc${closeTagOfContainer}<div><${tag}></div>`,
`${openTagOfContainer}abc<br>${closeTagOfContainer}<div><${tag}></div>`,
`${openTagOfContainer}abc${closeTagOfContainer}<div><${tag}><br></div>`,
`${openTagOfContainer}abc<br>${closeTagOfContainer}<div><${tag}><br></div>`,
],
`The paragraph should be split before the <${tag}> element`
);
} else {
assert_in_array(
editor.innerHTML,
[
`${openTagOfContainer}abc${closeAndOpenTagsOfSplitPoint}<${tag}>${closeTagOfContainer}`,
`${openTagOfContainer}abc<br>${closeAndOpenTagsOfSplitPoint}<${tag}>${closeTagOfContainer}`,
`${openTagOfContainer}abc${closeAndOpenTagsOfSplitPoint}<${tag}><br>${closeTagOfContainer}`,
`${openTagOfContainer}abc<br>${closeAndOpenTagsOfSplitPoint}<${tag}><br>${closeTagOfContainer}`,
],
`The paragraph should be split before the <${tag}> element`
);
}
}, `Inserting paragraph when selection is collapsed in <${tag}> which follows a text node in <${container}>`);
test(() => {
editor.innerHTML = `${openTagOfContainer}<${tag}>abc${closeTagOfContainer}`;
const element = editor.querySelector(tag);
editor.focus();
const selection = getSelection();
selection.collapse(element, 0);
document.execCommand("insertParagraph");
assert_in_array(
editor.innerHTML,
[
`${openTagOfContainer}<br>${closeAndOpenTagsOfSplitPoint}<${tag}>abc${closeTagOfContainer}`,
`${openTagOfContainer}<br>${closeAndOpenTagsOfSplitPoint}<${tag}>abc<br>${closeTagOfContainer}`,
],
`The paragraph should be inserted before the <${tag}> element`
);
}, `Inserting paragraph when selection is collapsed in <${tag}> which is followed by a text node in <${container}>`);
}
}
</script>
|