summaryrefslogtreecommitdiff
path: root/editor/libeditor/tests/test_contenteditable_text_input_handling.html
blob: 06b95fbb89161cf1aeab584b32beb1843c365a81 (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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<html>
<head>
  <title>Test for text input event handling on contenteditable editor</title>
  <script type="text/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css"
          href="chrome://mochikit/content/tests/SimpleTest/test.css" />
</head>
<body>
<div id="display">
  <p id="static">static content<input id="inputInStatic"><textarea id="textareaInStatic"></textarea></p>
  <p id="editor"contenteditable="true">content editable<input id="inputInEditor"><textarea id="textareaInEditor"></textarea></p>
</div>
<div id="content" style="display: none">
  
</div>
<pre id="test">
</pre>

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

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

const kLF = !navigator.platform.indexOf("Win") ? "\r\n" : "\n";

function runTests()
{
  var fm = Components.classes["@mozilla.org/focus-manager;1"].
             getService(Components.interfaces.nsIFocusManager);

  var listener = {
    handleEvent: function _hv(aEvent)
    {
      aEvent.preventDefault(); // prevent the browser default behavior
    }
  };
  var els = Components.classes["@mozilla.org/eventlistenerservice;1"].
    getService(Components.interfaces.nsIEventListenerService);
  els.addSystemEventListener(window, "keypress", listener, false);

  var staticContent = document.getElementById("static");
  staticContent._defaultValue = getTextValue(staticContent);
  staticContent._isFocusable = false;
  staticContent._isEditable = false;
  staticContent._isContentEditable = false;
  staticContent._description = "non-editable p element";
  var inputInStatic = document.getElementById("inputInStatic");
  inputInStatic._defaultValue = getTextValue(inputInStatic);
  inputInStatic._isFocusable = true;
  inputInStatic._isEditable = true;
  inputInStatic._isContentEditable = false;
  inputInStatic._description = "input element in static content";
  var textareaInStatic = document.getElementById("textareaInStatic");
  textareaInStatic._defaultValue = getTextValue(textareaInStatic);
  textareaInStatic._isFocusable = true;
  textareaInStatic._isEditable = true;
  textareaInStatic._isContentEditable = false;
  textareaInStatic._description = "textarea element in static content";
  var editor = document.getElementById("editor");
  editor._defaultValue = getTextValue(editor);
  editor._isFocusable = true;
  editor._isEditable = true;
  editor._isContentEditable = true;
  editor._description = "contenteditable editor";
  var inputInEditor = document.getElementById("inputInEditor");
  inputInEditor._defaultValue = getTextValue(inputInEditor);
  inputInEditor._isFocusable = true;
  inputInEditor._isEditable = true;
  inputInEditor._isContentEditable = false;
  inputInEditor._description = "input element in contenteditable editor";
  var textareaInEditor = document.getElementById("textareaInEditor");
  textareaInEditor._defaultValue = getTextValue(textareaInEditor);
  textareaInEditor._isFocusable = true;
  textareaInEditor._isEditable = true;
  textareaInEditor._isContentEditable = false;
  textareaInEditor._description = "textarea element in contenteditable editor";

  function getTextValue(aElement)
  {
    if (aElement == editor) {
      var value = "";
      for (var node = aElement.firstChild; node; node = node.nextSibling) {
        if (node.nodeType == Node.TEXT_NODE) {
          value += node.data;
        } else if (node.nodeType == Node.ELEMENT_NODE) {
          var tagName = node.tagName.toLowerCase();
          switch (tagName) {
            case "input":
            case "textarea":
              value += kLF;
              break;
            default:
              ok(false, "Undefined tag is used in the editor: " + tagName);
              break;
          }
        }
      }
      return value;
    }
    return aElement.value;
  }

  function testTextInput(aFocus)
  {
    var when = " when " +
      ((aFocus && aFocus._isFocusable) ? aFocus._description + " has focus" :
                                         "nobody has focus");

    function checkValue(aElement, aInsertedText)
    {
      if (aElement == aFocus && aElement._isEditable) {
        is(getTextValue(aElement), aInsertedText + aElement._defaultValue,
           aElement._description +
             " wasn't edited by synthesized key events" + when);
        return;
      }
      is(getTextValue(aElement), aElement._defaultValue,
         aElement._description +
           " was edited by synthesized key events" + when);
    }

    if (aFocus && aFocus._isFocusable) {
      aFocus.focus();
      is(fm.focusedElement, aFocus,
         aFocus._description + " didn't get focus at preparing tests" + when);
    } else {
      var focusedElement = fm.focusedElement;
      if (focusedElement) {
        focusedElement.blur();
      }
      ok(!fm.focusedElement,
         "Failed to blur at preparing tests" + when);
    }

    if (aFocus && aFocus._isFocusable) {
      synthesizeKey("A", { });
      synthesizeKey("B", { });
      synthesizeKey("C", { });
      checkValue(staticContent, "ABC");
      checkValue(inputInStatic, "ABC");
      checkValue(textareaInStatic, "ABC");
      checkValue(editor, "ABC");
      checkValue(inputInEditor, "ABC");
      checkValue(textareaInEditor, "ABC");

      if (aFocus._isEditable) {
        synthesizeKey("VK_BACK_SPACE", { });
        synthesizeKey("VK_BACK_SPACE", { });
        synthesizeKey("VK_BACK_SPACE", { });
        checkValue(staticContent, "");
        checkValue(inputInStatic, "");
        checkValue(textareaInStatic, "");
        checkValue(editor, "");
        checkValue(inputInEditor, "");
        checkValue(textareaInEditor, "");
      }
    }

    // When key events are fired on unfocused editor.
    function testDispatchedKeyEvent(aTarget)
    {
      var targetDescription = " (dispatched to " + aTarget._description + ")";
      function dispatchKeyEvent(aKeyCode, aChar, aTarget)
      {
        var keyEvent = document.createEvent("KeyboardEvent");
        keyEvent.initKeyEvent("keypress", true, true, null, false, false,
                               false, false, aKeyCode,
                               aChar ? aChar.charCodeAt(0) : 0);
        aTarget.dispatchEvent(keyEvent);
      }

      function checkValueForDispatchedKeyEvent(aElement, aInsertedText)
      {
        if (aElement == aTarget && aElement._isEditable &&
            (!aElement._isContentEditable || aElement == aFocus)) {
          is(getTextValue(aElement), aInsertedText + aElement._defaultValue,
             aElement._description +
               " wasn't edited by dispatched key events" +
               when + targetDescription);
          return;
        }
        if (aElement == aTarget) {
          is(getTextValue(aElement), aElement._defaultValue,
             aElement._description +
               " was edited by dispatched key events" +
               when + targetDescription);
          return;
        }
        is(getTextValue(aElement), aElement._defaultValue,
           aElement._description +
             " was edited by key events unexpectedly" +
             when + targetDescription);
      }

      dispatchKeyEvent(0, "A", aTarget);
      dispatchKeyEvent(0, "B", aTarget);
      dispatchKeyEvent(0, "C", aTarget);

      checkValueForDispatchedKeyEvent(staticContent, "ABC");
      checkValueForDispatchedKeyEvent(inputInStatic, "ABC");
      checkValueForDispatchedKeyEvent(textareaInStatic, "ABC");
      checkValueForDispatchedKeyEvent(editor, "ABC");
      checkValueForDispatchedKeyEvent(inputInEditor, "ABC");
      checkValueForDispatchedKeyEvent(textareaInEditor, "ABC");

      const nsIDOMKeyEvent = Components.interfaces.nsIDOMKeyEvent;
      dispatchKeyEvent(nsIDOMKeyEvent.DOM_VK_BACK_SPACE, 0, aTarget);
      dispatchKeyEvent(nsIDOMKeyEvent.DOM_VK_BACK_SPACE, 0, aTarget);
      dispatchKeyEvent(nsIDOMKeyEvent.DOM_VK_BACK_SPACE, 0, aTarget);

      checkValueForDispatchedKeyEvent(staticContent, "");
      checkValueForDispatchedKeyEvent(inputInStatic, "");
      checkValueForDispatchedKeyEvent(textareaInStatic, "");
      checkValueForDispatchedKeyEvent(editor, "");
      checkValueForDispatchedKeyEvent(inputInEditor, "");
      checkValueForDispatchedKeyEvent(textareaInEditor, "");
    }

    testDispatchedKeyEvent(staticContent);
    testDispatchedKeyEvent(inputInStatic);
    testDispatchedKeyEvent(textareaInStatic);
    testDispatchedKeyEvent(editor);
    testDispatchedKeyEvent(inputInEditor);
    testDispatchedKeyEvent(textareaInEditor);

    if (!aFocus._isEditable) {
      return;
    }

    // IME
    // input first character
    synthesizeCompositionChange(
      { "composition":
        { "string": "\u3089",
          "clauses":
          [
            { "length": 1, "attr": COMPOSITION_ATTR_RAW_CLAUSE }
          ]
        },
        "caret": { "start": 1, "length": 0 }
      });
    var queryText = synthesizeQueryTextContent(0, 100);
    ok(queryText, "query text event result is null" + when);
    if (!queryText) {
      return;
    }
    ok(queryText.succeeded, "query text event failed" + when);
    if (!queryText.succeeded) {
      return;
    }
    is(queryText.text, "\u3089" + aFocus._defaultValue,
       "composing text is incorrect" + when);
    var querySelectedText = synthesizeQuerySelectedText();
    ok(querySelectedText, "query selected text event result is null" + when);
    if (!querySelectedText) {
      return;
    }
    ok(querySelectedText.succeeded, "query selected text event failed" + when);
    if (!querySelectedText.succeeded) {
      return;
    }
    is(querySelectedText.offset, 1,
       "query selected text event returns wrong offset" + when);
    is(querySelectedText.text, "",
       "query selected text event returns wrong selected text" + when);
    // commit composition
    synthesizeComposition({ type: "compositioncommitasis" });
    queryText = synthesizeQueryTextContent(0, 100);
    ok(queryText, "query text event result is null after commit" + when);
    if (!queryText) {
      return;
    }
    ok(queryText.succeeded, "query text event failed after commit" + when);
    if (!queryText.succeeded) {
      return;
    }
    is(queryText.text, "\u3089" + aFocus._defaultValue,
       "composing text is incorrect after commit" + when);
    querySelectedText = synthesizeQuerySelectedText();
    ok(querySelectedText,
       "query selected text event result is null after commit" + when);
    if (!querySelectedText) {
      return;
    }
    ok(querySelectedText.succeeded,
       "query selected text event failed after commit" + when);
    if (!querySelectedText.succeeded) {
      return;
    }
    is(querySelectedText.offset, 1,
       "query selected text event returns wrong offset after commit" + when);
    is(querySelectedText.text, "",
       "query selected text event returns wrong selected text after commit" +
         when);

    checkValue(staticContent, "\u3089");
    checkValue(inputInStatic, "\u3089");
    checkValue(textareaInStatic, "\u3089");
    checkValue(editor, "\u3089");
    checkValue(inputInEditor, "\u3089");
    checkValue(textareaInEditor, "\u3089");

    synthesizeKey("VK_BACK_SPACE", { });
    checkValue(staticContent, "");
    checkValue(inputInStatic, "");
    checkValue(textareaInStatic, "");
    checkValue(editor, "");
    checkValue(inputInEditor, "");
    checkValue(textareaInEditor, "");
  }

  testTextInput(inputInStatic);
  testTextInput(textareaInStatic);
  testTextInput(editor);
  testTextInput(inputInEditor);
  testTextInput(textareaInEditor);

  els.removeSystemEventListener(window, "keypress", listener, false);

  SimpleTest.finish();
}

</script>
</body>

</html>