summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_bug1310912.html
blob: 6c2036e4c081f5930a1d1b89221fdb6907188c7f (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
<!DOCTYPE html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1310912
-->
<html>
<head>
  <title>Test for Bug 1310912</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1310912">Mozilla Bug 1310912</a>
<p id="display"></p>
<div id="content" style="display: none;">

</div>

<div contenteditable>ABC</div>
<pre id="test">

<script class="testbody" type="application/javascript">
SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(function() {
  const editor = document.querySelector("div[contenteditable]");

  editor.focus();
  const sel = window.getSelection();
  sel.collapse(editor.childNodes[0], editor.textContent.length);

  (function testInsertEmptyTextNodeWhenCaretIsAtEndOfComposition() {
    const description =
      "testInsertEmptyTextNodeWhenCaretIsAtEndOfComposition: ";
    synthesizeCompositionChange({
      composition: {
        string: "DEF",
        clauses: [
          { length: 3, attr: COMPOSITION_ATTR_RAW_CLAUSE },
        ],
      },
      caret: { start: 3, length: 0 },
    });
    is(
      editor.textContent,
      "ABCDEF",
      `${description} Composing text "DEF" should be inserted at end of the text node`
    );

    window.getSelection().getRangeAt(0).insertNode(document.createTextNode(""));
    is(
      editor.childNodes[0].data,
      "ABCDEF",
      `${
        description
      } First text node should have both preceding text and the composing text`
    );
    is(
      editor.childNodes[1].data,
      "",
      `${description} Second text node should be empty`
    );
  })();

  (function testInsertEmptyTextNodeWhenCaretIsAtStartOfComposition() {
    const description =
      "testInsertEmptyTextNodeWhenCaretIsAtStartOfComposition: ";
    synthesizeCompositionChange({
      composition: {
        string: "GHI",
        clauses: [
          { length: 3, attr: COMPOSITION_ATTR_CONVERTED_CLAUSE },
        ],
      },
      caret: { start: 0, length: 0 },
    });
    is(
      editor.textContent,
      "ABCGHI",
      `${description} Composing text should be replaced with new one`
    );

    window.getSelection().getRangeAt(0).insertNode(document.createTextNode(""));
    is(
      editor.childNodes[0].data,
      "ABC",
      `${
        description
      } First text node should have only the preceding text of the composition`
    );
    is(
      editor.childNodes[1].data,
      "",
      `${description} Second text node should have be empty`
    );
    is(
      editor.childNodes[2].data,
      "GHI",
      `${description} Third text node should have only composing text`
    );
  })();

  (function testInsertEmptyTextNodeWhenCaretIsAtStartOfCompositionAgain() {
    const description =
      "testInsertEmptyTextNodeWhenCaretIsAtStartOfCompositionAgain: ";
    synthesizeCompositionChange({
      composition: {
        string: "JKL",
        clauses: [
          { length: 3, attr: COMPOSITION_ATTR_CONVERTED_CLAUSE },
        ],
      },
      caret: { start: 0, length: 0 },
    });
    is(
      editor.textContent,
      "ABCJKL",
      `${description} Composing text should be replaced`
    );

    window.getSelection().getRangeAt(0).insertNode(document.createTextNode(""));
    is(
      editor.childNodes[0].data,
      "ABC",
      `${
        description
      } First text node should have only the preceding text of the composition`
    );
    is(
      editor.childNodes[1].data,
      "",
      `${description} Second text node should have be empty`
    );
    is(
      editor.childNodes[2].data,
      "JKL",
      `${description} Third text node should have only composing text`
    );
  })();

  (function testInsertEmptyTextNodeWhenCaretIsAtMiddleOfComposition() {
    const description =
      "testInsertEmptyTextNodeWhenCaretIsAtMiddleOfComposition: ";
    synthesizeCompositionChange({
      composition: {
        string: "MNO",
        clauses: [
          { length: 3, attr: COMPOSITION_ATTR_CONVERTED_CLAUSE },
        ],
      },
      caret: { start: 1, length: 0 },
    });
    is(
      editor.textContent,
      "ABCMNO",
      `${description} Composing text should be replaced`
    );

    // Normal selection is the caret, therefore, inserting empty text node
    // creates the following DOM tree:
    // <div contenteditable>
    //  |- #text ("ABCM")
    //  |- #text ("")
    //  +- #text ("NO")
    window.getSelection().getRangeAt(0).insertNode(document.createTextNode(""));
    is(
      editor.childNodes[0].data,
      "ABCM",
      `${
        description
      } First text node should have the preceding text and composing string before the split point`
    );
    is(
      editor.childNodes[1].data,
      "",
      `${description} Second text node should be empty`
    );
    is(
      editor.childNodes[2].data,
      "NO",
      `${
        description
      } Third text node should have the remaining composing string`
    );
    todo_is(editor.childNodes[3].nodeName, "BR",
      "Forth node is empty text node, but I don't where this comes from");
  })();

  // Then, committing composition makes the commit string into the first
  // text node and makes the following text nodes empty.
  // XXX I don't know whether the empty text nodes should be removed or not
  //     at this moment.
  (function testCommitComposition() {
    const description = "testCommitComposition: ";
    synthesizeComposition({ type: "compositioncommitasis" });
    is(
      editor.textContent,
      "ABCMNO",
      `${description} Composing text should be committed as-is`
    );
    is(
      editor.childNodes[0].data,
      "ABCMNO",
      `${description} First text node should have the committed string`
    );
  })();

  (function testUndoComposition() {
    const description = "testUndoComposition: ";
    synthesizeKey("Z", { accelKey: true });
    is(
      editor.textContent,
      "ABC",
      `${description} Text should be undone (commit string should've gone)`
    );
    is(
      editor.childNodes[0].data,
      "ABC",
      `${description} First text node should have all text`
    );
  })();

  (function testUndoAgain() {
    const description = "testUndoAgain: ";
    synthesizeKey("Z", { accelKey: true, shiftKey: true });
    is(
      editor.textContent,
      "ABCMNO",
      `${description} Text should be redone (commit string should've be back)`
    );
    is(
      editor.childNodes[0].data,
      "ABCMNO",
      `${description} First text node should have all text`
    );
  })();

  SimpleTest.finish();
});
</script>
</pre>
</body>
</html>