summaryrefslogtreecommitdiff
path: root/editor/libeditor/tests/test_bug414526.html
blob: 0975b6a5ab46ac37d9fcb64dde6e59aa55af18a5 (plain)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<html>
<head>
  <title>Test for backspace key and delete key shouldn't remove another editing host's text</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<div id="display"></div>
<div id="content" style="display: none">
  
</div>
<pre id="test">
</pre>

<script class="testbody" type="application/javascript">

SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(runTests);

function runTests()
{

  var container = document.getElementById("display");

  function reset()
  {
    document.execCommand("Undo", false, null);
  }

  var selection = window.getSelection();
  function moveCaretToStartOf(aEditor)
  {
    selection.selectAllChildren(aEditor);
    selection.collapseToStart();
  }

  function moveCaretToEndOf(aEditor)
  {
    selection.selectAllChildren(aEditor);
    selection.collapseToEnd();
  }

  /* TestCase #1
   */
  const kTestCase1 =
    "<p id=\"editor1\" contenteditable=\"true\">editor1</p>" +
    "<p id=\"editor2\" contenteditable=\"true\">editor2</p>" +
    "<div id=\"editor3\" contenteditable=\"true\"><div>editor3</div></div>" +
    "<p id=\"editor4\" contenteditable=\"true\">editor4</p>" +
    "non-editable text" +
    "<p id=\"editor5\" contenteditable=\"true\">editor5</p>";

  const kTestCase1_editor3_deleteAtStart =
    "<p id=\"editor1\" contenteditable=\"true\">editor1</p>" +
    "<p id=\"editor2\" contenteditable=\"true\">editor2</p>" +
    "<div id=\"editor3\" contenteditable=\"true\"><div>ditor3</div></div>" +
    "<p id=\"editor4\" contenteditable=\"true\">editor4</p>" +
    "non-editable text" +
    "<p id=\"editor5\" contenteditable=\"true\">editor5</p>";

  const kTestCase1_editor3_backspaceAtEnd =
    "<p id=\"editor1\" contenteditable=\"true\">editor1</p>" +
    "<p id=\"editor2\" contenteditable=\"true\">editor2</p>" +
    "<div id=\"editor3\" contenteditable=\"true\"><div>editor</div></div>" +
    "<p id=\"editor4\" contenteditable=\"true\">editor4</p>" +
    "non-editable text" +
    "<p id=\"editor5\" contenteditable=\"true\">editor5</p>";

  container.innerHTML = kTestCase1;

  var editor1 = document.getElementById("editor1");
  var editor2 = document.getElementById("editor2");
  var editor3 = document.getElementById("editor3");
  var editor4 = document.getElementById("editor4");
  var editor5 = document.getElementById("editor5");

  /* TestCase #1:
   * pressing backspace key at start should not change the content.
   */
  editor2.focus();
  moveCaretToStartOf(editor2);
  synthesizeKey("VK_BACK_SPACE", { });
  is(container.innerHTML, kTestCase1,
     "Pressing backspace key at start of editor2 changes the content");
  reset();

  editor3.focus();
  moveCaretToStartOf(editor3);
  synthesizeKey("VK_BACK_SPACE", { });
  is(container.innerHTML, kTestCase1,
     "Pressing backspace key at start of editor3 changes the content");
  reset();

  editor4.focus();
  moveCaretToStartOf(editor4);
  synthesizeKey("VK_BACK_SPACE", { });
  is(container.innerHTML, kTestCase1,
     "Pressing backspace key at start of editor4 changes the content");
  reset();

  editor5.focus();
  moveCaretToStartOf(editor5);
  synthesizeKey("VK_BACK_SPACE", { });
  is(container.innerHTML, kTestCase1,
     "Pressing backspace key at start of editor5 changes the content");
  reset();

  /* TestCase #1:
   * pressing delete key at end should not change the content.
   */
  editor1.focus();
  moveCaretToEndOf(editor1);
  synthesizeKey("VK_DELETE", { });
  is(container.innerHTML, kTestCase1,
     "Pressing delete key at end of editor1 changes the content");
  reset();

  editor2.focus();
  moveCaretToEndOf(editor2);
  synthesizeKey("VK_DELETE", { });
  is(container.innerHTML, kTestCase1,
     "Pressing delete key at end of editor2 changes the content");
  reset();

  editor3.focus();
  moveCaretToEndOf(editor3);
  synthesizeKey("VK_DELETE", { });
  is(container.innerHTML, kTestCase1,
          "Pressing delete key at end of editor3 changes the content");
  reset();

  editor4.focus();
  moveCaretToEndOf(editor4);
  synthesizeKey("VK_DELETE", { });
  is(container.innerHTML, kTestCase1,
     "Pressing delete key at end of editor4 changes the content");
  reset();

  /* TestCase #1: cases when the caret is not on text node.
   *   - pressing delete key at start should remove the first character
   *   - pressing backspace key at end should remove the first character
   * and the adjacent blocks should not be changed.
   */
  editor3.focus();
  moveCaretToStartOf(editor3);
  synthesizeKey("VK_DELETE", { });
  is(container.innerHTML, kTestCase1_editor3_deleteAtStart,
     "Pressing delete key at start of editor3 changes adjacent elements"
     + " and/or does not remove the first character.");
  reset();

  // Backspace doesn't work here yet.
  editor3.focus();
  moveCaretToEndOf(editor3);
  synthesizeKey("VK_BACK_SPACE", { });
  todo_is(container.innerHTML, kTestCase1_editor3_backspaceAtEnd,
          "Pressing backspace key at end of editor3 changes adjacent elements"
          + " and/or does not remove the last character.");
  reset();
  //  We can still check that adjacent elements are not affected.
  editor3.focus();
  moveCaretToEndOf(editor3);
  synthesizeKey("VK_BACK_SPACE", { });
  is(container.innerHTML, kTestCase1,
     "Pressing backspace key at end of editor3 changes the content");
  reset();

  /* TestCase #2:
   * two adjacent editable <span> in a table cell.
   */
  const kTestCase2 = "<table><tbody><tr><td><span id=\"editor1\" contenteditable=\"true\">test</span>" +
    "<span id=\"editor2\" contenteditable=\"true\">test</span></td></tr></tbody></table>";

  container.innerHTML = kTestCase2;
  editor1 = document.getElementById("editor1");
  editor2 = document.getElementById("editor2");

  editor2.focus();
  moveCaretToStartOf(editor2);
  synthesizeKey("VK_BACK_SPACE", { });
  is(container.innerHTML, kTestCase2,
     "Pressing backspace key at the start of editor2 changes the content for kTestCase2");
  reset();

  editor1.focus();
  moveCaretToEndOf(editor1);
  synthesizeKey("VK_DELETE", { });
  is(container.innerHTML, kTestCase2,
     "Pressing delete key at the end of editor1 changes the content for kTestCase2");
  reset();

  /* TestCase #3:
   * editable <span> in two adjacent table cells.
   */
  const kTestCase3 = "<table><tbody><tr><td><span id=\"editor1\" contenteditable=\"true\">test</span></td>" +
    "<td><span id=\"editor2\" contenteditable=\"true\">test</span></td></tr></tbody></table>";

  container.innerHTML = kTestCase3;
  editor1 = document.getElementById("editor1");
  editor2 = document.getElementById("editor2");

  editor2.focus();
  moveCaretToStartOf(editor2);
  synthesizeKey("VK_BACK_SPACE", { });
  is(container.innerHTML, kTestCase3,
     "Pressing backspace key at the start of editor2 changes the content for kTestCase3");
  reset();

  editor1.focus();
  moveCaretToEndOf(editor1);
  synthesizeKey("VK_DELETE", { });
  is(container.innerHTML, kTestCase3,
     "Pressing delete key at the end of editor1 changes the content for kTestCase3");
  reset();

  /* TestCase #4:
   * editable <div> in two adjacent table cells.
   */
  const kTestCase4 = "<table><tbody><tr><td><div id=\"editor1\" contenteditable=\"true\">test</div></td>" +
    "<td><div id=\"editor2\" contenteditable=\"true\">test</div></td></tr></tbody></table>";

  container.innerHTML = kTestCase4;
  editor1 = document.getElementById("editor1");
  editor2 = document.getElementById("editor2");

  editor2.focus();
  moveCaretToStartOf(editor2);
  synthesizeKey("VK_BACK_SPACE", { });
  is(container.innerHTML, kTestCase4,
     "Pressing backspace key at the start of editor2 changes the content for kTestCase4");
  reset();

  editor1.focus();
  moveCaretToEndOf(editor1);
  synthesizeKey("VK_DELETE", { });
  is(container.innerHTML, kTestCase4,
     "Pressing delete key at the end of editor1 changes the content for kTestCase4");
  reset();

  SimpleTest.finish();
}

</script>
</body>

</html>